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