improve cleanup; release 2
[ginge.git] / loader / override.c
CommitLineData
7fd42181 1// vim:shiftwidth=2:expandtab
2#include <stdio.h>
3#include <stdlib.h>
4d045184 4#include <stdarg.h>
7fd42181 5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <sys/mman.h>
10#include <unistd.h>
11#include <sys/ioctl.h>
12#include <signal.h>
13#include <termios.h>
14
15#include "realfuncs.h"
16#include "header.h"
17
18#if (DBG & 1)
19#define strace printf
20#else
21#define strace(...)
22#endif
23
24#define UNUSED __attribute__((unused))
25
26static const struct dev_fd_t takeover_devs[] = {
27 { "/dev/mem", FAKEDEV_MEM },
28 { "/dev/GPIO", FAKEDEV_GPIO },
29 { "/dev/fb0", FAKEDEV_FB0 },
30 { "/dev/fb/0", FAKEDEV_FB0 },
31 { "/dev/fb1", FAKEDEV_FB1 },
32 { "/dev/fb/1", FAKEDEV_FB1 },
33 { "/dev/mmuhack", FAKEDEV_MMUHACK },
34 { "/dev/tty", -1 }, // XXX hmh..
35 { "/dev/tty0", FAKEDEV_TTY0 },
36};
37
38static int w_open(const char *pathname, int flags, mode_t mode)
39{
40 int i, ret;
41
42 for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
43 if (strcmp(pathname, takeover_devs[i].name) == 0) {
44 ret = takeover_devs[i].fd;
45 break;
46 }
47 }
48
49 if (i == ARRAY_SIZE(takeover_devs))
50 ret = open(pathname, flags, mode);
51
52 if (ret >= 0) {
53 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
54 if (strcmp(pathname, emu_interesting_fds[i].name) == 0) {
55 emu_interesting_fds[i].fd = ret;
56 break;
57 }
58 }
59 }
60
61 strace("open(%s) = %d\n", pathname, ret);
62 return ret;
63}
64
65static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
66{
67 void *ret;
68 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
69 ret = emu_do_mmap(length, prot, flags, fd, offset);
70 else
71 ret = mmap(addr, length, prot, flags, fd, offset);
72
73 // threads are using heap before they mmap their stack
74 // printf needs valid stack for pthread/errno
75 if (((long)&ret & 0xf0000000) == 0xb0000000)
76 strace("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret);
77 return ret;
78}
79#define w_mmap2 w_mmap
80
81static ssize_t w_read(int fd, void *buf, size_t count)
82{
83 ssize_t ret;
84 if (fd == FAKEDEV_GPIO)
85 ret = emu_read_gpiodev(buf, count);
86 else
87 ret = read(fd, buf, count);
88
89 //strace("read(%d, %p, %d) = %d\n", fd, buf, count, ret);
90 return ret;
91}
92
93static int w_ioctl(int fd, int request, void *argp)
94{
95 int ret;
96
97 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
98 fd == emu_interesting_fds[IFD_SOUND].fd)
99 ret = emu_do_ioctl(fd, request, argp);
100 else
101 ret = ioctl(fd, request, argp);
102
103 strace("ioctl(%d, %08x, %p) = %d\n", fd, request, argp, ret);
104 return ret;
105}
106
107static int w_sigaction(int signum, const void *act, void *oldact)
108{
109 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
110 return 0;
111}
112
113/* dynamic only */
114static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
115{
116 int ret;
117 if (fd != FAKEDEV_TTY0)
118 ret = tcgetattr(fd, termios_p);
119 else
120 ret = 0;
121
122 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
123 return ret;
124}
125
126static UNUSED int w_tcsetattr(int fd, int optional_actions,
127 const struct termios *termios_p)
128{
129 int ret;
130 if (fd != FAKEDEV_TTY0)
131 ret = tcsetattr(fd, optional_actions, termios_p);
132 else
133 ret = 0;
134
135 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
136 return ret;
137}
138
139static UNUSED FILE *w_fopen(const char *path, const char *mode)
140{
141 FILE *ret = emu_do_fopen(path, mode);
142 strace("fopen(%s, %s) = %p\n", path, mode, ret);
143 return ret;
144}
145
146static UNUSED int w_system(const char *command)
147{
148 int ret = emu_do_system(command);
149 strace("system(%s) = %d\n", command, ret);
150 return ret;
151}
152
db9191ed 153// 4 functions bellow are efforts to prevent gp2xmenu from being started..
154static UNUSED int w_execl(const char *path, const char *arg, ...)
155{
156 // don't allow exec (for now)
157 strace("execl(%s, %s, ...) = ?\n", path, arg);
158 exit(0);
159}
160
161static UNUSED int w_execlp(const char *file, const char *arg, ...)
162{
163 strace("execlp(%s, %s, ...) = ?\n", file, arg);
164 exit(0);
165}
166
167// static note: this can't safely return because of the way it's patched in
168// static note2: can't be used, execve hangs?
169static UNUSED int w_execve(const char *filename, char *const argv[],
170 char *const envp[])
171{
172 strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
173 if (filename != NULL && strstr(filename, "/gp2xmenu") != NULL)
174 exit(0);
175 return execve(filename, argv, envp);
176}
177
178static int w_chdir(const char *path)
179{
180 int ret;
181 if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
182 ret = 0;
183 else
184 ret = chdir(path);
185 strace("chdir(%s) = %d\n", path, ret);
186 return ret;
187}
188
7fd42181 189#undef open
190#undef fopen
191#undef mmap
192#undef read
193#undef ioctl
194#undef sigaction
195#undef tcgetattr
196#undef tcsetattr
197#undef system
db9191ed 198#undef execl
199#undef execlp
200#undef execve
201#undef chdir
7fd42181 202
203#ifdef DL
204
db9191ed 205#define MAKE_WRAP_SYM_N(sym) \
7fd42181 206 /* alias wrap symbols to real names */ \
db9191ed 207 typeof(sym) sym __attribute__((alias("w_" #sym)))
208
209#define MAKE_WRAP_SYM(sym) \
210 MAKE_WRAP_SYM_N(sym); \
7fd42181 211 /* wrapper to real functions, to be set up on load */ \
212 static typeof(sym) *p_real_##sym
213
4d045184 214// note: update symver too
7fd42181 215MAKE_WRAP_SYM(open);
216MAKE_WRAP_SYM(fopen);
217MAKE_WRAP_SYM(mmap);
218MAKE_WRAP_SYM(read);
219MAKE_WRAP_SYM(ioctl);
220MAKE_WRAP_SYM(sigaction);
221MAKE_WRAP_SYM(tcgetattr);
222MAKE_WRAP_SYM(tcsetattr);
223MAKE_WRAP_SYM(system);
db9191ed 224MAKE_WRAP_SYM_N(execl);
225MAKE_WRAP_SYM_N(execlp);
226MAKE_WRAP_SYM(execve);
227MAKE_WRAP_SYM(chdir);
7fd42181 228typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
229
230#define REAL_FUNC_NP(name) \
231 { #name, (void **)&p_real_##name }
232
233static const struct {
234 const char *name;
235 void **func_ptr;
236} real_funcs_np[] = {
237 REAL_FUNC_NP(open),
238 REAL_FUNC_NP(fopen),
239 REAL_FUNC_NP(mmap),
240 REAL_FUNC_NP(read),
241 REAL_FUNC_NP(ioctl),
242 REAL_FUNC_NP(sigaction),
243 REAL_FUNC_NP(tcgetattr),
244 REAL_FUNC_NP(tcsetattr),
245 REAL_FUNC_NP(system),
db9191ed 246 // exec* - skipped
247 REAL_FUNC_NP(execve),
248 REAL_FUNC_NP(chdir),
7fd42181 249};
250
251#define open p_real_open
252#define fopen p_real_fopen
253#define mmap p_real_mmap
254#define read p_real_read
255#define ioctl p_real_ioctl
256#define sigaction p_real_sigaction
257#define tcgetattr p_real_tcgetattr
258#define tcsetattr p_real_tcsetattr
259#define system p_real_system
db9191ed 260#define execve p_real_execve
261#define chdir p_real_chdir
7fd42181 262
263#undef MAKE_WRAP_SYM
264#undef REAL_FUNC_NP
265
266#endif
267
268// just call real funcs for static, pointer for dynamic
4d045184 269int real_open(const char *pathname, int flags, ...)
7fd42181 270{
4d045184 271 va_list ap;
272 mode_t mode;
273
274 va_start(ap, flags);
275 mode = va_arg(ap, mode_t);
276 va_end(ap);
7fd42181 277 return open(pathname, flags, mode);
278}
279
280FILE *real_fopen(const char *path, const char *mode)
281{
282 return fopen(path, mode);
283}
284
285void *real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
286{
287 return mmap(addr, length, prot, flags, fd, offset);
288}
289
290int real_read(int fd, void *buf, size_t count)
291{
292 return read(fd, buf, count);
293}
294
295int real_ioctl(int d, int request, void *argp)
296{
297 return ioctl(d, request, argp);
298}
299
300int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
301{
302 return sigaction(signum, act, oldact);
303}
304
305int real_tcgetattr(int fd, struct termios *termios_p)
306{
307 return tcgetattr(fd, termios_p);
308}
309
310int real_tcsetattr(int fd, int optional_actions,
311 const struct termios *termios_p)
312{
313 return tcsetattr(fd, optional_actions, termios_p);
314}
315
316int real_system(const char *command)
317{
318 return system(command);
319}
db9191ed 320
321// real_exec* is missing intentionally - we don't need them
322
323int real_execve(const char *filename, char *const argv[],
324 char *const envp[])
325{
326 return execve(filename, argv, envp);
327}
328
329int real_chdir(const char *path)
330{
331 return chdir(path);
332}