Main Page   Data Structures   File List   Data Fields   Globals   Related Pages  

sys_unix.c

Go to the documentation of this file.
00001 /*
00002  * This handles abstract system level calls.
00003  *
00004  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
00005  *
00006  * Copyright (C) 1999
00007  *  David Corcoran <corcoran@linuxnet.com>
00008  *
00009  * $Id: sys_unix.c 2264 2006-12-03 13:15:03Z rousseau $
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      * Nothing special
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 = 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(const 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(const 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_GetUID(void)
00168 {
00169     return getuid();
00170 }
00171 
00172 INTERNAL int SYS_GetGID(void)
00173 {
00174     return getgid();
00175 }
00176 
00177 INTERNAL int SYS_SeekFile(int iHandle, int iSeekLength)
00178 {
00179     int iOffset;
00180     iOffset = lseek(iHandle, iSeekLength, SEEK_SET);
00181     return iOffset;
00182 }
00183 
00184 INTERNAL int SYS_ReadFile(int iHandle, char *pcBuffer, int iLength)
00185 {
00186     return read(iHandle, pcBuffer, iLength);
00187 }
00188 
00189 INTERNAL int SYS_WriteFile(int iHandle, const char *pcBuffer, int iLength)
00190 {
00191     return write(iHandle, pcBuffer, iLength);
00192 }
00193 
00202 INTERNAL int SYS_GetPageSize(void)
00203 {
00204     return getpagesize();
00205 }
00206 
00217 INTERNAL void *SYS_MemoryMap(int iSize, int iFid, int iOffset)
00218 {
00219 
00220     void *vAddress;
00221 
00222     vAddress = 0;
00223     vAddress = mmap(0, iSize, PROT_READ | PROT_WRITE,
00224         MAP_SHARED, iFid, iOffset);
00225 
00226     /*
00227      * Here are some common error types: switch( errno ) { case EINVAL:
00228      * printf("EINVAL"); case EBADF: printf("EBADF"); break; case EACCES:
00229      * printf("EACCES"); break; case EAGAIN: printf("EAGAIN"); break; case
00230      * ENOMEM: printf("ENOMEM"); break; }
00231      */
00232 
00233     return vAddress;
00234 }
00235 
00245 INTERNAL void *SYS_PublicMemoryMap(int iSize, int iFid, int iOffset)
00246 {
00247 
00248     void *vAddress;
00249 
00250     vAddress = 0;
00251     vAddress = mmap(0, iSize, PROT_READ, MAP_SHARED, iFid, iOffset);
00252     if (vAddress == (void*)-1) /* mmap returns -1 on error */
00253     {
00254         Log2(PCSC_LOG_CRITICAL, "SYS_PublicMemoryMap() failed: %s",
00255             strerror(errno));
00256         vAddress = NULL;
00257     }
00258 
00259     return vAddress;
00260 }
00261 
00268 INTERNAL void SYS_PublicMemoryUnmap(void * ptr, int iSize)
00269 {
00270     munmap(ptr, iSize);
00271 }
00272 
00283 INTERNAL int SYS_MMapSynchronize(void *begin, int length)
00284 {
00285     int flags = 0;
00286 
00287 #ifdef MS_INVALIDATE
00288     flags |= MS_INVALIDATE;
00289 #endif
00290     return msync(begin, length, MS_SYNC | flags);
00291 }
00292 
00293 INTERNAL int SYS_Fork(void)
00294 {
00295     return fork();
00296 }
00297 
00308 INTERNAL int SYS_Daemon(int nochdir, int noclose)
00309 {
00310 #ifdef HAVE_DAEMON
00311     return daemon(nochdir, noclose);
00312 #else
00313 
00314 #if defined(__SVR4) && defined(__sun)
00315     pid_t pid;
00316 
00317     pid = SYS_Fork();
00318     if (-1 == pid)
00319     {
00320         Log2(PCSC_LOG_CRITICAL, "main: SYS_Fork() failed: %s", strerror(errno));
00321         return -1;
00322     }
00323     else
00324     {
00325         if (pid != 0)
00326             /* the father exits */
00327             exit(0);
00328     }
00329 
00330     setsid();
00331 
00332     pid = SYS_Fork();
00333     if (-1 == pid)
00334     {
00335         Log2(PCSC_LOG_CRITICAL, "main: SYS_Fork() failed: %s", strerror(errno));
00336         exit(1);
00337     }
00338     else
00339     {
00340         if (pid != 0)
00341             /* the father exits */
00342             exit(0);
00343     }
00344 #else
00345     switch (SYS_Fork())
00346     {
00347     case -1:
00348         return (-1);
00349     case 0:
00350         break;
00351     default:
00352         return (0);
00353     }
00354 #endif
00355 
00356     if (!noclose) {
00357         if (SYS_CloseFile(0))
00358             Log2(PCSC_LOG_ERROR, "SYS_CloseFile(0) failed: %s",
00359                 strerror(errno));
00360 
00361         if (SYS_CloseFile(1))
00362             Log2(PCSC_LOG_ERROR, "SYS_CloseFile(1) failed: %s",
00363                 strerror(errno));
00364 
00365         if (SYS_CloseFile(2))
00366             Log2(PCSC_LOG_ERROR, "SYS_CloseFile(2) failed: %s",
00367                 strerror(errno));
00368     }
00369     if (!nochdir) {
00370         if (SYS_Chdir("/"))
00371             Log2(PCSC_LOG_ERROR, "SYS_Chdir() failed: %s", strerror(errno));
00372     }
00373     return 0;
00374 #endif
00375 }
00376 
00377 INTERNAL int SYS_Stat(const char *pcFile, struct stat *psStatus)
00378 {
00379     return stat(pcFile, psStatus);
00380 }
00381 
00382 INTERNAL int SYS_RandomInt(int fStart, int fEnd)
00383 {
00384     static int iInitialized = 0;
00385     int iRandNum = 0;
00386 
00387     if (0 == iInitialized)
00388     {
00389         srand(SYS_GetSeed());
00390         iInitialized = 1;
00391     }
00392 
00393     iRandNum = (int)((float)rand()/RAND_MAX * (fEnd - fStart)) + fStart;
00394 
00395     return iRandNum;
00396 }
00397 
00398 INTERNAL int SYS_GetSeed(void)
00399 {
00400     struct timeval tv;
00401     struct timezone tz;
00402     long myseed = 0;
00403 
00404     tz.tz_minuteswest = 0;
00405     tz.tz_dsttime = 0;
00406     if (gettimeofday(&tv, &tz) == 0)
00407     {
00408         myseed = tv.tv_usec;
00409     } else
00410     {
00411         myseed = (long) time(NULL);
00412     }
00413     return myseed;
00414 }
00415 
00416 INTERNAL void SYS_Exit(int iRetVal)
00417 {
00418     _exit(iRetVal);
00419 }
00420 
00421 INTERNAL int SYS_Unlink(const char *pcFile)
00422 {
00423     return unlink(pcFile);
00424 }
00425 

Generated on Mon Nov 24 18:20:34 2008 for pcsc-lite by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002