get rid of some libc deps
[ginge.git] / loader / override.c
1 /*
2  * GINGE - GINGE Is Not Gp2x Emulator
3  * (C) notaz, 2010-2011,2016
4  *
5  * This work is licensed under the MAME license, see COPYING file for details.
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <sys/mman.h>
15 #include <unistd.h>
16 #include <sys/ioctl.h>
17 #include <signal.h>
18 #include <termios.h>
19 #include <errno.h>
20
21 #include "realfuncs.h"
22 #include "syscalls.h"
23 #include "llibc.h"
24 #include "header.h"
25
26 #if (DBG & 1)
27 #define strace g_printf
28 #else
29 #define strace(...)
30 #endif
31
32 #define UNUSED __attribute__((unused))
33
34 static const struct dev_fd_t takeover_devs[] = {
35   { "/dev/mem",     FAKEDEV_MEM },
36   { "/dev/GPIO",    FAKEDEV_GPIO },
37   { "/dev/fb0",     FAKEDEV_FB0 },
38   { "/dev/fb/0",    FAKEDEV_FB0 },
39   { "/dev/fb1",     FAKEDEV_FB1 },
40   { "/dev/fb/1",    FAKEDEV_FB1 },
41   { "/dev/mmuhack", FAKEDEV_MMUHACK },
42   { "/dev/tty",     FAKEDEV_TTY0 },
43   { "/dev/tty0",    FAKEDEV_TTY0 },
44 #ifdef PND
45   { "/dev/input/event*", -1 }, // hide for now, may cause dupe events
46 #endif
47 };
48
49 static long w_open_raw(const char *pathname, int flags, mode_t mode)
50 {
51   long ret;
52   int i;
53
54   for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
55     const char *p, *oname;
56     int len;
57
58     oname = takeover_devs[i].name;
59     p = strchr(oname, '*');
60     if (p != NULL)
61       len = p - oname;
62     else
63       len = strlen(oname) + 1;
64
65     if (strncmp(pathname, oname, len) == 0) {
66       ret = takeover_devs[i].fd;
67       break;
68     }
69   }
70
71   if (i == ARRAY_SIZE(takeover_devs))
72     ret = g_open_raw(pathname, flags, mode);
73
74   if (ret >= 0) {
75     for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
76       struct dev_fd_t *eifd = &emu_interesting_fds[i];
77       if (strcmp(pathname, eifd->name) == 0) {
78         eifd->fd = ret;
79         if (eifd->open_cb != NULL)
80           eifd->open_cb(ret);
81         break;
82       }
83     }
84   }
85
86   strace("open(%s) = %ld\n", pathname, ret);
87   return ret;
88 }
89
90 static int w_open(const char *pathname, int flags, mode_t mode)
91 {
92   long ret = w_open_raw(pathname, flags, mode);
93   return g_syscall_error(ret);
94 }
95
96 static long w_mmap_raw(void *addr, size_t length, int prot, int flags,
97   int fd, off_t offset)
98 {
99   long ret;
100
101   if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
102     ret = emu_do_mmap(length, prot, flags, fd, offset);
103   else
104     ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
105
106   strace("mmap(%p, %x, %x, %x, %d, %lx) = %lx\n",
107          addr, length, prot, flags, fd, (long)offset, ret);
108   return ret;
109 }
110
111 static long w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
112 {
113   long ret = w_mmap_raw(addr, length, prot, flags, fd, offset);
114   return g_syscall_error(ret);
115 }
116 #define w_mmap2 w_mmap
117
118 static long w_munmap_raw(void *addr, size_t length)
119 {
120   long ret;
121
122   ret = emu_do_munmap(addr, length);
123   if (ret == -EAGAIN)
124     ret = g_munmap_raw(addr, length);
125
126   strace("munmap(%p, %x) = %ld\n", addr, length, ret);
127   return ret;
128 }
129
130 static int w_munmap(void *addr, size_t length)
131 {
132   long ret = w_munmap_raw(addr, length);
133   return g_syscall_error(ret);
134 }
135
136 long w_read_raw(int fd, void *buf, size_t count)
137 {
138   long ret;
139
140   if (fd == FAKEDEV_GPIO)
141     ret = emu_read_gpiodev(buf, count);
142   else
143     ret = g_read_raw(fd, buf, count);
144
145   //strace("read(%d, %p, %ld) = %ld\n", fd, buf, count, ret);
146   return ret;
147 }
148
149 static ssize_t w_read(int fd, void *buf, size_t count)
150 {
151   long ret = w_read_raw(fd, buf, count);
152   return g_syscall_error(ret);
153 }
154
155 long w_ioctl_raw(int fd, int request, void *argp)
156 {
157   long ret;
158
159   if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
160       fd == emu_interesting_fds[IFD_SOUND].fd)
161     ret = emu_do_ioctl(fd, request, argp);
162   else
163     ret = g_ioctl_raw(fd, request, argp);
164
165   strace("ioctl(%d, %08x, %p) = %ld\n", fd, request, argp, ret);
166   return ret;
167 }
168
169 static int w_ioctl(int fd, int request, void *argp)
170 {
171   long ret = w_ioctl_raw(fd, request, argp);
172   return g_syscall_error(ret);
173 }
174
175 static int w_sigaction(int signum, const void *act, void *oldact)
176 {
177   strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
178   return 0;
179 }
180
181 /* dynamic only */
182 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
183 {
184   int ret;
185   if (fd != FAKEDEV_TTY0)
186     ret = tcgetattr(fd, termios_p);
187   else
188     ret = 0;
189
190   strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
191   return ret;
192 }
193
194 static UNUSED int w_tcsetattr(int fd, int optional_actions,
195                        const struct termios *termios_p)
196 {
197   int ret;
198   if (fd != FAKEDEV_TTY0)
199     ret = tcsetattr(fd, optional_actions, termios_p);
200   else
201     ret = 0;
202
203   strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
204   return ret;
205 }
206
207 static UNUSED FILE *w_fopen(const char *path, const char *mode)
208 {
209   FILE *ret = emu_do_fopen(path, mode);
210   strace("fopen(%s, %s) = %p\n", path, mode, ret);
211   return ret;
212 }
213
214 static UNUSED int w_system(const char *command)
215 {
216   int ret = emu_do_system(command);
217   strace("system(%s) = %d\n", command, ret);
218   return ret;
219 }
220
221 extern char **environ;
222
223 static UNUSED int w_execl(const char *path, const char *arg, ...)
224 {
225   // don't allow exec (for now)
226   strace("execl(%s, %s, ...) = ?\n", path, arg);
227   exit(1);
228 }
229
230 static UNUSED int w_execlp(const char *file, const char *arg, ...)
231 {
232   strace("execlp(%s, %s, ...) = ?\n", file, arg);
233   exit(1);
234 }
235
236 static UNUSED int w_execle(const char *path, const char *arg, ...)
237 {
238   strace("execle(%s, %s, ...) = ?\n", path, arg);
239   exit(1);
240 }
241
242 static UNUSED int w_execv(const char *path, char *const argv[])
243 {
244   strace("execv(%s, %p) = ?\n", path, argv);
245   return emu_do_execve(path, argv, environ);
246 }
247
248 static UNUSED int w_execvp(const char *file, char *const argv[])
249 {
250   strace("execvp(%s, %p) = ?\n", file, argv);
251   return emu_do_execve(file, argv, environ);
252 }
253
254 // static note: this can't safely return because of the way it's patched in
255 // static note2: can't be used, execve hangs?
256 static UNUSED int w_execve(const char *filename, char *const argv[],
257                   char *const envp[])
258 {
259   strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
260   return emu_do_execve(filename, argv, envp);
261 }
262
263 static int w_chdir(const char *path)
264 {
265   long ret;
266
267   if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
268     ret = 0;
269   else
270     ret = g_chdir_raw(path);
271   strace("chdir(%s) = %d\n", path, ret);
272   return g_syscall_error(ret);
273 }
274
275 #undef open
276 #undef fopen
277 #undef mmap
278 #undef munmap
279 #undef read
280 #undef ioctl
281 #undef close
282 #undef sigaction
283 #undef tcgetattr
284 #undef tcsetattr
285 #undef system
286 #undef execl
287 #undef execlp
288 #undef execle
289 #undef execv
290 #undef execvp
291 #undef execve
292 #undef chdir
293
294 #ifdef DL
295
296 #define MAKE_WRAP_SYM_N(sym) \
297   /* alias wrap symbols to real names */ \
298   typeof(sym) sym __attribute__((alias("w_" #sym)))
299
300 #define MAKE_WRAP_SYM(sym) \
301   MAKE_WRAP_SYM_N(sym); \
302   /* wrapper to real functions, to be set up on load */ \
303   static typeof(sym) *p_real_##sym
304
305 // note: update symver too
306 MAKE_WRAP_SYM_N(open);
307 MAKE_WRAP_SYM(fopen);
308 MAKE_WRAP_SYM_N(mmap);
309 MAKE_WRAP_SYM_N(munmap);
310 MAKE_WRAP_SYM_N(read);
311 MAKE_WRAP_SYM_N(ioctl);
312 MAKE_WRAP_SYM(sigaction);
313 MAKE_WRAP_SYM(tcgetattr);
314 MAKE_WRAP_SYM(tcsetattr);
315 MAKE_WRAP_SYM(system);
316 MAKE_WRAP_SYM_N(execl);
317 MAKE_WRAP_SYM_N(execlp);
318 MAKE_WRAP_SYM_N(execle);
319 MAKE_WRAP_SYM_N(execv);
320 MAKE_WRAP_SYM_N(execvp);
321 MAKE_WRAP_SYM(execve);
322 MAKE_WRAP_SYM_N(chdir);
323 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
324
325 #define REAL_FUNC_NP(name) \
326   { #name, (void **)&p_real_##name }
327
328 static const struct {
329   const char *name;
330   void **func_ptr;
331 } real_funcs_np[] = {
332   //REAL_FUNC_NP(open),
333   REAL_FUNC_NP(fopen),
334   //REAL_FUNC_NP(mmap),
335   //REAL_FUNC_NP(munmap),
336   //REAL_FUNC_NP(read),
337   //REAL_FUNC_NP(ioctl),
338   REAL_FUNC_NP(sigaction),
339   REAL_FUNC_NP(tcgetattr),
340   REAL_FUNC_NP(tcsetattr),
341   REAL_FUNC_NP(system),
342   // exec* - skipped
343   REAL_FUNC_NP(execve),
344   //REAL_FUNC_NP(chdir),
345 };
346
347 //#define open p_real_open
348 #define fopen p_real_fopen
349 //#define mmap p_real_mmap
350 //#define munmap p_real_munmap
351 //#define read p_real_read
352 //#define ioctl p_real_ioctl
353 #define sigaction p_real_sigaction
354 #define tcgetattr p_real_tcgetattr
355 #define tcsetattr p_real_tcsetattr
356 #define system p_real_system
357 #define execve p_real_execve
358 //#define chdir p_real_chdir
359
360 #undef MAKE_WRAP_SYM
361 #undef REAL_FUNC_NP
362
363 #endif
364
365 // just call real funcs for static, pointer for dynamic
366 int real_open(const char *pathname, int flags, ...)
367 {
368   va_list ap;
369   mode_t mode;
370   long ret;
371
372   va_start(ap, flags);
373   mode = va_arg(ap, mode_t);
374   va_end(ap);
375   ret = g_open_raw(pathname, flags, mode);
376   return g_syscall_error(ret);
377 }
378
379 FILE *real_fopen(const char *path, const char *mode)
380 {
381   return fopen(path, mode);
382 }
383
384 void *real_mmap(void *addr, size_t length, int prot, int flags,
385   int fd, off_t offset)
386 {
387   long ret = g_mmap2_raw(addr, length, prot, flags, fd, offset >> 12);
388   return (void *)g_syscall_error(ret);
389 }
390
391 int real_munmap(void *addr, size_t length)
392 {
393   return g_syscall_error(g_munmap_raw(addr, length));
394 }
395
396 int real_read(int fd, void *buf, size_t count)
397 {
398   long ret = g_read_raw(fd, buf, count);
399   return g_syscall_error(ret);
400 }
401
402 int real_ioctl(int fd, int request, void *argp)
403 {
404   long ret = g_ioctl_raw(fd, request, argp);
405   return g_syscall_error(ret);
406 }
407
408 int real_close(int fd)
409 {
410   long ret = g_close_raw(fd);
411   return g_syscall_error(ret);
412 }
413
414 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
415 {
416   return sigaction(signum, act, oldact);
417 }
418
419 int real_tcgetattr(int fd, struct termios *termios_p)
420 {
421   return tcgetattr(fd, termios_p);
422 }
423
424 int real_tcsetattr(int fd, int optional_actions,
425                        const struct termios *termios_p)
426 {
427   return tcsetattr(fd, optional_actions, termios_p);
428 }
429
430 int real_system(const char *command)
431 {
432   return system(command);
433 }
434
435 // real_exec* is missing intentionally - we don't need them
436
437 int real_execve(const char *filename, char *const argv[],
438                   char *const envp[])
439 {
440   return execve(filename, argv, envp);
441 }
442
443 int real_chdir(const char *path)
444 {
445   long ret = g_chdir_raw(path);
446   return g_syscall_error(ret);
447 }
448
449 void real_sleep(unsigned int seconds)
450 {
451   struct timespec ts = { seconds, 0 };
452   g_nanosleep_raw(&ts, NULL);
453 }
454
455 void real_usleep(unsigned int usec)
456 {
457   struct timespec ts = { usec / 1000000, usec % 1000000 };
458   g_nanosleep_raw(&ts, NULL);
459 }
460
461 void real_exit(int status)
462 {
463   g_exit_group_raw(status);
464 }
465
466 // vim:shiftwidth=2:expandtab