AcessNative - Spawn in libacess-native (with AN_GETPATH syscall)
[tpg/acess2.git] / AcessNative / acesskernel_src / syscalls.c
index 4458846..344745f 100644 (file)
 // === IMPORTS ===
 extern int     Threads_Fork(void);     // AcessNative only function
 extern int     Threads_Spawn(int nFD, int FDs[], const void *info);
+extern int     Syscall_AN_GetPath_Real(char *Dest, size_t DstLen, const char *Path);
 
 // === TYPES ===
 typedef int    (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int *Sizes);
 
 // === MACROS ===
+#define _SYSCALL_CHKFMT(_name,_fmtstr,Fmt) do{ \
+       if(strcmp(Fmt,_fmtstr) != 0) {\
+               *Errno = EINVAL;\
+               Log_Error("Syscalls", "Call %s takes args '%s', given '%s'", #_name, _fmtstr, Fmt);\
+               return -1;\
+       }\
+} while(0)
 #define SYSCALL6(_name, _fmtstr, _t0, _t1, _t2, _t3, _t4, _t5, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
        _t0 a0;_t1 a1;_t2 a2;_t3 a3;_t4 a4;_t5 a5;\
-       if(strcmp(Fmt,_fmtstr)!=0)return 0;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        a1 = *(_t1*)Args;Args+=sizeof(_t1);\
        a2 = *(_t2*)Args;Args+=sizeof(_t2);\
@@ -35,7 +43,7 @@ typedef int   (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int
 }
 #define SYSCALL5(_name, _fmtstr, _t0, _t1, _t2, _t3, _t4, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
        _t0 a0;_t1 a1;_t2 a2;_t3 a3;_t4 a4;\
-       if(strcmp(Fmt,_fmtstr)!=0)return 0;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        a1 = *(_t1*)Args;Args+=sizeof(_t1);\
        a2 = *(_t2*)Args;Args+=sizeof(_t2);\
@@ -46,7 +54,7 @@ typedef int   (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int
 }
 #define SYSCALL4(_name, _fmtstr, _t0, _t1, _t2, _t3, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
        _t0 a0;_t1 a1;_t2 a2;_t3 a3;\
-       if(strcmp(Fmt,_fmtstr)!=0)return 0;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        a1 = *(_t1*)Args;Args+=sizeof(_t1);\
        a2 = *(_t2*)Args;Args+=sizeof(_t2);\
