1 // vim:shiftwidth=2:expandtab
10 #include <sys/ioctl.h>
14 #include "realfuncs.h"
23 #define UNUSED __attribute__((unused))
25 static const struct dev_fd_t takeover_devs[] = {
26 { "/dev/mem", FAKEDEV_MEM },
27 { "/dev/GPIO", FAKEDEV_GPIO },
28 { "/dev/fb0", FAKEDEV_FB0 },
29 { "/dev/fb/0", FAKEDEV_FB0 },
30 { "/dev/fb1", FAKEDEV_FB1 },
31 { "/dev/fb/1", FAKEDEV_FB1 },
32 { "/dev/mmuhack", FAKEDEV_MMUHACK },
33 { "/dev/tty", -1 }, // XXX hmh..
34 { "/dev/tty0", FAKEDEV_TTY0 },
37 static int w_open(const char *pathname, int flags, mode_t mode)
41 for (i = 0; i < ARRAY_SIZE(takeover_devs); i++) {
42 if (strcmp(pathname, takeover_devs[i].name) == 0) {
43 ret = takeover_devs[i].fd;
48 if (i == ARRAY_SIZE(takeover_devs))
49 ret = open(pathname, flags, mode);
52 for (i = 0; emu_interesting_fds[i].name != NULL; i++) {
53 if (strcmp(pathname, emu_interesting_fds[i].name) == 0) {
54 emu_interesting_fds[i].fd = ret;
60 strace("open(%s) = %d\n", pathname, ret);
64 static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
67 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
68 ret = emu_do_mmap(length, prot, flags, fd, offset);
70 ret = mmap(addr, length, prot, flags, fd, offset);
72 // threads are using heap before they mmap their stack
73 // printf needs valid stack for pthread/errno
74 if (((long)&ret & 0xf0000000) == 0xb0000000)
75 strace("mmap(%p, %x, %x, %x, %d, %lx) = %p\n", addr, length, prot, flags, fd, (long)offset, ret);
78 #define w_mmap2 w_mmap
80 static ssize_t w_read(int fd, void *buf, size_t count)
83 if (fd == FAKEDEV_GPIO)
84 ret = emu_read_gpiodev(buf, count);
86 ret = read(fd, buf, count);
88 //strace("read(%d, %p, %d) = %d\n", fd, buf, count, ret);
92 static int w_ioctl(int fd, int request, void *argp)
96 if ((FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY) ||
97 fd == emu_interesting_fds[IFD_SOUND].fd)
98 ret = emu_do_ioctl(fd, request, argp);
100 ret = ioctl(fd, request, argp);
102 strace("ioctl(%d, %08x, %p) = %d\n", fd, request, argp, ret);
106 static int w_sigaction(int signum, const void *act, void *oldact)
108 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
113 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
116 if (fd != FAKEDEV_TTY0)
117 ret = tcgetattr(fd, termios_p);
121 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
125 static UNUSED int w_tcsetattr(int fd, int optional_actions,
126 const struct termios *termios_p)
129 if (fd != FAKEDEV_TTY0)
130 ret = tcsetattr(fd, optional_actions, termios_p);
134 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
138 static UNUSED FILE *w_fopen(const char *path, const char *mode)
140 FILE *ret = emu_do_fopen(path, mode);
141 strace("fopen(%s, %s) = %p\n", path, mode, ret);
145 static UNUSED int w_system(const char *command)
147 int ret = emu_do_system(command);
148 strace("system(%s) = %d\n", command, ret);
164 #define MAKE_WRAP_SYM(sym) \
165 /* alias wrap symbols to real names */ \
166 typeof(sym) sym __attribute__((alias("w_" #sym))); \
167 /* wrapper to real functions, to be set up on load */ \
168 static typeof(sym) *p_real_##sym
171 MAKE_WRAP_SYM(fopen);
174 MAKE_WRAP_SYM(ioctl);
175 MAKE_WRAP_SYM(sigaction);
176 MAKE_WRAP_SYM(tcgetattr);
177 MAKE_WRAP_SYM(tcsetattr);
178 MAKE_WRAP_SYM(system);
179 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
181 #define REAL_FUNC_NP(name) \
182 { #name, (void **)&p_real_##name }
184 static const struct {
187 } real_funcs_np[] = {
193 REAL_FUNC_NP(sigaction),
194 REAL_FUNC_NP(tcgetattr),
195 REAL_FUNC_NP(tcsetattr),
196 REAL_FUNC_NP(system),
199 #define open p_real_open
200 #define fopen p_real_fopen
201 #define mmap p_real_mmap
202 #define read p_real_read
203 #define ioctl p_real_ioctl
204 #define sigaction p_real_sigaction
205 #define tcgetattr p_real_tcgetattr
206 #define tcsetattr p_real_tcsetattr
207 #define system p_real_system
214 // just call real funcs for static, pointer for dynamic
215 int real_open(const char *pathname, int flags, mode_t mode)
217 return open(pathname, flags, mode);
220 FILE *real_fopen(const char *path, const char *mode)
222 return fopen(path, mode);
225 void *real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
227 return mmap(addr, length, prot, flags, fd, offset);
230 int real_read(int fd, void *buf, size_t count)
232 return read(fd, buf, count);
235 int real_ioctl(int d, int request, void *argp)
237 return ioctl(d, request, argp);
240 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
242 return sigaction(signum, act, oldact);
245 int real_tcgetattr(int fd, struct termios *termios_p)
247 return tcgetattr(fd, termios_p);
250 int real_tcsetattr(int fd, int optional_actions,
251 const struct termios *termios_p)
253 return tcsetattr(fd, optional_actions, termios_p);
256 int real_system(const char *command)
258 return system(command);