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