@@ -57,7 +65,7 @@ typedef int   (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int
 
 #define SYSCALL3(_name, _fmtstr, _t0, _t1, _t2, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
        _t0 a0;_t1 a1;_t2 a2;\
-       if(strcmp(Fmt,_fmtstr)!=0)return 0;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        a1 = *(_t1*)Args;Args+=sizeof(_t1);\
        a2 = *(_t2*)Args;Args+=sizeof(_t2);\
@@ -67,7 +75,7 @@ typedef int   (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int
 
 #define SYSCALL2(_name, _fmtstr, _t0, _t1, _call) int _name(Uint*Errno,const char*Fmt,void*Args,int*Sizes){\
        _t0 a0;_t1 a1;\
-       if(strcmp(Fmt,_fmtstr)!=0)return 0;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        a1 = *(_t1*)Args;Args+=sizeof(_t1);\
        LOG("SYSCALL2 '%s' %p %p", Fmt, (intptr_t)a0,(intptr_t)a1);\
@@ -76,14 +84,14 @@ typedef int (*tSyscallHandler)(Uint *Errno, const char *Format, void *Args, int
 
 #define SYSCALL1(_name, _fmtstr, _t0, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
        _t0 a0;\
-       if(strcmp(Fmt,_fmtstr)!=0)return 0;\
+       _SYSCALL_CHKFMT(_name,_fmtstr,Fmt);\
        a0 = *(_t0*)Args;Args+=sizeof(_t0);\
        LOG("SYSCALL1 '%s' %p", Fmt,(intptr_t)a0);\
        _call;\
 }
 
 #define SYSCALL0(_name, _call) int _name(Uint*Errno,const char*Fmt, void*Args,int*Sizes){\
-       if(strcmp(Fmt,"")!=0)return 0;\
+       _SYSCALL_CHKFMT(_name,"",Fmt);\
        LOG("SYSCALL0");\
        _call;\
 }
@@ -119,6 +127,7 @@ SYSCALL3(Syscall_Read, "iid", int, int, void *,
 );
 SYSCALL3(Syscall_Write, "iid", int, int, const void *,
        if( Sizes[2] < a1 ) {
+               Log_Warning("Syscalls", "Write - %x < %x", (int)Sizes[2], (int)a1);
                *Errno = EINVAL;
                return -1;
        }
@@ -166,6 +175,11 @@ SYSCALL4(Syscall_Mount, "ssss", const char *, const char *, const char *, const
 SYSCALL1(Syscall_Chdir, "s", const char *,
        return VFS_ChDir(a0);
 );
+
+SYSCALL2(Syscall_AN_Getpath, "ds", char *, const char *,
+       return Syscall_AN_GetPath_Real(a0, Sizes[0], a1);
+);
+
 SYSCALL0(Syscall_Sleep,
        Threads_Sleep();
        return 0;
@@ -235,26 +249,29 @@ SYSCALL1(Syscall_WaitEvent, "i", int,
 );
 
 const tSyscallHandler  caSyscalls[] = {
-       Syscall_Null,
-       Syscall_Exit,
-       Syscall_Open,
-       Syscall_Close,
-       Syscall_Read,
-       Syscall_Write,
-       Syscall_Seek,
-       Syscall_Tell,
-       Syscall_IOCtl,
-       Syscall_FInfo,
-       Syscall_ReadDir,
-       Syscall_OpenChild,
-       Syscall_GetACL,
-       Syscall_Mount,
-       NULL,   // SYS_REOPEN
-       Syscall_Chdir,
+       [SYS_NULL]      = Syscall_Null,
+       [SYS_EXIT]      = Syscall_Exit,
+       [SYS_OPEN]      = Syscall_Open,
+       [SYS_CLOSE]     = Syscall_Close,
+       [SYS_COPYFD]    = NULL,
+       [SYS_FDFLAGS]   = NULL,
+       [SYS_READ]      = Syscall_Read,
+       [SYS_WRITE]     = Syscall_Write,
+       [SYS_SEEK]      = Syscall_Seek,
+       [SYS_TELL]      = Syscall_Tell,
+       [SYS_IOCTL]     = Syscall_IOCtl,
+       [SYS_FINFO]     = Syscall_FInfo,
+       [SYS_READDIR]   = Syscall_ReadDir,
+       [SYS_OPENCHILD] = Syscall_OpenChild,
+       [SYS_GETACL]    = Syscall_GetACL,
+       [SYS_MOUNT]     = Syscall_Mount,
+       [SYS_REOPEN]    = NULL, // SYS_REOPEN
+       [SYS_CHDIR]     = Syscall_Chdir,
+       [SYS_AN_GETPATH] = Syscall_AN_Getpath,
        
-       Syscall_WaitTID,
-       Syscall_SetUID,
-       Syscall_SetGID,
+       [SYS_WAITTID]   = Syscall_WaitTID,
+       [SYS_SETUID]    = Syscall_SetUID,
+       [SYS_SETGID]    = Syscall_SetGID,
        
        Syscall_GetTID,
        Syscall_GetPID,
@@ -262,8 +279,8 @@ const tSyscallHandler       caSyscalls[] = {
        Syscall_GetGID,
 
        Syscall_Sleep,
-       Syscall_AN_Fork,
-       Syscall_AN_Spawn,
+       [SYS_AN_FORK]  = Syscall_AN_Fork,
+       [SYS_AN_SPAWN] = Syscall_AN_Spawn,
 
        Syscall_SendMessage,
        Syscall_GetMessage,
@@ -274,12 +291,12 @@ const int ciNumSyscalls = sizeof(caSyscalls)/sizeof(caSyscalls[0]);
 /**
  * \brief Recieve a syscall structure from the server code
  */
-tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
+tRequestHeader *SyscallRecieve(tRequestHeader *Request, size_t *ReturnLength)
 {
        char    formatString[Request->NParams+1];
        char    *inData = (char*)&Request->Params[Request->NParams];
         int    argListLen = 0;
-        int    i, retVal;
+        int    retVal;
        tRequestHeader  *ret;
         int    retValueCount;
         int    retDataLen;
@@ -306,7 +323,7 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
        retDataLen = sizeof(Uint64) + sizeof(Uint32);   
 
        // Get size of argument list
-       for( i = 0; i < Request->NParams; i ++ )
+       for( int i = 0; i < Request->NParams; i ++ )
        {
                argSizes[i] = Request->Params[i].Length;
                switch(Request->Params[i].Type)
@@ -341,7 +358,7 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
                        return NULL;    // ERROR!
                }
        }
-       formatString[i] = '\0';
+       formatString[Request->NParams] = '\0';
        
        LOG("Request %i(%s) '%s'", Request->CallID, casSYSCALL_NAMES[Request->CallID], formatString);
        
@@ -349,7 +366,7 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
                char    argListData[argListLen];
                argListLen = 0;
                // Build argument list
-               for( i = 0; i < Request->NParams; i ++ )
+               for( int i = 0; i < Request->NParams; i ++ )
                {
                        returnData[i] = NULL;
                        switch(Request->Params[i].Type)
@@ -446,7 +463,7 @@ tRequestHeader *SyscallRecieve(tRequestHeader *Request, int *ReturnLength)
        //Log_Debug("Syscalls", "Return 0x%llx", retVal);
        
        retValueCount = 2;
-       for( i = 0; i < Request->NParams; i ++ )
+       for( int i = 0; i < Request->NParams; i ++ )
        {
                if( Request->Params[i].Type != ARG_TYPE_DATA )  continue;
                if( !(Request->Params[i].Flags & ARG_FLAG_RETURN) )     continue;

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