AcessNative - Hexdump sycall, stub shm
[tpg/acess2.git] / AcessNative / ld-acess_src / syscalls.c
index 3c07ef2..965cc4a 100644 (file)
@@ -8,15 +8,25 @@
 #include <stdarg.h>
 #include <string.h>
 #include <stddef.h>
+#include <unistd.h>
+#ifndef __WIN32__
+# include <spawn.h>    // posix_spawn
+#endif
 #include "request.h"
 
-#define DEBUG(str, x...)       Debug(str, x)
+#define assert(cnd) do{ \
+       if( !(cnd) ) { \
+               fprintf(stderr, "%s:%i - assert failed - " #cnd"\n", __FILE__, __LINE__);\
+               exit(-1); \
+       } \
+}while(0)
 
 #define        MAX_FPS 16
 
 // === Types ===
 
 // === IMPORTS ===
+extern int     gbSyscallDebugEnabled;
 
 // === GLOBALS ===
 FILE   *gaSyscall_LocalFPs[MAX_FPS];
@@ -29,11 +39,13 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
         int    direction = 0;  // 0: Invalid, 1: Out, 2: In, 3: Out
        char    *str;
         int    len;
-       
+
        // Eat whitespace
        while(*ArgTypes && *ArgTypes == ' ')    ArgTypes ++;
        if( *ArgTypes == '\0' ) return ArgTypes;
        
