AcessNative - Add userland build to makefile
[tpg/acess2.git] / AcessNative / libacess-native.so_src / main.c
1 /*
2  */
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdbool.h>
9 #include "common.h"
10 #include <stdint.h>
11 #include "../ld-acess_src/exports.h"
12
13 extern int      gbSyscallDebugEnabled;
14
15 #ifdef __WINDOWS__
16 int DllMain(void)
17 {
18         fprintf(stderr, "TODO: Windows libacessnative setup\n");
19         return 0;
20 }
21
22 #endif
23
24 #ifdef __linux__
25 int __attribute__ ((constructor(102))) libacessnative_init(int argc, char *argv[], char **envp);
26
27 const char *getenv_p(char **envp, const char *name)
28 {
29         size_t  namelen = strlen(name);
30         for(; *envp; envp ++)
31         {
32                 if( strncmp(*envp, name, namelen) != 0 )
33                         continue ;
34                 if( (*envp)[namelen] != '=' )
35                         continue ;
36                 return (*envp)+namelen+1;
37         }
38         return 0;
39 }
40
41 int libacessnative_init(int argc, char *argv[], char **envp)
42 {
43         Request_Preinit();
44         
45         //gbSyscallDebugEnabled = 1;
46         
47         const char *preopens = getenv_p(envp, ENV_VAR_PREOPENS);
48         printf("preopens = %s\n", preopens);
49         if( preopens )
50         {
51                 while( *preopens )
52                 {
53                         const char *splitter = strchr(preopens, ':');
54                         size_t  len;
55                         if( !splitter ) {
56                                 len = strlen(preopens);
57                         }
58                         else {
59                                 len = splitter - preopens;
60                         }
61                         char path[len+1];
62                         memcpy(path, preopens, len);
63                         path[len] = 0;
64                         int fd = acess__SysOpen(path, 6);       // WRITE,READ,no EXEC
65                         if( fd == -1 ) {
66                                 fprintf(stderr, "Unable to preopen '%s' errno=%i\n", path, acess__errno);
67                         }
68                         
69                         if( !splitter )
70                                 break;
71                         preopens = splitter + 1;
72                 }
73         }
74
75 //      if( !getenv(ENV_VAR_KEY)
76         
77         return 0;
78 }
79 #endif
80
81 int acessnative_spawn(const char *Binary, int SyscallID, const char * const * argv, const char * const * envp)
82 {
83          int    envc = 0;
84         while( envp && envp[envc++] )
85                 envc ++;
86
87         // Set environment variables for libacess-native
88         // > ACESSNATIVE_KEY=`newID`
89         size_t keystr_len = snprintf(NULL, 0, "%s=%i", ENV_VAR_KEY, SyscallID);
90         char keystr[keystr_len+1];
91         snprintf(keystr, keystr_len+1, "%s=%i", ENV_VAR_KEY, SyscallID);
92         bool    bKeyHit = false;
93         
94         const char *newenv[envc+2+1];
95          int    i = 0;
96         for( ; envp && envp[i]; i ++ )
97         {
98                 const char      *ev = envp[i];
99                 if( strncmp(ev, ENV_VAR_KEY"=", sizeof(ENV_VAR_KEY"=")) == 0 ) {
100                         ev = keystr;
101                         bKeyHit = true;
102                 }
103                 newenv[i] = ev;
104         }
105         if( !bKeyHit )
106                 newenv[i++] = keystr;
107         newenv[i++] = getenv("LD_LIBRARY_PATH") - (sizeof("LD_LIBRARY_PATH=")-1);       // VERY hacky
108         newenv[i] = NULL;
109         
110         // TODO: Detect native_spawn failing
111         return native_spawn(Binary, argv, newenv);
112 }
113
114 void Debug(const char *format, ...)
115 {
116         va_list args;
117         printf("Debug: ");
118         va_start(args, format);
119         vfprintf(stdout, format, args);
120         va_end(args);
121         printf("\n");
122 }
123
124 void Warning(const char *format, ...)
125 {
126         va_list args;
127         printf("Warning: ");
128         va_start(args, format);
129         vfprintf(stdout, format, args);
130         va_end(args);
131         printf("\n");
132 }
133
134 void __libc_csu_fini()
135 {
136 }
137
138 void __libc_csu_init()
139 {
140 }
141
142 void __stack_chk_fail(void)
143 {
144         fprintf(stderr, "__stack_chk_fail");
145         exit(1);
146 }
147

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