1 // vim:shiftwidth=2:expandtab
11 #include <sys/ioctl.h>
15 #include "realfuncs.h"
24 #define UNUSED __attribute__((unused))
26 static 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 },
37 { "/dev/input/event*", -1 }, // hide for now, may cause dupe events
41 static int w_open(const char *pathname, int flags, mode_t mode)
45 for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
46 const char *p, *oname;
49 oname = takeover_devs[i].name;
50 p = strchr(oname, '*');
54 len = strlen(oname) + 1;
56 if (strncmp(pathname, oname, len) == 0) {
57 ret = takeover_devs[i].fd;
62 if (i == ARRAY_SIZE(takeover_devs))
63 ret = open(pathname, flags, mode);
66 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
67 struct dev_fd_t *eifd = &emu_interesting_fds[i];
68 if (strcmp(pathname, eifd->name) == 0) {
70 if (eifd->open_cb != NULL)
77 strace("open(%s) = %d\n", pathname, ret);
81 static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
84 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
85 ret = emu_do_mmap(length, prot, flags, fd, offset);
87 ret = mmap(addr, length, prot, flags, fd, offset);
89 // threads are using heap before they mmap their stack
90 // printf needs valid stack for pthread/errno
91 if (((long)&ret & 0xf0000000) == 0xb0000000)
92 strace("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret);
95 #define w_mmap2 w_mmap
97 static ssize_t w_read(int fd, void *buf, size_t count)
100 if (fd == FAKEDEV_GPIO)
101 ret = emu_read_gpiodev(buf, count);
103 ret = read(fd, buf, count);
105 //strace("read(%d, %p, %d) = %d\n", fd, buf, count, ret);
109 static int w_ioctl(int fd, int request, void *argp)
113 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
114 fd == emu_interesting_fds[IFD_SOUND].fd)
115 ret = emu_do_ioctl(fd, request, argp);
117 ret = ioctl(fd, request, argp);
119 strace("ioctl(%d, %08x, %p) = %d\n", fd, request, argp, ret);
123 static int w_sigaction(int signum, const void *act, void *oldact)
125 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
130 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
133 if (fd != FAKEDEV_TTY0)
134 ret = tcgetattr(fd, termios_p);
138 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
142 static UNUSED int w_tcsetattr(int fd, int optional_actions,
143 const struct termios *termios_p)
146 if (fd != FAKEDEV_TTY0)
147 ret = tcsetattr(fd, optional_actions, termios_p);
151 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
155 static UNUSED FILE *w_fopen(const char *path, const char *mode)
157 FILE *ret = emu_do_fopen(path, mode);
158 strace("fopen(%s, %s) = %p\n", path, mode, ret);
162 static UNUSED int w_system(const char *command)
164 int ret = emu_do_system(command);
165 strace("system(%s) = %d\n", command, ret);
169 extern char **environ;
171 static UNUSED int w_execl(const char *path, const char *arg, ...)
173 // don't allow exec (for now)
174 strace("execl(%s, %s, ...) = ?\n", path, arg);
178 static UNUSED int w_execlp(const char *file, const char *arg, ...)
180 strace("execlp(%s, %s, ...) = ?\n", file, arg);
184 static UNUSED int w_execle(const char *path, const char *arg, ...)
186 strace("execle(%s, %s, ...) = ?\n", path, arg);
190 static UNUSED int w_execv(const char *path, char *const argv[])
192 strace("execv(%s, %p) = ?\n", path, argv);
193 return emu_do_execve(path, argv, environ);
196 static UNUSED int w_execvp(const char *file, char *const argv[])
198 strace("execvp(%s, %p) = ?\n", file, argv);
199 return emu_do_execve(file, argv, environ);
202 // static note: this can't safely return because of the way it's patched in
203 // static note2: can't be used, execve hangs?
204 static UNUSED int w_execve(const char *filename, char *const argv[],
207 strace("execve(%s, %p, %p) = ?\n", filename, argv, envp);
208 return emu_do_execve(filename, argv, envp);
211 static int w_chdir(const char *path)
214 if (path != NULL && strstr(path, "/usr/gp2x") != NULL)
218 strace("chdir(%s) = %d\n", path, ret);
241 #define MAKE_WRAP_SYM_N(sym) \
242 /* alias wrap symbols to real names */ \
243 typeof(sym) sym __attribute__((alias("w_" #sym)))
245 #define MAKE_WRAP_SYM(sym) \
246 MAKE_WRAP_SYM_N(sym); \
247 /* wrapper to real functions, to be set up on load */ \
248 static typeof(sym) *p_real_##sym
250 // note: update symver too
252 MAKE_WRAP_SYM(fopen);
255 MAKE_WRAP_SYM(ioctl);
256 MAKE_WRAP_SYM(sigaction);
257 MAKE_WRAP_SYM(tcgetattr);
258 MAKE_WRAP_SYM(tcsetattr);
259 MAKE_WRAP_SYM(system);
260 MAKE_WRAP_SYM_N(execl);
261 MAKE_WRAP_SYM_N(execlp);
262 MAKE_WRAP_SYM_N(execle);
263 MAKE_WRAP_SYM_N(execv);
264 MAKE_WRAP_SYM_N(execvp);
265 MAKE_WRAP_SYM(execve);
266 MAKE_WRAP_SYM(chdir);
267 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
269 #define REAL_FUNC_NP(name) \
270 { #name, (void **)&p_real_##name }
272 static const struct {
275 } real_funcs_np[] = {
281 REAL_FUNC_NP(sigaction),
282 REAL_FUNC_NP(tcgetattr),
283 REAL_FUNC_NP(tcsetattr),
284 REAL_FUNC_NP(system),
286 REAL_FUNC_NP(execve),
290 #define open p_real_open
291 #define fopen p_real_fopen
292 #define mmap p_real_mmap
293 #define read p_real_read
294 #define ioctl p_real_ioctl
295 #define sigaction p_real_sigaction
296 #define tcgetattr p_real_tcgetattr
297 #define tcsetattr p_real_tcsetattr
298 #define system p_real_system
299 #define execve p_real_execve
300 #define chdir p_real_chdir
307 // just call real funcs for static, pointer for dynamic
308 int real_open(const char *pathname, int flags, ...)
314 mode = va_arg(ap, mode_t);
316 return open(pathname, flags, mode);
319 FILE *real_fopen(const char *path, const char *mode)
321 return fopen(path, mode);
324 void *real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
326 return mmap(addr, length, prot, flags, fd, offset);
329 int real_read(int fd, void *buf, size_t count)
331 return read(fd, buf, count);
334 int real_ioctl(int d, int request, void *argp)
336 return ioctl(d, request, argp);
339 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
341 return sigaction(signum, act, oldact);
344 int real_tcgetattr(int fd, struct termios *termios_p)
346 return tcgetattr(fd, termios_p);
349 int real_tcsetattr(int fd, int optional_actions,
350 const struct termios *termios_p)
352 return tcsetattr(fd, optional_actions, termios_p);
355 int real_system(const char *command)
357 return system(command);
360 // real_exec* is missing intentionally - we don't need them
362 int real_execve(const char *filename, char *const argv[],
365 return execve(filename, argv, envp);
368 int real_chdir(const char *path)