hijack.c revision 1.21 1 1.21 christos /* $NetBSD: hijack.c,v 1.21 2011/01/26 18:48:32 christos Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 2011 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.1 pooka #include <sys/cdefs.h>
29 1.21 christos __RCSID("$NetBSD: hijack.c,v 1.21 2011/01/26 18:48:32 christos Exp $");
30 1.21 christos
31 1.21 christos #define __ssp_weak_name(fun) _hijack_ ## fun
32 1.1 pooka
33 1.1 pooka #include <sys/param.h>
34 1.1 pooka #include <sys/types.h>
35 1.10 pooka #include <sys/event.h>
36 1.1 pooka #include <sys/ioctl.h>
37 1.1 pooka #include <sys/socket.h>
38 1.1 pooka #include <sys/poll.h>
39 1.1 pooka
40 1.1 pooka #include <rump/rumpclient.h>
41 1.1 pooka #include <rump/rump_syscalls.h>
42 1.1 pooka
43 1.1 pooka #include <assert.h>
44 1.1 pooka #include <dlfcn.h>
45 1.1 pooka #include <err.h>
46 1.1 pooka #include <errno.h>
47 1.1 pooka #include <fcntl.h>
48 1.1 pooka #include <poll.h>
49 1.1 pooka #include <pthread.h>
50 1.3 pooka #include <signal.h>
51 1.1 pooka #include <stdarg.h>
52 1.8 pooka #include <stdbool.h>
53 1.1 pooka #include <stdio.h>
54 1.1 pooka #include <stdlib.h>
55 1.3 pooka #include <time.h>
56 1.1 pooka #include <unistd.h>
57 1.1 pooka
58 1.17 pooka enum dualcall {
59 1.17 pooka DUALCALL_WRITE, DUALCALL_WRITEV,
60 1.17 pooka DUALCALL_IOCTL, DUALCALL_FCNTL,
61 1.17 pooka DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_BIND, DUALCALL_CONNECT,
62 1.17 pooka DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN,
63 1.17 pooka DUALCALL_RECVFROM, DUALCALL_RECVMSG,
64 1.17 pooka DUALCALL_SENDTO, DUALCALL_SENDMSG,
65 1.17 pooka DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT,
66 1.17 pooka DUALCALL_SHUTDOWN,
67 1.17 pooka DUALCALL_READ, DUALCALL_READV,
68 1.17 pooka DUALCALL_DUP2, DUALCALL_CLOSE,
69 1.17 pooka DUALCALL_POLLTS,
70 1.17 pooka DUALCALL__NUM
71 1.1 pooka };
72 1.1 pooka
73 1.8 pooka #define RSYS_STRING(a) __STRING(a)
74 1.8 pooka #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a))
75 1.8 pooka
76 1.1 pooka /*
77 1.14 pooka * Would be nice to get this automatically in sync with libc.
78 1.14 pooka * Also, this does not work for compat-using binaries!
79 1.14 pooka */
80 1.14 pooka #if !__NetBSD_Prereq__(5,99,7)
81 1.17 pooka #define LIBCSELECT select
82 1.17 pooka #define LIBCPOLLTS pollts
83 1.17 pooka #define LIBCPOLL poll
84 1.14 pooka #else
85 1.17 pooka #define LIBCSELECT __select50
86 1.17 pooka #define LIBCPOLLTS __pollts50
87 1.17 pooka #define LIBCPOLL __poll50
88 1.17 pooka #endif
89 1.14 pooka
90 1.20 pooka int LIBCSELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *);
91 1.20 pooka int LIBCPOLLTS(struct pollfd *, nfds_t,
92 1.20 pooka const struct timespec *, const sigset_t *);
93 1.20 pooka int LIBCPOLL(struct pollfd *, nfds_t, int);
94 1.17 pooka
95 1.17 pooka #define S(a) __STRING(a)
96 1.17 pooka struct sysnames {
97 1.17 pooka enum dualcall scm_callnum;
98 1.17 pooka const char *scm_hostname;
99 1.17 pooka const char *scm_rumpname;
100 1.17 pooka } syscnames[] = {
101 1.17 pooka { DUALCALL_SOCKET, "__socket30", RSYS_NAME(SOCKET) },
102 1.17 pooka { DUALCALL_ACCEPT, "accept", RSYS_NAME(ACCEPT) },
103 1.17 pooka { DUALCALL_BIND, "bind", RSYS_NAME(BIND) },
104 1.17 pooka { DUALCALL_CONNECT, "connect", RSYS_NAME(CONNECT) },
105 1.17 pooka { DUALCALL_GETPEERNAME, "getpeername", RSYS_NAME(GETPEERNAME) },
106 1.17 pooka { DUALCALL_GETSOCKNAME, "getsockname", RSYS_NAME(GETSOCKNAME) },
107 1.17 pooka { DUALCALL_LISTEN, "listen", RSYS_NAME(LISTEN) },
108 1.17 pooka { DUALCALL_RECVFROM, "recvfrom", RSYS_NAME(RECVFROM) },
109 1.17 pooka { DUALCALL_RECVMSG, "recvmsg", RSYS_NAME(RECVMSG) },
110 1.17 pooka { DUALCALL_SENDTO, "sendto", RSYS_NAME(SENDTO) },
111 1.17 pooka { DUALCALL_SENDMSG, "sendmsg", RSYS_NAME(SENDMSG) },
112 1.17 pooka { DUALCALL_GETSOCKOPT, "getsockopt", RSYS_NAME(GETSOCKOPT) },
113 1.17 pooka { DUALCALL_SETSOCKOPT, "setsockopt", RSYS_NAME(SETSOCKOPT) },
114 1.17 pooka { DUALCALL_SHUTDOWN, "shutdown", RSYS_NAME(SHUTDOWN) },
115 1.17 pooka { DUALCALL_READ, "read", RSYS_NAME(READ) },
116 1.17 pooka { DUALCALL_READV, "readv", RSYS_NAME(READV) },
117 1.17 pooka { DUALCALL_WRITE, "write", RSYS_NAME(WRITE) },
118 1.17 pooka { DUALCALL_WRITEV, "writev", RSYS_NAME(WRITEV) },
119 1.17 pooka { DUALCALL_IOCTL, "ioctl", RSYS_NAME(IOCTL) },
120 1.17 pooka { DUALCALL_FCNTL, "fcntl", RSYS_NAME(FCNTL) },
121 1.17 pooka { DUALCALL_DUP2, "dup2", RSYS_NAME(DUP2) },
122 1.17 pooka { DUALCALL_CLOSE, "close", RSYS_NAME(CLOSE) },
123 1.17 pooka { DUALCALL_POLLTS, S(LIBCPOLLTS), RSYS_NAME(POLLTS) },
124 1.17 pooka };
125 1.17 pooka #undef S
126 1.17 pooka
127 1.17 pooka struct bothsys {
128 1.17 pooka void *bs_host;
129 1.17 pooka void *bs_rump;
130 1.17 pooka } syscalls[DUALCALL__NUM];
131 1.17 pooka #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which
132 1.17 pooka
133 1.17 pooka pid_t (*host_fork)(void);
134 1.17 pooka
135 1.17 pooka static unsigned dup2mask;
136 1.17 pooka #define ISDUP2D(fd) (1<<(fd) & dup2mask)
137 1.17 pooka
138 1.17 pooka //#define DEBUGJACK
139 1.17 pooka #ifdef DEBUGJACK
140 1.17 pooka #define DPRINTF(x) mydprintf x
141 1.17 pooka static void
142 1.17 pooka mydprintf(const char *fmt, ...)
143 1.17 pooka {
144 1.17 pooka va_list ap;
145 1.17 pooka
146 1.17 pooka if (ISDUP2D(STDERR_FILENO))
147 1.17 pooka return;
148 1.17 pooka
149 1.17 pooka va_start(ap, fmt);
150 1.17 pooka vfprintf(stderr, fmt, ap);
151 1.17 pooka va_end(ap);
152 1.17 pooka }
153 1.17 pooka
154 1.17 pooka #else
155 1.17 pooka #define DPRINTF(x)
156 1.14 pooka #endif
157 1.14 pooka
158 1.17 pooka #define FDCALL(type, name, rcname, args, proto, vars) \
159 1.17 pooka type name args \
160 1.17 pooka { \
161 1.17 pooka type (*fun) proto; \
162 1.17 pooka \
163 1.17 pooka if (fd_isrump(fd)) { \
164 1.17 pooka fun = syscalls[rcname].bs_rump; \
165 1.17 pooka fd = fd_host2rump(fd); \
166 1.17 pooka } else { \
167 1.17 pooka fun = syscalls[rcname].bs_host; \
168 1.17 pooka } \
169 1.17 pooka \
170 1.17 pooka return fun vars; \
171 1.17 pooka }
172 1.17 pooka
173 1.14 pooka /*
174 1.1 pooka * This is called from librumpclient in case of LD_PRELOAD.
175 1.1 pooka * It ensures correct RTLD_NEXT.
176 1.1 pooka */
177 1.1 pooka static void *
178 1.1 pooka hijackdlsym(void *handle, const char *symbol)
179 1.1 pooka {
180 1.1 pooka
181 1.1 pooka return dlsym(handle, symbol);
182 1.1 pooka }
183 1.1 pooka
184 1.7 pooka /* low calorie sockets? */
185 1.14 pooka static bool hostlocalsockets = true;
186 1.7 pooka
187 1.1 pooka static void __attribute__((constructor))
188 1.1 pooka rcinit(void)
189 1.1 pooka {
190 1.1 pooka int (*rumpcinit)(void);
191 1.1 pooka void **rumpcdlsym;
192 1.1 pooka void *hand;
193 1.19 pooka unsigned i, j;
194 1.1 pooka
195 1.1 pooka hand = dlopen("librumpclient.so", RTLD_LAZY|RTLD_GLOBAL);
196 1.1 pooka if (!hand)
197 1.1 pooka err(1, "cannot open librumpclient.so");
198 1.1 pooka rumpcinit = dlsym(hand, "rumpclient_init");
199 1.1 pooka _DIAGASSERT(rumpcinit);
200 1.1 pooka
201 1.1 pooka rumpcdlsym = dlsym(hand, "rumpclient_dlsym");
202 1.1 pooka *rumpcdlsym = hijackdlsym;
203 1.17 pooka host_fork = dlsym(RTLD_NEXT, "fork");
204 1.17 pooka
205 1.17 pooka /*
206 1.17 pooka * In theory cannot print anything during lookups because
207 1.17 pooka * we might not have the call vector set up. so, the errx()
208 1.17 pooka * is a bit of a strech, but it might work.
209 1.17 pooka */
210 1.1 pooka
211 1.17 pooka for (i = 0; i < DUALCALL__NUM; i++) {
212 1.17 pooka /* build runtime O(1) access */
213 1.17 pooka for (j = 0; j < __arraycount(syscnames); j++) {
214 1.17 pooka if (syscnames[j].scm_callnum == i)
215 1.17 pooka break;
216 1.17 pooka }
217 1.17 pooka
218 1.17 pooka if (j == __arraycount(syscnames))
219 1.17 pooka errx(1, "rumphijack error: syscall pos %d missing", i);
220 1.17 pooka
221 1.17 pooka syscalls[i].bs_host = dlsym(hand,syscnames[j].scm_hostname);
222 1.17 pooka if (syscalls[i].bs_host == NULL)
223 1.17 pooka errx(1, "hostcall %s not found missing",
224 1.17 pooka syscnames[j].scm_hostname);
225 1.17 pooka
226 1.17 pooka syscalls[i].bs_rump = dlsym(hand,syscnames[j].scm_rumpname);
227 1.17 pooka if (syscalls[i].bs_rump == NULL)
228 1.17 pooka errx(1, "rumpcall %s not found missing",
229 1.17 pooka syscnames[j].scm_rumpname);
230 1.1 pooka }
231 1.1 pooka
232 1.1 pooka if (rumpcinit() == -1)
233 1.1 pooka err(1, "rumpclient init");
234 1.1 pooka }
235 1.1 pooka
236 1.2 pooka /* XXX: need runtime selection. low for now due to FD_SETSIZE */
237 1.2 pooka #define HIJACK_FDOFF 128
238 1.2 pooka #define HIJACK_SELECT 128 /* XXX */
239 1.2 pooka #define HIJACK_ASSERT 128 /* XXX */
240 1.2 pooka static int
241 1.2 pooka fd_rump2host(int fd)
242 1.2 pooka {
243 1.2 pooka
244 1.2 pooka if (fd == -1)
245 1.2 pooka return fd;
246 1.2 pooka
247 1.2 pooka if (!ISDUP2D(fd))
248 1.2 pooka fd += HIJACK_FDOFF;
249 1.2 pooka
250 1.2 pooka return fd;
251 1.2 pooka }
252 1.2 pooka
253 1.2 pooka static int
254 1.2 pooka fd_host2rump(int fd)
255 1.2 pooka {
256 1.2 pooka
257 1.2 pooka if (!ISDUP2D(fd))
258 1.2 pooka fd -= HIJACK_FDOFF;
259 1.2 pooka return fd;
260 1.2 pooka }
261 1.2 pooka
262 1.2 pooka static bool
263 1.2 pooka fd_isrump(int fd)
264 1.2 pooka {
265 1.2 pooka
266 1.2 pooka return ISDUP2D(fd) || fd >= HIJACK_FDOFF;
267 1.2 pooka }
268 1.2 pooka
269 1.2 pooka #define assertfd(_fd_) assert(ISDUP2D(_fd_) || (_fd_) >= HIJACK_ASSERT)
270 1.2 pooka #undef HIJACK_FDOFF
271 1.2 pooka
272 1.1 pooka int __socket30(int, int, int);
273 1.1 pooka int
274 1.1 pooka __socket30(int domain, int type, int protocol)
275 1.1 pooka {
276 1.17 pooka int (*op_socket)(int, int, int);
277 1.1 pooka int fd;
278 1.7 pooka bool dohost;
279 1.7 pooka
280 1.7 pooka dohost = hostlocalsockets && (domain == AF_LOCAL);
281 1.1 pooka
282 1.7 pooka if (dohost)
283 1.17 pooka op_socket = GETSYSCALL(host, SOCKET);
284 1.7 pooka else
285 1.17 pooka op_socket = GETSYSCALL(rump, SOCKET);
286 1.17 pooka fd = op_socket(domain, type, protocol);
287 1.2 pooka
288 1.7 pooka if (!dohost)
289 1.7 pooka fd = fd_rump2host(fd);
290 1.7 pooka DPRINTF(("socket <- %d\n", fd));
291 1.2 pooka
292 1.7 pooka return fd;
293 1.1 pooka }
294 1.1 pooka
295 1.1 pooka int
296 1.1 pooka accept(int s, struct sockaddr *addr, socklen_t *addrlen)
297 1.1 pooka {
298 1.17 pooka int (*op_accept)(int, struct sockaddr *, socklen_t *);
299 1.1 pooka int fd;
300 1.7 pooka bool isrump;
301 1.7 pooka
302 1.7 pooka isrump = fd_isrump(s);
303 1.1 pooka
304 1.2 pooka DPRINTF(("accept -> %d", s));
305 1.7 pooka if (isrump) {
306 1.17 pooka op_accept = GETSYSCALL(rump, ACCEPT);
307 1.7 pooka s = fd_host2rump(s);
308 1.7 pooka } else {
309 1.17 pooka op_accept = GETSYSCALL(host, ACCEPT);
310 1.7 pooka }
311 1.17 pooka fd = op_accept(s, addr, addrlen);
312 1.7 pooka if (fd != -1 && isrump)
313 1.7 pooka fd = fd_rump2host(fd);
314 1.7 pooka
315 1.7 pooka DPRINTF((" <- %d\n", fd));
316 1.2 pooka
317 1.7 pooka return fd;
318 1.1 pooka }
319 1.1 pooka
320 1.17 pooka /*
321 1.17 pooka * ioctl and fcntl are varargs calls and need special treatment
322 1.17 pooka */
323 1.1 pooka int
324 1.17 pooka ioctl(int fd, unsigned long cmd, ...)
325 1.1 pooka {
326 1.17 pooka int (*op_ioctl)(int, unsigned long cmd, ...);
327 1.17 pooka va_list ap;
328 1.17 pooka int rv;
329 1.1 pooka
330 1.17 pooka DPRINTF(("ioctl -> %d\n", fd));
331 1.17 pooka if (fd_isrump(fd)) {
332 1.17 pooka fd = fd_host2rump(fd);
333 1.17 pooka op_ioctl = GETSYSCALL(rump, IOCTL);
334 1.7 pooka } else {
335 1.17 pooka op_ioctl = GETSYSCALL(host, IOCTL);
336 1.7 pooka }
337 1.1 pooka
338 1.17 pooka va_start(ap, cmd);
339 1.17 pooka rv = op_ioctl(fd, cmd, va_arg(ap, void *));
340 1.17 pooka va_end(ap);
341 1.17 pooka return rv;
342 1.1 pooka }
343 1.1 pooka
344 1.1 pooka int
345 1.17 pooka fcntl(int fd, int cmd, ...)
346 1.1 pooka {
347 1.17 pooka int (*op_fcntl)(int, int, ...);
348 1.17 pooka va_list ap;
349 1.17 pooka int rv;
350 1.1 pooka
351 1.17 pooka DPRINTF(("fcntl -> %d\n", fd));
352 1.17 pooka if (fd_isrump(fd)) {
353 1.17 pooka fd = fd_host2rump(fd);
354 1.17 pooka op_fcntl = GETSYSCALL(rump, FCNTL);
355 1.7 pooka } else {
356 1.17 pooka op_fcntl = GETSYSCALL(host, FCNTL);
357 1.7 pooka }
358 1.1 pooka
359 1.17 pooka va_start(ap, cmd);
360 1.17 pooka rv = op_fcntl(fd, cmd, va_arg(ap, void *));
361 1.17 pooka va_end(ap);
362 1.17 pooka return rv;
363 1.1 pooka }
364 1.1 pooka
365 1.17 pooka /*
366 1.17 pooka * write cannot issue a standard debug printf due to recursion
367 1.17 pooka */
368 1.1 pooka ssize_t
369 1.17 pooka write(int fd, const void *buf, size_t blen)
370 1.1 pooka {
371 1.17 pooka ssize_t (*op_write)(int, const void *, size_t);
372 1.1 pooka
373 1.17 pooka if (fd_isrump(fd)) {
374 1.17 pooka fd = fd_host2rump(fd);
375 1.17 pooka op_write = GETSYSCALL(rump, WRITE);
376 1.16 pooka } else {
377 1.17 pooka op_write = GETSYSCALL(host, WRITE);
378 1.16 pooka }
379 1.1 pooka
380 1.17 pooka return op_write(fd, buf, blen);
381 1.2 pooka }
382 1.2 pooka
383 1.2 pooka /*
384 1.2 pooka * dup2 is special. we allow dup2 of a rump kernel fd to 0-2 since
385 1.2 pooka * many programs do that. dup2 of a rump kernel fd to another value
386 1.2 pooka * not >= fdoff is an error.
387 1.2 pooka *
388 1.2 pooka * Note: cannot rump2host newd, because it is often hardcoded.
389 1.2 pooka */
390 1.2 pooka int
391 1.2 pooka dup2(int oldd, int newd)
392 1.2 pooka {
393 1.17 pooka int (*host_dup2)(int, int);
394 1.2 pooka int rv;
395 1.2 pooka
396 1.2 pooka DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
397 1.2 pooka
398 1.2 pooka if (fd_isrump(oldd)) {
399 1.2 pooka if (!(newd >= 0 && newd <= 2))
400 1.2 pooka return EBADF;
401 1.2 pooka oldd = fd_host2rump(oldd);
402 1.2 pooka rv = rump_sys_dup2(oldd, newd);
403 1.2 pooka if (rv != -1)
404 1.10 pooka dup2mask |= 1<<newd;
405 1.2 pooka } else {
406 1.17 pooka host_dup2 = syscalls[DUALCALL_DUP2].bs_host;
407 1.10 pooka rv = host_dup2(oldd, newd);
408 1.2 pooka }
409 1.10 pooka
410 1.10 pooka return rv;
411 1.2 pooka }
412 1.2 pooka
413 1.2 pooka /*
414 1.2 pooka * We just wrap fork the appropriate rump client calls to preserve
415 1.2 pooka * the file descriptors of the forked parent in the child, but
416 1.2 pooka * prevent double use of connection fd.
417 1.2 pooka */
418 1.2 pooka pid_t
419 1.2 pooka fork()
420 1.2 pooka {
421 1.2 pooka struct rumpclient_fork *rf;
422 1.2 pooka pid_t rv;
423 1.2 pooka
424 1.2 pooka DPRINTF(("fork\n"));
425 1.2 pooka
426 1.2 pooka if ((rf = rumpclient_prefork()) == NULL)
427 1.2 pooka return -1;
428 1.2 pooka
429 1.2 pooka switch ((rv = host_fork())) {
430 1.2 pooka case -1:
431 1.2 pooka /* XXX: cancel rf */
432 1.2 pooka break;
433 1.2 pooka case 0:
434 1.2 pooka if (rumpclient_fork_init(rf) == -1)
435 1.2 pooka rv = -1;
436 1.2 pooka break;
437 1.2 pooka default:
438 1.2 pooka break;
439 1.2 pooka }
440 1.2 pooka
441 1.2 pooka DPRINTF(("fork returns %d\n", rv));
442 1.2 pooka return rv;
443 1.1 pooka }
444 1.1 pooka
445 1.1 pooka /*
446 1.17 pooka * select is done by calling poll.
447 1.1 pooka */
448 1.1 pooka int
449 1.17 pooka LIBCSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
450 1.4 pooka struct timeval *timeout)
451 1.1 pooka {
452 1.4 pooka struct pollfd *pfds;
453 1.4 pooka struct timespec ts, *tsp = NULL;
454 1.19 pooka nfds_t realnfds;
455 1.19 pooka int i, j;
456 1.4 pooka int rv, incr;
457 1.4 pooka
458 1.7 pooka DPRINTF(("select\n"));
459 1.7 pooka
460 1.4 pooka /*
461 1.4 pooka * Well, first we must scan the fds to figure out how many
462 1.4 pooka * fds there really are. This is because up to and including
463 1.17 pooka * nb5 poll() silently refuses nfds > process_maxopen_fds.
464 1.4 pooka * Seems to be fixed in current, thank the maker.
465 1.4 pooka * god damn cluster...bomb.
466 1.4 pooka */
467 1.4 pooka
468 1.4 pooka for (i = 0, realnfds = 0; i < nfds; i++) {
469 1.4 pooka if (readfds && FD_ISSET(i, readfds)) {
470 1.4 pooka realnfds++;
471 1.4 pooka continue;
472 1.4 pooka }
473 1.4 pooka if (writefds && FD_ISSET(i, writefds)) {
474 1.4 pooka realnfds++;
475 1.4 pooka continue;
476 1.4 pooka }
477 1.4 pooka if (exceptfds && FD_ISSET(i, exceptfds)) {
478 1.4 pooka realnfds++;
479 1.4 pooka continue;
480 1.1 pooka }
481 1.1 pooka }
482 1.1 pooka
483 1.6 pooka if (realnfds) {
484 1.6 pooka pfds = malloc(sizeof(*pfds) * realnfds);
485 1.6 pooka if (!pfds)
486 1.6 pooka return -1;
487 1.6 pooka } else {
488 1.6 pooka pfds = NULL;
489 1.6 pooka }
490 1.1 pooka
491 1.4 pooka for (i = 0, j = 0; i < nfds; i++) {
492 1.4 pooka incr = 0;
493 1.4 pooka pfds[j].events = pfds[j].revents = 0;
494 1.4 pooka if (readfds && FD_ISSET(i, readfds)) {
495 1.4 pooka pfds[j].fd = i;
496 1.4 pooka pfds[j].events |= POLLIN;
497 1.4 pooka incr=1;
498 1.4 pooka }
499 1.4 pooka if (writefds && FD_ISSET(i, writefds)) {
500 1.4 pooka pfds[j].fd = i;
501 1.4 pooka pfds[j].events |= POLLOUT;
502 1.4 pooka incr=1;
503 1.4 pooka }
504 1.4 pooka if (exceptfds && FD_ISSET(i, exceptfds)) {
505 1.4 pooka pfds[j].fd = i;
506 1.4 pooka pfds[j].events |= POLLHUP|POLLERR;
507 1.4 pooka incr=1;
508 1.1 pooka }
509 1.4 pooka if (incr)
510 1.4 pooka j++;
511 1.1 pooka }
512 1.1 pooka
513 1.4 pooka if (timeout) {
514 1.4 pooka TIMEVAL_TO_TIMESPEC(timeout, &ts);
515 1.4 pooka tsp = &ts;
516 1.4 pooka }
517 1.4 pooka rv = pollts(pfds, realnfds, tsp, NULL);
518 1.4 pooka if (rv <= 0)
519 1.4 pooka goto out;
520 1.4 pooka
521 1.4 pooka /*
522 1.4 pooka * ok, harvest results. first zero out entries (can't use
523 1.4 pooka * FD_ZERO for the obvious select-me-not reason). whee.
524 1.4 pooka */
525 1.4 pooka for (i = 0; i < nfds; i++) {
526 1.4 pooka if (readfds)
527 1.4 pooka FD_CLR(i, readfds);
528 1.4 pooka if (writefds)
529 1.4 pooka FD_CLR(i, writefds);
530 1.4 pooka if (exceptfds)
531 1.4 pooka FD_CLR(i, exceptfds);
532 1.1 pooka }
533 1.1 pooka
534 1.4 pooka /* and then plug in the results */
535 1.19 pooka for (i = 0; i < (int)realnfds; i++) {
536 1.4 pooka if (readfds) {
537 1.4 pooka if (pfds[i].revents & POLLIN) {
538 1.4 pooka FD_SET(pfds[i].fd, readfds);
539 1.4 pooka }
540 1.4 pooka }
541 1.4 pooka if (writefds) {
542 1.4 pooka if (pfds[i].revents & POLLOUT) {
543 1.4 pooka FD_SET(pfds[i].fd, writefds);
544 1.4 pooka }
545 1.4 pooka }
546 1.4 pooka if (exceptfds) {
547 1.4 pooka if (pfds[i].revents & (POLLHUP|POLLERR)) {
548 1.4 pooka FD_SET(pfds[i].fd, exceptfds);
549 1.4 pooka }
550 1.4 pooka }
551 1.1 pooka }
552 1.1 pooka
553 1.4 pooka out:
554 1.4 pooka free(pfds);
555 1.1 pooka return rv;
556 1.1 pooka }
557 1.1 pooka
558 1.1 pooka static void
559 1.1 pooka checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
560 1.1 pooka {
561 1.1 pooka nfds_t i;
562 1.1 pooka
563 1.1 pooka for (i = 0; i < nfds; i++) {
564 1.12 pooka if (fds[i].fd == -1)
565 1.12 pooka continue;
566 1.12 pooka
567 1.2 pooka if (fd_isrump(fds[i].fd))
568 1.2 pooka (*rumpcall)++;
569 1.2 pooka else
570 1.1 pooka (*hostcall)++;
571 1.1 pooka }
572 1.1 pooka }
573 1.1 pooka
574 1.1 pooka static void
575 1.2 pooka adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
576 1.1 pooka {
577 1.1 pooka nfds_t i;
578 1.1 pooka
579 1.1 pooka for (i = 0; i < nfds; i++) {
580 1.2 pooka fds[i].fd = fdadj(fds[i].fd);
581 1.1 pooka }
582 1.1 pooka }
583 1.1 pooka
584 1.1 pooka /*
585 1.1 pooka * poll is easy as long as the call comes in the fds only in one
586 1.1 pooka * kernel. otherwise its quite tricky...
587 1.1 pooka */
588 1.1 pooka struct pollarg {
589 1.1 pooka struct pollfd *pfds;
590 1.1 pooka nfds_t nfds;
591 1.3 pooka const struct timespec *ts;
592 1.3 pooka const sigset_t *sigmask;
593 1.1 pooka int pipefd;
594 1.1 pooka int errnum;
595 1.1 pooka };
596 1.1 pooka
597 1.1 pooka static void *
598 1.1 pooka hostpoll(void *arg)
599 1.1 pooka {
600 1.17 pooka int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
601 1.17 pooka const sigset_t *);
602 1.1 pooka struct pollarg *parg = arg;
603 1.1 pooka intptr_t rv;
604 1.1 pooka
605 1.17 pooka op_pollts = syscalls[DUALCALL_POLLTS].bs_host;
606 1.17 pooka rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
607 1.1 pooka if (rv == -1)
608 1.1 pooka parg->errnum = errno;
609 1.1 pooka rump_sys_write(parg->pipefd, &rv, sizeof(rv));
610 1.1 pooka
611 1.1 pooka return (void *)(intptr_t)rv;
612 1.1 pooka }
613 1.1 pooka
614 1.1 pooka int
615 1.17 pooka LIBCPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
616 1.3 pooka const sigset_t *sigmask)
617 1.1 pooka {
618 1.3 pooka int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
619 1.3 pooka const sigset_t *);
620 1.17 pooka int (*host_close)(int);
621 1.1 pooka int hostcall = 0, rumpcall = 0;
622 1.1 pooka pthread_t pt;
623 1.1 pooka nfds_t i;
624 1.1 pooka int rv;
625 1.1 pooka
626 1.2 pooka DPRINTF(("poll\n"));
627 1.1 pooka checkpoll(fds, nfds, &hostcall, &rumpcall);
628 1.1 pooka
629 1.1 pooka if (hostcall && rumpcall) {
630 1.1 pooka struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
631 1.1 pooka int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
632 1.1 pooka struct pollarg parg;
633 1.1 pooka uintptr_t lrv;
634 1.1 pooka int sverrno = 0, trv;
635 1.1 pooka
636 1.1 pooka /*
637 1.1 pooka * ok, this is where it gets tricky. We must support
638 1.1 pooka * this since it's a very common operation in certain
639 1.1 pooka * types of software (telnet, netcat, etc). We allocate
640 1.1 pooka * two vectors and run two poll commands in separate
641 1.1 pooka * threads. Whichever returns first "wins" and the
642 1.1 pooka * other kernel's fds won't show activity.
643 1.1 pooka */
644 1.1 pooka rv = -1;
645 1.1 pooka
646 1.1 pooka /* allocate full vector for O(n) joining after call */
647 1.1 pooka pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
648 1.1 pooka if (!pfd_host)
649 1.1 pooka goto out;
650 1.1 pooka pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
651 1.1 pooka if (!pfd_rump) {
652 1.1 pooka goto out;
653 1.1 pooka }
654 1.1 pooka
655 1.1 pooka /* split vectors */
656 1.1 pooka for (i = 0; i < nfds; i++) {
657 1.3 pooka if (fds[i].fd == -1) {
658 1.3 pooka pfd_host[i].fd = -1;
659 1.3 pooka pfd_rump[i].fd = -1;
660 1.3 pooka } else if (fd_isrump(fds[i].fd)) {
661 1.2 pooka pfd_host[i].fd = -1;
662 1.2 pooka pfd_rump[i].fd = fd_host2rump(fds[i].fd);
663 1.2 pooka pfd_rump[i].events = fds[i].events;
664 1.2 pooka } else {
665 1.2 pooka pfd_rump[i].fd = -1;
666 1.1 pooka pfd_host[i].fd = fds[i].fd;
667 1.1 pooka pfd_host[i].events = fds[i].events;
668 1.1 pooka }
669 1.13 pooka fds[i].revents = 0;
670 1.1 pooka }
671 1.1 pooka
672 1.1 pooka /*
673 1.1 pooka * then, open two pipes, one for notifications
674 1.1 pooka * to each kernel.
675 1.1 pooka */
676 1.1 pooka if (rump_sys_pipe(rpipe) == -1)
677 1.1 pooka goto out;
678 1.1 pooka if (pipe(hpipe) == -1)
679 1.1 pooka goto out;
680 1.1 pooka
681 1.1 pooka pfd_host[nfds].fd = hpipe[0];
682 1.1 pooka pfd_host[nfds].events = POLLIN;
683 1.1 pooka pfd_rump[nfds].fd = rpipe[0];
684 1.1 pooka pfd_rump[nfds].events = POLLIN;
685 1.1 pooka
686 1.1 pooka /*
687 1.1 pooka * then, create a thread to do host part and meanwhile
688 1.1 pooka * do rump kernel part right here
689 1.1 pooka */
690 1.1 pooka
691 1.1 pooka parg.pfds = pfd_host;
692 1.1 pooka parg.nfds = nfds+1;
693 1.3 pooka parg.ts = ts;
694 1.3 pooka parg.sigmask = sigmask;
695 1.1 pooka parg.pipefd = rpipe[1];
696 1.1 pooka pthread_create(&pt, NULL, hostpoll, &parg);
697 1.1 pooka
698 1.17 pooka op_pollts = syscalls[DUALCALL_POLLTS].bs_rump;
699 1.3 pooka lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
700 1.1 pooka sverrno = errno;
701 1.1 pooka write(hpipe[1], &rv, sizeof(rv));
702 1.1 pooka pthread_join(pt, (void *)&trv);
703 1.1 pooka
704 1.1 pooka /* check who "won" and merge results */
705 1.1 pooka if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
706 1.1 pooka rv = trv;
707 1.1 pooka
708 1.1 pooka for (i = 0; i < nfds; i++) {
709 1.1 pooka if (pfd_rump[i].fd != -1)
710 1.1 pooka fds[i].revents = pfd_rump[i].revents;
711 1.1 pooka }
712 1.1 pooka sverrno = parg.errnum;
713 1.1 pooka } else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
714 1.1 pooka rv = trv;
715 1.1 pooka
716 1.1 pooka for (i = 0; i < nfds; i++) {
717 1.1 pooka if (pfd_host[i].fd != -1)
718 1.1 pooka fds[i].revents = pfd_host[i].revents;
719 1.1 pooka }
720 1.1 pooka } else {
721 1.1 pooka rv = 0;
722 1.1 pooka }
723 1.1 pooka
724 1.1 pooka out:
725 1.17 pooka host_close = syscalls[DUALCALL_CLOSE].bs_host;
726 1.1 pooka if (rpipe[0] != -1)
727 1.1 pooka rump_sys_close(rpipe[0]);
728 1.1 pooka if (rpipe[1] != -1)
729 1.1 pooka rump_sys_close(rpipe[1]);
730 1.1 pooka if (hpipe[0] != -1)
731 1.9 pooka host_close(hpipe[0]);
732 1.1 pooka if (hpipe[1] != -1)
733 1.9 pooka host_close(hpipe[1]);
734 1.1 pooka free(pfd_host);
735 1.1 pooka free(pfd_rump);
736 1.1 pooka errno = sverrno;
737 1.1 pooka } else {
738 1.1 pooka if (hostcall) {
739 1.17 pooka op_pollts = syscalls[DUALCALL_POLLTS].bs_host;
740 1.1 pooka } else {
741 1.17 pooka op_pollts = syscalls[DUALCALL_POLLTS].bs_rump;
742 1.2 pooka adjustpoll(fds, nfds, fd_host2rump);
743 1.1 pooka }
744 1.1 pooka
745 1.3 pooka rv = op_pollts(fds, nfds, ts, sigmask);
746 1.1 pooka if (rumpcall)
747 1.2 pooka adjustpoll(fds, nfds, fd_rump2host);
748 1.1 pooka }
749 1.1 pooka
750 1.1 pooka return rv;
751 1.1 pooka }
752 1.1 pooka
753 1.1 pooka int
754 1.17 pooka LIBCPOLL(struct pollfd *fds, nfds_t nfds, int timeout)
755 1.1 pooka {
756 1.3 pooka struct timespec ts;
757 1.3 pooka struct timespec *tsp = NULL;
758 1.3 pooka
759 1.3 pooka if (timeout != INFTIM) {
760 1.3 pooka ts.tv_sec = timeout / 1000;
761 1.11 pooka ts.tv_nsec = (timeout % 1000) * 1000*1000;
762 1.3 pooka
763 1.3 pooka tsp = &ts;
764 1.3 pooka }
765 1.1 pooka
766 1.3 pooka return pollts(fds, nfds, tsp, NULL);
767 1.1 pooka }
768 1.10 pooka
769 1.10 pooka int
770 1.10 pooka kqueue(void)
771 1.10 pooka {
772 1.10 pooka
773 1.17 pooka fprintf(stderr, "kqueue unsupported");
774 1.10 pooka abort();
775 1.17 pooka /*NOTREACHED*/
776 1.10 pooka }
777 1.10 pooka
778 1.17 pooka /*ARGSUSED*/
779 1.10 pooka int
780 1.10 pooka kevent(int kq, const struct kevent *changelist, size_t nchanges,
781 1.10 pooka struct kevent *eventlist, size_t nevents,
782 1.10 pooka const struct timespec *timeout)
783 1.10 pooka {
784 1.10 pooka
785 1.17 pooka fprintf(stderr, "kqueue unsupported");
786 1.10 pooka abort();
787 1.17 pooka /*NOTREACHED*/
788 1.10 pooka }
789 1.17 pooka
790 1.17 pooka /*
791 1.17 pooka * Rest are std type calls.
792 1.17 pooka */
793 1.17 pooka
794 1.17 pooka FDCALL(int, bind, DUALCALL_BIND, \
795 1.17 pooka (int fd, const struct sockaddr *name, socklen_t namelen), \
796 1.17 pooka (int, const struct sockaddr *, socklen_t), \
797 1.17 pooka (fd, name, namelen))
798 1.17 pooka
799 1.17 pooka FDCALL(int, connect, DUALCALL_CONNECT, \
800 1.17 pooka (int fd, const struct sockaddr *name, socklen_t namelen), \
801 1.17 pooka (int, const struct sockaddr *, socklen_t), \
802 1.17 pooka (fd, name, namelen))
803 1.17 pooka
804 1.17 pooka FDCALL(int, getpeername, DUALCALL_GETPEERNAME, \
805 1.17 pooka (int fd, struct sockaddr *name, socklen_t *namelen), \
806 1.17 pooka (int, struct sockaddr *, socklen_t *), \
807 1.17 pooka (fd, name, namelen))
808 1.17 pooka
809 1.17 pooka FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, \
810 1.17 pooka (int fd, struct sockaddr *name, socklen_t *namelen), \
811 1.17 pooka (int, struct sockaddr *, socklen_t *), \
812 1.17 pooka (fd, name, namelen))
813 1.17 pooka
814 1.17 pooka FDCALL(int, listen, DUALCALL_LISTEN, \
815 1.17 pooka (int fd, int backlog), \
816 1.17 pooka (int, int), \
817 1.17 pooka (fd, backlog))
818 1.17 pooka
819 1.17 pooka FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, \
820 1.17 pooka (int fd, void *buf, size_t len, int flags, \
821 1.17 pooka struct sockaddr *from, socklen_t *fromlen), \
822 1.17 pooka (int, void *, size_t, int, struct sockaddr *, socklen_t *), \
823 1.17 pooka (fd, buf, len, flags, from, fromlen))
824 1.17 pooka
825 1.17 pooka FDCALL(ssize_t, sendto, DUALCALL_SENDTO, \
826 1.17 pooka (int fd, const void *buf, size_t len, int flags, \
827 1.17 pooka const struct sockaddr *to, socklen_t tolen), \
828 1.17 pooka (int, const void *, size_t, int, \
829 1.17 pooka const struct sockaddr *, socklen_t), \
830 1.17 pooka (fd, buf, len, flags, to, tolen))
831 1.17 pooka
832 1.17 pooka FDCALL(ssize_t, recvmsg, DUALCALL_RECVMSG, \
833 1.17 pooka (int fd, struct msghdr *msg, int flags), \
834 1.17 pooka (int, struct msghdr *, int), \
835 1.17 pooka (fd, msg, flags))
836 1.17 pooka
837 1.17 pooka FDCALL(ssize_t, sendmsg, DUALCALL_SENDMSG, \
838 1.17 pooka (int fd, const struct msghdr *msg, int flags), \
839 1.17 pooka (int, const struct msghdr *, int), \
840 1.17 pooka (fd, msg, flags))
841 1.17 pooka
842 1.17 pooka FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, \
843 1.17 pooka (int fd, int level, int optn, void *optval, socklen_t *optlen), \
844 1.17 pooka (int, int, int, void *, socklen_t *), \
845 1.17 pooka (fd, level, optn, optval, optlen))
846 1.17 pooka
847 1.17 pooka FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, \
848 1.17 pooka (int fd, int level, int optn, \
849 1.17 pooka const void *optval, socklen_t optlen), \
850 1.17 pooka (int, int, int, const void *, socklen_t), \
851 1.17 pooka (fd, level, optn, optval, optlen))
852 1.17 pooka
853 1.17 pooka FDCALL(int, shutdown, DUALCALL_SHUTDOWN, \
854 1.17 pooka (int fd, int how), \
855 1.17 pooka (int, int), \
856 1.17 pooka (fd, how))
857 1.17 pooka
858 1.21 christos #if _FORTIFY_SOURCE > 0
859 1.21 christos #define STUB(fun) __ssp_weak_name(fun)
860 1.21 christos ssize_t _sys_readlink(const char * __restrict, char * __restrict, size_t);
861 1.21 christos ssize_t
862 1.21 christos STUB(readlink)(const char * __restrict path, char * __restrict buf,
863 1.21 christos size_t bufsiz)
864 1.21 christos {
865 1.21 christos return _sys_readlink(path, buf, bufsiz);
866 1.21 christos }
867 1.21 christos
868 1.21 christos char *_sys_getcwd(char *, size_t);
869 1.21 christos char *
870 1.21 christos STUB(getcwd)(char *buf, size_t size)
871 1.21 christos {
872 1.21 christos return _sys_getcwd(buf, size);
873 1.21 christos }
874 1.21 christos #else
875 1.21 christos #define STUB(fun) fun
876 1.21 christos #endif
877 1.21 christos
878 1.21 christos FDCALL(ssize_t, STUB(read), DUALCALL_READ, \
879 1.17 pooka (int fd, void *buf, size_t buflen), \
880 1.17 pooka (int, void *, size_t), \
881 1.17 pooka (fd, buf, buflen))
882 1.17 pooka
883 1.18 pooka FDCALL(ssize_t, readv, DUALCALL_READV, \
884 1.17 pooka (int fd, const struct iovec *iov, int iovcnt), \
885 1.17 pooka (int, const struct iovec *, int), \
886 1.17 pooka (fd, iov, iovcnt))
887 1.17 pooka
888 1.17 pooka FDCALL(ssize_t, writev, DUALCALL_WRITEV, \
889 1.17 pooka (int fd, const struct iovec *iov, int iovcnt), \
890 1.17 pooka (int, const struct iovec *, int), \
891 1.17 pooka (fd, iov, iovcnt))
892 1.17 pooka
893 1.17 pooka FDCALL(int, close, DUALCALL_CLOSE, \
894 1.17 pooka (int fd), \
895 1.17 pooka (int), \
896 1.17 pooka (fd))
897