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