AcessNative - Added PTY init to AcessKernel
[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 DEBUG(v...)   do{}while(0)//Debug(v)
16 #define PAGE_SIZE       4096
17
18 typedef struct sFILE    FILE;
19
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     Warning(const char *Format, ...);
33 extern void     Debug(const char *Format, ...);
34 extern int      AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
35
36 // === CONSTANTS ===
37 #define NATIVE_FILE_MASK        0x40000000
38
39 // === GLOBALS ===
40 int     acess__errno;
41 char    *gsExecutablePath = "./ld-acess";
42
43 // === CODE ===
44 // --- VFS Calls
45 int acess__SysChdir(const char *Path)
46 {
47         return _Syscall(SYS_CHDIR, ">s", Path);
48 }
49
50 int acess__SysOpen(const char *Path, int Flags)
51 {
52         if( strncmp(Path, "$$$$", 4) == 0 )
53         {
54                 return native_open(Path, Flags) | NATIVE_FILE_MASK;
55         }
56         DEBUG("open(\"%s\", 0x%x)", Path, Flags);
57         return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
58 }
59
60 void acess__SysClose(int FD)
61 {
62         if(FD & NATIVE_FILE_MASK) {
63                 return native_close(FD & (NATIVE_FILE_MASK-1));
64         }
65         DEBUG("close(%i)", FD);
66         _Syscall(SYS_CLOSE, ">i", FD);
67 }
68
69 int acess__SysReopen(int FD, const char *Path, int Flags) {
70         DEBUG("reopen(0x%x, \"%s\", 0x%x)", FD, Path, Flags);
71         return _Syscall(SYS_REOPEN, ">i >s >i", FD, Path, Flags);
72 }
73
74 int acess__SysCopyFD(int srcfd, int dstfd) {
75         return _Syscall(SYS_COPYFD, ">i >i", srcfd, dstfd);
76 }
77
78 int acess__SysFDFlags(int fd, int mask, int newflags) {
79         return _Syscall(SYS_FDFLAGS, ">i >i >i", fd, mask, newflags);
80 }
81
82 size_t acess__SysRead(int FD, void *Dest, size_t Bytes) {
83         if(FD & NATIVE_FILE_MASK)
84                 return native_read(FD & (NATIVE_FILE_MASK-1), Dest, Bytes);
85 //      if( FD > 2 )
86                 DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
87         return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
88 }
89
90 size_t acess__SysWrite(int FD, const void *Src, size_t Bytes) {
91         if(FD & NATIVE_FILE_MASK)
92                 return native_write(FD & (NATIVE_FILE_MASK-1), Src, Bytes);
93 //      if( FD > 2 )
94                 DEBUG("write(0x%x, 0x%x, %p\"%.*s\")", FD, Bytes, Src, Bytes, (char*)Src);
95         return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
96 }
97
98 int acess__SysSeek(int FD, int64_t Ofs, int Dir)
99 {
100         if(FD & NATIVE_FILE_MASK) {
101                 return native_seek(FD & (NATIVE_FILE_MASK-1), Ofs, Dir);
102         }
103         DEBUG("seek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
104         return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
105 }
106
107 uint64_t acess__SysTell(int FD)
108 {
109         if(FD & NATIVE_FILE_MASK)
110                 return native_tell( FD & (NATIVE_FILE_MASK-1) );
111         DEBUG("tell(0x%x)", FD);
112         return _Syscall(SYS_TELL, ">i", FD);
113 }
114
115 int acess__SysIOCtl(int fd, int id, void *data) {
116          int    len;
117         DEBUG("ioctl(%i, %i, %p)", fd, id, data);
118         // NOTE: The length here is hacky and could break
119         if( data == NULL )
120                 len = 0;
121         else
122                 len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
123         return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
124 }
125 int acess__SysFInfo(int fd, t_sysFInfo *info, int maxacls) {
126 //      DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
127         DEBUG("finfo(%i, %p, %i)", fd, info, maxacls);
128         return _Syscall(SYS_FINFO, ">i <d >i",
129                 fd,
130                 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
131                 maxacls
132                 );
133 }
134
135 int acess__SysReadDir(int fd, char *dest) {
136         DEBUG("SysReadDir(%i, %p)", fd, dest);
137         return _Syscall(SYS_READDIR, ">i <d", fd, 256, dest);
138 }
139
140 int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, int64_t *timeout, uint32_t events)
141 {
142         DEBUG("_SysSelect(%i, %p, %p, %p, %p, 0x%x)", nfds, read, write, error, timeout, events);
143         return _Syscall(SYS_SELECT, ">i ?d ?d ?d >d >i", nfds,
144                 read ? (nfds+7)/8 : 0, read,
145                 write ? (nfds+7)/8 : 0, write,
146                 error ? (nfds+7)/8 : 0, error,
147                 sizeof(*timeout), timeout,
148                 events
149                 );
150 }
151 int acess__SysMkDir(const char *pathname)
152 {
153         DEBUG("TODO: _SysMkDir");
154         return 0;
155 }
156 int acess__SysUnlink(const char *pathname)
157 {
158         // TODO:
159         DEBUG("TODO: _Unlink");
160         return 0;
161 }
162
163 int acess__SysOpenChild(int fd, char *name, int flags) {
164         DEBUG("_SysOpenChild(0x%x, '%s', 0x%x)", fd, name, flags);
165         return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
166 }
167
168 int acess__SysGetACL(int fd, t_sysACL *dest) {
169         DEBUG("%s(0x%x, %p)", __func__, fd, dest);
170         return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
171 }
172
173 int acess__SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
174         DEBUG("%s('%s', '%s', '%s', '%s')", __func__, Device, Directory, Type, Options);
175         return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
176 }
177
178
179 // --- Error Handler
180 int acess__SysSetFaultHandler(int (*Handler)(int)) {
181         printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
182         return 0;
183 }
184
185 void acess__SysSetName(const char *Name)
186 {
187         // TODO:
188 }
189
190 int acess__SysGetName(char *NameDest)
191 {
192         // TODO: 
193         return 0;
194 }
195
196 int acess__SysSetPri(int Priority)
197 {
198         // TODO:
199         return 0;
200 }
201
202 // --- Binaries? ---
203 void *acess_SysLoadBin(const char *path, void **entry)
204 {
205         // ERROR!
206         return NULL;
207 }
208
209 int acess__SysUnloadBin(void *base)
210 {
211         // ERROR!
212         return -1;
213 }
214
215 // --- Timekeeping ---
216 int64_t acess__SysTimestamp(void)
217 {
218         // TODO: Better impl
219 //      return now()*1000;
220         return 0;
221 }
222
223 // --- Memory Management ---
224 uint64_t acess__SysGetPhys(uintptr_t vaddr)
225 {
226         // TODO:
227         return 0;
228 }
229
230 uint64_t acess__SysAllocate(uintptr_t vaddr)
231 {
232         if( AllocateMemory(vaddr, 0x1000) == -1 )       // Allocate a page
233                 return 0;
234                 
235         return vaddr;   // Just ignore the need for paddrs :)
236 }
237
238 // --- Process Management ---
239 int acess__SysClone(int flags, void *stack)
240 {
241         #ifdef __WIN32__
242         Warning("Win32 does not support anything like fork(2), cannot emulate");
243         exit(-1);
244         #else
245         extern int fork(void);
246         if(flags & CLONE_VM) {
247                  int    ret, newID, kernel_tid=0;
248                 Debug("USERSIDE fork()");
249                 
250                 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
251                 ret = fork();
252                 if(ret < 0) {
253                         return ret;
254                 }
255                 
256                 if(ret == 0)
257                 {
258                         _CloseSyscalls();
259                         giSyscall_ClientID = newID;
260                         _InitSyscalls();
261                         return 0;
262                 }
263                 
264                 // Return the acess TID instead
265                 return kernel_tid;
266         }
267         else
268         {
269                 Warning("ERROR: Threads currently unsupported\n");
270                 exit(-1);
271         }
272         #endif
273 }
274
275 int acess__SysKill(int pid, int sig)
276 {
277         // TODO: Impliment SysKill
278         return -1;
279 }
280
281 int acess__SysExecVE(char *path, char **argv, const char **envp)
282 {
283          int    i, argc;
284         
285         DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
286         
287         // Get argument count
288         for( argc = 0; argv[argc]; argc ++ ) ;
289         DEBUG(" acess_execve: argc = %i", argc);
290
291         const char      *new_argv[7+argc+1];
292         char    client_id_str[11];
293         char    socket_fd_str[11];
294         sprintf(client_id_str, "%i", giSyscall_ClientID);
295         sprintf(socket_fd_str, "%i", gSocket);
296         new_argv[0] = "ld-acess";       // TODO: Get path to ld-acess executable
297         new_argv[1] = "--key";          // Set client ID for Request.c
298         new_argv[2] = client_id_str;
299         new_argv[3] = "--socket";       // Socket
300         new_argv[4] = socket_fd_str;
301         new_argv[5] = "--binary";       // Set the binary path (instead of using argv[0])
302         new_argv[6] = path;
303         for( i = 0; i < argc; i ++ )    new_argv[7+i] = argv[i];
304         new_argv[7+i] = NULL;
305         
306         #if 1
307         argc += 7;
308         for( i = 0; i < argc; i ++ )
309                 printf("\"%s\" ", new_argv[i]);
310         printf("\n");
311         if(envp)
312         {
313                 printf("envp = %p\n", envp);
314                 for( i = 0; envp[i]; i ++ )
315                         printf("%i: \"%s\"\n", i, envp[i]);
316                 printf("envc = %i\n", i);
317         }
318         #endif
319         
320         // Call actual execve
321         return native_execve("./ld-acess", new_argv, envp);
322 }
323
324 int acess__SysSpawn(const char *binary, const char **argv, const char **envp, int nfd, int fds[], struct s_sys_spawninfo *info)
325 {
326          int    argc = 0;
327         while( argv[argc++] );
328
329         Debug("_SysSpawn('%s', %p (%i), %p, %i, %p, %p)",
330                 binary, argv, argc, envp, nfd, fds, info);
331
332          int    kernel_tid;
333          int    newID;
334         newID = _Syscall(SYS_AN_SPAWN, "<d >d >d", sizeof(int), &kernel_tid,
335                 nfd*sizeof(int), fds,
336                 info ? sizeof(*info) : 0, info);
337
338         const char      *new_argv[5+argc+1];
339          int    new_argc = 0, i;
340         char    client_id_str[11];
341         sprintf(client_id_str, "%i", newID);
342         new_argv[new_argc++] = gsExecutablePath;       // TODO: Get path to ld-acess executable
343         new_argv[new_argc++] = "--key";
344         new_argv[new_argc++] = client_id_str;
345         new_argv[new_argc++] = "--binary";
346         new_argv[new_argc++] = binary;
347         for( i = 0; argv[i]; i ++)
348                 new_argv[new_argc++] = argv[i];
349         new_argv[new_argc++] = NULL;
350         
351         // TODO: Debug output?
352         
353         native_spawn(gsExecutablePath, new_argv, envp);
354
355         return kernel_tid;
356 }
357
358 //void acess_sleep(void)
359 //{
360 //      DEBUG("%s()", __func__);
361 //      _Syscall(SYS_SLEEP, "");
362 //}
363
364 int acess__SysWaitTID(int TID, int *ExitStatus)
365 {
366         DEBUG("%s(%i, %p)", __func__, TID, ExitStatus);
367         return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
368 }
369
370 int acess_setuid(int ID) { return _Syscall(SYS_SETUID, ">i", ID); }
371 int acess_setgid(int ID) { return _Syscall(SYS_SETGID, ">i", ID); }
372 int acess_gettid(void) { return _Syscall(SYS_GETTID, ""); }
373 int acess__SysGetPID(void) { return _Syscall(SYS_GETPID, ""); }
374 int acess__SysGetUID(void) { return _Syscall(SYS_GETUID, ""); }
375 int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
376
377 int acess__SysSendMessage(int DestTID, int Length, void *Data)
378 {
379         DEBUG("%s(%i, 0x%x, %p)", __func__, DestTID, Length, Data);
380         return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
381 }
382
383 int acess__SysGetMessage(int *SourceTID, int BufLen, void *Data)
384 {
385         DEBUG("%s(%p, %p)", __func__, SourceTID, Data);
386         return _Syscall(SYS_GETMSG, "<d <d",
387                 SourceTID ? sizeof(uint32_t) : 0, SourceTID,
388                 BufLen, Data
389                 );
390 }
391
392 int acess__SysWaitEvent(int Mask)
393 {
394         DEBUG("%s(%x)", __func__, Mask);
395         return _Syscall(SYS_WAITEVENT, ">i", Mask);
396 }
397
398 // --- Logging
399 void acess__SysDebug(const char *Format, ...)
400 {
401         va_list args;
402         
403         va_start(args, Format);
404         
405         printf("[_SysDebug %i] ", giSyscall_ClientID);
406         vprintf(Format, args);
407         printf("\n");
408         
409         va_end(args);
410 }
411
412 void acess__exit(int Status)
413 {
414         DEBUG("_exit(%i)", Status);
415         _Syscall(SYS_EXIT, ">i", Status);
416         exit(Status);
417 }
418
419 uint32_t acess__SysSetMemFlags(uintptr_t vaddr, uint32_t flags, uint32_t mask)
420 {
421         // TODO: Impliment acess__SysSetMemFlags?
422         return 0;
423 }
424
425
426 // === Symbol List ===
427 #ifndef DEFSYM
428 # define DEFSYM(name)   {#name, &acess_##name}
429 #endif
430 const tSym      caBuiltinSymbols[] = {
431         DEFSYM(_exit),
432         
433         DEFSYM(_SysChdir),
434         DEFSYM(_SysOpen),
435         DEFSYM(_SysOpenChild),
436         DEFSYM(_SysReopen),
437         DEFSYM(_SysClose),
438         DEFSYM(_SysRead),
439         DEFSYM(_SysWrite),
440         DEFSYM(_SysSeek),
441         DEFSYM(_SysTell),
442         DEFSYM(_SysIOCtl),
443         DEFSYM(_SysFInfo),
444         DEFSYM(_SysReadDir),
445         DEFSYM(_SysGetACL),
446         DEFSYM(_SysMount),
447         DEFSYM(_SysSelect),
448         DEFSYM(_SysMkDir),
449         DEFSYM(_SysUnlink),
450         
451         DEFSYM(_SysClone),
452         DEFSYM(_SysExecVE),
453         DEFSYM(_SysSpawn),
454 //      DEFSYM(sleep),
455         
456         DEFSYM(_SysWaitTID),
457         DEFSYM(gettid),
458         DEFSYM(_SysGetPID),
459         DEFSYM(setuid),
460         DEFSYM(setgid),
461         DEFSYM(_SysGetUID),
462         DEFSYM(getgid),
463
464         DEFSYM(_SysSendMessage),
465         DEFSYM(_SysGetMessage),
466         
467         DEFSYM(_SysAllocate),
468         DEFSYM(_SysSetMemFlags),
469         DEFSYM(_SysDebug),
470         DEFSYM(_SysSetFaultHandler),
471         DEFSYM(_SysWaitEvent),
472         
473         DEFSYM(_errno)
474 };
475
476 const int       ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);
477

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