Home | History | Annotate | Line # | Download | only in librumphijack
hijack.c revision 1.77
      1 /*      $NetBSD: hijack.c,v 1.77 2011/03/09 09:17:12 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.77 2011/03/09 09:17:12 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/mman.h>
     38 #include <sys/mount.h>
     39 #include <sys/poll.h>
     40 #include <sys/socket.h>
     41 #include <sys/statvfs.h>
     42 
     43 #include <rump/rumpclient.h>
     44 #include <rump/rump_syscalls.h>
     45 
     46 #include <assert.h>
     47 #include <dlfcn.h>
     48 #include <err.h>
     49 #include <errno.h>
     50 #include <fcntl.h>
     51 #include <poll.h>
     52 #include <pthread.h>
     53 #include <signal.h>
     54 #include <stdarg.h>
     55 #include <stdbool.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <time.h>
     60 #include <unistd.h>
     61 
     62 #include "hijack.h"
     63 
     64 enum dualcall {
     65 	DUALCALL_WRITE, DUALCALL_WRITEV, DUALCALL_PWRITE, DUALCALL_PWRITEV,
     66 	DUALCALL_IOCTL, DUALCALL_FCNTL,
     67 	DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_BIND, DUALCALL_CONNECT,
     68 	DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN,
     69 	DUALCALL_RECVFROM, DUALCALL_RECVMSG,
     70 	DUALCALL_SENDTO, DUALCALL_SENDMSG,
     71 	DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT,
     72 	DUALCALL_SHUTDOWN,
     73 	DUALCALL_READ, DUALCALL_READV, DUALCALL_PREAD, DUALCALL_PREADV,
     74 	DUALCALL_DUP2,
     75 	DUALCALL_CLOSE,
     76 	DUALCALL_POLLTS,
     77 	DUALCALL_KEVENT,
     78 	DUALCALL_STAT, DUALCALL_LSTAT, DUALCALL_FSTAT,
     79 	DUALCALL_CHMOD, DUALCALL_LCHMOD, DUALCALL_FCHMOD,
     80 	DUALCALL_CHOWN, DUALCALL_LCHOWN, DUALCALL_FCHOWN,
     81 	DUALCALL_OPEN,
     82 	DUALCALL_STATVFS1, DUALCALL_FSTATVFS1,
     83 	DUALCALL_CHDIR, DUALCALL_FCHDIR,
     84 	DUALCALL_LSEEK,
     85 	DUALCALL_GETDENTS,
     86 	DUALCALL_UNLINK, DUALCALL_SYMLINK, DUALCALL_READLINK,
     87 	DUALCALL_RENAME,
     88 	DUALCALL_MKDIR, DUALCALL_RMDIR,
     89 	DUALCALL_UTIMES, DUALCALL_LUTIMES, DUALCALL_FUTIMES,
     90 	DUALCALL_TRUNCATE, DUALCALL_FTRUNCATE,
     91 	DUALCALL_FSYNC, DUALCALL_FSYNC_RANGE,
     92 	DUALCALL_MOUNT, DUALCALL_UNMOUNT,
     93 	DUALCALL___GETCWD,
     94 	DUALCALL_CHFLAGS, DUALCALL_LCHFLAGS, DUALCALL_FCHFLAGS,
     95 	DUALCALL_ACCESS,
     96 	DUALCALL_MKNOD,
     97 	DUALCALL__NUM
     98 };
     99 
    100 #define RSYS_STRING(a) __STRING(a)
    101 #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a))
    102 
    103 /*
    104  * Would be nice to get this automatically in sync with libc.
    105  * Also, this does not work for compat-using binaries!
    106  */
    107 #if !__NetBSD_Prereq__(5,99,7)
    108 #define REALSELECT select
    109 #define REALPOLLTS pollts
    110 #define REALKEVENT kevent
    111 #define REALSTAT __stat30
    112 #define REALLSTAT __lstat30
    113 #define REALFSTAT __fstat30
    114 #define REALUTIMES utimes
    115 #define REALLUTIMES lutimes
    116 #define REALFUTIMES futimes
    117 #define REALMKNOD mknod
    118 #else
    119 #define REALSELECT _sys___select50
    120 #define REALPOLLTS _sys___pollts50
    121 #define REALKEVENT _sys___kevent50
    122 #define REALSTAT __stat50
    123 #define REALLSTAT __lstat50
    124 #define REALFSTAT __fstat50
    125 #define REALUTIMES __utimes50
    126 #define REALLUTIMES __lutimes50
    127 #define REALFUTIMES __futimes50
    128 #define REALMKNOD __mknod50
    129 #endif
    130 #define REALREAD _sys_read
    131 #define REALPREAD _sys_pread
    132 #define REALPWRITE _sys_pwrite
    133 #define REALGETDENTS __getdents30
    134 #define REALMOUNT __mount50
    135 
    136 int REALSELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *);
    137 int REALPOLLTS(struct pollfd *, nfds_t,
    138 	       const struct timespec *, const sigset_t *);
    139 int REALKEVENT(int, const struct kevent *, size_t, struct kevent *, size_t,
    140 	       const struct timespec *);
    141 ssize_t REALREAD(int, void *, size_t);
    142 ssize_t REALPREAD(int, void *, size_t, off_t);
    143 ssize_t REALPWRITE(int, const void *, size_t, off_t);
    144 int REALSTAT(const char *, struct stat *);
    145 int REALLSTAT(const char *, struct stat *);
    146 int REALFSTAT(int, struct stat *);
    147 int REALGETDENTS(int, char *, size_t);
    148 int REALUTIMES(const char *, const struct timeval [2]);
    149 int REALLUTIMES(const char *, const struct timeval [2]);
    150 int REALFUTIMES(int, const struct timeval [2]);
    151 int REALMOUNT(const char *, const char *, int, void *, size_t);
    152 int __getcwd(char *, size_t);
    153 int REALMKNOD(const char *, mode_t, dev_t);
    154 
    155 #define S(a) __STRING(a)
    156 struct sysnames {
    157 	enum dualcall scm_callnum;
    158 	const char *scm_hostname;
    159 	const char *scm_rumpname;
    160 } syscnames[] = {
    161 	{ DUALCALL_SOCKET,	"__socket30",	RSYS_NAME(SOCKET)	},
    162 	{ DUALCALL_ACCEPT,	"accept",	RSYS_NAME(ACCEPT)	},
    163 	{ DUALCALL_BIND,	"bind",		RSYS_NAME(BIND)		},
    164 	{ DUALCALL_CONNECT,	"connect",	RSYS_NAME(CONNECT)	},
    165 	{ DUALCALL_GETPEERNAME,	"getpeername",	RSYS_NAME(GETPEERNAME)	},
    166 	{ DUALCALL_GETSOCKNAME,	"getsockname",	RSYS_NAME(GETSOCKNAME)	},
    167 	{ DUALCALL_LISTEN,	"listen",	RSYS_NAME(LISTEN)	},
    168 	{ DUALCALL_RECVFROM,	"recvfrom",	RSYS_NAME(RECVFROM)	},
    169 	{ DUALCALL_RECVMSG,	"recvmsg",	RSYS_NAME(RECVMSG)	},
    170 	{ DUALCALL_SENDTO,	"sendto",	RSYS_NAME(SENDTO)	},
    171 	{ DUALCALL_SENDMSG,	"sendmsg",	RSYS_NAME(SENDMSG)	},
    172 	{ DUALCALL_GETSOCKOPT,	"getsockopt",	RSYS_NAME(GETSOCKOPT)	},
    173 	{ DUALCALL_SETSOCKOPT,	"setsockopt",	RSYS_NAME(SETSOCKOPT)	},
    174 	{ DUALCALL_SHUTDOWN,	"shutdown",	RSYS_NAME(SHUTDOWN)	},
    175 	{ DUALCALL_READ,	S(REALREAD),	RSYS_NAME(READ)		},
    176 	{ DUALCALL_READV,	"readv",	RSYS_NAME(READV)	},
    177 	{ DUALCALL_PREAD,	S(REALPREAD),	RSYS_NAME(PREAD)	},
    178 	{ DUALCALL_PREADV,	"preadv",	RSYS_NAME(PREADV)	},
    179 	{ DUALCALL_WRITE,	"write",	RSYS_NAME(WRITE)	},
    180 	{ DUALCALL_WRITEV,	"writev",	RSYS_NAME(WRITEV)	},
    181 	{ DUALCALL_PWRITE,	S(REALPWRITE),	RSYS_NAME(PWRITE)	},
    182 	{ DUALCALL_PWRITEV,	"pwritev",	RSYS_NAME(PWRITEV)	},
    183 	{ DUALCALL_IOCTL,	"ioctl",	RSYS_NAME(IOCTL)	},
    184 	{ DUALCALL_FCNTL,	"fcntl",	RSYS_NAME(FCNTL)	},
    185 	{ DUALCALL_DUP2,	"dup2",		RSYS_NAME(DUP2)		},
    186 	{ DUALCALL_CLOSE,	"close",	RSYS_NAME(CLOSE)	},
    187 	{ DUALCALL_POLLTS,	S(REALPOLLTS),	RSYS_NAME(POLLTS)	},
    188 	{ DUALCALL_KEVENT,	S(REALKEVENT),	RSYS_NAME(KEVENT)	},
    189 	{ DUALCALL_STAT,	S(REALSTAT),	RSYS_NAME(STAT)		},
    190 	{ DUALCALL_LSTAT,	S(REALLSTAT),	RSYS_NAME(LSTAT)	},
    191 	{ DUALCALL_FSTAT,	S(REALFSTAT),	RSYS_NAME(FSTAT)	},
    192 	{ DUALCALL_CHOWN,	"chown",	RSYS_NAME(CHOWN)	},
    193 	{ DUALCALL_LCHOWN,	"lchown",	RSYS_NAME(LCHOWN)	},
    194 	{ DUALCALL_FCHOWN,	"fchown",	RSYS_NAME(FCHOWN)	},
    195 	{ DUALCALL_CHMOD,	"chmod",	RSYS_NAME(CHMOD)	},
    196 	{ DUALCALL_LCHMOD,	"lchmod",	RSYS_NAME(LCHMOD)	},
    197 	{ DUALCALL_FCHMOD,	"fchmod",	RSYS_NAME(FCHMOD)	},
    198 	{ DUALCALL_UTIMES,	S(REALUTIMES),	RSYS_NAME(UTIMES)	},
    199 	{ DUALCALL_LUTIMES,	S(REALLUTIMES),	RSYS_NAME(LUTIMES)	},
    200 	{ DUALCALL_FUTIMES,	S(REALFUTIMES),	RSYS_NAME(FUTIMES)	},
    201 	{ DUALCALL_OPEN,	"open",		RSYS_NAME(OPEN)		},
    202 	{ DUALCALL_STATVFS1,	"statvfs1",	RSYS_NAME(STATVFS1)	},
    203 	{ DUALCALL_FSTATVFS1,	"fstatvfs1",	RSYS_NAME(FSTATVFS1)	},
    204 	{ DUALCALL_CHDIR,	"chdir",	RSYS_NAME(CHDIR)	},
    205 	{ DUALCALL_FCHDIR,	"fchdir",	RSYS_NAME(FCHDIR)	},
    206 	{ DUALCALL_LSEEK,	"lseek",	RSYS_NAME(LSEEK)	},
    207 	{ DUALCALL_GETDENTS,	"__getdents30",	RSYS_NAME(GETDENTS)	},
    208 	{ DUALCALL_UNLINK,	"unlink",	RSYS_NAME(UNLINK)	},
    209 	{ DUALCALL_SYMLINK,	"symlink",	RSYS_NAME(SYMLINK)	},
    210 	{ DUALCALL_READLINK,	"readlink",	RSYS_NAME(READLINK)	},
    211 	{ DUALCALL_RENAME,	"rename",	RSYS_NAME(RENAME)	},
    212 	{ DUALCALL_MKDIR,	"mkdir",	RSYS_NAME(MKDIR)	},
    213 	{ DUALCALL_RMDIR,	"rmdir",	RSYS_NAME(RMDIR)	},
    214 	{ DUALCALL_TRUNCATE,	"truncate",	RSYS_NAME(TRUNCATE)	},
    215 	{ DUALCALL_FTRUNCATE,	"ftruncate",	RSYS_NAME(FTRUNCATE)	},
    216 	{ DUALCALL_FSYNC,	"fsync",	RSYS_NAME(FSYNC)	},
    217 	{ DUALCALL_FSYNC_RANGE,	"fsync_range",	RSYS_NAME(FSYNC_RANGE)	},
    218 	{ DUALCALL_MOUNT,	S(REALMOUNT),	RSYS_NAME(MOUNT)	},
    219 	{ DUALCALL_UNMOUNT,	"unmount",	RSYS_NAME(UNMOUNT)	},
    220 	{ DUALCALL___GETCWD,	"__getcwd",	RSYS_NAME(__GETCWD)	},
    221 	{ DUALCALL_CHFLAGS,	"chflags",	RSYS_NAME(CHFLAGS)	},
    222 	{ DUALCALL_LCHFLAGS,	"lchflags",	RSYS_NAME(LCHFLAGS)	},
    223 	{ DUALCALL_FCHFLAGS,	"fchflags",	RSYS_NAME(FCHFLAGS)	},
    224 	{ DUALCALL_ACCESS,	"access",	RSYS_NAME(ACCESS)	},
    225 	{ DUALCALL_MKNOD,	S(REALMKNOD),	RSYS_NAME(MKNOD)	},
    226 };
    227 #undef S
    228 
    229 struct bothsys {
    230 	void *bs_host;
    231 	void *bs_rump;
    232 } syscalls[DUALCALL__NUM];
    233 #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which
    234 
    235 static pid_t	(*host_fork)(void);
    236 static int	(*host_daemon)(int, int);
    237 static void *	(*host_mmap)(void *, size_t, int, int, int, off_t);
    238 
    239 /*
    240  * This tracks if our process is in a subdirectory of /rump.
    241  * It's preserved over exec.
    242  */
    243 static bool pwdinrump;
    244 
    245 enum pathtype { PATH_HOST, PATH_RUMP, PATH_RUMPBLANKET };
    246 
    247 static bool		fd_isrump(int);
    248 static enum pathtype	path_isrump(const char *);
    249 
    250 /*
    251  * Maintain a mapping table for the usual dup2 suspects.
    252  * Could use atomic ops to operate on dup2vec, but an application
    253  * racing there is not well-defined, so don't bother.
    254  */
    255 /* note: you cannot change this without editing the env-passing code */
    256 #define DUP2HIGH 2
    257 static uint32_t dup2vec[DUP2HIGH+1];
    258 #define DUP2BIT (1<<31)
    259 #define DUP2ALIAS (1<<30)
    260 #define DUP2FDMASK ((1<<30)-1)
    261 
    262 static bool
    263 isdup2d(int fd)
    264 {
    265 
    266 	return fd <= DUP2HIGH && fd >= 0 && dup2vec[fd] & DUP2BIT;
    267 }
    268 
    269 static int
    270 mapdup2(int hostfd)
    271 {
    272 
    273 	_DIAGASSERT(isdup2d(hostfd));
    274 	return dup2vec[hostfd] & DUP2FDMASK;
    275 }
    276 
    277 static int
    278 unmapdup2(int rumpfd)
    279 {
    280 	int i;
    281 
    282 	for (i = 0; i <= DUP2HIGH; i++) {
    283 		if (dup2vec[i] & DUP2BIT &&
    284 		    (dup2vec[i] & DUP2FDMASK) == (unsigned)rumpfd)
    285 			return i;
    286 	}
    287 	return -1;
    288 }
    289 
    290 static void
    291 setdup2(int hostfd, int rumpfd)
    292 {
    293 
    294 	if (hostfd > DUP2HIGH) {
    295 		_DIAGASSERT(0);
    296 		return;
    297 	}
    298 
    299 	dup2vec[hostfd] = DUP2BIT | DUP2ALIAS | rumpfd;
    300 }
    301 
    302 static void
    303 clrdup2(int hostfd)
    304 {
    305 
    306 	if (hostfd > DUP2HIGH) {
    307 		_DIAGASSERT(0);
    308 		return;
    309 	}
    310 
    311 	dup2vec[hostfd] = 0;
    312 }
    313 
    314 static bool
    315 killdup2alias(int rumpfd)
    316 {
    317 	int hostfd;
    318 
    319 	if ((hostfd = unmapdup2(rumpfd)) == -1)
    320 		return false;
    321 
    322 	if (dup2vec[hostfd] & DUP2ALIAS) {
    323 		dup2vec[hostfd] &= ~DUP2ALIAS;
    324 		return true;
    325 	}
    326 	return false;
    327 }
    328 
    329 //#define DEBUGJACK
    330 #ifdef DEBUGJACK
    331 #define DPRINTF(x) mydprintf x
    332 static void
    333 mydprintf(const char *fmt, ...)
    334 {
    335 	va_list ap;
    336 
    337 	if (isdup2d(STDERR_FILENO))
    338 		return;
    339 
    340 	va_start(ap, fmt);
    341 	vfprintf(stderr, fmt, ap);
    342 	va_end(ap);
    343 }
    344 
    345 static const char *
    346 whichfd(int fd)
    347 {
    348 
    349 	if (fd == -1)
    350 		return "-1";
    351 	else if (fd_isrump(fd))
    352 		return "rump";
    353 	else
    354 		return "host";
    355 }
    356 
    357 static const char *
    358 whichpath(const char *path)
    359 {
    360 
    361 	if (path_isrump(path))
    362 		return "rump";
    363 	else
    364 		return "host";
    365 }
    366 
    367 #else
    368 #define DPRINTF(x)
    369 #endif
    370 
    371 #define FDCALL(type, name, rcname, args, proto, vars)			\
    372 type name args								\
    373 {									\
    374 	type (*fun) proto;						\
    375 									\
    376 	DPRINTF(("%s -> %d (%s)\n", __STRING(name), fd,	whichfd(fd)));	\
    377 	if (fd_isrump(fd)) {						\
    378 		fun = syscalls[rcname].bs_rump;				\
    379 		fd = fd_host2rump(fd);					\
    380 	} else {							\
    381 		fun = syscalls[rcname].bs_host;				\
    382 	}								\
    383 									\
    384 	return fun vars;						\
    385 }
    386 
    387 #define PATHCALL(type, name, rcname, args, proto, vars)			\
    388 type name args								\
    389 {									\
    390 	type (*fun) proto;						\
    391 	enum pathtype pt;						\
    392 									\
    393 	DPRINTF(("%s -> %s (%s)\n", __STRING(name), path,		\
    394 	    whichpath(path)));						\
    395 	if ((pt = path_isrump(path)) != PATH_HOST) {			\
    396 		fun = syscalls[rcname].bs_rump;				\
    397 		if (pt == PATH_RUMP)					\
    398 			path = path_host2rump(path);			\
    399 	} else {							\
    400 		fun = syscalls[rcname].bs_host;				\
    401 	}								\
    402 									\
    403 	return fun vars;						\
    404 }
    405 
    406 /*
    407  * These variables are set from the RUMPHIJACK string and control
    408  * which operations can product rump kernel file descriptors.
    409  * This should be easily extendable for future needs.
    410  */
    411 #define RUMPHIJACK_DEFAULT "path=/rump,socket=all:nolocal"
    412 static bool rumpsockets[PF_MAX];
    413 static const char *rumpprefix;
    414 static size_t rumpprefixlen;
    415 
    416 static struct {
    417 	int pf;
    418 	const char *name;
    419 } socketmap[] = {
    420 	{ PF_LOCAL, "local" },
    421 	{ PF_INET, "inet" },
    422 	{ PF_LINK, "link" },
    423 #ifdef PF_OROUTE
    424 	{ PF_OROUTE, "oroute" },
    425 #endif
    426 	{ PF_ROUTE, "route" },
    427 	{ PF_INET6, "inet6" },
    428 #ifdef PF_MPLS
    429 	{ PF_MPLS, "mpls" },
    430 #endif
    431 	{ -1, NULL }
    432 };
    433 
    434 static void
    435 sockparser(char *buf)
    436 {
    437 	char *p, *l;
    438 	bool value;
    439 	int i;
    440 
    441 	/* if "all" is present, it must be specified first */
    442 	if (strncmp(buf, "all", strlen("all")) == 0) {
    443 		for (i = 0; i < (int)__arraycount(rumpsockets); i++) {
    444 			rumpsockets[i] = true;
    445 		}
    446 		buf += strlen("all");
    447 		if (*buf == ':')
    448 			buf++;
    449 	}
    450 
    451 	for (p = strtok_r(buf, ":", &l); p; p = strtok_r(NULL, ":", &l)) {
    452 		value = true;
    453 		if (strncmp(p, "no", strlen("no")) == 0) {
    454 			value = false;
    455 			p += strlen("no");
    456 		}
    457 
    458 		for (i = 0; socketmap[i].name; i++) {
    459 			if (strcmp(p, socketmap[i].name) == 0) {
    460 				rumpsockets[socketmap[i].pf] = value;
    461 				break;
    462 			}
    463 		}
    464 		if (socketmap[i].name == NULL) {
    465 			warnx("invalid socket specifier %s", p);
    466 		}
    467 	}
    468 }
    469 
    470 static void
    471 pathparser(char *buf)
    472 {
    473 
    474 	/* sanity-check */
    475 	if (*buf != '/')
    476 		errx(1, "hijack path specifier must begin with ``/''");
    477 	rumpprefixlen = strlen(buf);
    478 	if (rumpprefixlen < 2)
    479 		errx(1, "invalid hijack prefix: %s", buf);
    480 	if (buf[rumpprefixlen-1] == '/' && strspn(buf, "/") != rumpprefixlen)
    481 		errx(1, "hijack prefix may end in slash only if pure "
    482 		    "slash, gave %s", buf);
    483 
    484 	if ((rumpprefix = strdup(buf)) == NULL)
    485 		err(1, "strdup");
    486 	rumpprefixlen = strlen(rumpprefix);
    487 }
    488 
    489 static struct blanket {
    490 	const char *pfx;
    491 	size_t len;
    492 } *blanket;
    493 static int nblanket;
    494 
    495 static void
    496 blanketparser(char *buf)
    497 {
    498 	char *p, *l;
    499 	int i;
    500 
    501 	for (nblanket = 0, p = buf; p; p = strchr(p+1, ':'), nblanket++)
    502 		continue;
    503 
    504 	blanket = malloc(nblanket * sizeof(*blanket));
    505 	if (blanket == NULL)
    506 		err(1, "alloc blanket %d", nblanket);
    507 
    508 	for (p = strtok_r(buf, ":", &l), i = 0; p;
    509 	    p = strtok_r(NULL, ":", &l), i++) {
    510 		blanket[i].pfx = strdup(p);
    511 		if (blanket[i].pfx == NULL)
    512 			err(1, "strdup blanket");
    513 		blanket[i].len = strlen(p);
    514 
    515 		if (blanket[i].len == 0 || *blanket[i].pfx != '/')
    516 			errx(1, "invalid blanket specifier %s", p);
    517 		if (*(blanket[i].pfx + blanket[i].len-1) == '/')
    518 			errx(1, "invalid blanket specifier %s", p);
    519 	}
    520 }
    521 
    522 static struct {
    523 	void (*parsefn)(char *);
    524 	const char *name;
    525 } hijackparse[] = {
    526 	{ sockparser, "socket" },
    527 	{ pathparser, "path" },
    528 	{ blanketparser, "blanket" },
    529 	{ NULL, NULL },
    530 };
    531 
    532 static void
    533 parsehijack(char *hijack)
    534 {
    535 	char *p, *p2, *l;
    536 	const char *hijackcopy;
    537 	int i;
    538 
    539 	if ((hijackcopy = strdup(hijack)) == NULL)
    540 		err(1, "strdup");
    541 
    542 	/* disable everything explicitly */
    543 	for (i = 0; i < PF_MAX; i++)
    544 		rumpsockets[i] = false;
    545 
    546 	for (p = strtok_r(hijack, ",", &l); p; p = strtok_r(NULL, ",", &l)) {
    547 		p2 = strchr(p, '=');
    548 		if (!p2)
    549 			errx(1, "invalid hijack specifier: %s", hijackcopy);
    550 
    551 		for (i = 0; hijackparse[i].parsefn; i++) {
    552 			if (strncmp(hijackparse[i].name, p,
    553 			    (size_t)(p2-p)) == 0) {
    554 				hijackparse[i].parsefn(p2+1);
    555 				break;
    556 			}
    557 		}
    558 	}
    559 
    560 }
    561 
    562 static void __attribute__((constructor))
    563 rcinit(void)
    564 {
    565 	char buf[1024];
    566 	unsigned i, j;
    567 
    568 	host_fork = dlsym(RTLD_NEXT, "fork");
    569 	host_daemon = dlsym(RTLD_NEXT, "daemon");
    570 	host_mmap = dlsym(RTLD_NEXT, "mmap");
    571 
    572 	/*
    573 	 * In theory cannot print anything during lookups because
    574 	 * we might not have the call vector set up.  so, the errx()
    575 	 * is a bit of a strech, but it might work.
    576 	 */
    577 
    578 	for (i = 0; i < DUALCALL__NUM; i++) {
    579 		/* build runtime O(1) access */
    580 		for (j = 0; j < __arraycount(syscnames); j++) {
    581 			if (syscnames[j].scm_callnum == i)
    582 				break;
    583 		}
    584 
    585 		if (j == __arraycount(syscnames))
    586 			errx(1, "rumphijack error: syscall pos %d missing", i);
    587 
    588 		syscalls[i].bs_host = dlsym(RTLD_NEXT,
    589 		    syscnames[j].scm_hostname);
    590 		if (syscalls[i].bs_host == NULL)
    591 			errx(1, "hostcall %s not found!",
    592 			    syscnames[j].scm_hostname);
    593 
    594 		syscalls[i].bs_rump = dlsym(RTLD_NEXT,
    595 		    syscnames[j].scm_rumpname);
    596 		if (syscalls[i].bs_rump == NULL)
    597 			errx(1, "rumpcall %s not found!",
    598 			    syscnames[j].scm_rumpname);
    599 	}
    600 
    601 	if (rumpclient_init() == -1)
    602 		err(1, "rumpclient init");
    603 
    604 	/* check which syscalls we're supposed to hijack */
    605 	if (getenv_r("RUMPHIJACK", buf, sizeof(buf)) == -1) {
    606 		strcpy(buf, RUMPHIJACK_DEFAULT);
    607 	}
    608 	parsehijack(buf);
    609 
    610 	/* set client persistence level */
    611 	if (getenv_r("RUMPHIJACK_RETRYCONNECT", buf, sizeof(buf)) != -1) {
    612 		if (strcmp(buf, "die") == 0)
    613 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_DIE);
    614 		else if (strcmp(buf, "inftime") == 0)
    615 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME);
    616 		else if (strcmp(buf, "once") == 0)
    617 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_ONCE);
    618 		else {
    619 			time_t timeout;
    620 			char *ep;
    621 
    622 			timeout = (time_t)strtoll(buf, &ep, 10);
    623 			if (timeout <= 0 || ep != buf + strlen(buf))
    624 				errx(1, "RUMPHIJACK_RETRYCONNECT must be "
    625 				    "keyword or integer, got: %s", buf);
    626 
    627 			rumpclient_setconnretry(timeout);
    628 		}
    629 	}
    630 
    631 	if (getenv_r("RUMPHIJACK__DUP2INFO", buf, sizeof(buf)) == 0) {
    632 		if (sscanf(buf, "%u,%u,%u",
    633 		    &dup2vec[0], &dup2vec[1], &dup2vec[2]) != 3) {
    634 			warnx("invalid dup2mask: %s", buf);
    635 			memset(dup2vec, 0, sizeof(dup2vec));
    636 		}
    637 		unsetenv("RUMPHIJACK__DUP2INFO");
    638 	}
    639 	if (getenv_r("RUMPHIJACK__PWDINRUMP", buf, sizeof(buf)) == 0) {
    640 		pwdinrump = true;
    641 		unsetenv("RUMPHIJACK__PWDINRUMP");
    642 	}
    643 }
    644 
    645 /* Need runtime selection.  low for now due to FD_SETSIZE */
    646 #define HIJACK_FDOFF 128
    647 
    648 static int
    649 fd_rump2host(int fd)
    650 {
    651 
    652 	if (fd == -1)
    653 		return fd;
    654 	return fd + HIJACK_FDOFF;
    655 }
    656 
    657 static int
    658 fd_rump2host_withdup(int fd)
    659 {
    660 	int hfd;
    661 
    662 	_DIAGASSERT(fd != -1);
    663 	hfd = unmapdup2(fd);
    664 	if (hfd != -1) {
    665 		_DIAGASSERT(hfd <= DUP2HIGH);
    666 		return hfd;
    667 	}
    668 	return fd_rump2host(fd);
    669 }
    670 
    671 static int
    672 fd_host2rump(int fd)
    673 {
    674 
    675 	if (!isdup2d(fd))
    676 		return fd - HIJACK_FDOFF;
    677 	else
    678 		return mapdup2(fd);
    679 }
    680 
    681 static bool
    682 fd_isrump(int fd)
    683 {
    684 
    685 	return isdup2d(fd) || fd >= HIJACK_FDOFF;
    686 }
    687 
    688 #define assertfd(_fd_) assert(ISDUP2D(_fd_) || (_fd_) >= HIJACK_FDOFF)
    689 
    690 static enum pathtype
    691 path_isrump(const char *path)
    692 {
    693 	size_t plen;
    694 	int i;
    695 
    696 	if (rumpprefix == NULL && nblanket == 0)
    697 		return PATH_HOST;
    698 
    699 	if (*path == '/') {
    700 		plen = strlen(path);
    701 		if (rumpprefix && plen >= rumpprefixlen) {
    702 			if (strncmp(path, rumpprefix, rumpprefixlen) == 0
    703 			    && (plen == rumpprefixlen
    704 			      || *(path + rumpprefixlen) == '/')) {
    705 				return PATH_RUMP;
    706 			}
    707 		}
    708 		for (i = 0; i < nblanket; i++) {
    709 			if (strncmp(path, blanket[i].pfx, blanket[i].len) == 0)
    710 				return PATH_RUMPBLANKET;
    711 		}
    712 
    713 		return PATH_HOST;
    714 	} else {
    715 		return pwdinrump ? PATH_RUMP : PATH_HOST;
    716 	}
    717 }
    718 
    719 static const char *rootpath = "/";
    720 static const char *
    721 path_host2rump(const char *path)
    722 {
    723 	const char *rv;
    724 
    725 	if (*path == '/') {
    726 		rv = path + rumpprefixlen;
    727 		if (*rv == '\0')
    728 			rv = rootpath;
    729 	} else {
    730 		rv = path;
    731 	}
    732 
    733 	return rv;
    734 }
    735 
    736 static int
    737 dodup(int oldd, int minfd)
    738 {
    739 	int (*op_fcntl)(int, int, ...);
    740 	int newd;
    741 	int isrump;
    742 
    743 	DPRINTF(("dup -> %d (minfd %d)\n", oldd, minfd));
    744 	if (fd_isrump(oldd)) {
    745 		op_fcntl = GETSYSCALL(rump, FCNTL);
    746 		oldd = fd_host2rump(oldd);
    747 		if (minfd >= HIJACK_FDOFF)
    748 			minfd -= HIJACK_FDOFF;
    749 		isrump = 1;
    750 	} else {
    751 		op_fcntl = GETSYSCALL(host, FCNTL);
    752 		isrump = 0;
    753 	}
    754 
    755 	newd = op_fcntl(oldd, F_DUPFD, minfd);
    756 
    757 	if (isrump)
    758 		newd = fd_rump2host(newd);
    759 	DPRINTF(("dup <- %d\n", newd));
    760 
    761 	return newd;
    762 }
    763 
    764 /*
    765  * dup a host file descriptor so that it doesn't collide with the dup2mask
    766  */
    767 static int
    768 fd_dupgood(int fd)
    769 {
    770 	int (*op_fcntl)(int, int, ...) = GETSYSCALL(host, FCNTL);
    771 	int (*op_close)(int) = GETSYSCALL(host, CLOSE);
    772 	int ofd, i;
    773 
    774 	for (i = 1; isdup2d(fd); i++) {
    775 		ofd = fd;
    776 		fd = op_fcntl(ofd, F_DUPFD, i);
    777 		op_close(ofd);
    778 	}
    779 
    780 	return fd;
    781 }
    782 
    783 int
    784 open(const char *path, int flags, ...)
    785 {
    786 	int (*op_open)(const char *, int, ...);
    787 	bool isrump;
    788 	va_list ap;
    789 	enum pathtype pt;
    790 	int fd;
    791 
    792 	DPRINTF(("open -> %s (%s)\n", path, whichpath(path)));
    793 
    794 	if ((pt = path_isrump(path)) != PATH_HOST) {
    795 		if (pt == PATH_RUMP)
    796 			path = path_host2rump(path);
    797 		op_open = GETSYSCALL(rump, OPEN);
    798 		isrump = true;
    799 	} else {
    800 		op_open = GETSYSCALL(host, OPEN);
    801 		isrump = false;
    802 	}
    803 
    804 	va_start(ap, flags);
    805 	fd = op_open(path, flags, va_arg(ap, mode_t));
    806 	va_end(ap);
    807 
    808 	if (isrump)
    809 		fd = fd_rump2host(fd);
    810 	else
    811 		fd = fd_dupgood(fd);
    812 
    813 	DPRINTF(("open <- %d (%s)\n", fd, whichfd(fd)));
    814 	return fd;
    815 }
    816 
    817 int
    818 chdir(const char *path)
    819 {
    820 	int (*op_chdir)(const char *);
    821 	enum pathtype pt;
    822 	int rv;
    823 
    824 	if ((pt = path_isrump(path)) != PATH_HOST) {
    825 		op_chdir = GETSYSCALL(rump, CHDIR);
    826 		if (pt == PATH_RUMP)
    827 			path = path_host2rump(path);
    828 	} else {
    829 		op_chdir = GETSYSCALL(host, CHDIR);
    830 	}
    831 
    832 	rv = op_chdir(path);
    833 	if (rv == 0)
    834 		pwdinrump = pt != PATH_HOST;
    835 
    836 	return rv;
    837 }
    838 
    839 int
    840 fchdir(int fd)
    841 {
    842 	int (*op_fchdir)(int);
    843 	bool isrump;
    844 	int rv;
    845 
    846 	if (fd_isrump(fd)) {
    847 		op_fchdir = GETSYSCALL(rump, FCHDIR);
    848 		isrump = true;
    849 		fd = fd_host2rump(fd);
    850 	} else {
    851 		op_fchdir = GETSYSCALL(host, FCHDIR);
    852 		isrump = false;
    853 	}
    854 
    855 	rv = op_fchdir(fd);
    856 	if (rv == 0) {
    857 		pwdinrump = isrump;
    858 	}
    859 
    860 	return rv;
    861 }
    862 
    863 int
    864 __getcwd(char *bufp, size_t len)
    865 {
    866 	int (*op___getcwd)(char *, size_t);
    867 	size_t prefixgap;
    868 	bool iamslash;
    869 	int rv;
    870 
    871 	if (pwdinrump && rumpprefix) {
    872 		if (rumpprefix[rumpprefixlen-1] == '/')
    873 			iamslash = true;
    874 		else
    875 			iamslash = false;
    876 
    877 		if (iamslash)
    878 			prefixgap = rumpprefixlen - 1; /* ``//+path'' */
    879 		else
    880 			prefixgap = rumpprefixlen; /* ``/pfx+/path'' */
    881 		if (len <= prefixgap) {
    882 			errno = ERANGE;
    883 			return -1;
    884 		}
    885 
    886 		op___getcwd = GETSYSCALL(rump, __GETCWD);
    887 		rv = op___getcwd(bufp + prefixgap, len - prefixgap);
    888 		if (rv == -1)
    889 			return rv;
    890 
    891 		/* augment the "/" part only for a non-root path */
    892 		memcpy(bufp, rumpprefix, rumpprefixlen);
    893 
    894 		/* append / only to non-root cwd */
    895 		if (rv != 2)
    896 			bufp[prefixgap] = '/';
    897 
    898 		/* don't append extra slash in the purely-slash case */
    899 		if (rv == 2 && !iamslash)
    900 			bufp[rumpprefixlen] = '\0';
    901 	} else if (pwdinrump) {
    902 		/* assume blanket.  we can't provide a prefix here */
    903 		op___getcwd = GETSYSCALL(rump, __GETCWD);
    904 		rv = op___getcwd(bufp, len);
    905 	} else {
    906 		op___getcwd = GETSYSCALL(host, __GETCWD);
    907 		rv = op___getcwd(bufp, len);
    908 	}
    909 
    910 	return rv;
    911 }
    912 
    913 int
    914 rename(const char *from, const char *to)
    915 {
    916 	int (*op_rename)(const char *, const char *);
    917 	enum pathtype ptf, ptt;
    918 
    919 	if ((ptf = path_isrump(from)) != PATH_HOST) {
    920 		if ((ptt = path_isrump(to)) == PATH_HOST) {
    921 			errno = EXDEV;
    922 			return -1;
    923 		}
    924 
    925 		if (ptf == PATH_RUMP)
    926 			from = path_host2rump(from);
    927 		if (ptt == PATH_RUMP)
    928 			to = path_host2rump(to);
    929 		op_rename = GETSYSCALL(rump, RENAME);
    930 	} else {
    931 		if (path_isrump(to) != PATH_HOST) {
    932 			errno = EXDEV;
    933 			return -1;
    934 		}
    935 
    936 		op_rename = GETSYSCALL(host, RENAME);
    937 	}
    938 
    939 	return op_rename(from, to);
    940 }
    941 
    942 int __socket30(int, int, int);
    943 int
    944 __socket30(int domain, int type, int protocol)
    945 {
    946 	int (*op_socket)(int, int, int);
    947 	int fd;
    948 	bool isrump;
    949 
    950 	isrump = domain < PF_MAX && rumpsockets[domain];
    951 
    952 	if (isrump)
    953 		op_socket = GETSYSCALL(rump, SOCKET);
    954 	else
    955 		op_socket = GETSYSCALL(host, SOCKET);
    956 	fd = op_socket(domain, type, protocol);
    957 
    958 	if (isrump)
    959 		fd = fd_rump2host(fd);
    960 	else
    961 		fd = fd_dupgood(fd);
    962 	DPRINTF(("socket <- %d\n", fd));
    963 
    964 	return fd;
    965 }
    966 
    967 int
    968 accept(int s, struct sockaddr *addr, socklen_t *addrlen)
    969 {
    970 	int (*op_accept)(int, struct sockaddr *, socklen_t *);
    971 	int fd;
    972 	bool isrump;
    973 
    974 	isrump = fd_isrump(s);
    975 
    976 	DPRINTF(("accept -> %d", s));
    977 	if (isrump) {
    978 		op_accept = GETSYSCALL(rump, ACCEPT);
    979 		s = fd_host2rump(s);
    980 	} else {
    981 		op_accept = GETSYSCALL(host, ACCEPT);
    982 	}
    983 	fd = op_accept(s, addr, addrlen);
    984 	if (fd != -1 && isrump)
    985 		fd = fd_rump2host(fd);
    986 	else
    987 		fd = fd_dupgood(fd);
    988 
    989 	DPRINTF((" <- %d\n", fd));
    990 
    991 	return fd;
    992 }
    993 
    994 /*
    995  * ioctl and fcntl are varargs calls and need special treatment
    996  */
    997 int
    998 ioctl(int fd, unsigned long cmd, ...)
    999 {
   1000 	int (*op_ioctl)(int, unsigned long cmd, ...);
   1001 	va_list ap;
   1002 	int rv;
   1003 
   1004 	DPRINTF(("ioctl -> %d\n", fd));
   1005 	if (fd_isrump(fd)) {
   1006 		fd = fd_host2rump(fd);
   1007 		op_ioctl = GETSYSCALL(rump, IOCTL);
   1008 	} else {
   1009 		op_ioctl = GETSYSCALL(host, IOCTL);
   1010 	}
   1011 
   1012 	va_start(ap, cmd);
   1013 	rv = op_ioctl(fd, cmd, va_arg(ap, void *));
   1014 	va_end(ap);
   1015 	return rv;
   1016 }
   1017 
   1018 int
   1019 fcntl(int fd, int cmd, ...)
   1020 {
   1021 	int (*op_fcntl)(int, int, ...);
   1022 	va_list ap;
   1023 	int rv, minfd, i, maxdup2;
   1024 
   1025 	DPRINTF(("fcntl -> %d (cmd %d)\n", fd, cmd));
   1026 
   1027 	switch (cmd) {
   1028 	case F_DUPFD:
   1029 		va_start(ap, cmd);
   1030 		minfd = va_arg(ap, int);
   1031 		va_end(ap);
   1032 		return dodup(fd, minfd);
   1033 
   1034 	case F_CLOSEM:
   1035 		/*
   1036 		 * So, if fd < HIJACKOFF, we want to do a host closem.
   1037 		 */
   1038 
   1039 		if (fd < HIJACK_FDOFF) {
   1040 			int closemfd = fd;
   1041 
   1042 			if (rumpclient__closenotify(&closemfd,
   1043 			    RUMPCLIENT_CLOSE_FCLOSEM) == -1)
   1044 				return -1;
   1045 			op_fcntl = GETSYSCALL(host, FCNTL);
   1046 			rv = op_fcntl(closemfd, cmd);
   1047 			if (rv)
   1048 				return rv;
   1049 		}
   1050 
   1051 		/*
   1052 		 * Additionally, we want to do a rump closem, but only
   1053 		 * for the file descriptors not dup2'd.
   1054 		 */
   1055 
   1056 		for (i = 0, maxdup2 = 0; i <= DUP2HIGH; i++) {
   1057 			if (dup2vec[i] & DUP2BIT) {
   1058 				int val;
   1059 
   1060 				val = dup2vec[i] & DUP2FDMASK;
   1061 				maxdup2 = MAX(val, maxdup2);
   1062 			}
   1063 		}
   1064 
   1065 		if (fd >= HIJACK_FDOFF)
   1066 			fd -= HIJACK_FDOFF;
   1067 		else
   1068 			fd = 0;
   1069 		fd = MAX(maxdup2+1, fd);
   1070 
   1071 		/* hmm, maybe we should close rump fd's not within dup2mask? */
   1072 		return rump_sys_fcntl(fd, F_CLOSEM);
   1073 
   1074 	case F_MAXFD:
   1075 		/*
   1076 		 * For maxfd, if there's a rump kernel fd, return
   1077 		 * it hostified.  Otherwise, return host's MAXFD
   1078 		 * return value.
   1079 		 */
   1080 		if ((rv = rump_sys_fcntl(fd, F_MAXFD)) != -1) {
   1081 			/*
   1082 			 * This might go a little wrong in case
   1083 			 * of dup2 to [012], but I'm not sure if
   1084 			 * there's a justification for tracking
   1085 			 * that info.  Consider e.g.
   1086 			 * dup2(rumpfd, 2) followed by rump_sys_open()
   1087 			 * returning 1.  We should return 1+HIJACKOFF,
   1088 			 * not 2+HIJACKOFF.  However, if [01] is not
   1089 			 * open, the correct return value is 2.
   1090 			 */
   1091 			return fd_rump2host(fd);
   1092 		} else {
   1093 			op_fcntl = GETSYSCALL(host, FCNTL);
   1094 			return op_fcntl(fd, F_MAXFD);
   1095 		}
   1096 		/*NOTREACHED*/
   1097 
   1098 	default:
   1099 		if (fd_isrump(fd)) {
   1100 			fd = fd_host2rump(fd);
   1101 			op_fcntl = GETSYSCALL(rump, FCNTL);
   1102 		} else {
   1103 			op_fcntl = GETSYSCALL(host, FCNTL);
   1104 		}
   1105 
   1106 		va_start(ap, cmd);
   1107 		rv = op_fcntl(fd, cmd, va_arg(ap, void *));
   1108 		va_end(ap);
   1109 		return rv;
   1110 	}
   1111 	/*NOTREACHED*/
   1112 }
   1113 
   1114 int
   1115 close(int fd)
   1116 {
   1117 	int (*op_close)(int);
   1118 	int rv;
   1119 
   1120 	DPRINTF(("close -> %d\n", fd));
   1121 	if (fd_isrump(fd)) {
   1122 		bool undup2 = false;
   1123 		int ofd;
   1124 
   1125 		if (isdup2d(ofd = fd)) {
   1126 			undup2 = true;
   1127 		}
   1128 
   1129 		fd = fd_host2rump(fd);
   1130 		if (!undup2 && killdup2alias(fd)) {
   1131 			return 0;
   1132 		}
   1133 
   1134 		op_close = GETSYSCALL(rump, CLOSE);
   1135 		rv = op_close(fd);
   1136 		if (rv == 0 && undup2) {
   1137 			clrdup2(ofd);
   1138 		}
   1139 	} else {
   1140 		if (rumpclient__closenotify(&fd, RUMPCLIENT_CLOSE_CLOSE) == -1)
   1141 			return -1;
   1142 		op_close = GETSYSCALL(host, CLOSE);
   1143 		rv = op_close(fd);
   1144 	}
   1145 
   1146 	return rv;
   1147 }
   1148 
   1149 /*
   1150  * write cannot issue a standard debug printf due to recursion
   1151  */
   1152 ssize_t
   1153 write(int fd, const void *buf, size_t blen)
   1154 {
   1155 	ssize_t (*op_write)(int, const void *, size_t);
   1156 
   1157 	if (fd_isrump(fd)) {
   1158 		fd = fd_host2rump(fd);
   1159 		op_write = GETSYSCALL(rump, WRITE);
   1160 	} else {
   1161 		op_write = GETSYSCALL(host, WRITE);
   1162 	}
   1163 
   1164 	return op_write(fd, buf, blen);
   1165 }
   1166 
   1167 /*
   1168  * dup2 is special.  we allow dup2 of a rump kernel fd to 0-2 since
   1169  * many programs do that.  dup2 of a rump kernel fd to another value
   1170  * not >= fdoff is an error.
   1171  *
   1172  * Note: cannot rump2host newd, because it is often hardcoded.
   1173  */
   1174 int
   1175 dup2(int oldd, int newd)
   1176 {
   1177 	int (*host_dup2)(int, int);
   1178 	int rv;
   1179 
   1180 	DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
   1181 
   1182 	if (fd_isrump(oldd)) {
   1183 		int (*op_close)(int) = GETSYSCALL(host, CLOSE);
   1184 
   1185 		/* only allow fd 0-2 for cross-kernel dup */
   1186 		if (!(newd >= 0 && newd <= 2 && !fd_isrump(newd))) {
   1187 			errno = EBADF;
   1188 			return -1;
   1189 		}
   1190 
   1191 		/* regular dup2? */
   1192 		if (fd_isrump(newd)) {
   1193 			newd = fd_host2rump(newd);
   1194 			rv = rump_sys_dup2(oldd, newd);
   1195 			return fd_rump2host(rv);
   1196 		}
   1197 
   1198 		/*
   1199 		 * dup2 rump => host?  just establish an
   1200 		 * entry in the mapping table.
   1201 		 */
   1202 		op_close(newd);
   1203 		setdup2(newd, fd_host2rump(oldd));
   1204 		rv = 0;
   1205 	} else {
   1206 		host_dup2 = syscalls[DUALCALL_DUP2].bs_host;
   1207 		if (rumpclient__closenotify(&newd, RUMPCLIENT_CLOSE_DUP2) == -1)
   1208 			return -1;
   1209 		rv = host_dup2(oldd, newd);
   1210 	}
   1211 
   1212 	return rv;
   1213 }
   1214 
   1215 int
   1216 dup(int oldd)
   1217 {
   1218 
   1219 	return dodup(oldd, 0);
   1220 }
   1221 
   1222 pid_t
   1223 fork()
   1224 {
   1225 	pid_t rv;
   1226 
   1227 	DPRINTF(("fork\n"));
   1228 
   1229 	rv = rumpclient__dofork(host_fork);
   1230 
   1231 	DPRINTF(("fork returns %d\n", rv));
   1232 	return rv;
   1233 }
   1234 /* we do not have the luxury of not requiring a stackframe */
   1235 __strong_alias(__vfork14,fork);
   1236 
   1237 int
   1238 daemon(int nochdir, int noclose)
   1239 {
   1240 	struct rumpclient_fork *rf;
   1241 
   1242 	if ((rf = rumpclient_prefork()) == NULL)
   1243 		return -1;
   1244 
   1245 	if (host_daemon(nochdir, noclose) == -1)
   1246 		return -1;
   1247 
   1248 	if (rumpclient_fork_init(rf) == -1)
   1249 		return -1;
   1250 
   1251 	return 0;
   1252 }
   1253 
   1254 int
   1255 execve(const char *path, char *const argv[], char *const envp[])
   1256 {
   1257 	char buf[128];
   1258 	char *dup2str;
   1259 	const char *pwdinrumpstr;
   1260 	char **newenv;
   1261 	size_t nelem;
   1262 	int rv, sverrno;
   1263 	int bonus = 2, i = 0;
   1264 
   1265 	snprintf(buf, sizeof(buf), "RUMPHIJACK__DUP2INFO=%u,%u,%u",
   1266 	    dup2vec[0], dup2vec[1], dup2vec[2]);
   1267 	dup2str = strdup(buf);
   1268 	if (dup2str == NULL) {
   1269 		errno = ENOMEM;
   1270 		return -1;
   1271 	}
   1272 
   1273 	if (pwdinrump) {
   1274 		pwdinrumpstr = "RUMPHIJACK__PWDINRUMP=true";
   1275 		bonus++;
   1276 	} else {
   1277 		pwdinrumpstr = NULL;
   1278 	}
   1279 
   1280 	for (nelem = 0; envp && envp[nelem]; nelem++)
   1281 		continue;
   1282 	newenv = malloc(sizeof(*newenv) * (nelem+bonus));
   1283 	if (newenv == NULL) {
   1284 		free(dup2str);
   1285 		errno = ENOMEM;
   1286 		return -1;
   1287 	}
   1288 	memcpy(newenv, envp, nelem*sizeof(*newenv));
   1289 	newenv[nelem+i] = dup2str;
   1290 	i++;
   1291 
   1292 	if (pwdinrumpstr) {
   1293 		newenv[nelem+i] = __UNCONST(pwdinrumpstr);
   1294 		i++;
   1295 	}
   1296 	newenv[nelem+i] = NULL;
   1297 	_DIAGASSERT(i < bonus);
   1298 
   1299 	rv = rumpclient_exec(path, argv, newenv);
   1300 
   1301 	_DIAGASSERT(rv != 0);
   1302 	sverrno = errno;
   1303 	free(newenv);
   1304 	free(dup2str);
   1305 	errno = sverrno;
   1306 	return rv;
   1307 }
   1308 
   1309 /*
   1310  * select is done by calling poll.
   1311  */
   1312 int
   1313 REALSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
   1314 	struct timeval *timeout)
   1315 {
   1316 	struct pollfd *pfds;
   1317 	struct timespec ts, *tsp = NULL;
   1318 	nfds_t realnfds;
   1319 	int i, j;
   1320 	int rv, incr;
   1321 
   1322 	DPRINTF(("select\n"));
   1323 
   1324 	/*
   1325 	 * Well, first we must scan the fds to figure out how many
   1326 	 * fds there really are.  This is because up to and including
   1327 	 * nb5 poll() silently refuses nfds > process_maxopen_fds.
   1328 	 * Seems to be fixed in current, thank the maker.
   1329 	 * god damn cluster...bomb.
   1330 	 */
   1331 
   1332 	for (i = 0, realnfds = 0; i < nfds; i++) {
   1333 		if (readfds && FD_ISSET(i, readfds)) {
   1334 			realnfds++;
   1335 			continue;
   1336 		}
   1337 		if (writefds && FD_ISSET(i, writefds)) {
   1338 			realnfds++;
   1339 			continue;
   1340 		}
   1341 		if (exceptfds && FD_ISSET(i, exceptfds)) {
   1342 			realnfds++;
   1343 			continue;
   1344 		}
   1345 	}
   1346 
   1347 	if (realnfds) {
   1348 		pfds = calloc(realnfds, sizeof(*pfds));
   1349 		if (!pfds)
   1350 			return -1;
   1351 	} else {
   1352 		pfds = NULL;
   1353 	}
   1354 
   1355 	for (i = 0, j = 0; i < nfds; i++) {
   1356 		incr = 0;
   1357 		if (readfds && FD_ISSET(i, readfds)) {
   1358 			pfds[j].fd = i;
   1359 			pfds[j].events |= POLLIN;
   1360 			incr=1;
   1361 		}
   1362 		if (writefds && FD_ISSET(i, writefds)) {
   1363 			pfds[j].fd = i;
   1364 			pfds[j].events |= POLLOUT;
   1365 			incr=1;
   1366 		}
   1367 		if (exceptfds && FD_ISSET(i, exceptfds)) {
   1368 			pfds[j].fd = i;
   1369 			pfds[j].events |= POLLHUP|POLLERR;
   1370 			incr=1;
   1371 		}
   1372 		if (incr)
   1373 			j++;
   1374 	}
   1375 	assert(j == (int)realnfds);
   1376 
   1377 	if (timeout) {
   1378 		TIMEVAL_TO_TIMESPEC(timeout, &ts);
   1379 		tsp = &ts;
   1380 	}
   1381 	rv = REALPOLLTS(pfds, realnfds, tsp, NULL);
   1382 	/*
   1383 	 * "If select() returns with an error the descriptor sets
   1384 	 * will be unmodified"
   1385 	 */
   1386 	if (rv < 0)
   1387 		goto out;
   1388 
   1389 	/*
   1390 	 * zero out results (can't use FD_ZERO for the
   1391 	 * obvious select-me-not reason).  whee.
   1392 	 *
   1393 	 * We do this here since some software ignores the return
   1394 	 * value of select, and hence if the timeout expires, it may
   1395 	 * assume all input descriptors have activity.
   1396 	 */
   1397 	for (i = 0; i < nfds; i++) {
   1398 		if (readfds)
   1399 			FD_CLR(i, readfds);
   1400 		if (writefds)
   1401 			FD_CLR(i, writefds);
   1402 		if (exceptfds)
   1403 			FD_CLR(i, exceptfds);
   1404 	}
   1405 	if (rv == 0)
   1406 		goto out;
   1407 
   1408 	/*
   1409 	 * We have >0 fds with activity.  Harvest the results.
   1410 	 */
   1411 	for (i = 0; i < (int)realnfds; i++) {
   1412 		if (readfds) {
   1413 			if (pfds[i].revents & POLLIN) {
   1414 				FD_SET(pfds[i].fd, readfds);
   1415 			}
   1416 		}
   1417 		if (writefds) {
   1418 			if (pfds[i].revents & POLLOUT) {
   1419 				FD_SET(pfds[i].fd, writefds);
   1420 			}
   1421 		}
   1422 		if (exceptfds) {
   1423 			if (pfds[i].revents & (POLLHUP|POLLERR)) {
   1424 				FD_SET(pfds[i].fd, exceptfds);
   1425 			}
   1426 		}
   1427 	}
   1428 
   1429  out:
   1430 	free(pfds);
   1431 	return rv;
   1432 }
   1433 
   1434 static void
   1435 checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
   1436 {
   1437 	nfds_t i;
   1438 
   1439 	for (i = 0; i < nfds; i++) {
   1440 		if (fds[i].fd == -1)
   1441 			continue;
   1442 
   1443 		if (fd_isrump(fds[i].fd))
   1444 			(*rumpcall)++;
   1445 		else
   1446 			(*hostcall)++;
   1447 	}
   1448 }
   1449 
   1450 static void
   1451 adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
   1452 {
   1453 	nfds_t i;
   1454 
   1455 	for (i = 0; i < nfds; i++) {
   1456 		fds[i].fd = fdadj(fds[i].fd);
   1457 	}
   1458 }
   1459 
   1460 /*
   1461  * poll is easy as long as the call comes in the fds only in one
   1462  * kernel.  otherwise its quite tricky...
   1463  */
   1464 struct pollarg {
   1465 	struct pollfd *pfds;
   1466 	nfds_t nfds;
   1467 	const struct timespec *ts;
   1468 	const sigset_t *sigmask;
   1469 	int pipefd;
   1470 	int errnum;
   1471 };
   1472 
   1473 static void *
   1474 hostpoll(void *arg)
   1475 {
   1476 	int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
   1477 			 const sigset_t *);
   1478 	struct pollarg *parg = arg;
   1479 	intptr_t rv;
   1480 
   1481 	op_pollts = GETSYSCALL(host, POLLTS);
   1482 	rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
   1483 	if (rv == -1)
   1484 		parg->errnum = errno;
   1485 	rump_sys_write(parg->pipefd, &rv, sizeof(rv));
   1486 
   1487 	return (void *)(intptr_t)rv;
   1488 }
   1489 
   1490 int
   1491 REALPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
   1492 	const sigset_t *sigmask)
   1493 {
   1494 	int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
   1495 			 const sigset_t *);
   1496 	int (*host_close)(int);
   1497 	int hostcall = 0, rumpcall = 0;
   1498 	pthread_t pt;
   1499 	nfds_t i;
   1500 	int rv;
   1501 
   1502 	DPRINTF(("poll\n"));
   1503 	checkpoll(fds, nfds, &hostcall, &rumpcall);
   1504 
   1505 	if (hostcall && rumpcall) {
   1506 		struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
   1507 		int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
   1508 		struct pollarg parg;
   1509 		uintptr_t lrv;
   1510 		int sverrno = 0, trv;
   1511 
   1512 		/*
   1513 		 * ok, this is where it gets tricky.  We must support
   1514 		 * this since it's a very common operation in certain
   1515 		 * types of software (telnet, netcat, etc).  We allocate
   1516 		 * two vectors and run two poll commands in separate
   1517 		 * threads.  Whichever returns first "wins" and the
   1518 		 * other kernel's fds won't show activity.
   1519 		 */
   1520 		rv = -1;
   1521 
   1522 		/* allocate full vector for O(n) joining after call */
   1523 		pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
   1524 		if (!pfd_host)
   1525 			goto out;
   1526 		pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
   1527 		if (!pfd_rump) {
   1528 			goto out;
   1529 		}
   1530 
   1531 		/*
   1532 		 * then, open two pipes, one for notifications
   1533 		 * to each kernel.
   1534 		 *
   1535 		 * At least the rump pipe should probably be
   1536 		 * cached, along with the helper threads.  This
   1537 		 * should give a microbenchmark improvement (haven't
   1538 		 * experienced a macro-level problem yet, though).
   1539 		 */
   1540 		if ((rv = rump_sys_pipe(rpipe)) == -1) {
   1541 			sverrno = errno;
   1542 		}
   1543 		if (rv == 0 && (rv = pipe(hpipe)) == -1) {
   1544 			sverrno = errno;
   1545 		}
   1546 
   1547 		/* split vectors (or signal errors) */
   1548 		for (i = 0; i < nfds; i++) {
   1549 			int fd;
   1550 
   1551 			fds[i].revents = 0;
   1552 			if (fds[i].fd == -1) {
   1553 				pfd_host[i].fd = -1;
   1554 				pfd_rump[i].fd = -1;
   1555 			} else if (fd_isrump(fds[i].fd)) {
   1556 				pfd_host[i].fd = -1;
   1557 				fd = fd_host2rump(fds[i].fd);
   1558 				if (fd == rpipe[0] || fd == rpipe[1]) {
   1559 					fds[i].revents = POLLNVAL;
   1560 					if (rv != -1)
   1561 						rv++;
   1562 				}
   1563 				pfd_rump[i].fd = fd;
   1564 				pfd_rump[i].events = fds[i].events;
   1565 			} else {
   1566 				pfd_rump[i].fd = -1;
   1567 				fd = fds[i].fd;
   1568 				if (fd == hpipe[0] || fd == hpipe[1]) {
   1569 					fds[i].revents = POLLNVAL;
   1570 					if (rv != -1)
   1571 						rv++;
   1572 				}
   1573 				pfd_host[i].fd = fd;
   1574 				pfd_host[i].events = fds[i].events;
   1575 			}
   1576 			pfd_rump[i].revents = pfd_host[i].revents = 0;
   1577 		}
   1578 		if (rv) {
   1579 			goto out;
   1580 		}
   1581 
   1582 		pfd_host[nfds].fd = hpipe[0];
   1583 		pfd_host[nfds].events = POLLIN;
   1584 		pfd_rump[nfds].fd = rpipe[0];
   1585 		pfd_rump[nfds].events = POLLIN;
   1586 
   1587 		/*
   1588 		 * then, create a thread to do host part and meanwhile
   1589 		 * do rump kernel part right here
   1590 		 */
   1591 
   1592 		parg.pfds = pfd_host;
   1593 		parg.nfds = nfds+1;
   1594 		parg.ts = ts;
   1595 		parg.sigmask = sigmask;
   1596 		parg.pipefd = rpipe[1];
   1597 		pthread_create(&pt, NULL, hostpoll, &parg);
   1598 
   1599 		op_pollts = GETSYSCALL(rump, POLLTS);
   1600 		lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
   1601 		sverrno = errno;
   1602 		write(hpipe[1], &rv, sizeof(rv));
   1603 		pthread_join(pt, (void *)&trv);
   1604 
   1605 		/* check who "won" and merge results */
   1606 		if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
   1607 			rv = trv;
   1608 
   1609 			for (i = 0; i < nfds; i++) {
   1610 				if (pfd_rump[i].fd != -1)
   1611 					fds[i].revents = pfd_rump[i].revents;
   1612 			}
   1613 			sverrno = parg.errnum;
   1614 		} else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
   1615 			rv = trv;
   1616 
   1617 			for (i = 0; i < nfds; i++) {
   1618 				if (pfd_host[i].fd != -1)
   1619 					fds[i].revents = pfd_host[i].revents;
   1620 			}
   1621 		} else {
   1622 			rv = 0;
   1623 		}
   1624 
   1625  out:
   1626 		host_close = GETSYSCALL(host, CLOSE);
   1627 		if (rpipe[0] != -1)
   1628 			rump_sys_close(rpipe[0]);
   1629 		if (rpipe[1] != -1)
   1630 			rump_sys_close(rpipe[1]);
   1631 		if (hpipe[0] != -1)
   1632 			host_close(hpipe[0]);
   1633 		if (hpipe[1] != -1)
   1634 			host_close(hpipe[1]);
   1635 		free(pfd_host);
   1636 		free(pfd_rump);
   1637 		errno = sverrno;
   1638 	} else {
   1639 		if (hostcall) {
   1640 			op_pollts = GETSYSCALL(host, POLLTS);
   1641 		} else {
   1642 			op_pollts = GETSYSCALL(rump, POLLTS);
   1643 			adjustpoll(fds, nfds, fd_host2rump);
   1644 		}
   1645 
   1646 		rv = op_pollts(fds, nfds, ts, sigmask);
   1647 		if (rumpcall)
   1648 			adjustpoll(fds, nfds, fd_rump2host_withdup);
   1649 	}
   1650 
   1651 	return rv;
   1652 }
   1653 
   1654 int
   1655 poll(struct pollfd *fds, nfds_t nfds, int timeout)
   1656 {
   1657 	struct timespec ts;
   1658 	struct timespec *tsp = NULL;
   1659 
   1660 	if (timeout != INFTIM) {
   1661 		ts.tv_sec = timeout / 1000;
   1662 		ts.tv_nsec = (timeout % 1000) * 1000*1000;
   1663 
   1664 		tsp = &ts;
   1665 	}
   1666 
   1667 	return REALPOLLTS(fds, nfds, tsp, NULL);
   1668 }
   1669 
   1670 int
   1671 REALKEVENT(int kq, const struct kevent *changelist, size_t nchanges,
   1672 	struct kevent *eventlist, size_t nevents,
   1673 	const struct timespec *timeout)
   1674 {
   1675 	int (*op_kevent)(int, const struct kevent *, size_t,
   1676 		struct kevent *, size_t, const struct timespec *);
   1677 	const struct kevent *ev;
   1678 	size_t i;
   1679 
   1680 	/*
   1681 	 * Check that we don't attempt to kevent rump kernel fd's.
   1682 	 * That needs similar treatment to select/poll, but is slightly
   1683 	 * trickier since we need to manage to different kq descriptors.
   1684 	 * (TODO, in case you're wondering).
   1685 	 */
   1686 	for (i = 0; i < nchanges; i++) {
   1687 		ev = &changelist[i];
   1688 		if (ev->filter == EVFILT_READ || ev->filter == EVFILT_WRITE ||
   1689 		    ev->filter == EVFILT_VNODE) {
   1690 			if (fd_isrump((int)ev->ident)) {
   1691 				errno = ENOTSUP;
   1692 				return -1;
   1693 			}
   1694 		}
   1695 	}
   1696 
   1697 	op_kevent = GETSYSCALL(host, KEVENT);
   1698 	return op_kevent(kq, changelist, nchanges, eventlist, nevents, timeout);
   1699 }
   1700 
   1701 /*
   1702  * mmapping from a rump kernel is not supported, so disallow it.
   1703  */
   1704 void *
   1705 mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
   1706 {
   1707 
   1708 	if (flags & MAP_FILE && fd_isrump(fd)) {
   1709 		errno = ENOSYS;
   1710 		return MAP_FAILED;
   1711 	}
   1712 	return host_mmap(addr, len, prot, flags, fd, offset);
   1713 }
   1714 
   1715 /*
   1716  * Rest are std type calls.
   1717  */
   1718 
   1719 FDCALL(int, bind, DUALCALL_BIND,					\
   1720 	(int fd, const struct sockaddr *name, socklen_t namelen),	\
   1721 	(int, const struct sockaddr *, socklen_t),			\
   1722 	(fd, name, namelen))
   1723 
   1724 FDCALL(int, connect, DUALCALL_CONNECT,					\
   1725 	(int fd, const struct sockaddr *name, socklen_t namelen),	\
   1726 	(int, const struct sockaddr *, socklen_t),			\
   1727 	(fd, name, namelen))
   1728 
   1729 FDCALL(int, getpeername, DUALCALL_GETPEERNAME,				\
   1730 	(int fd, struct sockaddr *name, socklen_t *namelen),		\
   1731 	(int, struct sockaddr *, socklen_t *),				\
   1732 	(fd, name, namelen))
   1733 
   1734 FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, 				\
   1735 	(int fd, struct sockaddr *name, socklen_t *namelen),		\
   1736 	(int, struct sockaddr *, socklen_t *),				\
   1737 	(fd, name, namelen))
   1738 
   1739 FDCALL(int, listen, DUALCALL_LISTEN,	 				\
   1740 	(int fd, int backlog),						\
   1741 	(int, int),							\
   1742 	(fd, backlog))
   1743 
   1744 FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, 				\
   1745 	(int fd, void *buf, size_t len, int flags,			\
   1746 	    struct sockaddr *from, socklen_t *fromlen),			\
   1747 	(int, void *, size_t, int, struct sockaddr *, socklen_t *),	\
   1748 	(fd, buf, len, flags, from, fromlen))
   1749 
   1750 FDCALL(ssize_t, sendto, DUALCALL_SENDTO, 				\
   1751 	(int fd, const void *buf, size_t len, int flags,		\
   1752 	    const struct sockaddr *to, socklen_t tolen),		\
   1753 	(int, const void *, size_t, int,				\
   1754 	    const struct sockaddr *, socklen_t),			\
   1755 	(fd, buf, len, flags, to, tolen))
   1756 
   1757 FDCALL(ssize_t, recvmsg, DUALCALL_RECVMSG, 				\
   1758 	(int fd, struct msghdr *msg, int flags),			\
   1759 	(int, struct msghdr *, int),					\
   1760 	(fd, msg, flags))
   1761 
   1762 FDCALL(ssize_t, sendmsg, DUALCALL_SENDMSG, 				\
   1763 	(int fd, const struct msghdr *msg, int flags),			\
   1764 	(int, const struct msghdr *, int),				\
   1765 	(fd, msg, flags))
   1766 
   1767 FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, 				\
   1768 	(int fd, int level, int optn, void *optval, socklen_t *optlen),	\
   1769 	(int, int, int, void *, socklen_t *),				\
   1770 	(fd, level, optn, optval, optlen))
   1771 
   1772 FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, 				\
   1773 	(int fd, int level, int optn,					\
   1774 	    const void *optval, socklen_t optlen),			\
   1775 	(int, int, int, const void *, socklen_t),			\
   1776 	(fd, level, optn, optval, optlen))
   1777 
   1778 FDCALL(int, shutdown, DUALCALL_SHUTDOWN, 				\
   1779 	(int fd, int how),						\
   1780 	(int, int),							\
   1781 	(fd, how))
   1782 
   1783 #if _FORTIFY_SOURCE > 0
   1784 #define STUB(fun) __ssp_weak_name(fun)
   1785 ssize_t _sys_readlink(const char * __restrict, char * __restrict, size_t);
   1786 ssize_t
   1787 STUB(readlink)(const char * __restrict path, char * __restrict buf,
   1788     size_t bufsiz)
   1789 {
   1790 	return _sys_readlink(path, buf, bufsiz);
   1791 }
   1792 
   1793 char *_sys_getcwd(char *, size_t);
   1794 char *
   1795 STUB(getcwd)(char *buf, size_t size)
   1796 {
   1797 	return _sys_getcwd(buf, size);
   1798 }
   1799 #else
   1800 #define STUB(fun) fun
   1801 #endif
   1802 
   1803 FDCALL(ssize_t, REALREAD, DUALCALL_READ,				\
   1804 	(int fd, void *buf, size_t buflen),				\
   1805 	(int, void *, size_t),						\
   1806 	(fd, buf, buflen))
   1807 
   1808 FDCALL(ssize_t, readv, DUALCALL_READV, 					\
   1809 	(int fd, const struct iovec *iov, int iovcnt),			\
   1810 	(int, const struct iovec *, int),				\
   1811 	(fd, iov, iovcnt))
   1812 
   1813 FDCALL(ssize_t, REALPREAD, DUALCALL_PREAD,				\
   1814 	(int fd, void *buf, size_t nbytes, off_t offset),		\
   1815 	(int, void *, size_t, off_t),					\
   1816 	(fd, buf, nbytes, offset))
   1817 
   1818 FDCALL(ssize_t, preadv, DUALCALL_PREADV, 				\
   1819 	(int fd, const struct iovec *iov, int iovcnt, off_t offset),	\
   1820 	(int, const struct iovec *, int, off_t),			\
   1821 	(fd, iov, iovcnt, offset))
   1822 
   1823 FDCALL(ssize_t, writev, DUALCALL_WRITEV, 				\
   1824 	(int fd, const struct iovec *iov, int iovcnt),			\
   1825 	(int, const struct iovec *, int),				\
   1826 	(fd, iov, iovcnt))
   1827 
   1828 FDCALL(ssize_t, REALPWRITE, DUALCALL_PWRITE,				\
   1829 	(int fd, const void *buf, size_t nbytes, off_t offset),		\
   1830 	(int, const void *, size_t, off_t),				\
   1831 	(fd, buf, nbytes, offset))
   1832 
   1833 FDCALL(ssize_t, pwritev, DUALCALL_PWRITEV, 				\
   1834 	(int fd, const struct iovec *iov, int iovcnt, off_t offset),	\
   1835 	(int, const struct iovec *, int, off_t),			\
   1836 	(fd, iov, iovcnt, offset))
   1837 
   1838 FDCALL(int, REALFSTAT, DUALCALL_FSTAT,					\
   1839 	(int fd, struct stat *sb),					\
   1840 	(int, struct stat *),						\
   1841 	(fd, sb))
   1842 
   1843 FDCALL(int, fstatvfs1, DUALCALL_FSTATVFS1,				\
   1844 	(int fd, struct statvfs *buf, int flags),			\
   1845 	(int, struct statvfs *, int),					\
   1846 	(fd, buf, flags))
   1847 
   1848 FDCALL(off_t, lseek, DUALCALL_LSEEK,					\
   1849 	(int fd, off_t offset, int whence),				\
   1850 	(int, off_t, int),						\
   1851 	(fd, offset, whence))
   1852 __strong_alias(_lseek,lseek);
   1853 
   1854 FDCALL(int, REALGETDENTS, DUALCALL_GETDENTS,				\
   1855 	(int fd, char *buf, size_t nbytes),				\
   1856 	(int, char *, size_t),						\
   1857 	(fd, buf, nbytes))
   1858 
   1859 FDCALL(int, fchown, DUALCALL_FCHOWN,					\
   1860 	(int fd, uid_t owner, gid_t group),				\
   1861 	(int, uid_t, gid_t),						\
   1862 	(fd, owner, group))
   1863 
   1864 FDCALL(int, fchmod, DUALCALL_FCHMOD,					\
   1865 	(int fd, mode_t mode),						\
   1866 	(int, mode_t),							\
   1867 	(fd, mode))
   1868 
   1869 FDCALL(int, ftruncate, DUALCALL_FTRUNCATE,				\
   1870 	(int fd, off_t length),						\
   1871 	(int, off_t),							\
   1872 	(fd, length))
   1873 
   1874 FDCALL(int, fsync, DUALCALL_FSYNC,					\
   1875 	(int fd),							\
   1876 	(int),								\
   1877 	(fd))
   1878 
   1879 FDCALL(int, fsync_range, DUALCALL_FSYNC_RANGE,				\
   1880 	(int fd, int how, off_t start, off_t length),			\
   1881 	(int, int, off_t, off_t),					\
   1882 	(fd, how, start, length))
   1883 
   1884 FDCALL(int, futimes, DUALCALL_FUTIMES,					\
   1885 	(int fd, const struct timeval *tv),				\
   1886 	(int, const struct timeval *),					\
   1887 	(fd, tv))
   1888 
   1889 FDCALL(int, fchflags, DUALCALL_FCHFLAGS,				\
   1890 	(int fd, u_long flags),						\
   1891 	(int, u_long),							\
   1892 	(fd, flags))
   1893 
   1894 /*
   1895  * path-based selectors
   1896  */
   1897 
   1898 PATHCALL(int, REALSTAT, DUALCALL_STAT,					\
   1899 	(const char *path, struct stat *sb),				\
   1900 	(const char *, struct stat *),					\
   1901 	(path, sb))
   1902 
   1903 PATHCALL(int, REALLSTAT, DUALCALL_LSTAT,				\
   1904 	(const char *path, struct stat *sb),				\
   1905 	(const char *, struct stat *),					\
   1906 	(path, sb))
   1907 
   1908 PATHCALL(int, chown, DUALCALL_CHOWN,					\
   1909 	(const char *path, uid_t owner, gid_t group),			\
   1910 	(const char *, uid_t, gid_t),					\
   1911 	(path, owner, group))
   1912 
   1913 PATHCALL(int, lchown, DUALCALL_LCHOWN,					\
   1914 	(const char *path, uid_t owner, gid_t group),			\
   1915 	(const char *, uid_t, gid_t),					\
   1916 	(path, owner, group))
   1917 
   1918 PATHCALL(int, chmod, DUALCALL_CHMOD,					\
   1919 	(const char *path, mode_t mode),				\
   1920 	(const char *, mode_t),						\
   1921 	(path, mode))
   1922 
   1923 PATHCALL(int, lchmod, DUALCALL_LCHMOD,					\
   1924 	(const char *path, mode_t mode),				\
   1925 	(const char *, mode_t),						\
   1926 	(path, mode))
   1927 
   1928 PATHCALL(int, statvfs1, DUALCALL_STATVFS1,				\
   1929 	(const char *path, struct statvfs *buf, int flags),		\
   1930 	(const char *, struct statvfs *, int),				\
   1931 	(path, buf, flags))
   1932 
   1933 PATHCALL(int, unlink, DUALCALL_UNLINK,					\
   1934 	(const char *path),						\
   1935 	(const char *),							\
   1936 	(path))
   1937 
   1938 PATHCALL(int, symlink, DUALCALL_SYMLINK,				\
   1939 	(const char *target, const char *path),				\
   1940 	(const char *, const char *),					\
   1941 	(target, path))
   1942 
   1943 PATHCALL(ssize_t, readlink, DUALCALL_READLINK,				\
   1944 	(const char *path, char *buf, size_t bufsiz),			\
   1945 	(const char *, char *, size_t),					\
   1946 	(path, buf, bufsiz))
   1947 
   1948 PATHCALL(int, mkdir, DUALCALL_MKDIR,					\
   1949 	(const char *path, mode_t mode),				\
   1950 	(const char *, mode_t),						\
   1951 	(path, mode))
   1952 
   1953 PATHCALL(int, rmdir, DUALCALL_RMDIR,					\
   1954 	(const char *path),						\
   1955 	(const char *),							\
   1956 	(path))
   1957 
   1958 PATHCALL(int, utimes, DUALCALL_UTIMES,					\
   1959 	(const char *path, const struct timeval *tv),			\
   1960 	(const char *, const struct timeval *),				\
   1961 	(path, tv))
   1962 
   1963 PATHCALL(int, lutimes, DUALCALL_LUTIMES,				\
   1964 	(const char *path, const struct timeval *tv),			\
   1965 	(const char *, const struct timeval *),				\
   1966 	(path, tv))
   1967 
   1968 PATHCALL(int, chflags, DUALCALL_CHFLAGS,				\
   1969 	(const char *path, u_long flags),				\
   1970 	(const char *, u_long),						\
   1971 	(path, flags))
   1972 
   1973 PATHCALL(int, lchflags, DUALCALL_LCHFLAGS,				\
   1974 	(const char *path, u_long flags),				\
   1975 	(const char *, u_long),						\
   1976 	(path, flags))
   1977 
   1978 PATHCALL(int, truncate, DUALCALL_TRUNCATE,				\
   1979 	(const char *path, off_t length),				\
   1980 	(const char *, off_t),						\
   1981 	(path, length))
   1982 
   1983 PATHCALL(int, access, DUALCALL_ACCESS,					\
   1984 	(const char *path, int mode),					\
   1985 	(const char *, int),						\
   1986 	(path, mode))
   1987 
   1988 PATHCALL(int, REALMKNOD, DUALCALL_MKNOD,				\
   1989 	(const char *path, mode_t mode, dev_t dev),			\
   1990 	(const char *, mode_t, dev_t),					\
   1991 	(path, mode, dev))
   1992 
   1993 /*
   1994  * Note: with mount the decisive parameter is the mount
   1995  * destination directory.  This is because we don't really know
   1996  * about the "source" directory in a generic call (and besides,
   1997  * it might not even exist, cf. nfs).
   1998  */
   1999 PATHCALL(int, REALMOUNT, DUALCALL_MOUNT,				\
   2000 	(const char *type, const char *path, int flags,			\
   2001 	    void *data, size_t dlen),					\
   2002 	(const char *, const char *, int, void *, size_t),		\
   2003 	(type, path, flags, data, dlen))
   2004 
   2005 PATHCALL(int, unmount, DUALCALL_UNMOUNT,				\
   2006 	(const char *path, int flags),					\
   2007 	(const char *, int),						\
   2008 	(path, flags))
   2009