+//     DEBUG("ArgTypes = '%s'", ArgTypes);
+       
        // Get direction
        switch(*ArgTypes)
        {
@@ -105,7 +117,7 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
                break;
        // Data (special handling)
        case 'd':
-               len = va_arg(*Args, int);
+               len = va_arg(*Args, size_t);
                str = va_arg(*Args, char*);
                
                // Save the pointer for later
@@ -113,7 +125,7 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
                
                // Create parameter block
                Dest->Type = ARG_TYPE_DATA;
-               Dest->Length = len;
+               Dest->Length = str ? len : 0;
                Dest->Flags = 0;
                if( direction & 2 )
                        Dest->Flags |= ARG_FLAG_RETURN;
@@ -121,7 +133,7 @@ const char *ReadEntry(tRequestValue *Dest, void *DataDest, void **PtrDest, const
                // Has data?
                if( direction & 1 )
                {
-                       if( DataDest )
+                       if( DataDest && str )
                                memcpy(DataDest, str, len);
                }
                else
@@ -153,13 +165,12 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
 {
        va_list args;
         int    paramCount, dataLength;
-        int    retCount = 1, retLength = sizeof(uint64_t);
+        int    retCount = 2, retLength = sizeof(uint64_t) + sizeof(uint32_t);
        void    **retPtrs;      // Pointers to return buffers
        const char      *str;
        tRequestHeader  *req;
        void    *dataPtr;
        uint64_t        retValue;
-        int    i;
        
        // DEBUG!
 //     printf("&tRequestHeader->Params = %i\n", offsetof(tRequestHeader, Params));
@@ -203,6 +214,7 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        req->ClientID = 0;      //< Filled later
        req->CallID = SyscallID;
        req->NParams = paramCount;
+       req->MessageLength = dataLength;
        dataPtr = &req->Params[paramCount];
        
        // Fill `output` and `input`
@@ -225,33 +237,36 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        }
        va_end(args);
        
-       // Send syscall request
+       // --- Send syscall request
        if( SendRequest(req, dataLength, retLength) < 0 ) {
                fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID);
                exit(127);
        }
        
-       // Parse return value
-       dataPtr = &req->Params[req->NParams];
-       retValue = 0;
-       if( req->NParams >= 1 )
-       {
-               switch(req->Params[0].Type)
-               {
-               case ARG_TYPE_INT64:
-                       retValue = *(uint64_t*)dataPtr;
-                       dataPtr += req->Params[0].Length;
-                       break;
-               case ARG_TYPE_INT32:
-                       retValue = *(uint32_t*)dataPtr;
-                       dataPtr += req->Params[0].Length;
-                       break;
-               }       
+       if( !(req->NParams >= 2) ) {
+               fprintf(stderr, "syscalls.c: Too few return params (%i)", req->NParams);
+               exit(127);
        }
+       dataPtr = (void*)&req->Params[req->NParams];
+       // return
+       assert(req->Params[0].Type == ARG_TYPE_INT64);
+       assert(req->Params[0].Length == sizeof(uint64_t));
+       retValue = *(uint64_t*)dataPtr;
+       dataPtr += sizeof(uint64_t);
+       // errno
+       assert(req->Params[1].Type == ARG_TYPE_INT32);
+       assert(req->Params[1].Length == sizeof(uint32_t));
+       acess__errno = *(uint32_t*)dataPtr;
+       dataPtr += sizeof(uint32_t);
        
        // Write changes to buffers
+       if( req->NParams - 2 != retCount ) {
+               fprintf(stderr, "syscalls.c: Return count inbalance (%i - 1 != exp %i) [Call %i]\n",
+                       req->NParams, retCount, SyscallID);
+               exit(127);
+       }
        retCount = 0;
-       for( i = 1; i < req->NParams; i ++ )
+       for( unsigned int i = 2; i < req->NParams; i ++ )
        {
                #if 0
                 int     j;
@@ -260,6 +275,7 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
                        printf(" %02x", ((uint8_t*)dataPtr)[j]);
                printf("\n");
                #endif
+               assert( req->Params[i].Type == ARG_TYPE_DATA );
                memcpy( retPtrs[retCount++], dataPtr, req->Params[i].Length );
                dataPtr += req->Params[i].Length;
        }
@@ -267,35 +283,59 @@ uint64_t _Syscall(int SyscallID, const char *ArgTypes, ...)
        free( req );
        free( retPtrs );
        
-       DEBUG(": %llx", retValue);
+       if( gbSyscallDebugEnabled ) {
+               SYSTRACE(": %i 0x%llx", SyscallID, retValue);
+       }
        
        return retValue;
 }
 
+int native_int_getfd(void)
+{
+       for(int ret = 0; ret < MAX_FPS; ret ++ )
+               if( gaSyscall_LocalFPs[ret] == NULL )
+                       return ret;
+       return -1;
+}
 
 int native_open(const char *Path, int Flags)
 {
-       int     ret;
-       for(ret = 0; ret < MAX_FPS && gaSyscall_LocalFPs[ret]; ret ++ ) ;
-       if(ret == MAX_FPS)      return -1;
+       int     ret = native_int_getfd();
+       if(ret == -1)   return -1;
        // TODO: Handle directories
-       gaSyscall_LocalFPs[ret] = fopen(&Path[4], "r+");
+       gaSyscall_LocalFPs[ret] = fopen(Path, "r+");
        if(!gaSyscall_LocalFPs[ret])    return -1;
        return ret;
 }
 
+int native_shm(const char *Tag, int Flags)
+{
+       // int ret = native_int_getfd();
+       //if(ret == -1) return -1;
+       //if( strcmp(Tag, "anon") == 0 )
+       //      path = "/AcessNative/anon/RAND";
+       //      FD = shm_open(path, O_RDWD);
+       //shm_unlink(path);
+       //else
+       //      path = "/Acessnative/named/<TAG>";
+       // int FD = shm_open(path, O_RDWD);
+       //shm_unlink(path);
+       //gaSyscall_LocalFPs[ret] = 
+       return -1;
+}
+
 void native_close(int FD)
 {
        fclose( gaSyscall_LocalFPs[FD] );
        gaSyscall_LocalFPs[FD] = NULL;
 }
 
-size_t native_read(int FD, size_t Bytes, void *Dest)
+size_t native_read(int FD, void *Dest, size_t Bytes)
 {
        return fread( Dest, Bytes, 1, gaSyscall_LocalFPs[FD] );
 }
 
-size_t native_write(int FD, size_t Bytes, const void *Src)
+size_t native_write(int FD, const void *Src, size_t Bytes)
 {
        return fwrite( Src, Bytes, 1, gaSyscall_LocalFPs[FD] );
 }
@@ -314,3 +354,30 @@ uint64_t native_tell(int FD)
 {
        return ftell( gaSyscall_LocalFPs[FD] );
 }
+
+int native_execve(const char *filename, const char *const argv[], const char *const envp[])
+{
+       int ret;
+       ret = execve(filename, (void*)argv, (void*)envp);
+       perror("native_execve");
+       return ret;
+}
+
+int native_spawn(const char *filename, const char *const argv[], const char *const envp[])
+{
+       int rv;
+
+       fprintf(stderr, "native_spawn('%s')\n", filename);
+
+       #if __WIN32__
+       rv = _spawnve(_P_NOWAIT, filename, argv, envp);
+       #else
+       rv = posix_spawn(NULL, filename, NULL, NULL, (void*)argv, (void*)envp);
+       #endif
+       
+       if( rv == 0 ) {
+               perror("native_spawn");
+       }
+       
+       return rv;
+}

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