Merge branch 'master' of git://git.ucc.asn.au/tpg/acess2
[tpg/acess2.git] / AcessNative / ld-acess_src / exports.c
index 55606a8..dcfc526 100644 (file)
 #include <stddef.h>
 
 #define DEBUG(v...)    Debug(v)
+//#define DEBUG(v...)  do{}while(0)//Debug(v)
 #define PAGE_SIZE      4096
 
 typedef struct sFILE   FILE;
 
-extern FILE    *stderr;
 extern void    exit(int) __attribute__ ((noreturn));
 extern int     printf(const char *, ...);
 extern int     fprintf(FILE *,const char *, ...);
@@ -29,6 +29,7 @@ extern int    giSyscall_ClientID;     // Needed for execve
 extern void    _InitSyscalls(void);
 extern void    _CloseSyscalls(void);
 
+extern void    Warning(const char *Format, ...);
 extern void    Debug(const char *Format, ...);
 extern int     AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
 
@@ -37,15 +38,16 @@ extern int  AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
 
 // === GLOBALS ===
 int    acess__errno;
+char   *gsExecutablePath = "./ld-acess";
 
 // === CODE ===
 // --- VFS Calls
-int acess_chdir(const char *Path)
+int acess__SysChdir(const char *Path)
 {
        return _Syscall(SYS_CHDIR, ">s", Path);
 }
 
