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