#include <dlfcn.h>
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <pthread.h>
namespace ZhengYangSpace {
typedef void (*sighandler_t)(int);
typedef void *(*pthread_func_t)(void* )
class SystemCallServer
{
public:
/*filname: so_name flag:RTLD_LAZY or RTLD_NOW*/
void *SystemCallDlOpen(const char *filename, int flag);
void *SystemCallDlDlSym(void *handle, const char* symbol);
int SystemCallDlClose(void *handle);
void *SystemCallDlError(void);
void SystemCallPerror(const char* s);
void SystemCallStrerror(int errnum);
/*
addr:指定了映射区域的起始地址
length:指定了映射区域的长度
port: PROT_EXEC | PORT_READ | PROT_WRITE | PROT_NONE
flags: MAP_SHARED | MAP_PRIVATE
fd:
offset:
*/
void *SystemCallMmap(void *addr, size_t length, int port, int flags, int fd, off_t offset);
int SystemCallMunmap(void *addr, size_t length);
/*
pathname:路径
flags:O_RDONLY | O_WRONLYM| O_RDWR | O_CREAT
mode : 0664
*/
int SystemCallFileOpen(const char *pathname, int flags, int mode);
int SystemCallFileClose(int fd);
int SystemCallFileWrite(int fd, const void *buf, size_t connt);
int SystemCallFileRead(int fd, void *buf, size_t count);
int SystemCallFcntl(int fd, int cmd, struct flock* lock);
/*SEEK_SET SEEK_CUR SEEK_END*/
off_t SystemCallFileLseek(int fd, off_t offset, int whence);
DIR *SystemCallOpenDir(const char *name);
struct dirtent *SystemCallReadDir(Dir *dirent);
int SystemCallCloseDir(Dir *dirent);
pid_t SystemCallCreatProcess(void);
void SystemCallExitProcess(void);
pid_t SystemCallWaitPid(pid_t pid, int *status, int options);
void SystemCallChangeChildImage(const char*\ filename, char* const argv[], char* const envp);
int SystemCallProcessComunicationPipe(int fd[2]);
int SystemCallProcessComunicationFifo(const char* filename, mode_t mode);
key_t SystemCallGetSystemComunicationSystemId(const char* pathname, int proj_id);
int SystemCallMsgget(ket_t key, int msgflg);
sighandler_t SystemCallSendSingle(int signum, sighandler_t handler);
unsigned int SystemCallSendSinAlrmSelf(unsigned int seconds);
/*
how: SIG_BLOCK 将信号掩码设置成为当前掩码 与 set |
SIG_UNBLOCK 将set的信号 从当前的 信号阻塞里面移除
SIG_SETMASK 将set设置为当前信号的掩码集(信号屏蔽集)
*/
int SystemCallGetAndSetSingleSet(int how, const sigset_t *set,
sigset_t *oldset);
int SystemCallEmptySingleSet(sigset_t *set);
int SystemCallAddSingleSet(sigset_t *set);
int SystemCallSingleSigpending(sigset_t *set);
int SystemCallWaitSingle();
key_t SystemCallGetSystemComunicationSystemId(const char* pathname, int proj_id);
int SystemCallGetMsgDuque(ket_t key, int msgflg);
int SystemCallSendMsg(int msgid, void* msgp, size_t msgsz, int msgflg);
size_t SystemCallGetMsg(int msgid, void* msgp, size_t msgsz, long msgtype);
int SystemCallGetShm(key_t key, size_t size, int shmflg);
void *SystemCallShmat(int shmid, const void* shmaddr,int shmflg);
int SystemCallShmdt(const void* shmaddr);
int SystemCallCreatPthread(pthread_t* tid_t, const pthread_addr_t *attr, pthread_func_t func, void *arg);
void SystemCallExitPthread(void * retval);
int SystemCallJoinPhtread(pthread_t thread, void ** retval);
int SystemCallDetachPthread(pthread_t thread);
int SystemCallPthreadLockInit(pthread_mutex_t * mutex, const pthread_mutexattr_t * mutexattr);
int SystemCallPthreadLockLocked(pthread_mutex_t * mutex);
int SystemCallPthreadLockUnlocked(pthread_mutex_t * mutex);
int SystemCallPthreadLockDestroy(pthread_mutex_t * mutex);
int SystemCallPthreadCondInit(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
int SystemCallPthreadCondWait(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
int SystemCallPthreadCondSignal(pthread_cond_t *cond);
int SystemCallPthreadCondBroadcast(pthread_cond_t *cond);
int SystemCallPthreadCondDestroy(pthread_cond_t *cond);
int SystemCallSemInit(sem_t *sem, int pshared, unsigned int value);
int SystemCallSemPost(set_t *sem);
int SystemCallSetWait(set_t *sem);
int SystemCallSetDestroy(set_t *sem);
}
}//ZhengYangSpace
#include "system_call_server.h"
namespace ZhengYangSpace {
void* SystemCallServer::SystemCallDlOpen(const char *filename, int flag)
{
return dlopen(filename, flag);
}
void* SystemCallServer::SystemCallDlSym(void *handle, const char* symbol)
{
dlsys(handle, symbol)
}
int SystemCallServer::SystemCallDlClose(void *handle)
{
return dlclose(handle);
}
void *SystemCallServer::SystemCallDlError(void)
{
dlerror();
}
void SystemCallServer::SystemCallPerror(const char* s)
{
perror(s);
}
void SystemCallServer::SystemCallStrerror(int errnum)
{
strerror(errnum);
}
void *SystemCallServer::SystemCallMmap(void *addr, size_t length, int port, int flags, int fd, off_t offset)
{
mmap(addr, length, port, flags, fd, offset);
}
int SystemCallServer::SytemCallMunmap(void *addr, size_t length)
{
munmap(addr, length);
}
int SystemCallServer::SystemCallFileOpen(const char *pathname, int flags, int mode)
{
return open(filename, flags, mode);
}
int SystemCallServer::SystemCallFileClose(int fd)
{
return close(fd);
}
int SystemCallServer::SystemCallFileWrite(int fd, const void *buf, size_t connt)
{
return write(fd, buf, length);
}
int SystemCallServer::SystemCallFileRead(int fd, void *buf, size_t count)
{
return read(fd, buf, length);
}
off_t SystemCallServer::SystemCallFileLseek(int fd, off_t offset, int whence)
{
return lseek(fd, offset, whence);
}
DIR *SystemCallServer::SystemCallOpenDir(const char *name)
{
return opendir(name);
}
struct dirtent *SystemCallServer::SystemCallReadDir(Dir *dir)
{
return readdir(dir);
}
int SystemCallServer::SystemCallCloseDir(Dir *dir)
{
return closedir(dir);
}
int SystemCallServer::SystemCallFcntl(int fd, int cmd, struct flock* lock)
{
return fcntl(fd, cmd, lock);
}
pid_t SystemCallServer::SystemCallCreatProcess(void)
{
return fork();
}
void SystemCallServer::SystemCallExitProcess(int status)
{
_exit(status);
}
pid_t SystemCallServer::SystemCallWaitPid(bool type, pid_t pid, int *status, int options)
{
if (type == 0) {
return wait(status);
} else {
return waitpid(pid, status, options);
}
}
void SystemCallServer::SystemCallChangeChildImage(const char*\ filename, char* const argv[], char* const envp)
)
{
exevce(filename, argv, envp);
}
int SystemCallServer::SystemCallProcessComunicationPipe(int fd[2])
{
return pipe(fd);
}
sighandler_t SystemCallServer::SystemCallChangeSingleHandle(int signum, sighandler_t handler);
{
return single(signum, handler);
}
int SystemCallServer::SystemCallSendSingle(pid_t pid, int sig);
{
return kill(pid, sig);
}
unsigned int SystemCallServer::SystemCallSendSinAlrmSelf(unsigned int seconds)
{
return alarm(seconds);
}
int SystemCallServer::SystemCallGetAndSetSingleSet(int how, const sigset_t *set, sigset_t *oldset)
{
return sigprocmask(how, set, oldset);
}
int SystemCallServer::SystemCallEmptySingleSet(sigset_t *set)
{
return sigemptyset(&set);
}
int SystemCallServer::SystemCallAddSingleSet(sigset_t *set, int sig)
{
return sigaddset(set, sig);
}
int SystemCallServer::SystemCallSingleSigpending(sigset_t *set)
{
return sigpending(set);
}
int SystemCallServer::SystemCallWaitSingle()
{
return pause();
}
key_t SystemCallServer::SystemCallGetSystemComunicationSystemId(const char* pathname, int proj_id)
{
return ftok(pathname, proj_id);
}
int SystemCallServer::SystemCallGetMsgDuque(ket_t key, int msgflg)
{
return msgget(key, msgflg);
}
int SystemCallServer::SystemCallSendMsg(int msgid, void* msgp, size_t msgsz, int msgflg)
{
return msgsnd(msgid, msgp, msgsz, msgflg);
}
size_t SystemCallServer::SystemCallGetMsg(int msgid, void* msgp, size_t msgsz, int msgflg, long msgtype)
{
return msgrcv(msgid, msgp, msgsz, msgflg, msgtype);
}
int SystemCallServer::SystemCallGetShm(key_t key, size_t size, int shmflg)
{
return shmget(key, size, shmflg);
}
void *SystemCallServer::SystemCallShmat(int shmid, const void* shmaddr,int shmflg)
{
return shmat(shmid, shmaddr, shmflg);
}
int SystemCallServer::SystemCallShmdt(const void* shmaddr)
{
return shmdt(shmaddr);
}
int SystemCallServer::SystemCallCreatPthread(pthread_t* tid_t, const pthread_addr_t *attr, pthread_func_t func, void *arg)
{
return pthread_create(tid_t, attr, func, arg);
}
void SystemCallServer::SystemCallExitPthread(void * retval)
{
return pthread_exit(retval);
}
int SystemCallServer::SystemCallJoinPhtread(pthread_t thread, void ** retval)
{
return pthread_join(thread, retval);
}
int SystemCallServer::SystemCallDetachPthread(pthread_t thread)
{
return pthread_detach(thread);
}
int SystemCallServer::SystemCallPthreadLockInit(pthread_mutex_t * mutex, const pthread_mutexattr_t * mutexattr)
{
return pthread_mutex_init(mutex, mutexattr);
}
int SystemCallServer::SystemCallPthreadLockLocked(pthread_mutex_t * mutex)
{
return pthread_mutex_lock(mutex);
}
int SystemCallServer::SystemCallPthreadLockUnlocked(pthread_mutex_t * mutex)
{
return pthread_mutex_unlock(mutex);
}
int SystemCallServer::SystemCallPthreadLockDestroy(pthread_mutex_t * mutex)
{
return pthread_mutex_destroy(mutex);
}
int SystemCallServer::SystemCallPthreadCondInit(pthread_cond_t *cond, pthread_condattr_t *cond_attr)
{
return pthread_cond_init(cond, cond_attr);
}
int SystemCallServer::SystemCallPthreadCondWait(pthread_cond_t *cond, pthread_condattr_t *cond_attr)
{
return pthread_cond_wait(cond, cond_attr);
}
int SystemCallServer::SystemCallPthreadCondSignal(pthread_cond_t *cond)
{
return pthread_cond_signal(cond);
}
int SystemCallServer::SystemCallPthreadCondBroadcast(pthread_cond_t *cond)
{
return pthread_cond_broadcast(cond);
}
int SystemCallServer::SystemCallPthreadCondDestroy(pthread_cond_t *cond)
{
return pthread_cond_destroy(cond);
}
int SystemCallServer::SystemCallSemInit(sem_t *sem, int pshared, unsigned int value)
{
set_init(sem, pshared, value);
}
int SystemCallServer::SystemCallSemPost(set_t *sem)
{
set_post(sem);
}
int SystemCallServer::SystemCallSetWait(set_t *sem)
{
set_wait(sem);
}
int SystemCallServer::SystemCallSetDestroy(set_t *sem)
{
set_destroy(sem);
}
}//ZhengYangSpace