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