00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00017 #include "config.h"
00018 #include <sys/types.h>
00019 #include <sys/mman.h>
00020 #include <sys/stat.h>
00021 #include <sys/wait.h>
00022 #include <sys/time.h>
00023 #include <sys/file.h>
00024 #include <fcntl.h>
00025 #include <errno.h>
00026 #include <unistd.h>
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <signal.h>
00031 #include <time.h>
00032
00033 #include "misc.h"
00034 #include "sys_generic.h"
00035 #include "debuglog.h"
00036
00043 INTERNAL int SYS_Initialize(void)
00044 {
00045
00046
00047
00048 return 0;
00049 }
00050
00061 INTERNAL int SYS_Mkdir(char *path, int perms)
00062 {
00063 return mkdir(path, perms);
00064 }
00065
00071 INTERNAL int SYS_GetPID(void)
00072 {
00073 return getpid();
00074 }
00075
00081 INTERNAL int SYS_Sleep(int iTimeVal)
00082 {
00083 #ifdef HAVE_NANOSLEEP
00084 struct timespec mrqtp;
00085 mrqtp.tv_sec = iTimeVal;
00086 mrqtp.tv_nsec = 0;
00087
00088 return nanosleep(&mrqtp, NULL);
00089 #else
00090 return sleep(iTimeVal);
00091 #endif
00092 }
00093
00099 INTERNAL int SYS_USleep(int iTimeVal)
00100 {
00101 #ifdef HAVE_NANOSLEEP
00102 struct timespec mrqtp;
00103 mrqtp.tv_sec = 0;
00104 mrqtp.tv_nsec = iTimeVal * 1000;
00105
00106 return nanosleep(&mrqtp, NULL);
00107 #else
00108 usleep(iTimeVal);
00109 return iTimeVal;
00110 #endif
00111 }
00112
00124 INTERNAL int SYS_OpenFile(char *pcFile, int flags, int mode)
00125 {
00126 return open(pcFile, flags, mode);
00127 }
00128
00138 INTERNAL int SYS_CloseFile(int iHandle)
00139 {
00140 return close(iHandle);
00141 }
00142
00152 INTERNAL int SYS_RemoveFile(char *pcFile)
00153 {
00154 return remove(pcFile);
00155 }
00156
00157 INTERNAL int SYS_Chmod(const char *path, int mode)
00158 {
00159 return chmod(path, mode);
00160 }
00161
00162 INTERNAL int SYS_Chdir(const char *path)
00163 {
00164 return chdir(path);
00165 }
00166
00167 INTERNAL int SYS_Mkfifo(const char *path, int mode)
00168 {
00169 return mkfifo(path, mode);
00170 }
00171
00172 INTERNAL int SYS_Mknod(const char *path, int mode, int dev)
00173 {
00174 return mknod(path, mode, dev);
00175 }
00176
00177 INTERNAL int SYS_GetUID(void)
00178 {
00179 return getuid();
00180 }
00181
00182 INTERNAL int SYS_GetGID(void)
00183 {
00184 return getgid();
00185 }
00186
00187 INTERNAL int SYS_Chown(const char *fname, int uid, int gid)
00188 {
00189 return chown(fname, uid, gid);
00190 }
00191
00192 INTERNAL int SYS_ChangePermissions(char *pcFile, int mode)
00193 {
00194 return chmod(pcFile, mode);
00195 }
00196
00206 INTERNAL int SYS_LockFile(int iHandle)
00207 {
00208 #ifdef HAVE_FLOCK
00209 return flock(iHandle, LOCK_EX | LOCK_NB);
00210 #else
00211 struct flock lock_s;
00212
00213 lock_s.l_type = F_WRLCK;
00214 lock_s.l_whence = 0;
00215 lock_s.l_start = 0L;
00216 lock_s.l_len = 0L;
00217
00218 return fcntl(iHandle, F_SETLK, &lock_s);
00219 #endif
00220 }
00221
00231 INTERNAL int SYS_LockAndBlock(int iHandle)
00232 {
00233 #ifdef HAVE_FLOCK
00234 return flock(iHandle, LOCK_EX);
00235 #else
00236 struct flock lock_s;
00237
00238 lock_s.l_type = F_RDLCK;
00239 lock_s.l_whence = 0;
00240 lock_s.l_start = 0L;
00241 lock_s.l_len = 0L;
00242
00243 return fcntl(iHandle, F_SETLKW, &lock_s);
00244 #endif
00245 }
00246
00256 INTERNAL int SYS_UnlockFile(int iHandle)
00257 {
00258 #ifdef HAVE_FLOCK
00259 return flock(iHandle, LOCK_UN);
00260 #else
00261 struct flock lock_s;
00262
00263 lock_s.l_type = F_UNLCK;
00264 lock_s.l_whence = 0;
00265 lock_s.l_start = 0L;
00266 lock_s.l_len = 0L;
00267
00268 return fcntl(iHandle, F_SETLK, &lock_s);
00269 #endif
00270 }
00271
00272 INTERNAL int SYS_SeekFile(int iHandle, int iSeekLength)
00273 {
00274 int iOffset;
00275 iOffset = lseek(iHandle, iSeekLength, SEEK_SET);
00276 return iOffset;
00277 }
00278
00279 INTERNAL int SYS_ReadFile(int iHandle, char *pcBuffer, int iLength)
00280 {
00281 return read(iHandle, pcBuffer, iLength);
00282 }
00283
00284 INTERNAL int SYS_WriteFile(int iHandle, char *pcBuffer, int iLength)
00285 {
00286 return write(iHandle, pcBuffer, iLength);
00287 }
00288
00297 INTERNAL int SYS_GetPageSize(void)
00298 {
00299 return getpagesize();
00300 }
00301
00311 INTERNAL void *SYS_MemoryMap(int iSize, int iFid, int iOffset)
00312 {
00313
00314 void *vAddress;
00315
00316 vAddress = 0;
00317 vAddress = mmap(0, iSize, PROT_READ | PROT_WRITE,
00318 MAP_SHARED, iFid, iOffset);
00319
00320
00321
00322
00323
00324
00325
00326
00327 return vAddress;
00328 }
00329
00339 INTERNAL void *SYS_PublicMemoryMap(int iSize, int iFid, int iOffset)
00340 {
00341
00342 void *vAddress;
00343
00344 vAddress = 0;
00345 vAddress = mmap(0, iSize, PROT_READ, MAP_SHARED, iFid, iOffset);
00346 return vAddress;
00347 }
00348
00359 INTERNAL int SYS_MMapSynchronize(void *begin, int length)
00360 {
00361 int flags = 0;
00362
00363 #ifdef MS_INVALIDATE
00364 flags |= MS_INVALIDATE;
00365 #endif
00366 return msync(begin, length, MS_SYNC | flags);
00367 }
00368
00369 INTERNAL int SYS_Fork(void)
00370 {
00371 return fork();
00372 }
00373
00384 INTERNAL int SYS_Daemon(int nochdir, int noclose)
00385 {
00386 #ifdef HAVE_DAEMON
00387 return daemon(nochdir, noclose);
00388 #else
00389
00390 #if defined(__SVR4) && defined(__sun)
00391 pid_t pid;
00392
00393 pid = SYS_Fork();
00394 if (-1 == pid)
00395 {
00396 Log2(PCSC_LOG_CRITICAL, "main: SYS_Fork() failed: %s", strerror(errno));
00397 return -1;
00398 }
00399 else
00400 {
00401 if (pid != 0)
00402
00403 exit(0);
00404 }
00405
00406 setsid();
00407
00408 pid = SYS_Fork();
00409 if (-1 == pid)
00410 {
00411 Log2(PCSC_LOG_CRITICAL, "main: SYS_Fork() failed: %s", strerror(errno));
00412 exit(1);
00413 }
00414 else
00415 {
00416 if (pid != 0)
00417
00418 exit(0);
00419 }
00420 #else
00421 switch (SYS_Fork())
00422 {
00423 case -1:
00424 return (-1);
00425 case 0:
00426 break;
00427 default:
00428 return (0);
00429 }
00430 #endif
00431
00432 if (!noclose) {
00433 if (SYS_CloseFile(0))
00434 Log2(PCSC_LOG_ERROR, "SYS_CloseFile(0) failed: %s",
00435 strerror(errno));
00436
00437 if (SYS_CloseFile(1))
00438 Log2(PCSC_LOG_ERROR, "SYS_CloseFile(1) failed: %s",
00439 strerror(errno));
00440
00441 if (SYS_CloseFile(2))
00442 Log2(PCSC_LOG_ERROR, "SYS_CloseFile(2) failed: %s",
00443 strerror(errno));
00444 }
00445 if (!nochdir) {
00446 if (SYS_Chdir("/"))
00447 Log2(PCSC_LOG_ERROR, "SYS_Chdir() failed: %s", strerror(errno));
00448 }
00449 return 0;
00450 #endif
00451 }
00452
00453 INTERNAL int SYS_Wait(int iPid, int iWait)
00454 {
00455 return waitpid(-1, 0, WNOHANG);
00456 }
00457
00458 INTERNAL int SYS_Stat(char *pcFile, struct stat *psStatus)
00459 {
00460 return stat(pcFile, psStatus);
00461 }
00462
00463 INTERNAL int SYS_Fstat(int iFd)
00464 {
00465 struct stat sStatus;
00466 return fstat(iFd, &sStatus);
00467 }
00468
00469 INTERNAL int SYS_RandomInt(int fStart, int fEnd)
00470 {
00471 static int iInitialized = 0;
00472 int iRandNum = 0;
00473
00474 if (0 == iInitialized)
00475 {
00476 srand(SYS_GetSeed());
00477 iInitialized = 1;
00478 }
00479
00480 iRandNum = (int)((float)rand()/RAND_MAX * (fEnd - fStart)) + fStart;
00481
00482 return iRandNum;
00483 }
00484
00485 INTERNAL int SYS_GetSeed(void)
00486 {
00487 struct timeval tv;
00488 struct timezone tz;
00489 long myseed = 0;
00490
00491 tz.tz_minuteswest = 0;
00492 tz.tz_dsttime = 0;
00493 if (gettimeofday(&tv, &tz) == 0)
00494 {
00495 myseed = tv.tv_usec;
00496 } else
00497 {
00498 myseed = (long) time(NULL);
00499 }
00500 return myseed;
00501 }
00502
00503 INTERNAL void SYS_Exit(int iRetVal)
00504 {
00505 _exit(iRetVal);
00506 }
00507
00508 INTERNAL int SYS_Rmdir(char *pcFile)
00509 {
00510 return rmdir(pcFile);
00511 }
00512
00513 INTERNAL int SYS_Unlink(char *pcFile)
00514 {
00515 return unlink(pcFile);
00516 }
00517