Home | History | Annotate | Line # | Download | only in librumphijack
hijack.c revision 1.47
      1 /*      $NetBSD: hijack.c,v 1.47 2011/02/17 15:20:10 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.47 2011/02/17 15:20:10 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 #include <sys/statvfs.h>
     40 
     41 #include <rump/rumpclient.h>
     42 #include <rump/rump_syscalls.h>
     43 
     44 #include <assert.h>
     45 #include <dlfcn.h>
     46 #include <err.h>
     47 #include <errno.h>
     48 #include <fcntl.h>
     49 #include <poll.h>
     50 #include <pthread.h>
     51 #include <signal.h>
     52 #include <stdarg.h>
     53 #include <stdbool.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <time.h>
     58 #include <unistd.h>
     59 
     60 enum dualcall {
     61 	DUALCALL_WRITE, DUALCALL_WRITEV,
     62 	DUALCALL_IOCTL, DUALCALL_FCNTL,
     63 	DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_BIND, DUALCALL_CONNECT,
     64 	DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN,
     65 	DUALCALL_RECVFROM, DUALCALL_RECVMSG,
     66 	DUALCALL_SENDTO, DUALCALL_SENDMSG,
     67 	DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT,
     68 	DUALCALL_SHUTDOWN,
     69 	DUALCALL_READ, DUALCALL_READV,
     70 	DUALCALL_DUP2,
     71 	DUALCALL_CLOSE,
     72 	DUALCALL_POLLTS,
     73 	DUALCALL_KEVENT,
     74 	DUALCALL_STAT, DUALCALL_LSTAT, DUALCALL_FSTAT,
     75 	DUALCALL_CHMOD, DUALCALL_LCHMOD, DUALCALL_FCHMOD,
     76 	DUALCALL_CHOWN, DUALCALL_LCHOWN, DUALCALL_FCHOWN,
     77 	DUALCALL_OPEN,
     78 	DUALCALL_STATVFS1, DUALCALL_FSTATVFS1,
     79 	DUALCALL_CHDIR, DUALCALL_FCHDIR,
     80 	DUALCALL_LSEEK,
     81 	DUALCALL_GETDENTS,
     82 	DUALCALL_UNLINK, DUALCALL_SYMLINK, DUALCALL_READLINK,
     83 	DUALCALL_RENAME,
     84 	DUALCALL_MKDIR, DUALCALL_RMDIR,
     85 	DUALCALL_UTIMES, DUALCALL_LUTIMES, DUALCALL_FUTIMES,
     86 	DUALCALL_TRUNCATE, DUALCALL_FTRUNCATE,
     87 	DUALCALL_FSYNC, DUALCALL_FSYNC_RANGE,
     88 	DUALCALL__NUM
     89 };
     90 
     91 #define RSYS_STRING(a) __STRING(a)
     92 #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a))
     93 
     94 /*
     95  * Would be nice to get this automatically in sync with libc.
     96  * Also, this does not work for compat-using binaries!
     97  */
     98 #if !__NetBSD_Prereq__(5,99,7)
     99 #define REALSELECT select
    100 #define REALPOLLTS pollts
    101 #define REALKEVENT kevent
    102 #define REALSTAT __stat30
    103 #define REALLSTAT __lstat30
    104 #define REALFSTAT __fstat30
    105 #define REALUTIMES utimes
    106 #define REALLUTIMES lutimes
    107 #define REALFUTIMES futimes
    108 #else
    109 #define REALSELECT _sys___select50
    110 #define REALPOLLTS _sys___pollts50
    111 #define REALKEVENT _sys___kevent50
    112 #define REALSTAT __stat50
    113 #define REALLSTAT __lstat50
    114 #define REALFSTAT __fstat50
    115 #define REALUTIMES __utimes50
    116 #define REALLUTIMES __lutimes50
    117 #define REALFUTIMES __futimes50
    118 #endif
    119 #define REALREAD _sys_read
    120 #define REALGETDENTS __getdents30
    121 
    122 int REALSELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *);
    123 int REALPOLLTS(struct pollfd *, nfds_t,
    124 	       const struct timespec *, const sigset_t *);
    125 int REALKEVENT(int, const struct kevent *, size_t, struct kevent *, size_t,
    126 	       const struct timespec *);
    127 ssize_t REALREAD(int, void *, size_t);
    128 int REALSTAT(const char *, struct stat *);
    129 int REALLSTAT(const char *, struct stat *);
    130 int REALFSTAT(int, struct stat *);
    131 int REALGETDENTS(int, char *, size_t);
    132 int REALUTIMES(const char *, const struct timeval [2]);
    133 int REALLUTIMES(const char *, const struct timeval [2]);
    134 int REALFUTIMES(int, const struct timeval [2]);
    135 
    136 #define S(a) __STRING(a)
    137 struct sysnames {
    138 	enum dualcall scm_callnum;
    139 	const char *scm_hostname;
    140 	const char *scm_rumpname;
    141 } syscnames[] = {
    142 	{ DUALCALL_SOCKET,	"__socket30",	RSYS_NAME(SOCKET)	},
    143 	{ DUALCALL_ACCEPT,	"accept",	RSYS_NAME(ACCEPT)	},
    144 	{ DUALCALL_BIND,	"bind",		RSYS_NAME(BIND)		},
    145 	{ DUALCALL_CONNECT,	"connect",	RSYS_NAME(CONNECT)	},
    146 	{ DUALCALL_GETPEERNAME,	"getpeername",	RSYS_NAME(GETPEERNAME)	},
    147 	{ DUALCALL_GETSOCKNAME,	"getsockname",	RSYS_NAME(GETSOCKNAME)	},
    148 	{ DUALCALL_LISTEN,	"listen",	RSYS_NAME(LISTEN)	},
    149 	{ DUALCALL_RECVFROM,	"recvfrom",	RSYS_NAME(RECVFROM)	},
    150 	{ DUALCALL_RECVMSG,	"recvmsg",	RSYS_NAME(RECVMSG)	},
    151 	{ DUALCALL_SENDTO,	"sendto",	RSYS_NAME(SENDTO)	},
    152 	{ DUALCALL_SENDMSG,	"sendmsg",	RSYS_NAME(SENDMSG)	},
    153 	{ DUALCALL_GETSOCKOPT,	"getsockopt",	RSYS_NAME(GETSOCKOPT)	},
    154 	{ DUALCALL_SETSOCKOPT,	"setsockopt",	RSYS_NAME(SETSOCKOPT)	},
    155 	{ DUALCALL_SHUTDOWN,	"shutdown",	RSYS_NAME(SHUTDOWN)	},
    156 	{ DUALCALL_READ,	S(REALREAD),	RSYS_NAME(READ)		},
    157 	{ DUALCALL_READV,	"readv",	RSYS_NAME(READV)	},
    158 	{ DUALCALL_WRITE,	"write",	RSYS_NAME(WRITE)	},
    159 	{ DUALCALL_WRITEV,	"writev",	RSYS_NAME(WRITEV)	},
    160 	{ DUALCALL_IOCTL,	"ioctl",	RSYS_NAME(IOCTL)	},
    161 	{ DUALCALL_FCNTL,	"fcntl",	RSYS_NAME(FCNTL)	},
    162 	{ DUALCALL_DUP2,	"dup2",		RSYS_NAME(DUP2)		},
    163 	{ DUALCALL_CLOSE,	"close",	RSYS_NAME(CLOSE)	},
    164 	{ DUALCALL_POLLTS,	S(REALPOLLTS),	RSYS_NAME(POLLTS)	},
    165 	{ DUALCALL_KEVENT,	S(REALKEVENT),	RSYS_NAME(KEVENT)	},
    166 	{ DUALCALL_STAT,	S(REALSTAT),	RSYS_NAME(STAT)		},
    167 	{ DUALCALL_LSTAT,	S(REALLSTAT),	RSYS_NAME(LSTAT)	},
    168 	{ DUALCALL_FSTAT,	S(REALFSTAT),	RSYS_NAME(FSTAT)	},
    169 	{ DUALCALL_CHOWN,	"chown",	RSYS_NAME(CHOWN)	},
    170 	{ DUALCALL_LCHOWN,	"lchown",	RSYS_NAME(LCHOWN)	},
    171 	{ DUALCALL_FCHOWN,	"fchown",	RSYS_NAME(FCHOWN)	},
    172 	{ DUALCALL_CHMOD,	"chmod",	RSYS_NAME(CHMOD)	},
    173 	{ DUALCALL_LCHMOD,	"lchmod",	RSYS_NAME(LCHMOD)	},
    174 	{ DUALCALL_FCHMOD,	"fchmod",	RSYS_NAME(FCHMOD)	},
    175 	{ DUALCALL_UTIMES,	S(REALUTIMES),	RSYS_NAME(UTIMES)	},
    176 	{ DUALCALL_LUTIMES,	S(REALLUTIMES),	RSYS_NAME(LUTIMES)	},
    177 	{ DUALCALL_FUTIMES,	S(REALFUTIMES),	RSYS_NAME(FUTIMES)	},
    178 	{ DUALCALL_OPEN,	"open",		RSYS_NAME(OPEN)		},
    179 	{ DUALCALL_STATVFS1,	"statvfs1",	RSYS_NAME(STATVFS1)	},
    180 	{ DUALCALL_FSTATVFS1,	"fstatvfs1",	RSYS_NAME(FSTATVFS1)	},
    181 	{ DUALCALL_CHDIR,	"chdir",	RSYS_NAME(CHDIR)	},
    182 	{ DUALCALL_FCHDIR,	"fchdir",	RSYS_NAME(FCHDIR)	},
    183 	{ DUALCALL_LSEEK,	"lseek",	RSYS_NAME(LSEEK)	},
    184 	{ DUALCALL_GETDENTS,	"__getdents30",	RSYS_NAME(GETDENTS)	},
    185 	{ DUALCALL_UNLINK,	"unlink",	RSYS_NAME(UNLINK)	},
    186 	{ DUALCALL_SYMLINK,	"symlink",	RSYS_NAME(SYMLINK)	},
    187 	{ DUALCALL_READLINK,	"readlink",	RSYS_NAME(READLINK)	},
    188 	{ DUALCALL_RENAME,	"rename",	RSYS_NAME(RENAME)	},
    189 	{ DUALCALL_MKDIR,	"mkdir",	RSYS_NAME(MKDIR)	},
    190 	{ DUALCALL_RMDIR,	"rmdir",	RSYS_NAME(RMDIR)	},
    191 	{ DUALCALL_TRUNCATE,	"truncate",	RSYS_NAME(TRUNCATE)	},
    192 	{ DUALCALL_FTRUNCATE,	"ftruncate",	RSYS_NAME(FTRUNCATE)	},
    193 	{ DUALCALL_FSYNC,	"fsync",	RSYS_NAME(FSYNC)	},
    194 	{ DUALCALL_FSYNC_RANGE,	"fsync_range",	RSYS_NAME(FSYNC_RANGE)	},
    195 };
    196 #undef S
    197 
    198 struct bothsys {
    199 	void *bs_host;
    200 	void *bs_rump;
    201 } syscalls[DUALCALL__NUM];
    202 #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which
    203 
    204 pid_t	(*host_fork)(void);
    205 int	(*host_daemon)(int, int);
    206 int	(*host_execve)(const char *, char *const[], char *const[]);
    207 
    208 /* ok, we need *two* bits per dup2'd fd to track fd+HIJACKOFF aliases */
    209 static uint32_t dup2mask;
    210 #define ISDUP2D(fd) (((fd) < 16) && (1<<(fd) & dup2mask))
    211 #define SETDUP2(fd) \
    212     do { if ((fd) < 16) dup2mask |= (1<<(fd)); } while (/*CONSTCOND*/0)
    213 #define CLRDUP2(fd) \
    214     do { if ((fd) < 16) dup2mask &= ~(1<<(fd)); } while (/*CONSTCOND*/0)
    215 #define ISDUP2ALIAS(fd) (((fd) < 16) && (1<<((fd)+16) & dup2mask))
    216 #define SETDUP2ALIAS(fd) \
    217     do { if ((fd) < 16) dup2mask |= (1<<((fd)+16)); } while (/*CONSTCOND*/0)
    218 #define CLRDUP2ALIAS(fd) \
    219     do { if ((fd) < 16) dup2mask &= ~(1<<((fd)+16)); } while (/*CONSTCOND*/0)
    220 
    221 //#define DEBUGJACK
    222 #ifdef DEBUGJACK
    223 #define DPRINTF(x) mydprintf x
    224 static void
    225 mydprintf(const char *fmt, ...)
    226 {
    227 	va_list ap;
    228 
    229 	if (ISDUP2D(STDERR_FILENO))
    230 		return;
    231 
    232 	va_start(ap, fmt);
    233 	vfprintf(stderr, fmt, ap);
    234 	va_end(ap);
    235 }
    236 
    237 #else
    238 #define DPRINTF(x)
    239 #endif
    240 
    241 #define FDCALL(type, name, rcname, args, proto, vars)			\
    242 type name args								\
    243 {									\
    244 	type (*fun) proto;						\
    245 									\
    246 	DPRINTF(("%s -> %d\n", __STRING(name), fd));			\
    247 	if (fd_isrump(fd)) {						\
    248 		fun = syscalls[rcname].bs_rump;				\
    249 		fd = fd_host2rump(fd);					\
    250 	} else {							\
    251 		fun = syscalls[rcname].bs_host;				\
    252 	}								\
    253 									\
    254 	return fun vars;						\
    255 }
    256 
    257 #define PATHCALL(type, name, rcname, args, proto, vars)			\
    258 type name args								\
    259 {									\
    260 	type (*fun) proto;						\
    261 									\
    262 	DPRINTF(("%s -> %s\n", __STRING(name), path));			\
    263 	if (path_isrump(path)) {					\
    264 		fun = syscalls[rcname].bs_rump;				\
    265 		path = path_host2rump(path);				\
    266 	} else {							\
    267 		fun = syscalls[rcname].bs_host;				\
    268 	}								\
    269 									\
    270 	return fun vars;						\
    271 }
    272 
    273 /*
    274  * This is called from librumpclient in case of LD_PRELOAD.
    275  * It ensures correct RTLD_NEXT.
    276  *
    277  * ... except, it's apparently extremely difficult to force
    278  * at least gcc to generate an actual stack frame here.  So
    279  * sprinkle some volatile foobar and baz to throw the optimizer
    280  * off the scent and generate a variable assignment with the
    281  * return value.  The posterboy for this meltdown is amd64
    282  * with -O2.  At least with gcc 4.1.3 i386 works regardless of
    283  * optimization.
    284  */
    285 volatile int rumphijack_unrope; /* there, unhang yourself */
    286 static void *
    287 hijackdlsym(void *handle, const char *symbol)
    288 {
    289 	void *rv;
    290 
    291 	rv = dlsym(handle, symbol);
    292 	rumphijack_unrope = *(volatile int *)rv;
    293 
    294 	return (void *)rv;
    295 }
    296 
    297 static int pwdinrump = 0;
    298 
    299 /* low calorie sockets? */
    300 static bool hostlocalsockets = true;
    301 
    302 static void __attribute__((constructor))
    303 rcinit(void)
    304 {
    305 	char buf[64];
    306 	extern void *(*rumpclient_dlsym)(void *, const char *);
    307 	unsigned i, j;
    308 
    309 	rumpclient_dlsym = hijackdlsym;
    310 	host_fork = dlsym(RTLD_NEXT, "fork");
    311 	host_daemon = dlsym(RTLD_NEXT, "daemon");
    312 	host_execve = dlsym(RTLD_NEXT, "execve");
    313 
    314 	/*
    315 	 * In theory cannot print anything during lookups because
    316 	 * we might not have the call vector set up.  so, the errx()
    317 	 * is a bit of a strech, but it might work.
    318 	 */
    319 
    320 	for (i = 0; i < DUALCALL__NUM; i++) {
    321 		/* build runtime O(1) access */
    322 		for (j = 0; j < __arraycount(syscnames); j++) {
    323 			if (syscnames[j].scm_callnum == i)
    324 				break;
    325 		}
    326 
    327 		if (j == __arraycount(syscnames))
    328 			errx(1, "rumphijack error: syscall pos %d missing", i);
    329 
    330 		syscalls[i].bs_host = dlsym(RTLD_NEXT,
    331 		    syscnames[j].scm_hostname);
    332 		if (syscalls[i].bs_host == NULL)
    333 			errx(1, "hostcall %s not found missing",
    334 			    syscnames[j].scm_hostname);
    335 
    336 		syscalls[i].bs_rump = dlsym(RTLD_NEXT,
    337 		    syscnames[j].scm_rumpname);
    338 		if (syscalls[i].bs_rump == NULL)
    339 			errx(1, "rumpcall %s not found missing",
    340 			    syscnames[j].scm_rumpname);
    341 	}
    342 
    343 	if (rumpclient_init() == -1)
    344 		err(1, "rumpclient init");
    345 
    346 	/* set client persistence level */
    347 	if (getenv_r("RUMPHIJACK_RETRYCONNECT", buf, sizeof(buf)) != -1) {
    348 		if (strcmp(buf, "die") == 0)
    349 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_DIE);
    350 		else if (strcmp(buf, "inftime") == 0)
    351 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME);
    352 		else if (strcmp(buf, "once") == 0)
    353 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_ONCE);
    354 		else {
    355 			time_t timeout;
    356 			char *ep;
    357 
    358 			timeout = (time_t)strtoll(buf, &ep, 10);
    359 			if (timeout <= 0 || ep != buf + strlen(buf))
    360 				errx(1, "RUMPHIJACK_RETRYCONNECT must be "
    361 				    "keyword or integer, got: %s", buf);
    362 
    363 			rumpclient_setconnretry(timeout);
    364 		}
    365 	}
    366 
    367 	if (getenv_r("RUMPHIJACK__DUP2MASK", buf, sizeof(buf)) == 0) {
    368 		dup2mask = strtoul(buf, NULL, 10);
    369 		unsetenv("RUMPHIJACK__DUP2MASK");
    370 	}
    371 	if (getenv_r("RUMPHIJACK__PWDINRUMP", buf, sizeof(buf)) == 0) {
    372 		pwdinrump = strtoul(buf, NULL, 10);
    373 		unsetenv("RUMPHIJACK__PWDINRUMP");
    374 	}
    375 }
    376 
    377 /* XXX: need runtime selection.  low for now due to FD_SETSIZE */
    378 #define HIJACK_FDOFF 128
    379 static int
    380 fd_rump2host(int fd)
    381 {
    382 
    383 	if (fd == -1)
    384 		return fd;
    385 
    386 	if (!ISDUP2D(fd))
    387 		fd += HIJACK_FDOFF;
    388 
    389 	return fd;
    390 }
    391 
    392 static int
    393 fd_host2rump(int fd)
    394 {
    395 
    396 	if (!ISDUP2D(fd))
    397 		fd -= HIJACK_FDOFF;
    398 	return fd;
    399 }
    400 
    401 static bool
    402 fd_isrump(int fd)
    403 {
    404 
    405 	return ISDUP2D(fd) || fd >= HIJACK_FDOFF;
    406 }
    407 
    408 #define assertfd(_fd_) assert(ISDUP2D(_fd_) || (_fd_) >= HIJACK_FDOFF)
    409 
    410 #define RUMPPREFIX "/rump"
    411 static int
    412 path_isrump(const char *path)
    413 {
    414 
    415 	if (*path == '/') {
    416 		if (strncmp(path, RUMPPREFIX, sizeof(RUMPPREFIX)-1) == 0)
    417 			return 1;
    418 		return 0;
    419 	} else {
    420 		return pwdinrump;
    421 	}
    422 }
    423 
    424 static const char *rootpath = "/";
    425 static const char *
    426 path_host2rump(const char *path)
    427 {
    428 	const char *rv;
    429 
    430 	if (*path == '/') {
    431 		rv = path + (sizeof(RUMPPREFIX)-1);
    432 		if (*rv == '\0')
    433 			rv = rootpath;
    434 	} else {
    435 		rv = path;
    436 	}
    437 
    438 	return rv;
    439 }
    440 
    441 static int
    442 dodup(int oldd, int minfd)
    443 {
    444 	int (*op_fcntl)(int, int, ...);
    445 	int newd;
    446 	int isrump;
    447 
    448 	DPRINTF(("dup -> %d (minfd %d)\n", oldd, minfd));
    449 	if (fd_isrump(oldd)) {
    450 		op_fcntl = GETSYSCALL(rump, FCNTL);
    451 		oldd = fd_host2rump(oldd);
    452 		isrump = 1;
    453 	} else {
    454 		op_fcntl = GETSYSCALL(host, FCNTL);
    455 		isrump = 0;
    456 	}
    457 
    458 	newd = op_fcntl(oldd, F_DUPFD, minfd);
    459 
    460 	if (isrump)
    461 		newd = fd_rump2host(newd);
    462 	DPRINTF(("dup <- %d\n", newd));
    463 
    464 	return newd;
    465 }
    466 
    467 /*
    468  * dup a host file descriptor so that it doesn't collide with the dup2mask
    469  */
    470 static int
    471 fd_dupgood(int fd)
    472 {
    473 	int (*op_fcntl)(int, int, ...) = GETSYSCALL(host, FCNTL);
    474 	int (*op_close)(int) = GETSYSCALL(host, CLOSE);
    475 	int ofd, i;
    476 
    477 	for (i = 1; ISDUP2D(fd); i++) {
    478 		ofd = fd;
    479 		fd = op_fcntl(ofd, F_DUPFD, i);
    480 		op_close(ofd);
    481 	}
    482 
    483 	return fd;
    484 }
    485 
    486 int
    487 open(const char *path, int flags, ...)
    488 {
    489 	int (*op_open)(const char *, int, ...);
    490 	bool isrump;
    491 	va_list ap;
    492 	int fd;
    493 
    494 	if (path_isrump(path)) {
    495 		path = path_host2rump(path);
    496 		op_open = GETSYSCALL(rump, OPEN);
    497 		isrump = true;
    498 	} else {
    499 		op_open = GETSYSCALL(host, OPEN);
    500 		isrump = false;
    501 	}
    502 
    503 	va_start(ap, flags);
    504 	fd = op_open(path, flags, va_arg(ap, mode_t));
    505 	va_end(ap);
    506 
    507 	if (isrump)
    508 		fd = fd_rump2host(fd);
    509 	else
    510 		fd = fd_dupgood(fd);
    511 	return fd;
    512 }
    513 
    514 int
    515 chdir(const char *path)
    516 {
    517 	int (*op_chdir)(const char *);
    518 	bool isrump;
    519 	int rv;
    520 
    521 	if (path_isrump(path)) {
    522 		op_chdir = GETSYSCALL(rump, CHDIR);
    523 		isrump = true;
    524 		path = path_host2rump(path);
    525 	} else {
    526 		op_chdir = GETSYSCALL(host, CHDIR);
    527 		isrump = false;
    528 	}
    529 
    530 	rv = op_chdir(path);
    531 	if (rv == 0) {
    532 		if (isrump)
    533 			pwdinrump = true;
    534 		else
    535 			pwdinrump = false;
    536 	}
    537 
    538 	return rv;
    539 }
    540 
    541 int
    542 fchdir(int fd)
    543 {
    544 	int (*op_fchdir)(int);
    545 	bool isrump;
    546 	int rv;
    547 
    548 	if (fd_isrump(fd)) {
    549 		op_fchdir = GETSYSCALL(rump, FCHDIR);
    550 		isrump = true;
    551 		fd = fd_host2rump(fd);
    552 	} else {
    553 		op_fchdir = GETSYSCALL(host, FCHDIR);
    554 		isrump = false;
    555 	}
    556 
    557 	rv = op_fchdir(fd);
    558 	if (rv == 0) {
    559 		if (isrump)
    560 			pwdinrump = true;
    561 		else
    562 			pwdinrump = false;
    563 	}
    564 
    565 	return rv;
    566 }
    567 
    568 int __socket30(int, int, int);
    569 int
    570 __socket30(int domain, int type, int protocol)
    571 {
    572 	int (*op_socket)(int, int, int);
    573 	int fd;
    574 	bool dohost;
    575 
    576 	dohost = hostlocalsockets && (domain == AF_LOCAL);
    577 
    578 	if (dohost)
    579 		op_socket = GETSYSCALL(host, SOCKET);
    580 	else
    581 		op_socket = GETSYSCALL(rump, SOCKET);
    582 	fd = op_socket(domain, type, protocol);
    583 
    584 	if (!dohost)
    585 		fd = fd_rump2host(fd);
    586 	else
    587 		fd = fd_dupgood(fd);
    588 	DPRINTF(("socket <- %d\n", fd));
    589 
    590 	return fd;
    591 }
    592 
    593 int
    594 accept(int s, struct sockaddr *addr, socklen_t *addrlen)
    595 {
    596 	int (*op_accept)(int, struct sockaddr *, socklen_t *);
    597 	int fd;
    598 	bool isrump;
    599 
    600 	isrump = fd_isrump(s);
    601 
    602 	DPRINTF(("accept -> %d", s));
    603 	if (isrump) {
    604 		op_accept = GETSYSCALL(rump, ACCEPT);
    605 		s = fd_host2rump(s);
    606 	} else {
    607 		op_accept = GETSYSCALL(host, ACCEPT);
    608 	}
    609 	fd = op_accept(s, addr, addrlen);
    610 	if (fd != -1 && isrump)
    611 		fd = fd_rump2host(fd);
    612 	else
    613 		fd = fd_dupgood(fd);
    614 
    615 	DPRINTF((" <- %d\n", fd));
    616 
    617 	return fd;
    618 }
    619 
    620 /*
    621  * ioctl and fcntl are varargs calls and need special treatment
    622  */
    623 int
    624 ioctl(int fd, unsigned long cmd, ...)
    625 {
    626 	int (*op_ioctl)(int, unsigned long cmd, ...);
    627 	va_list ap;
    628 	int rv;
    629 
    630 	DPRINTF(("ioctl -> %d\n", fd));
    631 	if (fd_isrump(fd)) {
    632 		fd = fd_host2rump(fd);
    633 		op_ioctl = GETSYSCALL(rump, IOCTL);
    634 	} else {
    635 		op_ioctl = GETSYSCALL(host, IOCTL);
    636 	}
    637 
    638 	va_start(ap, cmd);
    639 	rv = op_ioctl(fd, cmd, va_arg(ap, void *));
    640 	va_end(ap);
    641 	return rv;
    642 }
    643 
    644 #include <syslog.h>
    645 int
    646 fcntl(int fd, int cmd, ...)
    647 {
    648 	int (*op_fcntl)(int, int, ...);
    649 	va_list ap;
    650 	int rv, minfd, i;
    651 
    652 	DPRINTF(("fcntl -> %d (cmd %d)\n", fd, cmd));
    653 
    654 	switch (cmd) {
    655 	case F_DUPFD:
    656 		va_start(ap, cmd);
    657 		minfd = va_arg(ap, int);
    658 		va_end(ap);
    659 		return dodup(fd, minfd);
    660 
    661 	case F_CLOSEM:
    662 		/*
    663 		 * So, if fd < HIJACKOFF, we want to do a host closem.
    664 		 */
    665 
    666 		if (fd < HIJACK_FDOFF) {
    667 			int closemfd = fd;
    668 
    669 			if (rumpclient__closenotify(&closemfd,
    670 			    RUMPCLIENT_CLOSE_FCLOSEM) == -1)
    671 				return -1;
    672 			op_fcntl = GETSYSCALL(host, FCNTL);
    673 			rv = op_fcntl(closemfd, cmd);
    674 			if (rv)
    675 				return rv;
    676 		}
    677 
    678 		/*
    679 		 * Additionally, we want to do a rump closem, but only
    680 		 * for the file descriptors not within the dup2mask.
    681 		 */
    682 
    683 		/* why don't we offer fls()? */
    684 		for (i = 15; i >= 0; i--) {
    685 			if (ISDUP2D(i))
    686 				break;
    687 		}
    688 
    689 		if (fd >= HIJACK_FDOFF)
    690 			fd -= HIJACK_FDOFF;
    691 		else
    692 			fd = 0;
    693 		fd = MAX(i+1, fd);
    694 
    695 		/* hmm, maybe we should close rump fd's not within dup2mask? */
    696 
    697 		return rump_sys_fcntl(fd, F_CLOSEM);
    698 
    699 	case F_MAXFD:
    700 		/*
    701 		 * For maxfd, if there's a rump kernel fd, return
    702 		 * it hostified.  Otherwise, return host's MAXFD
    703 		 * return value.
    704 		 */
    705 		if ((rv = rump_sys_fcntl(fd, F_MAXFD)) != -1) {
    706 			/*
    707 			 * This might go a little wrong in case
    708 			 * of dup2 to [012], but I'm not sure if
    709 			 * there's a justification for tracking
    710 			 * that info.  Consider e.g.
    711 			 * dup2(rumpfd, 2) followed by rump_sys_open()
    712 			 * returning 1.  We should return 1+HIJACKOFF,
    713 			 * not 2+HIJACKOFF.  However, if [01] is not
    714 			 * open, the correct return value is 2.
    715 			 */
    716 			return fd_rump2host(fd);
    717 		} else {
    718 			op_fcntl = GETSYSCALL(host, FCNTL);
    719 			return op_fcntl(fd, F_MAXFD);
    720 		}
    721 		/*NOTREACHED*/
    722 
    723 	default:
    724 		if (fd_isrump(fd)) {
    725 			fd = fd_host2rump(fd);
    726 			op_fcntl = GETSYSCALL(rump, FCNTL);
    727 		} else {
    728 			op_fcntl = GETSYSCALL(host, FCNTL);
    729 		}
    730 
    731 		va_start(ap, cmd);
    732 		rv = op_fcntl(fd, cmd, va_arg(ap, void *));
    733 		va_end(ap);
    734 		return rv;
    735 	}
    736 	/*NOTREACHED*/
    737 }
    738 
    739 int
    740 close(int fd)
    741 {
    742 	int (*op_close)(int);
    743 	int rv;
    744 
    745 	DPRINTF(("close -> %d\n", fd));
    746 	if (fd_isrump(fd)) {
    747 		int undup2 = 0;
    748 
    749 		fd = fd_host2rump(fd);
    750 		if (ISDUP2ALIAS(fd)) {
    751 			_DIAGASSERT(ISDUP2D(fd));
    752 			CLRDUP2ALIAS(fd);
    753 			return 0;
    754 		}
    755 
    756 		if (ISDUP2D(fd))
    757 			undup2 = 1;
    758 		op_close = GETSYSCALL(rump, CLOSE);
    759 		rv = op_close(fd);
    760 		if (rv == 0 && undup2)
    761 			CLRDUP2(fd);
    762 	} else {
    763 		if (rumpclient__closenotify(&fd, RUMPCLIENT_CLOSE_CLOSE) == -1)
    764 			return -1;
    765 		op_close = GETSYSCALL(host, CLOSE);
    766 		rv = op_close(fd);
    767 	}
    768 
    769 	return rv;
    770 }
    771 
    772 /*
    773  * write cannot issue a standard debug printf due to recursion
    774  */
    775 ssize_t
    776 write(int fd, const void *buf, size_t blen)
    777 {
    778 	ssize_t (*op_write)(int, const void *, size_t);
    779 
    780 	if (fd_isrump(fd)) {
    781 		fd = fd_host2rump(fd);
    782 		op_write = GETSYSCALL(rump, WRITE);
    783 	} else {
    784 		op_write = GETSYSCALL(host, WRITE);
    785 	}
    786 
    787 	return op_write(fd, buf, blen);
    788 }
    789 
    790 /*
    791  * dup2 is special.  we allow dup2 of a rump kernel fd to 0-2 since
    792  * many programs do that.  dup2 of a rump kernel fd to another value
    793  * not >= fdoff is an error.
    794  *
    795  * Note: cannot rump2host newd, because it is often hardcoded.
    796  */
    797 int
    798 dup2(int oldd, int newd)
    799 {
    800 	int (*host_dup2)(int, int);
    801 	int rv;
    802 
    803 	DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
    804 
    805 	if (fd_isrump(oldd)) {
    806 		if (!(newd >= 0 && newd <= 2))
    807 			return EBADF;
    808 		oldd = fd_host2rump(oldd);
    809 		if (oldd == newd) {
    810 			SETDUP2(newd);
    811 			SETDUP2ALIAS(newd);
    812 			return newd;
    813 		}
    814 		rv = rump_sys_dup2(oldd, newd);
    815 		if (rv != -1)
    816 			SETDUP2(newd);
    817 	} else {
    818 		host_dup2 = syscalls[DUALCALL_DUP2].bs_host;
    819 		if (rumpclient__closenotify(&newd, RUMPCLIENT_CLOSE_DUP2) == -1)
    820 			return -1;
    821 		rv = host_dup2(oldd, newd);
    822 	}
    823 
    824 	return rv;
    825 }
    826 
    827 int
    828 dup(int oldd)
    829 {
    830 
    831 	return dodup(oldd, 0);
    832 }
    833 
    834 pid_t
    835 fork()
    836 {
    837 	pid_t rv;
    838 
    839 	DPRINTF(("fork\n"));
    840 
    841 	rv = rumpclient__dofork(host_fork);
    842 
    843 	DPRINTF(("fork returns %d\n", rv));
    844 	return rv;
    845 }
    846 /* we do not have the luxury of not requiring a stackframe */
    847 __strong_alias(__vfork14,fork);
    848 
    849 int
    850 daemon(int nochdir, int noclose)
    851 {
    852 	struct rumpclient_fork *rf;
    853 
    854 	if ((rf = rumpclient_prefork()) == NULL)
    855 		return -1;
    856 
    857 	if (host_daemon(nochdir, noclose) == -1)
    858 		return -1;
    859 
    860 	if (rumpclient_fork_init(rf) == -1)
    861 		return -1;
    862 
    863 	return 0;
    864 }
    865 
    866 int
    867 execve(const char *path, char *const argv[], char *const envp[])
    868 {
    869 	char buf[128];
    870 	char *dup2str;
    871 	char *pwdinrumpstr;
    872 	char **newenv;
    873 	size_t nelem;
    874 	int rv, sverrno;
    875 	int bonus = 1, i = 0;
    876 
    877 	if (dup2mask) {
    878 		snprintf(buf, sizeof(buf), "RUMPHIJACK__DUP2MASK=%u", dup2mask);
    879 		dup2str = malloc(strlen(buf)+1);
    880 		if (dup2str == NULL)
    881 			return ENOMEM;
    882 		strcpy(dup2str, buf);
    883 		bonus++;
    884 	} else {
    885 		dup2str = NULL;
    886 	}
    887 
    888 	if (pwdinrump) {
    889 		snprintf(buf, sizeof(buf), "RUMPHIJACK__PWDINRUMP=%u",
    890 		    pwdinrump);
    891 		pwdinrumpstr = malloc(strlen(buf)+1);
    892 		if (pwdinrumpstr == NULL) {
    893 			free(dup2str);
    894 			return ENOMEM;
    895 		}
    896 		strcpy(pwdinrumpstr, buf);
    897 		bonus++;
    898 	} else {
    899 		pwdinrumpstr = NULL;
    900 	}
    901 
    902 	for (nelem = 0; envp && envp[nelem]; nelem++)
    903 		continue;
    904 	newenv = malloc(sizeof(*newenv) * nelem+bonus);
    905 	if (newenv == NULL) {
    906 		free(dup2str);
    907 		free(pwdinrumpstr);
    908 		return ENOMEM;
    909 	}
    910 	memcpy(newenv, envp, nelem*sizeof(*newenv));
    911 	if (dup2str) {
    912 		newenv[nelem+i] = dup2str;
    913 		i++;
    914 	}
    915 	if (pwdinrumpstr) {
    916 		newenv[nelem+i] = pwdinrumpstr;
    917 		i++;
    918 	}
    919 	newenv[nelem+i] = NULL;
    920 	_DIAGASSERT(i < bonus);
    921 
    922 	rv = rumpclient_exec(path, argv, newenv);
    923 
    924 	_DIAGASSERT(rv != 0);
    925 	sverrno = errno;
    926 	free(newenv);
    927 	free(dup2str);
    928 	errno = sverrno;
    929 	return rv;
    930 }
    931 
    932 /*
    933  * select is done by calling poll.
    934  */
    935 int
    936 REALSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
    937 	struct timeval *timeout)
    938 {
    939 	struct pollfd *pfds;
    940 	struct timespec ts, *tsp = NULL;
    941 	nfds_t realnfds;
    942 	int i, j;
    943 	int rv, incr;
    944 
    945 	DPRINTF(("select\n"));
    946 
    947 	/*
    948 	 * Well, first we must scan the fds to figure out how many
    949 	 * fds there really are.  This is because up to and including
    950 	 * nb5 poll() silently refuses nfds > process_maxopen_fds.
    951 	 * Seems to be fixed in current, thank the maker.
    952 	 * god damn cluster...bomb.
    953 	 */
    954 
    955 	for (i = 0, realnfds = 0; i < nfds; i++) {
    956 		if (readfds && FD_ISSET(i, readfds)) {
    957 			realnfds++;
    958 			continue;
    959 		}
    960 		if (writefds && FD_ISSET(i, writefds)) {
    961 			realnfds++;
    962 			continue;
    963 		}
    964 		if (exceptfds && FD_ISSET(i, exceptfds)) {
    965 			realnfds++;
    966 			continue;
    967 		}
    968 	}
    969 
    970 	if (realnfds) {
    971 		pfds = calloc(realnfds, sizeof(*pfds));
    972 		if (!pfds)
    973 			return -1;
    974 	} else {
    975 		pfds = NULL;
    976 	}
    977 
    978 	for (i = 0, j = 0; i < nfds; i++) {
    979 		incr = 0;
    980 		if (readfds && FD_ISSET(i, readfds)) {
    981 			pfds[j].fd = i;
    982 			pfds[j].events |= POLLIN;
    983 			incr=1;
    984 		}
    985 		if (writefds && FD_ISSET(i, writefds)) {
    986 			pfds[j].fd = i;
    987 			pfds[j].events |= POLLOUT;
    988 			incr=1;
    989 		}
    990 		if (exceptfds && FD_ISSET(i, exceptfds)) {
    991 			pfds[j].fd = i;
    992 			pfds[j].events |= POLLHUP|POLLERR;
    993 			incr=1;
    994 		}
    995 		if (incr)
    996 			j++;
    997 	}
    998 	assert(j == (int)realnfds);
    999 
   1000 	if (timeout) {
   1001 		TIMEVAL_TO_TIMESPEC(timeout, &ts);
   1002 		tsp = &ts;
   1003 	}
   1004 	rv = REALPOLLTS(pfds, realnfds, tsp, NULL);
   1005 	/*
   1006 	 * "If select() returns with an error the descriptor sets
   1007 	 * will be unmodified"
   1008 	 */
   1009 	if (rv < 0)
   1010 		goto out;
   1011 
   1012 	/*
   1013 	 * zero out results (can't use FD_ZERO for the
   1014 	 * obvious select-me-not reason).  whee.
   1015 	 *
   1016 	 * We do this here since some software ignores the return
   1017 	 * value of select, and hence if the timeout expires, it may
   1018 	 * assume all input descriptors have activity.
   1019 	 */
   1020 	for (i = 0; i < nfds; i++) {
   1021 		if (readfds)
   1022 			FD_CLR(i, readfds);
   1023 		if (writefds)
   1024 			FD_CLR(i, writefds);
   1025 		if (exceptfds)
   1026 			FD_CLR(i, exceptfds);
   1027 	}
   1028 	if (rv == 0)
   1029 		goto out;
   1030 
   1031 	/*
   1032 	 * We have >0 fds with activity.  Harvest the results.
   1033 	 */
   1034 	for (i = 0; i < (int)realnfds; i++) {
   1035 		if (readfds) {
   1036 			if (pfds[i].revents & POLLIN) {
   1037 				FD_SET(pfds[i].fd, readfds);
   1038 			}
   1039 		}
   1040 		if (writefds) {
   1041 			if (pfds[i].revents & POLLOUT) {
   1042 				FD_SET(pfds[i].fd, writefds);
   1043 			}
   1044 		}
   1045 		if (exceptfds) {
   1046 			if (pfds[i].revents & (POLLHUP|POLLERR)) {
   1047 				FD_SET(pfds[i].fd, exceptfds);
   1048 			}
   1049 		}
   1050 	}
   1051 
   1052  out:
   1053 	free(pfds);
   1054 	return rv;
   1055 }
   1056 
   1057 static void
   1058 checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
   1059 {
   1060 	nfds_t i;
   1061 
   1062 	for (i = 0; i < nfds; i++) {
   1063 		if (fds[i].fd == -1)
   1064 			continue;
   1065 
   1066 		if (fd_isrump(fds[i].fd))
   1067 			(*rumpcall)++;
   1068 		else
   1069 			(*hostcall)++;
   1070 	}
   1071 }
   1072 
   1073 static void
   1074 adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
   1075 {
   1076 	nfds_t i;
   1077 
   1078 	for (i = 0; i < nfds; i++) {
   1079 		fds[i].fd = fdadj(fds[i].fd);
   1080 	}
   1081 }
   1082 
   1083 /*
   1084  * poll is easy as long as the call comes in the fds only in one
   1085  * kernel.  otherwise its quite tricky...
   1086  */
   1087 struct pollarg {
   1088 	struct pollfd *pfds;
   1089 	nfds_t nfds;
   1090 	const struct timespec *ts;
   1091 	const sigset_t *sigmask;
   1092 	int pipefd;
   1093 	int errnum;
   1094 };
   1095 
   1096 static void *
   1097 hostpoll(void *arg)
   1098 {
   1099 	int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
   1100 			 const sigset_t *);
   1101 	struct pollarg *parg = arg;
   1102 	intptr_t rv;
   1103 
   1104 	op_pollts = GETSYSCALL(host, POLLTS);
   1105 	rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
   1106 	if (rv == -1)
   1107 		parg->errnum = errno;
   1108 	rump_sys_write(parg->pipefd, &rv, sizeof(rv));
   1109 
   1110 	return (void *)(intptr_t)rv;
   1111 }
   1112 
   1113 int
   1114 REALPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
   1115 	const sigset_t *sigmask)
   1116 {
   1117 	int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
   1118 			 const sigset_t *);
   1119 	int (*host_close)(int);
   1120 	int hostcall = 0, rumpcall = 0;
   1121 	pthread_t pt;
   1122 	nfds_t i;
   1123 	int rv;
   1124 
   1125 	DPRINTF(("poll\n"));
   1126 	checkpoll(fds, nfds, &hostcall, &rumpcall);
   1127 
   1128 	if (hostcall && rumpcall) {
   1129 		struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
   1130 		int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
   1131 		struct pollarg parg;
   1132 		uintptr_t lrv;
   1133 		int sverrno = 0, trv;
   1134 
   1135 		/*
   1136 		 * ok, this is where it gets tricky.  We must support
   1137 		 * this since it's a very common operation in certain
   1138 		 * types of software (telnet, netcat, etc).  We allocate
   1139 		 * two vectors and run two poll commands in separate
   1140 		 * threads.  Whichever returns first "wins" and the
   1141 		 * other kernel's fds won't show activity.
   1142 		 */
   1143 		rv = -1;
   1144 
   1145 		/* allocate full vector for O(n) joining after call */
   1146 		pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
   1147 		if (!pfd_host)
   1148 			goto out;
   1149 		pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
   1150 		if (!pfd_rump) {
   1151 			goto out;
   1152 		}
   1153 
   1154 		/* split vectors */
   1155 		for (i = 0; i < nfds; i++) {
   1156 			if (fds[i].fd == -1) {
   1157 				pfd_host[i].fd = -1;
   1158 				pfd_rump[i].fd = -1;
   1159 			} else if (fd_isrump(fds[i].fd)) {
   1160 				pfd_host[i].fd = -1;
   1161 				pfd_rump[i].fd = fd_host2rump(fds[i].fd);
   1162 				pfd_rump[i].events = fds[i].events;
   1163 			} else {
   1164 				pfd_rump[i].fd = -1;
   1165 				pfd_host[i].fd = fds[i].fd;
   1166 				pfd_host[i].events = fds[i].events;
   1167 			}
   1168 			pfd_rump[i].revents = pfd_host[i].revents = 0;
   1169 			fds[i].revents = 0;
   1170 		}
   1171 
   1172 		/*
   1173 		 * then, open two pipes, one for notifications
   1174 		 * to each kernel.
   1175 		 */
   1176 		if (rump_sys_pipe(rpipe) == -1)
   1177 			goto out;
   1178 		if (pipe(hpipe) == -1)
   1179 			goto out;
   1180 
   1181 		pfd_host[nfds].fd = hpipe[0];
   1182 		pfd_host[nfds].events = POLLIN;
   1183 		pfd_rump[nfds].fd = rpipe[0];
   1184 		pfd_rump[nfds].events = POLLIN;
   1185 
   1186 		/*
   1187 		 * then, create a thread to do host part and meanwhile
   1188 		 * do rump kernel part right here
   1189 		 */
   1190 
   1191 		parg.pfds = pfd_host;
   1192 		parg.nfds = nfds+1;
   1193 		parg.ts = ts;
   1194 		parg.sigmask = sigmask;
   1195 		parg.pipefd = rpipe[1];
   1196 		pthread_create(&pt, NULL, hostpoll, &parg);
   1197 
   1198 		op_pollts = GETSYSCALL(rump, POLLTS);
   1199 		lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
   1200 		sverrno = errno;
   1201 		write(hpipe[1], &rv, sizeof(rv));
   1202 		pthread_join(pt, (void *)&trv);
   1203 
   1204 		/* check who "won" and merge results */
   1205 		if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
   1206 			rv = trv;
   1207 
   1208 			for (i = 0; i < nfds; i++) {
   1209 				if (pfd_rump[i].fd != -1)
   1210 					fds[i].revents = pfd_rump[i].revents;
   1211 			}
   1212 			sverrno = parg.errnum;
   1213 		} else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
   1214 			rv = trv;
   1215 
   1216 			for (i = 0; i < nfds; i++) {
   1217 				if (pfd_host[i].fd != -1)
   1218 					fds[i].revents = pfd_host[i].revents;
   1219 			}
   1220 		} else {
   1221 			rv = 0;
   1222 		}
   1223 
   1224  out:
   1225 		host_close = GETSYSCALL(host, CLOSE);
   1226 		if (rpipe[0] != -1)
   1227 			rump_sys_close(rpipe[0]);
   1228 		if (rpipe[1] != -1)
   1229 			rump_sys_close(rpipe[1]);
   1230 		if (hpipe[0] != -1)
   1231 			host_close(hpipe[0]);
   1232 		if (hpipe[1] != -1)
   1233 			host_close(hpipe[1]);
   1234 		free(pfd_host);
   1235 		free(pfd_rump);
   1236 		errno = sverrno;
   1237 	} else {
   1238 		if (hostcall) {
   1239 			op_pollts = GETSYSCALL(host, POLLTS);
   1240 		} else {
   1241 			op_pollts = GETSYSCALL(rump, POLLTS);
   1242 			adjustpoll(fds, nfds, fd_host2rump);
   1243 		}
   1244 
   1245 		rv = op_pollts(fds, nfds, ts, sigmask);
   1246 		if (rumpcall)
   1247 			adjustpoll(fds, nfds, fd_rump2host);
   1248 	}
   1249 
   1250 	return rv;
   1251 }
   1252 
   1253 int
   1254 poll(struct pollfd *fds, nfds_t nfds, int timeout)
   1255 {
   1256 	struct timespec ts;
   1257 	struct timespec *tsp = NULL;
   1258 
   1259 	if (timeout != INFTIM) {
   1260 		ts.tv_sec = timeout / 1000;
   1261 		ts.tv_nsec = (timeout % 1000) * 1000*1000;
   1262 
   1263 		tsp = &ts;
   1264 	}
   1265 
   1266 	return REALPOLLTS(fds, nfds, tsp, NULL);
   1267 }
   1268 
   1269 int
   1270 REALKEVENT(int kq, const struct kevent *changelist, size_t nchanges,
   1271 	struct kevent *eventlist, size_t nevents,
   1272 	const struct timespec *timeout)
   1273 {
   1274 	int (*op_kevent)(int, const struct kevent *, size_t,
   1275 		struct kevent *, size_t, const struct timespec *);
   1276 	const struct kevent *ev;
   1277 	size_t i;
   1278 
   1279 	/*
   1280 	 * Check that we don't attempt to kevent rump kernel fd's.
   1281 	 * That needs similar treatment to select/poll, but is slightly
   1282 	 * trickier since we need to manage to different kq descriptors.
   1283 	 * (TODO, in case you're wondering).
   1284 	 */
   1285 	for (i = 0; i < nchanges; i++) {
   1286 		ev = &changelist[i];
   1287 		if (ev->filter == EVFILT_READ || ev->filter == EVFILT_WRITE ||
   1288 		    ev->filter == EVFILT_VNODE) {
   1289 			if (fd_isrump((int)ev->ident))
   1290 				return ENOTSUP;
   1291 		}
   1292 	}
   1293 
   1294 	op_kevent = GETSYSCALL(host, KEVENT);
   1295 	return op_kevent(kq, changelist, nchanges, eventlist, nevents, timeout);
   1296 }
   1297 
   1298 /*
   1299  * Rest are std type calls.
   1300  */
   1301 
   1302 FDCALL(int, bind, DUALCALL_BIND,					\
   1303 	(int fd, const struct sockaddr *name, socklen_t namelen),	\
   1304 	(int, const struct sockaddr *, socklen_t),			\
   1305 	(fd, name, namelen))
   1306 
   1307 FDCALL(int, connect, DUALCALL_CONNECT,					\
   1308 	(int fd, const struct sockaddr *name, socklen_t namelen),	\
   1309 	(int, const struct sockaddr *, socklen_t),			\
   1310 	(fd, name, namelen))
   1311 
   1312 FDCALL(int, getpeername, DUALCALL_GETPEERNAME,				\
   1313 	(int fd, struct sockaddr *name, socklen_t *namelen),		\
   1314 	(int, struct sockaddr *, socklen_t *),				\
   1315 	(fd, name, namelen))
   1316 
   1317 FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, 				\
   1318 	(int fd, struct sockaddr *name, socklen_t *namelen),		\
   1319 	(int, struct sockaddr *, socklen_t *),				\
   1320 	(fd, name, namelen))
   1321 
   1322 FDCALL(int, listen, DUALCALL_LISTEN,	 				\
   1323 	(int fd, int backlog),						\
   1324 	(int, int),							\
   1325 	(fd, backlog))
   1326 
   1327 FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, 				\
   1328 	(int fd, void *buf, size_t len, int flags,			\
   1329 	    struct sockaddr *from, socklen_t *fromlen),			\
   1330 	(int, void *, size_t, int, struct sockaddr *, socklen_t *),	\
   1331 	(fd, buf, len, flags, from, fromlen))
   1332 
   1333 FDCALL(ssize_t, sendto, DUALCALL_SENDTO, 				\
   1334 	(int fd, const void *buf, size_t len, int flags,		\
   1335 	    const struct sockaddr *to, socklen_t tolen),		\
   1336 	(int, const void *, size_t, int,				\
   1337 	    const struct sockaddr *, socklen_t),			\
   1338 	(fd, buf, len, flags, to, tolen))
   1339 
   1340 FDCALL(ssize_t, recvmsg, DUALCALL_RECVMSG, 				\
   1341 	(int fd, struct msghdr *msg, int flags),			\
   1342 	(int, struct msghdr *, int),					\
   1343 	(fd, msg, flags))
   1344 
   1345 FDCALL(ssize_t, sendmsg, DUALCALL_SENDMSG, 				\
   1346 	(int fd, const struct msghdr *msg, int flags),			\
   1347 	(int, const struct msghdr *, int),				\
   1348 	(fd, msg, flags))
   1349 
   1350 FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, 				\
   1351 	(int fd, int level, int optn, void *optval, socklen_t *optlen),	\
   1352 	(int, int, int, void *, socklen_t *),				\
   1353 	(fd, level, optn, optval, optlen))
   1354 
   1355 FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, 				\
   1356 	(int fd, int level, int optn,					\
   1357 	    const void *optval, socklen_t optlen),			\
   1358 	(int, int, int, const void *, socklen_t),			\
   1359 	(fd, level, optn, optval, optlen))
   1360 
   1361 FDCALL(int, shutdown, DUALCALL_SHUTDOWN, 				\
   1362 	(int fd, int how),						\
   1363 	(int, int),							\
   1364 	(fd, how))
   1365 
   1366 #if _FORTIFY_SOURCE > 0
   1367 #define STUB(fun) __ssp_weak_name(fun)
   1368 ssize_t _sys_readlink(const char * __restrict, char * __restrict, size_t);
   1369 ssize_t
   1370 STUB(readlink)(const char * __restrict path, char * __restrict buf,
   1371     size_t bufsiz)
   1372 {
   1373 	return _sys_readlink(path, buf, bufsiz);
   1374 }
   1375 
   1376 char *_sys_getcwd(char *, size_t);
   1377 char *
   1378 STUB(getcwd)(char *buf, size_t size)
   1379 {
   1380 	return _sys_getcwd(buf, size);
   1381 }
   1382 #else
   1383 #define STUB(fun) fun
   1384 #endif
   1385 
   1386 FDCALL(ssize_t, REALREAD, DUALCALL_READ,				\
   1387 	(int fd, void *buf, size_t buflen),				\
   1388 	(int, void *, size_t),						\
   1389 	(fd, buf, buflen))
   1390 
   1391 FDCALL(ssize_t, readv, DUALCALL_READV, 					\
   1392 	(int fd, const struct iovec *iov, int iovcnt),			\
   1393 	(int, const struct iovec *, int),				\
   1394 	(fd, iov, iovcnt))
   1395 
   1396 FDCALL(ssize_t, writev, DUALCALL_WRITEV, 				\
   1397 	(int fd, const struct iovec *iov, int iovcnt),			\
   1398 	(int, const struct iovec *, int),				\
   1399 	(fd, iov, iovcnt))
   1400 
   1401 FDCALL(int, REALFSTAT, DUALCALL_FSTAT,					\
   1402 	(int fd, struct stat *sb),					\
   1403 	(int, struct stat *),						\
   1404 	(fd, sb))
   1405 
   1406 FDCALL(int, fstatvfs1, DUALCALL_FSTATVFS1,				\
   1407 	(int fd, struct statvfs *buf, int flags),			\
   1408 	(int, struct statvfs *, int),					\
   1409 	(fd, buf, flags))
   1410 
   1411 FDCALL(off_t, lseek, DUALCALL_LSEEK,					\
   1412 	(int fd, off_t offset, int whence),				\
   1413 	(int, off_t, int),						\
   1414 	(fd, offset, whence))
   1415 
   1416 FDCALL(int, REALGETDENTS, DUALCALL_GETDENTS,				\
   1417 	(int fd, char *buf, size_t nbytes),				\
   1418 	(int, char *, size_t),						\
   1419 	(fd, buf, nbytes))
   1420 
   1421 FDCALL(int, fchown, DUALCALL_FCHOWN,					\
   1422 	(int fd, uid_t owner, gid_t group),				\
   1423 	(int, uid_t, gid_t),						\
   1424 	(fd, owner, group))
   1425 
   1426 FDCALL(int, fchmod, DUALCALL_FCHMOD,					\
   1427 	(int fd, mode_t mode),						\
   1428 	(int, mode_t),							\
   1429 	(fd, mode))
   1430 
   1431 FDCALL(int, ftruncate, DUALCALL_FTRUNCATE,				\
   1432 	(int fd, off_t length),						\
   1433 	(int, off_t),							\
   1434 	(fd, length))
   1435 
   1436 FDCALL(int, fsync, DUALCALL_FSYNC,					\
   1437 	(int fd),							\
   1438 	(int),								\
   1439 	(fd))
   1440 
   1441 FDCALL(int, fsync_range, DUALCALL_FSYNC_RANGE,				\
   1442 	(int fd, int how, off_t start, off_t length),			\
   1443 	(int, int, off_t, off_t),					\
   1444 	(fd, how, start, length))
   1445 
   1446 FDCALL(int, futimes, DUALCALL_FUTIMES,					\
   1447 	(int fd, const struct timeval *tv),				\
   1448 	(int, const struct timeval *),					\
   1449 	(fd, tv))
   1450 
   1451 /*
   1452  * path-based selectors
   1453  */
   1454 
   1455 PATHCALL(int, REALSTAT, DUALCALL_STAT,					\
   1456 	(const char *path, struct stat *sb),				\
   1457 	(const char *, struct stat *),					\
   1458 	(path, sb))
   1459 
   1460 PATHCALL(int, REALLSTAT, DUALCALL_LSTAT,				\
   1461 	(const char *path, struct stat *sb),				\
   1462 	(const char *, struct stat *),					\
   1463 	(path, sb))
   1464 
   1465 PATHCALL(int, chown, DUALCALL_CHOWN,					\
   1466 	(const char *path, uid_t owner, gid_t group),			\
   1467 	(const char *, uid_t, gid_t),					\
   1468 	(path, owner, group))
   1469 
   1470 PATHCALL(int, lchown, DUALCALL_LCHOWN,					\
   1471 	(const char *path, uid_t owner, gid_t group),			\
   1472 	(const char *, uid_t, gid_t),					\
   1473 	(path, owner, group))
   1474 
   1475 PATHCALL(int, chmod, DUALCALL_CHMOD,					\
   1476 	(const char *path, mode_t mode),				\
   1477 	(const char *, mode_t),						\
   1478 	(path, mode))
   1479 
   1480 PATHCALL(int, lchmod, DUALCALL_LCHMOD,					\
   1481 	(const char *path, mode_t mode),				\
   1482 	(const char *, mode_t),						\
   1483 	(path, mode))
   1484 
   1485 PATHCALL(int, statvfs1, DUALCALL_STATVFS1,				\
   1486 	(const char *path, struct statvfs *buf, int flags),		\
   1487 	(const char *, struct statvfs *, int),				\
   1488 	(path, buf, flags))
   1489 
   1490 PATHCALL(int, unlink, DUALCALL_UNLINK,					\
   1491 	(const char *path),						\
   1492 	(const char *),							\
   1493 	(path))
   1494 
   1495 PATHCALL(int, symlink, DUALCALL_SYMLINK,				\
   1496 	(const char *path, const char *target),				\
   1497 	(const char *, const char *),					\
   1498 	(path, target))
   1499 
   1500 PATHCALL(ssize_t, readlink, DUALCALL_READLINK,				\
   1501 	(const char *path, char *buf, size_t bufsiz),			\
   1502 	(const char *, char *, size_t),					\
   1503 	(path, buf, bufsiz))
   1504 
   1505 /* XXX: cross-kernel renames need to be blocked */
   1506 PATHCALL(int, rename, DUALCALL_RENAME,					\
   1507 	(const char *path, const char *to),				\
   1508 	(const char *, const char *),					\
   1509 	(path, to))
   1510 
   1511 PATHCALL(int, mkdir, DUALCALL_MKDIR,					\
   1512 	(const char *path, mode_t mode),				\
   1513 	(const char *, mode_t),						\
   1514 	(path, mode))
   1515 
   1516 PATHCALL(int, rmdir, DUALCALL_RMDIR,					\
   1517 	(const char *path),						\
   1518 	(const char *),							\
   1519 	(path))
   1520 
   1521 PATHCALL(int, utimes, DUALCALL_UTIMES,					\
   1522 	(const char *path, const struct timeval *tv),			\
   1523 	(const char *, const struct timeval *),				\
   1524 	(path, tv))
   1525 
   1526 PATHCALL(int, lutimes, DUALCALL_LUTIMES,				\
   1527 	(const char *path, const struct timeval *tv),			\
   1528 	(const char *, const struct timeval *),				\
   1529 	(path, tv))
   1530 
   1531 PATHCALL(int, truncate, DUALCALL_TRUNCATE,				\
   1532 	(const char *path, off_t length),				\
   1533 	(const char *, off_t),						\
   1534 	(path, length))
   1535