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