#include <sys_generic.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/file.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacTypes.h>
#include "pcscexport.h"
#include "debug.h"
#include "pcscdmonitor.h"
#include <securityd_client/ssclient.h>
#include "config.h"
extern "C" {
int SYS_Initialize()
{
return 0;
}
INTERNAL int SYS_Mkdir(const char *path, int perms)
{
return mkdir(path, perms);
}
INTERNAL int SYS_GetPID(void)
{
return getpid();
}
INTERNAL int SYS_Sleep(int iTimeVal)
{
#ifdef HAVE_NANOSLEEP
struct timespec mrqtp;
mrqtp.tv_sec = iTimeVal;
mrqtp.tv_nsec = 0;
return nanosleep(&mrqtp, NULL);
#else
return sleep(iTimeVal);
#endif
}
INTERNAL int SYS_USleep(int iTimeVal)
{
#ifdef HAVE_NANOSLEEP
struct timespec mrqtp;
mrqtp.tv_sec = iTimeVal/1000000;
mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000;
return nanosleep(&mrqtp, NULL);
#else
usleep(iTimeVal);
return iTimeVal;
#endif
}
INTERNAL int SYS_OpenFile(const char *pcFile, int flags, int mode)
{
return open(pcFile, flags, mode);
}
INTERNAL int SYS_CloseFile(int iHandle)
{
return close(iHandle);
}
INTERNAL int SYS_RemoveFile(const char *pcFile)
{
return remove(pcFile);
}
INTERNAL int SYS_Chmod(const char *path, int mode)
{
return chmod(path, mode);
}
INTERNAL int SYS_Chdir(const char *path)
{
return chdir(path);
}
int SYS_Mkfifo(const char *path, int mode)
{
return mkfifo(path, mode);
}
int SYS_Mknod(const char *path, int mode, int dev)
{
return mknod(path, mode, dev);
}
int SYS_GetUID()
{
return getuid();
}
INTERNAL int SYS_GetGID(void)
{
return getgid();
}
INTERNAL int SYS_SeekFile(int iHandle, int iSeekLength)
{
int iOffset;
iOffset = lseek(iHandle, iSeekLength, SEEK_SET);
return iOffset;
}
INTERNAL int SYS_ReadFile(int iHandle, char *pcBuffer, int iLength)
{
return read(iHandle, pcBuffer, iLength);
}
INTERNAL int SYS_WriteFile(int iHandle, const char *pcBuffer, int iLength)
{
return write(iHandle, pcBuffer, iLength);
}
INTERNAL int SYS_GetPageSize(void)
{
return getpagesize();
}
INTERNAL void *SYS_MemoryMap(int iSize, int iFid, int iOffset)
{
void *vAddress;
vAddress = 0;
vAddress = mmap(0, iSize, PROT_READ | PROT_WRITE,
MAP_SHARED, iFid, iOffset);
return vAddress;
}
INTERNAL void *SYS_PublicMemoryMap(int iSize, int iFid, int iOffset)
{
void *vAddress;
vAddress = 0;
vAddress = mmap(0, iSize, PROT_READ, MAP_SHARED, iFid, iOffset);
if (vAddress == (void*)-1)
{
Log2(PCSC_LOG_CRITICAL, "SYS_PublicMemoryMap() failed: %s",
strerror(errno));
vAddress = NULL;
}
return vAddress;
}
int SYS_MMapSynchronize(void *begin, int length)
{
int rc = msync(begin, length, MS_SYNC | MS_INVALIDATE);
PCSCDMonitor::postNotification(SecurityServer::kNotificationPCSCStateChange);
return rc;
}
int SYS_MUnmap(void *begin, int length)
{
return munmap(begin, length);
}
INTERNAL int SYS_Fork(void)
{
return fork();
}
#ifdef HAVE_DAEMON
int SYS_Daemon(int nochdir, int noclose)
{
return daemon(nochdir, noclose);
}
#endif
int SYS_Wait(int iPid, int iWait)
{
return waitpid(-1, 0, WNOHANG);
}
INTERNAL int SYS_Stat(const char *pcFile, struct stat *psStatus)
{
return stat(pcFile, psStatus);
}
int SYS_Fstat(int iFd)
{
struct stat sStatus;
return fstat(iFd, &sStatus);
}
int SYS_Random(int iSeed, float fStart, float fEnd)
{
int iRandNum = 0;
if (iSeed != 0)
{
srand(iSeed);
}
iRandNum = 1 + (int) (fEnd * rand() / (RAND_MAX + fStart));
srand(iRandNum);
return iRandNum;
}
INTERNAL int SYS_GetSeed(void)
{
struct timeval tv;
struct timezone tz;
long myseed = 0;
tz.tz_minuteswest = 0;
tz.tz_dsttime = 0;
if (gettimeofday(&tv, &tz) == 0)
{
myseed = tv.tv_usec;
} else
{
myseed = (long) time(NULL);
}
return myseed;
}
INTERNAL void SYS_Exit(int iRetVal)
{
_exit(iRetVal);
}
INTERNAL int SYS_Unlink(const char *pcFile)
{
return unlink(pcFile);
}
}