-int acess_open(const char *Path, int Flags)
+int acess__SysOpen(const char *Path, int Flags)
 {
        if( strncmp(Path, "$$$$", 4) == 0 )
        {
@@ -55,7 +57,8 @@ int acess_open(const char *Path, int Flags)
        return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
 }
 
-void acess_close(int FD) {
+void acess__SysClose(int FD)
+{
        if(FD & NATIVE_FILE_MASK) {
                return native_close(FD & (NATIVE_FILE_MASK-1));
        }
@@ -63,12 +66,12 @@ void acess_close(int FD) {
        _Syscall(SYS_CLOSE, ">i", FD);
 }
 
-int acess_reopen(int FD, const char *Path, int Flags) {
+int acess__SysReopen(int FD, const char *Path, int Flags) {
        DEBUG("reopen(0x%x, \"%s\", 0x%x)", FD, Path, Flags);
        return _Syscall(SYS_REOPEN, ">i >s >i", FD, Path, Flags);
 }
 
-size_t acess_read(int FD, void *Dest, size_t Bytes) {
+size_t acess__SysRead(int FD, void *Dest, size_t Bytes) {
        if(FD & NATIVE_FILE_MASK)
                return native_read(FD & (NATIVE_FILE_MASK-1), Dest, Bytes);
 //     if( FD > 2 )
@@ -76,7 +79,7 @@ size_t acess_read(int FD, void *Dest, size_t Bytes) {
        return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
 }
 
-size_t acess_write(int FD, const void *Src, size_t Bytes) {
+size_t acess__SysWrite(int FD, const void *Src, size_t Bytes) {
        if(FD & NATIVE_FILE_MASK)
                return native_write(FD & (NATIVE_FILE_MASK-1), Src, Bytes);
 //     if( FD > 2 )
@@ -84,7 +87,8 @@ size_t acess_write(int FD, const void *Src, size_t Bytes) {
        return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
 }
 
-int acess_seek(int FD, int64_t Ofs, int Dir) {
+int acess__SysSeek(int FD, int64_t Ofs, int Dir)
+{
        if(FD & NATIVE_FILE_MASK) {
                return native_seek(FD & (NATIVE_FILE_MASK-1), Ofs, Dir);
        }
@@ -92,13 +96,15 @@ int acess_seek(int FD, int64_t Ofs, int Dir) {
        return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
 }
 
-uint64_t acess_tell(int FD) {
+uint64_t acess__SysTell(int FD)
+{
        if(FD & NATIVE_FILE_MASK)
                return native_tell( FD & (NATIVE_FILE_MASK-1) );
+       DEBUG("tell(0x%x)", FD);
        return _Syscall(SYS_TELL, ">i", FD);
 }
 
-int acess_ioctl(int fd, int id, void *data) {
+int acess__SysIOCtl(int fd, int id, void *data) {
         int    len;
        DEBUG("ioctl(%i, %i, %p)", fd, id, data);
        // NOTE: The length here is hacky and could break
@@ -108,7 +114,7 @@ int acess_ioctl(int fd, int id, void *data) {
                len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
        return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
 }
-int acess_finfo(int fd, t_sysFInfo *info, int maxacls) {
+int acess__SysFInfo(int fd, t_sysFInfo *info, int maxacls) {
 //     DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
        DEBUG("finfo(%i, %p, %i)", fd, info, maxacls);
        return _Syscall(SYS_FINFO, ">i <d >i",
@@ -118,12 +124,12 @@ int acess_finfo(int fd, t_sysFInfo *info, int maxacls) {
                );
 }
 
-int acess_readdir(int fd, char *dest) {
-       DEBUG("readdir(%i, %p)", fd, dest);
+int acess__SysReadDir(int fd, char *dest) {
+       DEBUG("SysReadDir(%i, %p)", fd, dest);
        return _Syscall(SYS_READDIR, ">i <d", fd, 256, dest);
 }
 
-int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout, uint32_t events)
+int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, int64_t *timeout, uint32_t events)
 {
        DEBUG("_SysSelect(%i, %p, %p, %p, %p, 0x%x)", nfds, read, write, error, timeout, events);
        return _Syscall(SYS_SELECT, ">i ?d ?d ?d >d >i", nfds,
@@ -135,21 +141,18 @@ int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, time_
                );
 }
 
-int acess_select(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout)
-{
-       return acess__SysSelect(nfds, read, write, error, timeout, 0);
-}
-
-
 int acess__SysOpenChild(int fd, char *name, int flags) {
+       DEBUG("_SysOpenChild(0x%x, '%s', 0x%x)", fd, name, flags);
        return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
 }
 
 int acess__SysGetACL(int fd, t_sysACL *dest) {
+       DEBUG("%s(0x%x, %p)", __func__, fd, dest);
        return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
 }
 
 int acess__SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
+       DEBUG("%s('%s', '%s', '%s', '%s')", __func__, Device, Directory, Type, Options);
        return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
 }
 
@@ -161,7 +164,7 @@ int acess__SysSetFaultHandler(int (*Handler)(int)) {
 }
 
 // --- Memory Management ---
-uint64_t acess__SysAllocate(uint vaddr)
+uint64_t acess__SysAllocate(uintptr_t vaddr)
 {
        if( AllocateMemory(vaddr, 0x1000) == -1 )       // Allocate a page
                return 0;
@@ -170,12 +173,16 @@ uint64_t acess__SysAllocate(uint vaddr)
 }
 
 // --- Process Management ---
-int acess_clone(int flags, void *stack)
+int acess__SysClone(int flags, void *stack)
 {
+       #ifdef __WIN32__
+       Warning("Win32 does not support anything like fork(2), cannot emulate");
+       exit(-1);
+       #else
        extern int fork(void);
        if(flags & CLONE_VM) {
                 int    ret, newID, kernel_tid=0;
-               printf("USERSIDE fork()\n");
+               Debug("USERSIDE fork()");
                
                newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
                ret = fork();
@@ -186,8 +193,8 @@ int acess_clone(int flags, void *stack)
                if(ret == 0)
                {
                        _CloseSyscalls();
-                       _InitSyscalls();
                        giSyscall_ClientID = newID;
+                       _InitSyscalls();
                        return 0;
                }
                
@@ -196,12 +203,13 @@ int acess_clone(int flags, void *stack)
        }
        else
        {
-               fprintf(stderr, "ERROR: Threads currently unsupported\n");
+               Warning("ERROR: Threads currently unsupported\n");
                exit(-1);
        }
+       #endif
 }
 
-int acess_execve(char *path, char **argv, char **envp)
+int acess__SysExecVE(char *path, char **argv, const char **envp)
 {
         int    i, argc;
        
@@ -211,7 +219,7 @@ int acess_execve(char *path, char **argv, char **envp)
        for( argc = 0; argv[argc]; argc ++ ) ;
        DEBUG(" acess_execve: argc = %i", argc);
 
-       char    *new_argv[7+argc+1];
+       const char      *new_argv[7+argc+1];
        char    client_id_str[11];
        char    socket_fd_str[11];
        sprintf(client_id_str, "%i", giSyscall_ClientID);
@@ -244,13 +252,49 @@ int acess_execve(char *path, char **argv, char **envp)
        return native_execve("./ld-acess", new_argv, envp);
 }
 
-void acess_sleep(void)
+int acess__SysSpawn(const char *binary, const char **argv, const char **envp, int nfd, int fds[], struct s_sys_spawninfo *info)
 {
-       _Syscall(SYS_SLEEP, "");
+        int    argc = 0;
+       while( argv[argc++] );
+
+       Debug("_SysSpawn('%s', %p (%i), %p, %i, %p, %p)",
+               binary, argv, argc, envp, nfd, fds, info);
+
+        int    kernel_tid;
+        int    newID;
+       newID = _Syscall(SYS_AN_SPAWN, "<d >d >d", sizeof(int), &kernel_tid,
+               nfd*sizeof(int), fds,
+               info ? sizeof(*info) : 0, info);
+
+       const char      *new_argv[5+argc+1];
+        int    new_argc = 0, i;
+       char    client_id_str[11];
+       sprintf(client_id_str, "%i", newID);
+       new_argv[new_argc++] = gsExecutablePath;       // TODO: Get path to ld-acess executable
+       new_argv[new_argc++] = "--key";
+       new_argv[new_argc++] = client_id_str;
+       new_argv[new_argc++] = "--binary";
+       new_argv[new_argc++] = binary;
+       for( i = 0; argv[i]; i ++)
+               new_argv[new_argc++] = argv[i];
+       new_argv[new_argc++] = NULL;
+       
+       // TODO: Debug output?
+       
+       native_spawn(gsExecutablePath, new_argv, envp);
+
+       return kernel_tid;
 }
 
-int acess_waittid(int TID, int *ExitStatus)
+//void acess_sleep(void)
+//{
+//     DEBUG("%s()", __func__);
+//     _Syscall(SYS_SLEEP, "");
+//}
+
+int acess__SysWaitTID(int TID, int *ExitStatus)
 {
+       DEBUG("%s(%i, %p)", __func__, TID, ExitStatus);
        return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
 }
 
@@ -261,25 +305,24 @@ int acess_getpid(void) { return _Syscall(SYS_GETPID, ""); }
 int acess_getuid(void) { return _Syscall(SYS_GETUID, ""); }
 int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
 
-int acess_SysSendMessage(int DestTID, int Length, void *Data)
+int acess__SysSendMessage(int DestTID, int Length, void *Data)
 {
+       DEBUG("%s(%i, 0x%x, %p)", __func__, DestTID, Length, Data);
        return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
 }
 
-int acess_SysGetMessage(int *SourceTID, void *Data)
+int acess__SysGetMessage(int *SourceTID, int BufLen, void *Data)
 {
-//     static __thread int lastlen = 1024;
-       int lastlen;
-
-       lastlen = _Syscall(SYS_GETMSG, "<d <d",
+       DEBUG("%s(%p, %p)", __func__, SourceTID, Data);
+       return _Syscall(SYS_GETMSG, "<d <d",
                SourceTID ? sizeof(uint32_t) : 0, SourceTID,
-               Data ? 1024 : 0, Data
+               BufLen, Data
                );
-       return lastlen;
 }
 
 int acess__SysWaitEvent(int Mask)
 {
+       DEBUG("%s(%x)", __func__, Mask);
        return _Syscall(SYS_WAITEVENT, ">i", Mask);
 }
 
@@ -304,42 +347,51 @@ void acess__exit(int Status)
        exit(Status);
 }
 
+uint32_t acess__SysSetMemFlags(uintptr_t vaddr, uint32_t flags, uint32_t mask)
+{
+       // TODO: Impliment acess__SysSetMemFlags?
+       return 0;
+}
+
 
 // === Symbol List ===
 #define DEFSYM(name)   {#name, &acess_##name}
 const tSym     caBuiltinSymbols[] = {
        DEFSYM(_exit),
        
-       DEFSYM(chdir),
-       DEFSYM(open),
-       DEFSYM(close),
-       DEFSYM(reopen),
-       DEFSYM(read),
-       DEFSYM(write),
-       DEFSYM(seek),
-       DEFSYM(tell),
-       DEFSYM(ioctl),
-       DEFSYM(finfo),
-       DEFSYM(readdir),
-       DEFSYM(select),
+       DEFSYM(_SysChdir),
+       DEFSYM(_SysOpen),
        DEFSYM(_SysOpenChild),
+       DEFSYM(_SysReopen),
+       DEFSYM(_SysClose),
+       DEFSYM(_SysRead),
+       DEFSYM(_SysWrite),
+       DEFSYM(_SysSeek),
+       DEFSYM(_SysTell),
+       DEFSYM(_SysIOCtl),
+       DEFSYM(_SysFInfo),
+       DEFSYM(_SysReadDir),
        DEFSYM(_SysGetACL),
        DEFSYM(_SysMount),
        DEFSYM(_SysSelect),
        
-       DEFSYM(clone),
-       DEFSYM(execve),
-       DEFSYM(sleep),
+       DEFSYM(_SysClone),
+       DEFSYM(_SysExecVE),
+       DEFSYM(_SysSpawn),
+//     DEFSYM(sleep),
        
-       DEFSYM(waittid),
+       DEFSYM(_SysWaitTID),
+       DEFSYM(gettid),
        DEFSYM(setuid),
        DEFSYM(setgid),
-       DEFSYM(gettid),
+       DEFSYM(getuid),
+       DEFSYM(getgid),
 
-       DEFSYM(SysSendMessage),
-       DEFSYM(SysGetMessage),
+       DEFSYM(_SysSendMessage),
+       DEFSYM(_SysGetMessage),
        
        DEFSYM(_SysAllocate),
+       DEFSYM(_SysSetMemFlags),
        DEFSYM(_SysDebug),
        DEFSYM(_SysSetFaultHandler),
        DEFSYM(_SysWaitEvent),

UCC git Repository :: git.ucc.asn.au