X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=AcessNative%2Flibacess-native.so_src%2Fmain.c;h=b5f066f70809e5bc4868aec4c2affc7413b8e7bc;hb=3c283c4831c40c14d308a54cefb997832a860bca;hp=900a14c0c26c169ce3d8eb1b60689f27d446aff4;hpb=4ab2ee475505478c2e1fa6547e94b480ef1308ec;p=tpg%2Facess2.git diff --git a/AcessNative/libacess-native.so_src/main.c b/AcessNative/libacess-native.so_src/main.c index 900a14c0..b5f066f7 100644 --- a/AcessNative/libacess-native.so_src/main.c +++ b/AcessNative/libacess-native.so_src/main.c @@ -5,10 +5,12 @@ #include #include #include +#include +#include "common.h" +#include +#include "../ld-acess_src/exports.h" -extern int giSyscall_ClientID; -extern void Request_Preinit(void); -extern int acess__SysOpen(const char *Path, unsigned int flags); +extern int gbSyscallDebugEnabled; #ifdef __WINDOWS__ int DllMain(void) @@ -40,10 +42,13 @@ int libacessnative_init(int argc, char *argv[], char **envp) { Request_Preinit(); - const char *preopens = getenv_p(envp, "AN_PREOPEN"); + //gbSyscallDebugEnabled = 1; + + const char *preopens = getenv_p(envp, ENV_VAR_PREOPENS); printf("preopens = %s\n", preopens); if( preopens ) { + int exp_fd = 0; while( *preopens ) { const char *splitter = strchr(preopens, ':'); @@ -59,8 +64,16 @@ int libacessnative_init(int argc, char *argv[], char **envp) 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); + exit(1); + } + if( fd != exp_fd ) { + // Oh... this is bad + fprintf(stderr, "Pre-opening '%s' resulted in an incorrect FD (expected %i, got %i)", + path, exp_fd, fd); + exit(1); } + exp_fd += 1; if( !splitter ) break; @@ -68,12 +81,44 @@ int libacessnative_init(int argc, char *argv[], char **envp) } } -// 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, ...) { @@ -95,11 +140,3 @@ void Warning(const char *format, ...) printf("\n"); } -void __libc_csu_fini() -{ -} - -void __libc_csu_init() -{ -} -