AcessNative - Fixing bad handling of errors, add run script for ARCH=native
[tpg/acess2.git] / AcessNative / libacess-native.so_src / main.c
index f072864..7f62545 100644 (file)
@@ -4,10 +4,11 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-
-extern int     giSyscall_ClientID;
-extern void    Request_Preinit(void);
-extern int     acess__SysOpen(const char *Path, unsigned int flags);
+#include <unistd.h>
+#include <stdbool.h>
+#include "common.h"
+#include <stdint.h>
+#include "../ld-acess_src/exports.h"
 
 #ifdef __WINDOWS__
 int DllMain(void)
@@ -19,13 +20,28 @@ int DllMain(void)
 #endif
 
 #ifdef __linux__
-int __attribute__ ((constructor)) libacessnative_init(void);
+int __attribute__ ((constructor(102))) libacessnative_init(int argc, char *argv[], char **envp);
 
-int libacessnative_init(void)
+const char *getenv_p(char **envp, const char *name)
 {
-       Request_Preinit();
+       size_t  namelen = strlen(name);
+       for(; *envp; envp ++)
+       {
+               if( strncmp(*envp, name, namelen) != 0 )
+                       continue ;
+               if( (*envp)[namelen] != '=' )
+                       continue ;
+               return (*envp)+namelen+1;
+       }
+       return 0;
+}
 
-       const char *preopens = getenv("AN_PREOPEN");
+int libacessnative_init(int argc, char *argv[], char **envp)
+{
+       Request_Preinit();
+       
+       const char *preopens = getenv_p(envp, ENV_VAR_PREOPENS);
+       printf("preopens = %s\n", preopens);
        if( preopens )
        {
                while( *preopens )
@@ -43,7 +59,7 @@ int libacessnative_init(void)
                        path[len] = 0;
                        int fd = acess__SysOpen(path, 6);       // WRITE,READ,no EXEC
                        if( fd == -1 ) {
-                               fprintf(stderr, "Unable to preopen '%s'\n", path);
+                               fprintf(stderr, "Unable to preopen '%s' errno=%i\n", path, acess__errno);
                        }
                        
                        if( !splitter )
@@ -52,12 +68,44 @@ int libacessnative_init(void)
                }
        }
 
-//     if( !getenv("ACESSNATIVE_ID")
+//     if( !getenv(ENV_VAR_KEY)
        
        return 0;
 }
 #endif
 
+int acessnative_spawn(const char *Binary, int SyscallID, const char * const * argv, const char * const * envp)
+{
+        int    envc = 0;
+       while( envp && envp[envc++] )
+               envc ++;
+
+       // Set environment variables for libacess-native
+       // > ACESSNATIVE_KEY=`newID`
+       size_t keystr_len = snprintf(NULL, 0, "%s=%i", ENV_VAR_KEY, SyscallID);
+       char keystr[keystr_len+1];
+       snprintf(keystr, keystr_len+1, "%s=%i", ENV_VAR_KEY, SyscallID);
+       bool    bKeyHit = false;
+       
+       const char *newenv[envc+2+1];
+        int    i = 0;
+       for( ; envp && envp[i]; i ++ )
+       {
+               const char      *ev = envp[i];
+               if( strncmp(ev, ENV_VAR_KEY"=", sizeof(ENV_VAR_KEY"=")) == 0 ) {
+                       ev = keystr;
+                       bKeyHit = true;
+               }
+               newenv[i] = ev;
+       }
+       if( !bKeyHit )
+               newenv[i++] = keystr;
+       newenv[i++] = getenv("LD_LIBRARY_PATH") - (sizeof("LD_LIBRARY_PATH=")-1);       // VERY hacky
+       newenv[i] = NULL;
+       
+       // TODO: Detect native_spawn failing
+       return native_spawn(Binary, argv, newenv);
+}
 
 void Debug(const char *format, ...)
 {
@@ -87,3 +135,9 @@ void __libc_csu_init()
 {
 }
 
+void __stack_chk_fail(void)
+{
+       fprintf(stderr, "__stack_chk_fail");
+       exit(1);
+}
+

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