AcessNative - Loads login process and runs again (shell doesn't spawn)
[tpg/acess2.git] / AcessNative / ld-acess_src / exports.c
1 /*
2  * AcessNative
3  *
4  * exports.c
5  * - Exported functions
6  */
7 #define DONT_INCLUDE_SYSCALL_NAMES 1
8 #include "../../Usermode/Libraries/ld-acess.so_src/include_exp/acess/sys.h"
9 #include "../syscalls.h"
10 #include "exports.h"
11 #include <stdarg.h>
12 #include <stddef.h>
13
14 #define DEBUG(v...)     Debug(v)
15 #define PAGE_SIZE       4096
16
17 typedef struct sFILE    FILE;
18
19 extern FILE     *stderr;
20 extern void     exit(int) __attribute__ ((noreturn));
21 extern int      printf(const char *, ...);
22 extern int      fprintf(FILE *,const char *, ...);
23 extern int      sprintf(char *,const char *, ...);
24 extern int      vprintf(const char *, va_list);
25 extern int      strncmp(const char *, const char *, size_t);
26
27 extern int      gSocket;
28 extern int      giSyscall_ClientID;     // Needed for execve
29 extern void     _InitSyscalls(void);
30 extern void     _CloseSyscalls(void);
31
32 extern void     Debug(const char *Format, ...);
33 extern int      AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
34
35 // === CONSTANTS ===
36 #define NATIVE_FILE_MASK        0x40000000
37
38 // === GLOBALS ===
39 int     acess__errno;
40
41 // === CODE ===
42 // --- VFS Calls
43 int acess_chdir(const char *Path)
44 {
45         return _Syscall(SYS_CHDIR, ">s", Path);
46 }
47
48 int acess_open(const char *Path, int Flags)
49 {
50         if( strncmp(Path, "$$$$", 4) == 0 )
51         {
52                 return native_open(Path, Flags) | NATIVE_FILE_MASK;
53         }
54         DEBUG("open(\"%s\", 0x%x)", Path, Flags);
55         return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
56 }
57
58 void acess_close(int FD) {
59         if(FD & NATIVE_FILE_MASK) {
60                 return native_close(FD & (NATIVE_FILE_MASK-1));
61         }
62         DEBUG("close(%i)", FD);
63         _Syscall(SYS_CLOSE, ">i", FD);
64 }
65
66 int acess_reopen(int FD, const char *Path, int Flags) {
67         DEBUG("reopen(0x%x, \"%s\", 0x%x)", FD, Path, Flags);
68         return _Syscall(SYS_REOPEN, ">i >s >i", FD, Path, Flags);
69 }
70
71 size_t acess_read(int FD, void *Dest, size_t Bytes) {
72         if(FD & NATIVE_FILE_MASK)
73                 return native_read(FD & (NATIVE_FILE_MASK-1), Dest, Bytes);
74 //      if( FD > 2 )
75                 DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
76         return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
77 }
78
79 size_t acess_write(int FD, const void *Src, size_t Bytes) {
80         if(FD & NATIVE_FILE_MASK)
81                 return native_write(FD & (NATIVE_FILE_MASK-1), Src, Bytes);
82 //      if( FD > 2 )
83                 DEBUG("write(0x%x, 0x%x, %p\"%.*s\")", FD, Bytes, Src, Bytes, (char*)Src);
84         return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
85 }
86
87 int acess_seek(int FD, int64_t Ofs, int Dir) {
88         if(FD & NATIVE_FILE_MASK) {
89                 return native_seek(FD & (NATIVE_FILE_MASK-1), Ofs, Dir);
90         }
91         DEBUG("seek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
92         return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
93 }
94
95 uint64_t acess_tell(int FD) {
96         if(FD & NATIVE_FILE_MASK)
97                 return native_tell( FD & (NATIVE_FILE_MASK-1) );
98         return _Syscall(SYS_TELL, ">i", FD);
99 }
100
101 int acess_ioctl(int fd, int id, void *data) {
102          int    len;
103         DEBUG("ioctl(%i, %i, %p)", fd, id, data);
104         // NOTE: The length here is hacky and could break
105         if( data == NULL )
106                 len = 0;
107         else
108                 len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
109         return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
110 }
111 int acess_finfo(int fd, t_sysFInfo *info, int maxacls) {
112 //      DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
113         DEBUG("finfo(%i, %p, %i)", fd, info, maxacls);
114         return _Syscall(SYS_FINFO, ">i <d >i",
115                 fd,
116                 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
117                 maxacls
118                 );
119 }
120
121 int acess_readdir(int fd, char *dest) {
122         DEBUG("readdir(%i, %p)", fd, dest);
123         return _Syscall(SYS_READDIR, ">i <d", fd, 256, dest);
124 }
125
126 int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout, uint32_t events)
127 {
128         DEBUG("_SysSelect(%i, %p, %p, %p, %p, 0x%x)", nfds, read, write, error, timeout, events);
129         return _Syscall(SYS_SELECT, ">i ?d ?d ?d >d >i", nfds,
130                 read ? (nfds+7)/8 : 0, read,
131                 write ? (nfds+7)/8 : 0, write,
132                 error ? (nfds+7)/8 : 0, error,
133                 sizeof(*timeout), timeout,
134                 events
135                 );
136 }
137
138 int acess_select(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout)
139 {
140         return acess__SysSelect(nfds, read, write, error, timeout, 0);
141 }
142
143
144 int acess__SysOpenChild(int fd, char *name, int flags) {
145         return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
146 }
147
148 int acess__SysGetACL(int fd, t_sysACL *dest) {
149         return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
150 }
151
152 int acess__SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
153         return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
154 }
155
156
157 // --- Error Handler
158 int acess__SysSetFaultHandler(int (*Handler)(int)) {
159         printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
160         return 0;
161 }
162
163 // --- Memory Management ---
164 uint64_t acess__SysAllocate(uint vaddr)
165 {
166         if( AllocateMemory(vaddr, 0x1000) == -1 )       // Allocate a page
167                 return 0;
168                 
169         return vaddr;   // Just ignore the need for paddrs :)
170 }
171
172 // --- Process Management ---
173 int acess_clone(int flags, void *stack)
174 {
175         extern int fork(void);
176         if(flags & CLONE_VM) {
177                  int    ret, newID, kernel_tid=0;
178                 printf("USERSIDE fork()\n");
179                 
180                 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
181                 ret = fork();
182                 if(ret < 0) {
183                         return ret;
184                 }
185                 
186                 if(ret == 0)
187                 {
188                         _CloseSyscalls();
189                         _InitSyscalls();
190                         giSyscall_ClientID = newID;
191                         return 0;
192                 }
193                 
194                 // Return the acess TID instead
195                 return kernel_tid;
196         }
197         else
198         {
199                 fprintf(stderr, "ERROR: Threads currently unsupported\n");
200                 exit(-1);
201         }
202 }
203
204 int acess_execve(char *path, char **argv, char **envp)
205 {
206          int    i, argc;
207         
208         DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
209         
210         // Get argument count
211         for( argc = 0; argv[argc]; argc ++ ) ;
212         DEBUG(" acess_execve: argc = %i", argc);
213
214         char    *new_argv[7+argc+1];
215         char    client_id_str[11];
216         char    socket_fd_str[11];
217         sprintf(client_id_str, "%i", giSyscall_ClientID);
218         sprintf(socket_fd_str, "%i", gSocket);
219         new_argv[0] = "ld-acess";       // TODO: Get path to ld-acess executable
220         new_argv[1] = "--key";          // Set client ID for Request.c
221         new_argv[2] = client_id_str;
222         new_argv[3] = "--socket";       // Socket
223         new_argv[4] = socket_fd_str;
224         new_argv[5] = "--binary";       // Set the binary path (instead of using argv[0])
225         new_argv[6] = path;
226         for( i = 0; i < argc; i ++ )    new_argv[7+i] = argv[i];
227         new_argv[7+i] = NULL;
228         
229         #if 1
230         argc += 7;
231         for( i = 0; i < argc; i ++ )
232                 printf("\"%s\" ", new_argv[i]);
233         printf("\n");
234         if(envp)
235         {
236                 printf("envp = %p\n", envp);
237                 for( i = 0; envp[i]; i ++ )
238                         printf("%i: \"%s\"\n", i, envp[i]);
239                 printf("envc = %i\n", i);
240         }
241         #endif
242         
243         // Call actual execve
244         return native_execve("./ld-acess", new_argv, envp);
245 }
246
247 void acess_sleep(void)
248 {
249         _Syscall(SYS_SLEEP, "");
250 }
251
252 int acess_waittid(int TID, int *ExitStatus)
253 {
254         return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
255 }
256
257 int acess_setuid(int ID) { return _Syscall(SYS_SETUID, ">i", ID); }
258 int acess_setgid(int ID) { return _Syscall(SYS_SETGID, ">i", ID); }
259 int acess_gettid(void) { return _Syscall(SYS_GETTID, ""); }
260 int acess_getpid(void) { return _Syscall(SYS_GETPID, ""); }
261 int acess_getuid(void) { return _Syscall(SYS_GETUID, ""); }
262 int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
263
264 int acess_SysSendMessage(int DestTID, int Length, void *Data)
265 {
266         return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
267 }
268
269 int acess_SysGetMessage(int *SourceTID, void *Data)
270 {
271 //      static __thread int lastlen = 1024;
272         int lastlen;
273
274         lastlen = _Syscall(SYS_GETMSG, "<d <d",
275                 SourceTID ? sizeof(uint32_t) : 0, SourceTID,
276                 Data ? 1024 : 0, Data
277                 );
278         return lastlen;
279 }
280
281 int acess__SysWaitEvent(int Mask)
282 {
283         return _Syscall(SYS_WAITEVENT, ">i", Mask);
284 }
285
286 // --- Logging
287 void acess__SysDebug(const char *Format, ...)
288 {
289         va_list args;
290         
291         va_start(args, Format);
292         
293         printf("[_SysDebug %i] ", giSyscall_ClientID);
294         vprintf(Format, args);
295         printf("\n");
296         
297         va_end(args);
298 }
299
300 void acess__exit(int Status)
301 {
302         DEBUG("_exit(%i)", Status);
303         _Syscall(SYS_EXIT, ">i", Status);
304         exit(Status);
305 }
306
307
308 // === Symbol List ===
309 #define DEFSYM(name)    {#name, &acess_##name}
310 const tSym      caBuiltinSymbols[] = {
311         DEFSYM(_exit),
312         
313         DEFSYM(chdir),
314         DEFSYM(open),
315         DEFSYM(close),
316         DEFSYM(reopen),
317         DEFSYM(read),
318         DEFSYM(write),
319         DEFSYM(seek),
320         DEFSYM(tell),
321         DEFSYM(ioctl),
322         DEFSYM(finfo),
323         DEFSYM(readdir),
324         DEFSYM(select),
325         DEFSYM(_SysOpenChild),
326         DEFSYM(_SysGetACL),
327         DEFSYM(_SysMount),
328         DEFSYM(_SysSelect),
329         
330         DEFSYM(clone),
331         DEFSYM(execve),
332         DEFSYM(sleep),
333         
334         DEFSYM(waittid),
335         DEFSYM(setuid),
336         DEFSYM(setgid),
337         DEFSYM(gettid),
338
339         DEFSYM(SysSendMessage),
340         DEFSYM(SysGetMessage),
341         
342         DEFSYM(_SysAllocate),
343         DEFSYM(_SysDebug),
344         DEFSYM(_SysSetFaultHandler),
345         DEFSYM(_SysWaitEvent),
346         
347         DEFSYM(_errno)
348 };
349
350 const int       ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);
351

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