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 "debug.h"
00036
00043 INTERNAL int SYS_Initialize(void)
00044 {
00045
00046
00047
00048 return 0;
00049 }
00050
00061 INTERNAL int SYS_Mkdir(const 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 = iTimeVal/1000000;
00104 mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000;
00105
00106 return nanosleep(&mrqtp, NULL);
00107 #else
00108 struct timeval tv;
00109 tv.tv_sec = iTimeVal/1000000;
00110 tv.tv_usec = iTimeVal - (tv.tv_sec * 1000000);
00111 return select(0, NULL, NULL, NULL, &tv);
00112 #endif
00113 }
00114
00126 INTERNAL int SYS_OpenFile(const char *pcFile, int flags, int mode)
00127 {
00128 return open(pcFile, flags, mode);
00129 }
00130
00140 INTERNAL int SYS_CloseFile(int iHandle)
00141 {
00142 return close(iHandle);
00143 }
00144
00154 INTERNAL int SYS_RemoveFile(const char *pcFile)
00155 {
00156 return remove(pcFile);
00157 }
00158
00159 INTERNAL int SYS_Chmod(const char *path, int mode)
00160 {
00161 return chmod(path, mode);
00162 }
00163
00164 INTERNAL int SYS_Chdir(const char *path)
00165 {
00166 return chdir(path);
00167 }
00168
00169 INTERNAL int SYS_GetUID(void)
00170 {
00171 return getuid();
00172 }
00173
00174 INTERNAL int SYS_GetGID(void)
00175 {
00176 return getgid();
00177 }
00178
00179 INTERNAL int SYS_SeekFile(int iHandle, int iSeekLength)
00180 {
00181 int iOffset;
00182 iOffset = lseek(iHandle, iSeekLength, SEEK_SET);
00183 return iOffset;
00184 }
00185
00186 INTERNAL int SYS_ReadFile(int iHandle, char *pcBuffer, int iLength)
00187 {
00188 return read(iHandle, pcBuffer, iLength);
00189 }
00190
00191 INTERNAL int SYS_WriteFile(int iHandle, const char *pcBuffer, int iLength)
00192 {
00193 return write(iHandle, pcBuffer, iLength);
00194 }
00195
00204 INTERNAL int SYS_GetPageSize(void)
00205 {
00206 return getpagesize();
00207 }
00208
00219 INTERNAL void *SYS_MemoryMap(int iSize, int iFid, int iOffset)
00220 {
00221
00222 void *vAddress;
00223
00224 vAddress = 0;
00225 vAddress = mmap(0, iSize, PROT_READ | PROT_WRITE,
00226 MAP_SHARED, iFid, iOffset);
00227
00228
00229
00230
00231
00232
00233
00234
00235 return vAddress;
00236 }
00237
00247 INTERNAL void *SYS_PublicMemoryMap(int iSize, int iFid, int iOffset)
00248 {
00249
00250 void *vAddress;
00251
00252 vAddress = 0;
00253 vAddress = mmap(0, iSize, PROT_READ, MAP_SHARED, iFid, iOffset);
00254 if (vAddress == (void*)-1)
00255 {
00256 Log2(PCSC_LOG_CRITICAL, "SYS_PublicMemoryMap() failed: %s",
00257 strerror(errno));
00258 vAddress = NULL;
00259 }
00260
00261 return vAddress;
00262 }
00263
00270 INTERNAL void SYS_PublicMemoryUnmap(void * ptr, int iSize)
00271 {
00272 munmap(ptr, iSize);
00273 }
00274
00285 INTERNAL int SYS_MMapSynchronize(void *begin, int length)
00286 {
00287 int flags = 0;
00288
00289 #ifdef MS_INVALIDATE
00290 flags |= MS_INVALIDATE;
00291 #endif
00292 return msync(begin, length, MS_SYNC | flags);
00293 }
00294
00295 INTERNAL int SYS_Fork(void)
00296 {
00297 return fork();
00298 }
00299
00310 INTERNAL int SYS_Daemon(int nochdir, int noclose)
00311 {
00312 #ifdef HAVE_DAEMON
00313 return daemon(nochdir, noclose);
00314 #else
00315
00316 #if defined(__SVR4) && defined(__sun)
00317 pid_t pid;
00318
00319 pid = SYS_Fork();
00320 if (-1 == pid)
00321 {
00322 Log2(PCSC_LOG_CRITICAL, "main: SYS_Fork() failed: %s", strerror(errno));
00323 return -1;
00324 }
00325 else
00326 {
00327 if (pid != 0)
00328
00329 exit(0);
00330 }
00331
00332 setsid();
00333
00334 pid = SYS_Fork();
00335 if (-1 == pid)
00336 {
00337 Log2(PCSC_LOG_CRITICAL, "main: SYS_Fork() failed: %s", strerror(errno));
00338 exit(1);
00339 }
00340 else
00341 {
00342 if (pid != 0)
00343
00344 exit(0);
00345 }
00346 #else
00347 switch (SYS_Fork())
00348 {
00349 case -1:
00350 return (-1);
00351 case 0:
00352 break;
00353 default:
00354 return (0);
00355 }
00356 #endif
00357
00358 if (!noclose) {
00359 if (SYS_CloseFile(0))
00360 Log2(PCSC_LOG_ERROR, "SYS_CloseFile(0) failed: %s",
00361 strerror(errno));
00362
00363 if (SYS_CloseFile(1))
00364 Log2(PCSC_LOG_ERROR, "SYS_CloseFile(1) failed: %s",
00365 strerror(errno));
00366
00367 if (SYS_CloseFile(2))
00368 Log2(PCSC_LOG_ERROR, "SYS_CloseFile(2) failed: %s",
00369 strerror(errno));
00370 }
00371 if (!nochdir) {
00372 if (SYS_Chdir("/"))
00373 Log2(PCSC_LOG_ERROR, "SYS_Chdir() failed: %s", strerror(errno));
00374 }
00375 return 0;
00376 #endif
00377 }
00378
00379 INTERNAL int SYS_Stat(const char *pcFile, struct stat *psStatus)
00380 {
00381 return stat(pcFile, psStatus);
00382 }
00383
00384 INTERNAL int SYS_RandomInt(int fStart, int fEnd)
00385 {
00386 static int iInitialized = 0;
00387 int iRandNum = 0;
00388
00389 if (0 == iInitialized)
00390 {
00391 srand(SYS_GetSeed());
00392 iInitialized = 1;
00393 }
00394
00395 iRandNum = (int)((float)rand()/RAND_MAX * (fEnd - fStart)) + fStart;
00396
00397 return iRandNum;
00398 }
00399
00400 INTERNAL int SYS_GetSeed(void)
00401 {
00402 struct timeval tv;
00403 struct timezone tz;
00404 long myseed = 0;
00405
00406 tz.tz_minuteswest = 0;
00407 tz.tz_dsttime = 0;
00408 if (gettimeofday(&tv, &tz) == 0)
00409 {
00410 myseed = tv.tv_usec;
00411 } else
00412 {
00413 myseed = (long) time(NULL);
00414 }
00415 return myseed;
00416 }
00417
00418 INTERNAL void SYS_Exit(int iRetVal)
00419 {
00420 _exit(iRetVal);
00421 }
00422
00423 INTERNAL int SYS_Unlink(const char *pcFile)
00424 {
00425 return unlink(pcFile);
00426 }
00427