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 },
38 static int w_open(const char *pathname, int flags, mode_t mode)
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;
49 if (i == ARRAY_SIZE(takeover_devs))
50 ret = open(pathname, flags, mode);
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;
61 strace("open(%s) = %d\n", pathname, ret);
65 static void *w_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
68 if (FAKEDEV_MEM <= fd && fd < FAKEDEV_FD_BOUNDARY)
69 ret = emu_do_mmap(length, prot, flags, fd, offset);
71 ret = mmap(addr, length, prot, flags, fd, offset);
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);
79 #define w_mmap2 w_mmap
81 static ssize_t w_read(int fd, void *buf, size_t count)
84 if (fd == FAKEDEV_GPIO)
85 ret = emu_read_gpiodev(buf, count);
87 ret = read(fd, buf, count);
89 //strace("read(%d, %p, %d) = %d\n", fd, buf, count, ret);
93 static int w_ioctl(int fd, int request, void *argp)
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);
101 ret = ioctl(fd, request, argp);
103 strace("ioctl(%d, %08x, %p) = %d\n", fd, request, argp, ret);
107 static int w_sigaction(int signum, const void *act, void *oldact)
109 strace("sigaction(%d, %p, %p) = %d\n", signum, act, oldact, 0);
114 static UNUSED int w_tcgetattr(int fd, struct termios *termios_p)
117 if (fd != FAKEDEV_TTY0)
118 ret = tcgetattr(fd, termios_p);
122 strace("tcgetattr(%d, %p) = %d\n", fd, termios_p, ret);
126 static UNUSED int w_tcsetattr(int fd, int optional_actions,
127 const struct termios *termios_p)
130 if (fd != FAKEDEV_TTY0)
131 ret = tcsetattr(fd, optional_actions, termios_p);
135 strace("tcsetattr(%d, %x, %p) = %d\n", fd, optional_actions, termios_p, ret);
139 static UNUSED FILE *w_fopen(const char *path, const char *mode)
141 FILE *ret = emu_do_fopen(path, mode);
142 strace("fopen(%s, %s) = %p\n", path, mode, ret);
146 static UNUSED int w_system(const char *command)
148 int ret = emu_do_system(command);
149 strace("system(%s) = %d\n", command, ret);
165 #define MAKE_WRAP_SYM(sym) \
166 /* alias wrap symbols to real names */ \
167 typeof(sym) sym __attribute__((alias("w_" #sym))); \
168 /* wrapper to real functions, to be set up on load */ \
169 static typeof(sym) *p_real_##sym
171 // note: update symver too
173 MAKE_WRAP_SYM(fopen);
176 MAKE_WRAP_SYM(ioctl);
177 MAKE_WRAP_SYM(sigaction);
178 MAKE_WRAP_SYM(tcgetattr);
179 MAKE_WRAP_SYM(tcsetattr);
180 MAKE_WRAP_SYM(system);
181 typeof(mmap) mmap2 __attribute__((alias("w_mmap")));
183 #define REAL_FUNC_NP(name) \
184 { #name, (void **)&p_real_##name }
186 static const struct {
189 } real_funcs_np[] = {
195 REAL_FUNC_NP(sigaction),
196 REAL_FUNC_NP(tcgetattr),
197 REAL_FUNC_NP(tcsetattr),
198 REAL_FUNC_NP(system),
201 #define open p_real_open
202 #define fopen p_real_fopen
203 #define mmap p_real_mmap
204 #define read p_real_read
205 #define ioctl p_real_ioctl
206 #define sigaction p_real_sigaction
207 #define tcgetattr p_real_tcgetattr
208 #define tcsetattr p_real_tcsetattr
209 #define system p_real_system
216 // just call real funcs for static, pointer for dynamic
217 int real_open(const char *pathname, int flags, ...)
223 mode = va_arg(ap, mode_t);
225 return open(pathname, flags, mode);
228 FILE *real_fopen(const char *path, const char *mode)
230 return fopen(path, mode);
233 void *real_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
235 return mmap(addr, length, prot, flags, fd, offset);
238 int real_read(int fd, void *buf, size_t count)
240 return read(fd, buf, count);
243 int real_ioctl(int d, int request, void *argp)
245 return ioctl(d, request, argp);
248 int real_sigaction(int signum, const sigaction_t *act, sigaction_t *oldact)
250 return sigaction(signum, act, oldact);
253 int real_tcgetattr(int fd, struct termios *termios_p)
255 return tcgetattr(fd, termios_p);
258 int real_tcsetattr(int fd, int optional_actions,
259 const struct termios *termios_p)
261 return tcsetattr(fd, optional_actions, termios_p);
264 int real_system(const char *command)
266 return system(command);