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

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