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