Home | History | Annotate | Line # | Download | only in librumphijack
hijack.c revision 1.103
      1 /*      $NetBSD: hijack.c,v 1.103 2013/07/22 12:11:03 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 /* Disable namespace mangling, Fortification is useless here anyway. */
     29 #undef _FORTIFY_SOURCE
     30 
     31 #include "rumpuser_port.h"
     32 
     33 #if !defined(lint)
     34 __RCSID("$NetBSD: hijack.c,v 1.103 2013/07/22 12:11:03 pooka Exp $");
     35 #endif
     36 
     37 #include <sys/param.h>
     38 #include <sys/types.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/mman.h>
     41 #include <sys/mount.h>
     42 #include <sys/poll.h>
     43 #include <sys/socket.h>
     44 #include <sys/stat.h>
     45 #include <sys/statvfs.h>
     46 #include <sys/time.h>
     47 #include <sys/uio.h>
     48 
     49 #ifdef PLATFORM_HAS_KQUEUE
     50 #include <sys/event.h>
     51 #endif
     52 
     53 #ifdef PLATFORM_HAS_NBQUOTA
     54 #include <sys/quotactl.h>
     55 #endif
     56 
     57 #include <assert.h>
     58 #include <dlfcn.h>
     59 #include <err.h>
     60 #include <errno.h>
     61 #include <fcntl.h>
     62 #include <poll.h>
     63 #include <pthread.h>
     64 #include <signal.h>
     65 #include <stdarg.h>
     66 #include <stdbool.h>
     67 #include <stdint.h>
     68 #include <stdio.h>
     69 #include <stdlib.h>
     70 #include <string.h>
     71 #include <time.h>
     72 #include <unistd.h>
     73 
     74 #include <rump/rumpclient.h>
     75 #include <rump/rump_syscalls.h>
     76 
     77 #include "hijack.h"
     78 
     79 /*
     80  * XXX: Consider autogenerating this, syscnames[] and syscalls[] with
     81  * a DSL where the tool also checks the symbols exported by this library
     82  * to make sure all relevant calls are accounted for.
     83  */
     84 enum dualcall {
     85 	DUALCALL_WRITE, DUALCALL_WRITEV, DUALCALL_PWRITE, DUALCALL_PWRITEV,
     86 	DUALCALL_IOCTL, DUALCALL_FCNTL,
     87 	DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_BIND, DUALCALL_CONNECT,
     88 	DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN,
     89 	DUALCALL_RECVFROM, DUALCALL_RECVMSG,
     90 	DUALCALL_SENDTO, DUALCALL_SENDMSG,
     91 	DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT,
     92 	DUALCALL_SHUTDOWN,
     93 	DUALCALL_READ, DUALCALL_READV, DUALCALL_PREAD, DUALCALL_PREADV,
     94 	DUALCALL_DUP2,
     95 	DUALCALL_CLOSE,
     96 	DUALCALL_POLLTS,
     97 
     98 #ifndef __linux__
     99 	DUALCALL_STAT, DUALCALL_LSTAT, DUALCALL_FSTAT,
    100 #endif
    101 
    102 	DUALCALL_CHMOD, DUALCALL_LCHMOD, DUALCALL_FCHMOD,
    103 	DUALCALL_CHOWN, DUALCALL_LCHOWN, DUALCALL_FCHOWN,
    104 	DUALCALL_OPEN,
    105 	DUALCALL_CHDIR, DUALCALL_FCHDIR,
    106 	DUALCALL_LSEEK,
    107 	DUALCALL_UNLINK, DUALCALL_SYMLINK, DUALCALL_READLINK,
    108 	DUALCALL_LINK, DUALCALL_RENAME,
    109 	DUALCALL_MKDIR, DUALCALL_RMDIR,
    110 	DUALCALL_UTIMES, DUALCALL_LUTIMES, DUALCALL_FUTIMES,
    111 	DUALCALL_TRUNCATE, DUALCALL_FTRUNCATE,
    112 	DUALCALL_FSYNC,
    113 	DUALCALL_ACCESS,
    114 
    115 #ifndef __linux__
    116 	DUALCALL___GETCWD,
    117 	DUALCALL_GETDENTS,
    118 #endif
    119 
    120 #ifndef __linux__
    121 	DUALCALL_MKNOD,
    122 #endif
    123 
    124 #ifdef PLATFORM_HAS_NBFILEHANDLE
    125 	DUALCALL_GETFH, DUALCALL_FHOPEN, DUALCALL_FHSTAT, DUALCALL_FHSTATVFS1,
    126 #endif
    127 
    128 #ifdef PLATFORM_HAS_KQUEUE
    129 	DUALCALL_KEVENT,
    130 #endif
    131 
    132 #ifdef PLATFORM_HAS_NBSYSCTL
    133 	DUALCALL___SYSCTL,
    134 #endif
    135 
    136 #ifdef PLATFORM_HAS_NFSSVC
    137 	DUALCALL_NFSSVC,
    138 #endif
    139 
    140 #ifdef PLATFORM_HAS_NBVFSSTAT
    141 	DUALCALL_STATVFS1, DUALCALL_FSTATVFS1, DUALCALL_GETVFSSTAT,
    142 #endif
    143 
    144 #ifdef PLATFORM_HAS_NBMOUNT
    145 	DUALCALL_MOUNT, DUALCALL_UNMOUNT,
    146 #endif
    147 
    148 #ifdef PLATFORM_HAS_FSYNC_RANGE
    149 	DUALCALL_FSYNC_RANGE,
    150 #endif
    151 
    152 #ifdef PLATFORM_HAS_CHFLAGS
    153 	DUALCALL_CHFLAGS, DUALCALL_LCHFLAGS, DUALCALL_FCHFLAGS,
    154 #endif
    155 
    156 #ifdef PLATFORM_HAS_NBQUOTA
    157 	DUALCALL_QUOTACTL,
    158 #endif
    159 	DUALCALL__NUM
    160 };
    161 
    162 #define RSYS_STRING(a) __STRING(a)
    163 #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a))
    164 
    165 /*
    166  * Would be nice to get this automatically in sync with libc.
    167  * Also, this does not work for compat-using binaries (we should
    168  * provide all previous interfaces, not just the current ones)
    169  */
    170 #if defined(__NetBSD__)
    171 
    172 #if !__NetBSD_Prereq__(5,99,7)
    173 #define REALSELECT select
    174 #define REALPOLLTS pollts
    175 #define REALKEVENT kevent
    176 #define REALSTAT __stat30
    177 #define REALLSTAT __lstat30
    178 #define REALFSTAT __fstat30
    179 #define REALUTIMES utimes
    180 #define REALLUTIMES lutimes
    181 #define REALFUTIMES futimes
    182 #define REALMKNOD mknod
    183 #define REALFHSTAT __fhstat40
    184 #else /* >= 5.99.7 */
    185 #define REALSELECT _sys___select50
    186 #define REALPOLLTS _sys___pollts50
    187 #define REALKEVENT _sys___kevent50
    188 #define REALSTAT __stat50
    189 #define REALLSTAT __lstat50
    190 #define REALFSTAT __fstat50
    191 #define REALUTIMES __utimes50
    192 #define REALLUTIMES __lutimes50
    193 #define REALFUTIMES __futimes50
    194 #define REALMKNOD __mknod50
    195 #define REALFHSTAT __fhstat50
    196 #endif /* < 5.99.7 */
    197 
    198 #define REALREAD _sys_read
    199 #define REALPREAD _sys_pread
    200 #define REALPWRITE _sys_pwrite
    201 #define REALGETDENTS __getdents30
    202 #define REALMOUNT __mount50
    203 #define REALGETFH __getfh30
    204 #define REALFHOPEN __fhopen40
    205 #define REALFHSTATVFS1 __fhstatvfs140
    206 #define OLDREALQUOTACTL __quotactl50	/* 5.99.48-62 only */
    207 #define REALSOCKET __socket30
    208 
    209 #define LSEEK_ALIAS _lseek
    210 #define VFORK __vfork14
    211 
    212 int REALSTAT(const char *, struct stat *);
    213 int REALLSTAT(const char *, struct stat *);
    214 int REALFSTAT(int, struct stat *);
    215 int REALMKNOD(const char *, mode_t, dev_t);
    216 int REALGETDENTS(int, char *, size_t);
    217 
    218 int __getcwd(char *, size_t);
    219 
    220 #elif defined(__linux__) /* glibc, really */
    221 
    222 #define REALREAD read
    223 #define REALPREAD pread
    224 #define REALPWRITE pwrite
    225 #define REALSELECT select
    226 #define REALPOLLTS ppoll
    227 #define REALUTIMES utimes
    228 #define REALLUTIMES lutimes
    229 #define REALFUTIMES futimes
    230 #define REALFHSTAT fhstat
    231 #define REALSOCKET socket
    232 
    233 #else /* !NetBSD && !linux */
    234 
    235 #error platform not supported
    236 
    237 #endif /* platform */
    238 
    239 int REALSELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *);
    240 int REALPOLLTS(struct pollfd *, nfds_t,
    241 	       const struct timespec *, const sigset_t *);
    242 int REALKEVENT(int, const struct kevent *, size_t, struct kevent *, size_t,
    243 	       const struct timespec *);
    244 ssize_t REALREAD(int, void *, size_t);
    245 ssize_t REALPREAD(int, void *, size_t, off_t);
    246 ssize_t REALPWRITE(int, const void *, size_t, off_t);
    247 int REALUTIMES(const char *, const struct timeval [2]);
    248 int REALLUTIMES(const char *, const struct timeval [2]);
    249 int REALFUTIMES(int, const struct timeval [2]);
    250 int REALMOUNT(const char *, const char *, int, void *, size_t);
    251 int REALGETFH(const char *, void *, size_t *);
    252 int REALFHOPEN(const void *, size_t, int);
    253 int REALFHSTAT(const void *, size_t, struct stat *);
    254 int REALFHSTATVFS1(const void *, size_t, struct statvfs *, int);
    255 int REALSOCKET(int, int, int);
    256 
    257 #ifdef PLATFORM_HAS_NBQUOTA
    258 int OLDREALQUOTACTL(const char *, struct plistref *);
    259 #endif
    260 
    261 #define S(a) __STRING(a)
    262 struct sysnames {
    263 	enum dualcall scm_callnum;
    264 	const char *scm_hostname;
    265 	const char *scm_rumpname;
    266 } syscnames[] = {
    267 	{ DUALCALL_SOCKET,	S(REALSOCKET),	RSYS_NAME(SOCKET)	},
    268 	{ DUALCALL_ACCEPT,	"accept",	RSYS_NAME(ACCEPT)	},
    269 	{ DUALCALL_BIND,	"bind",		RSYS_NAME(BIND)		},
    270 	{ DUALCALL_CONNECT,	"connect",	RSYS_NAME(CONNECT)	},
    271 	{ DUALCALL_GETPEERNAME,	"getpeername",	RSYS_NAME(GETPEERNAME)	},
    272 	{ DUALCALL_GETSOCKNAME,	"getsockname",	RSYS_NAME(GETSOCKNAME)	},
    273 	{ DUALCALL_LISTEN,	"listen",	RSYS_NAME(LISTEN)	},
    274 	{ DUALCALL_RECVFROM,	"recvfrom",	RSYS_NAME(RECVFROM)	},
    275 	{ DUALCALL_RECVMSG,	"recvmsg",	RSYS_NAME(RECVMSG)	},
    276 	{ DUALCALL_SENDTO,	"sendto",	RSYS_NAME(SENDTO)	},
    277 	{ DUALCALL_SENDMSG,	"sendmsg",	RSYS_NAME(SENDMSG)	},
    278 	{ DUALCALL_GETSOCKOPT,	"getsockopt",	RSYS_NAME(GETSOCKOPT)	},
    279 	{ DUALCALL_SETSOCKOPT,	"setsockopt",	RSYS_NAME(SETSOCKOPT)	},
    280 	{ DUALCALL_SHUTDOWN,	"shutdown",	RSYS_NAME(SHUTDOWN)	},
    281 	{ DUALCALL_READ,	S(REALREAD),	RSYS_NAME(READ)		},
    282 	{ DUALCALL_READV,	"readv",	RSYS_NAME(READV)	},
    283 	{ DUALCALL_PREAD,	S(REALPREAD),	RSYS_NAME(PREAD)	},
    284 	{ DUALCALL_PREADV,	"preadv",	RSYS_NAME(PREADV)	},
    285 	{ DUALCALL_WRITE,	"write",	RSYS_NAME(WRITE)	},
    286 	{ DUALCALL_WRITEV,	"writev",	RSYS_NAME(WRITEV)	},
    287 	{ DUALCALL_PWRITE,	S(REALPWRITE),	RSYS_NAME(PWRITE)	},
    288 	{ DUALCALL_PWRITEV,	"pwritev",	RSYS_NAME(PWRITEV)	},
    289 	{ DUALCALL_IOCTL,	"ioctl",	RSYS_NAME(IOCTL)	},
    290 	{ DUALCALL_FCNTL,	"fcntl",	RSYS_NAME(FCNTL)	},
    291 	{ DUALCALL_DUP2,	"dup2",		RSYS_NAME(DUP2)		},
    292 	{ DUALCALL_CLOSE,	"close",	RSYS_NAME(CLOSE)	},
    293 	{ DUALCALL_POLLTS,	S(REALPOLLTS),	RSYS_NAME(POLLTS)	},
    294 #ifndef __linux__
    295 	{ DUALCALL_STAT,	S(REALSTAT),	RSYS_NAME(STAT)		},
    296 	{ DUALCALL_LSTAT,	S(REALLSTAT),	RSYS_NAME(LSTAT)	},
    297 	{ DUALCALL_FSTAT,	S(REALFSTAT),	RSYS_NAME(FSTAT)	},
    298 #endif
    299 	{ DUALCALL_CHOWN,	"chown",	RSYS_NAME(CHOWN)	},
    300 	{ DUALCALL_LCHOWN,	"lchown",	RSYS_NAME(LCHOWN)	},
    301 	{ DUALCALL_FCHOWN,	"fchown",	RSYS_NAME(FCHOWN)	},
    302 	{ DUALCALL_CHMOD,	"chmod",	RSYS_NAME(CHMOD)	},
    303 	{ DUALCALL_LCHMOD,	"lchmod",	RSYS_NAME(LCHMOD)	},
    304 	{ DUALCALL_FCHMOD,	"fchmod",	RSYS_NAME(FCHMOD)	},
    305 	{ DUALCALL_UTIMES,	S(REALUTIMES),	RSYS_NAME(UTIMES)	},
    306 	{ DUALCALL_LUTIMES,	S(REALLUTIMES),	RSYS_NAME(LUTIMES)	},
    307 	{ DUALCALL_FUTIMES,	S(REALFUTIMES),	RSYS_NAME(FUTIMES)	},
    308 	{ DUALCALL_OPEN,	"open",		RSYS_NAME(OPEN)		},
    309 	{ DUALCALL_CHDIR,	"chdir",	RSYS_NAME(CHDIR)	},
    310 	{ DUALCALL_FCHDIR,	"fchdir",	RSYS_NAME(FCHDIR)	},
    311 	{ DUALCALL_LSEEK,	"lseek",	RSYS_NAME(LSEEK)	},
    312 	{ DUALCALL_UNLINK,	"unlink",	RSYS_NAME(UNLINK)	},
    313 	{ DUALCALL_SYMLINK,	"symlink",	RSYS_NAME(SYMLINK)	},
    314 	{ DUALCALL_READLINK,	"readlink",	RSYS_NAME(READLINK)	},
    315 	{ DUALCALL_LINK,	"link",		RSYS_NAME(LINK)		},
    316 	{ DUALCALL_RENAME,	"rename",	RSYS_NAME(RENAME)	},
    317 	{ DUALCALL_MKDIR,	"mkdir",	RSYS_NAME(MKDIR)	},
    318 	{ DUALCALL_RMDIR,	"rmdir",	RSYS_NAME(RMDIR)	},
    319 	{ DUALCALL_TRUNCATE,	"truncate",	RSYS_NAME(TRUNCATE)	},
    320 	{ DUALCALL_FTRUNCATE,	"ftruncate",	RSYS_NAME(FTRUNCATE)	},
    321 	{ DUALCALL_FSYNC,	"fsync",	RSYS_NAME(FSYNC)	},
    322 	{ DUALCALL_ACCESS,	"access",	RSYS_NAME(ACCESS)	},
    323 
    324 #ifndef __linux__
    325 	{ DUALCALL___GETCWD,	"__getcwd",	RSYS_NAME(__GETCWD)	},
    326 	{ DUALCALL_GETDENTS,	S(REALGETDENTS),RSYS_NAME(GETDENTS)	},
    327 #endif
    328 
    329 #ifndef __linux__
    330 	{ DUALCALL_MKNOD,	S(REALMKNOD),	RSYS_NAME(MKNOD)	},
    331 #endif
    332 
    333 #ifdef PLATFORM_HAS_NBFILEHANDLE
    334 	{ DUALCALL_GETFH,	S(REALGETFH),	RSYS_NAME(GETFH)	},
    335 	{ DUALCALL_FHOPEN,	S(REALFHOPEN),	RSYS_NAME(FHOPEN)	},
    336 	{ DUALCALL_FHSTAT,	S(REALFHSTAT),	RSYS_NAME(FHSTAT)	},
    337 	{ DUALCALL_FHSTATVFS1,	S(REALFHSTATVFS1),RSYS_NAME(FHSTATVFS1)	},
    338 #endif
    339 
    340 #ifdef PLATFORM_HAS_KQUEUE
    341 	{ DUALCALL_KEVENT,	S(REALKEVENT),	RSYS_NAME(KEVENT)	},
    342 #endif
    343 
    344 #ifdef PLATFORM_HAS_NBSYSCTL
    345 	{ DUALCALL___SYSCTL,	"__sysctl",	RSYS_NAME(__SYSCTL)	},
    346 #endif
    347 
    348 #ifdef PLATFORM_HAS_NFSSVC
    349 	{ DUALCALL_NFSSVC,	"nfssvc",	RSYS_NAME(NFSSVC)	},
    350 #endif
    351 
    352 #ifdef PLATFORM_HAS_NBVFSSTAT
    353 	{ DUALCALL_STATVFS1,	"statvfs1",	RSYS_NAME(STATVFS1)	},
    354 	{ DUALCALL_FSTATVFS1,	"fstatvfs1",	RSYS_NAME(FSTATVFS1)	},
    355 	{ DUALCALL_GETVFSSTAT,	"getvfsstat",	RSYS_NAME(GETVFSSTAT)	},
    356 #endif
    357 
    358 #ifdef PLATFORM_HAS_NBMOUNT
    359 	{ DUALCALL_MOUNT,	S(REALMOUNT),	RSYS_NAME(MOUNT)	},
    360 	{ DUALCALL_UNMOUNT,	"unmount",	RSYS_NAME(UNMOUNT)	},
    361 #endif
    362 
    363 #ifdef PLATFORM_HAS_FSYNC_RANGE
    364 	{ DUALCALL_FSYNC_RANGE,	"fsync_range",	RSYS_NAME(FSYNC_RANGE)	},
    365 #endif
    366 
    367 #ifdef PLATFORM_HAS_CHFLAGS
    368 	{ DUALCALL_CHFLAGS,	"chflags",	RSYS_NAME(CHFLAGS)	},
    369 	{ DUALCALL_LCHFLAGS,	"lchflags",	RSYS_NAME(LCHFLAGS)	},
    370 	{ DUALCALL_FCHFLAGS,	"fchflags",	RSYS_NAME(FCHFLAGS)	},
    371 #endif /* PLATFORM_HAS_CHFLAGS */
    372 
    373 #ifdef PLATFORM_HAS_NBQUOTA
    374 #if __NetBSD_Prereq__(5,99,63)
    375 	{ DUALCALL_QUOTACTL,	"__quotactl",	RSYS_NAME(__QUOTACTL)	},
    376 #elif __NetBSD_Prereq__(5,99,48)
    377 	{ DUALCALL_QUOTACTL,	S(OLDREALQUOTACTL),RSYS_NAME(QUOTACTL)	},
    378 #endif
    379 #endif /* PLATFORM_HAS_NBQUOTA */
    380 
    381 };
    382 #undef S
    383 
    384 struct bothsys {
    385 	void *bs_host;
    386 	void *bs_rump;
    387 } syscalls[DUALCALL__NUM];
    388 #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which
    389 
    390 static pid_t	(*host_fork)(void);
    391 static int	(*host_daemon)(int, int);
    392 static void *	(*host_mmap)(void *, size_t, int, int, int, off_t);
    393 
    394 /*
    395  * This tracks if our process is in a subdirectory of /rump.
    396  * It's preserved over exec.
    397  */
    398 static bool pwdinrump;
    399 
    400 enum pathtype { PATH_HOST, PATH_RUMP, PATH_RUMPBLANKET };
    401 
    402 static bool		fd_isrump(int);
    403 static enum pathtype	path_isrump(const char *);
    404 
    405 /* default FD_SETSIZE is 256 ==> default fdoff is 128 */
    406 static int hijack_fdoff = FD_SETSIZE/2;
    407 
    408 /*
    409  * Maintain a mapping table for the usual dup2 suspects.
    410  * Could use atomic ops to operate on dup2vec, but an application
    411  * racing there is not well-defined, so don't bother.
    412  */
    413 /* note: you cannot change this without editing the env-passing code */
    414 #define DUP2HIGH 2
    415 static uint32_t dup2vec[DUP2HIGH+1];
    416 #define DUP2BIT (1<<31)
    417 #define DUP2ALIAS (1<<30)
    418 #define DUP2FDMASK ((1<<30)-1)
    419 
    420 static bool
    421 isdup2d(int fd)
    422 {
    423 
    424 	return fd <= DUP2HIGH && fd >= 0 && dup2vec[fd] & DUP2BIT;
    425 }
    426 
    427 static int
    428 mapdup2(int hostfd)
    429 {
    430 
    431 	_DIAGASSERT(isdup2d(hostfd));
    432 	return dup2vec[hostfd] & DUP2FDMASK;
    433 }
    434 
    435 static int
    436 unmapdup2(int rumpfd)
    437 {
    438 	int i;
    439 
    440 	for (i = 0; i <= DUP2HIGH; i++) {
    441 		if (dup2vec[i] & DUP2BIT &&
    442 		    (dup2vec[i] & DUP2FDMASK) == (unsigned)rumpfd)
    443 			return i;
    444 	}
    445 	return -1;
    446 }
    447 
    448 static void
    449 setdup2(int hostfd, int rumpfd)
    450 {
    451 
    452 	if (hostfd > DUP2HIGH) {
    453 		_DIAGASSERT(0);
    454 		return;
    455 	}
    456 
    457 	dup2vec[hostfd] = DUP2BIT | DUP2ALIAS | rumpfd;
    458 }
    459 
    460 static void
    461 clrdup2(int hostfd)
    462 {
    463 
    464 	if (hostfd > DUP2HIGH) {
    465 		_DIAGASSERT(0);
    466 		return;
    467 	}
    468 
    469 	dup2vec[hostfd] = 0;
    470 }
    471 
    472 static bool
    473 killdup2alias(int rumpfd)
    474 {
    475 	int hostfd;
    476 
    477 	if ((hostfd = unmapdup2(rumpfd)) == -1)
    478 		return false;
    479 
    480 	if (dup2vec[hostfd] & DUP2ALIAS) {
    481 		dup2vec[hostfd] &= ~DUP2ALIAS;
    482 		return true;
    483 	}
    484 	return false;
    485 }
    486 
    487 //#define DEBUGJACK
    488 #ifdef DEBUGJACK
    489 #define DPRINTF(x) mydprintf x
    490 static void
    491 mydprintf(const char *fmt, ...)
    492 {
    493 	va_list ap;
    494 
    495 	if (isdup2d(STDERR_FILENO))
    496 		return;
    497 
    498 	va_start(ap, fmt);
    499 	vfprintf(stderr, fmt, ap);
    500 	va_end(ap);
    501 }
    502 
    503 static const char *
    504 whichfd(int fd)
    505 {
    506 
    507 	if (fd == -1)
    508 		return "-1";
    509 	else if (fd_isrump(fd))
    510 		return "rump";
    511 	else
    512 		return "host";
    513 }
    514 
    515 static const char *
    516 whichpath(const char *path)
    517 {
    518 
    519 	if (path_isrump(path))
    520 		return "rump";
    521 	else
    522 		return "host";
    523 }
    524 
    525 #else
    526 #define DPRINTF(x)
    527 #endif
    528 
    529 #define FDCALL(type, name, rcname, args, proto, vars)			\
    530 type name args								\
    531 {									\
    532 	type (*fun) proto;						\
    533 									\
    534 	DPRINTF(("%s -> %d (%s)\n", __STRING(name), fd,	whichfd(fd)));	\
    535 	if (fd_isrump(fd)) {						\
    536 		fun = syscalls[rcname].bs_rump;				\
    537 		fd = fd_host2rump(fd);					\
    538 	} else {							\
    539 		fun = syscalls[rcname].bs_host;				\
    540 	}								\
    541 									\
    542 	return fun vars;						\
    543 }
    544 
    545 #define PATHCALL(type, name, rcname, args, proto, vars)			\
    546 type name args								\
    547 {									\
    548 	type (*fun) proto;						\
    549 	enum pathtype pt;						\
    550 									\
    551 	DPRINTF(("%s -> %s (%s)\n", __STRING(name), path,		\
    552 	    whichpath(path)));						\
    553 	if ((pt = path_isrump(path)) != PATH_HOST) {			\
    554 		fun = syscalls[rcname].bs_rump;				\
    555 		if (pt == PATH_RUMP)					\
    556 			path = path_host2rump(path);			\
    557 	} else {							\
    558 		fun = syscalls[rcname].bs_host;				\
    559 	}								\
    560 									\
    561 	return fun vars;						\
    562 }
    563 
    564 #define VFSCALL(bit, type, name, rcname, args, proto, vars)		\
    565 type name args								\
    566 {									\
    567 	type (*fun) proto;						\
    568 									\
    569 	DPRINTF(("%s (0x%x, 0x%x)\n", __STRING(name), bit, vfsbits));	\
    570 	if (vfsbits & bit) {						\
    571 		fun = syscalls[rcname].bs_rump;				\
    572 	} else {							\
    573 		fun = syscalls[rcname].bs_host;				\
    574 	}								\
    575 									\
    576 	return fun vars;						\
    577 }
    578 
    579 /*
    580  * These variables are set from the RUMPHIJACK string and control
    581  * which operations can product rump kernel file descriptors.
    582  * This should be easily extendable for future needs.
    583  */
    584 #define RUMPHIJACK_DEFAULT "path=/rump,socket=all:nolocal"
    585 static bool rumpsockets[PF_MAX];
    586 static const char *rumpprefix;
    587 static size_t rumpprefixlen;
    588 
    589 static struct {
    590 	int pf;
    591 	const char *name;
    592 } socketmap[] = {
    593 	{ PF_LOCAL, "local" },
    594 	{ PF_INET, "inet" },
    595 #ifdef PF_LINK
    596 	{ PF_LINK, "link" },
    597 #endif
    598 #ifdef PF_OROUTE
    599 	{ PF_OROUTE, "oroute" },
    600 #endif
    601 	{ PF_ROUTE, "route" },
    602 	{ PF_INET6, "inet6" },
    603 #ifdef PF_MPLS
    604 	{ PF_MPLS, "mpls" },
    605 #endif
    606 	{ -1, NULL }
    607 };
    608 
    609 static void
    610 sockparser(char *buf)
    611 {
    612 	char *p, *l = NULL;
    613 	bool value;
    614 	int i;
    615 
    616 	/* if "all" is present, it must be specified first */
    617 	if (strncmp(buf, "all", strlen("all")) == 0) {
    618 		for (i = 0; i < (int)__arraycount(rumpsockets); i++) {
    619 			rumpsockets[i] = true;
    620 		}
    621 		buf += strlen("all");
    622 		if (*buf == ':')
    623 			buf++;
    624 	}
    625 
    626 	for (p = strtok_r(buf, ":", &l); p; p = strtok_r(NULL, ":", &l)) {
    627 		value = true;
    628 		if (strncmp(p, "no", strlen("no")) == 0) {
    629 			value = false;
    630 			p += strlen("no");
    631 		}
    632 
    633 		for (i = 0; socketmap[i].name; i++) {
    634 			if (strcmp(p, socketmap[i].name) == 0) {
    635 				rumpsockets[socketmap[i].pf] = value;
    636 				break;
    637 			}
    638 		}
    639 		if (socketmap[i].name == NULL) {
    640 			errx(1, "invalid socket specifier %s", p);
    641 		}
    642 	}
    643 }
    644 
    645 static void
    646 pathparser(char *buf)
    647 {
    648 
    649 	/* sanity-check */
    650 	if (*buf != '/')
    651 		errx(1, "hijack path specifier must begin with ``/''");
    652 	rumpprefixlen = strlen(buf);
    653 	if (rumpprefixlen < 2)
    654 		errx(1, "invalid hijack prefix: %s", buf);
    655 	if (buf[rumpprefixlen-1] == '/' && strspn(buf, "/") != rumpprefixlen)
    656 		errx(1, "hijack prefix may end in slash only if pure "
    657 		    "slash, gave %s", buf);
    658 
    659 	if ((rumpprefix = strdup(buf)) == NULL)
    660 		err(1, "strdup");
    661 	rumpprefixlen = strlen(rumpprefix);
    662 }
    663 
    664 static struct blanket {
    665 	const char *pfx;
    666 	size_t len;
    667 } *blanket;
    668 static int nblanket;
    669 
    670 static void
    671 blanketparser(char *buf)
    672 {
    673 	char *p, *l = NULL;
    674 	int i;
    675 
    676 	for (nblanket = 0, p = buf; p; p = strchr(p+1, ':'), nblanket++)
    677 		continue;
    678 
    679 	blanket = malloc(nblanket * sizeof(*blanket));
    680 	if (blanket == NULL)
    681 		err(1, "alloc blanket %d", nblanket);
    682 
    683 	for (p = strtok_r(buf, ":", &l), i = 0; p;
    684 	    p = strtok_r(NULL, ":", &l), i++) {
    685 		blanket[i].pfx = strdup(p);
    686 		if (blanket[i].pfx == NULL)
    687 			err(1, "strdup blanket");
    688 		blanket[i].len = strlen(p);
    689 
    690 		if (blanket[i].len == 0 || *blanket[i].pfx != '/')
    691 			errx(1, "invalid blanket specifier %s", p);
    692 		if (*(blanket[i].pfx + blanket[i].len-1) == '/')
    693 			errx(1, "invalid blanket specifier %s", p);
    694 	}
    695 }
    696 
    697 #define VFSBIT_NFSSVC		0x01
    698 #define VFSBIT_GETVFSSTAT	0x02
    699 #define VFSBIT_FHCALLS		0x04
    700 static unsigned vfsbits;
    701 
    702 static struct {
    703 	int bit;
    704 	const char *name;
    705 } vfscalls[] = {
    706 	{ VFSBIT_NFSSVC, "nfssvc" },
    707 	{ VFSBIT_GETVFSSTAT, "getvfsstat" },
    708 	{ VFSBIT_FHCALLS, "fhcalls" },
    709 	{ -1, NULL }
    710 };
    711 
    712 static void
    713 vfsparser(char *buf)
    714 {
    715 	char *p, *l = NULL;
    716 	bool turnon;
    717 	unsigned int fullmask;
    718 	int i;
    719 
    720 	/* build the full mask and sanity-check while we're at it */
    721 	fullmask = 0;
    722 	for (i = 0; vfscalls[i].name != NULL; i++) {
    723 		if (fullmask & vfscalls[i].bit)
    724 			errx(1, "problem exists between vi and chair");
    725 		fullmask |= vfscalls[i].bit;
    726 	}
    727 
    728 
    729 	/* if "all" is present, it must be specified first */
    730 	if (strncmp(buf, "all", strlen("all")) == 0) {
    731 		vfsbits = fullmask;
    732 		buf += strlen("all");
    733 		if (*buf == ':')
    734 			buf++;
    735 	}
    736 
    737 	for (p = strtok_r(buf, ":", &l); p; p = strtok_r(NULL, ":", &l)) {
    738 		turnon = true;
    739 		if (strncmp(p, "no", strlen("no")) == 0) {
    740 			turnon = false;
    741 			p += strlen("no");
    742 		}
    743 
    744 		for (i = 0; vfscalls[i].name; i++) {
    745 			if (strcmp(p, vfscalls[i].name) == 0) {
    746 				if (turnon)
    747 					vfsbits |= vfscalls[i].bit;
    748 				else
    749 					vfsbits &= ~vfscalls[i].bit;
    750 				break;
    751 			}
    752 		}
    753 		if (vfscalls[i].name == NULL) {
    754 			errx(1, "invalid vfscall specifier %s", p);
    755 		}
    756 	}
    757 }
    758 
    759 static bool rumpsysctl = false;
    760 
    761 static void
    762 sysctlparser(char *buf)
    763 {
    764 
    765 	if (buf == NULL) {
    766 		rumpsysctl = true;
    767 		return;
    768 	}
    769 
    770 	if (strcasecmp(buf, "y") == 0 || strcasecmp(buf, "yes") == 0 ||
    771 	    strcasecmp(buf, "yep") == 0 || strcasecmp(buf, "tottakai") == 0) {
    772 		rumpsysctl = true;
    773 		return;
    774 	}
    775 	if (strcasecmp(buf, "n") == 0 || strcasecmp(buf, "no") == 0) {
    776 		rumpsysctl = false;
    777 		return;
    778 	}
    779 
    780 	errx(1, "sysctl value should be y(es)/n(o), gave: %s", buf);
    781 }
    782 
    783 static void
    784 fdoffparser(char *buf)
    785 {
    786 	unsigned long fdoff;
    787 	char *ep;
    788 
    789 	if (*buf == '-') {
    790 		errx(1, "fdoff must not be negative");
    791 	}
    792 	fdoff = strtoul(buf, &ep, 10);
    793 	if (*ep != '\0')
    794 		errx(1, "invalid fdoff specifier \"%s\"", buf);
    795 	if (fdoff >= INT_MAX/2 || fdoff < 3)
    796 		errx(1, "fdoff out of range");
    797 	hijack_fdoff = fdoff;
    798 }
    799 
    800 static struct {
    801 	void (*parsefn)(char *);
    802 	const char *name;
    803 	bool needvalues;
    804 } hijackparse[] = {
    805 	{ sockparser, "socket", true },
    806 	{ pathparser, "path", true },
    807 	{ blanketparser, "blanket", true },
    808 	{ vfsparser, "vfs", true },
    809 	{ sysctlparser, "sysctl", false },
    810 	{ fdoffparser, "fdoff", true },
    811 	{ NULL, NULL, false },
    812 };
    813 
    814 static void
    815 parsehijack(char *hijack)
    816 {
    817 	char *p, *p2, *l;
    818 	const char *hijackcopy;
    819 	bool nop2;
    820 	int i;
    821 
    822 	if ((hijackcopy = strdup(hijack)) == NULL)
    823 		err(1, "strdup");
    824 
    825 	/* disable everything explicitly */
    826 	for (i = 0; i < PF_MAX; i++)
    827 		rumpsockets[i] = false;
    828 
    829 	for (p = strtok_r(hijack, ",", &l); p; p = strtok_r(NULL, ",", &l)) {
    830 		nop2 = false;
    831 		p2 = strchr(p, '=');
    832 		if (!p2) {
    833 			nop2 = true;
    834 			p2 = p + strlen(p);
    835 		}
    836 
    837 		for (i = 0; hijackparse[i].parsefn; i++) {
    838 			if (strncmp(hijackparse[i].name, p,
    839 			    (size_t)(p2-p)) == 0) {
    840 				if (nop2 && hijackparse[i].needvalues)
    841 					errx(1, "invalid hijack specifier: %s",
    842 					    hijackcopy);
    843 				hijackparse[i].parsefn(nop2 ? NULL : p2+1);
    844 				break;
    845 			}
    846 		}
    847 
    848 		if (hijackparse[i].parsefn == NULL)
    849 			errx(1, "invalid hijack specifier name in %s", p);
    850 	}
    851 
    852 }
    853 
    854 static void __attribute__((constructor))
    855 rcinit(void)
    856 {
    857 	char buf[1024];
    858 	unsigned i, j;
    859 
    860 	host_fork = dlsym(RTLD_NEXT, "fork");
    861 	host_daemon = dlsym(RTLD_NEXT, "daemon");
    862 	host_mmap = dlsym(RTLD_NEXT, "mmap");
    863 
    864 	/*
    865 	 * In theory cannot print anything during lookups because
    866 	 * we might not have the call vector set up.  so, the errx()
    867 	 * is a bit of a strech, but it might work.
    868 	 */
    869 
    870 	for (i = 0; i < DUALCALL__NUM; i++) {
    871 		/* build runtime O(1) access */
    872 		for (j = 0; j < __arraycount(syscnames); j++) {
    873 			if (syscnames[j].scm_callnum == i)
    874 				break;
    875 		}
    876 
    877 		if (j == __arraycount(syscnames))
    878 			errx(1, "rumphijack error: syscall pos %d missing", i);
    879 
    880 		syscalls[i].bs_host = dlsym(RTLD_NEXT,
    881 		    syscnames[j].scm_hostname);
    882 		if (syscalls[i].bs_host == NULL)
    883 			errx(1, "hostcall %s not found!",
    884 			    syscnames[j].scm_hostname);
    885 
    886 		syscalls[i].bs_rump = dlsym(RTLD_NEXT,
    887 		    syscnames[j].scm_rumpname);
    888 		if (syscalls[i].bs_rump == NULL)
    889 			errx(1, "rumpcall %s not found!",
    890 			    syscnames[j].scm_rumpname);
    891 	}
    892 
    893 	if (rumpclient_init() == -1)
    894 		err(1, "rumpclient init");
    895 
    896 	/* check which syscalls we're supposed to hijack */
    897 	if (getenv_r("RUMPHIJACK", buf, sizeof(buf)) == -1) {
    898 		strcpy(buf, RUMPHIJACK_DEFAULT);
    899 	}
    900 	parsehijack(buf);
    901 
    902 	/* set client persistence level */
    903 	if (getenv_r("RUMPHIJACK_RETRYCONNECT", buf, sizeof(buf)) != -1) {
    904 		if (strcmp(buf, "die") == 0)
    905 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_DIE);
    906 		else if (strcmp(buf, "inftime") == 0)
    907 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME);
    908 		else if (strcmp(buf, "once") == 0)
    909 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_ONCE);
    910 		else {
    911 			time_t timeout;
    912 			char *ep;
    913 
    914 			timeout = (time_t)strtoll(buf, &ep, 10);
    915 			if (timeout <= 0 || ep != buf + strlen(buf))
    916 				errx(1, "RUMPHIJACK_RETRYCONNECT must be "
    917 				    "keyword or integer, got: %s", buf);
    918 
    919 			rumpclient_setconnretry(timeout);
    920 		}
    921 	}
    922 
    923 	if (getenv_r("RUMPHIJACK__DUP2INFO", buf, sizeof(buf)) == 0) {
    924 		if (sscanf(buf, "%u,%u,%u",
    925 		    &dup2vec[0], &dup2vec[1], &dup2vec[2]) != 3) {
    926 			warnx("invalid dup2mask: %s", buf);
    927 			memset(dup2vec, 0, sizeof(dup2vec));
    928 		}
    929 		unsetenv("RUMPHIJACK__DUP2INFO");
    930 	}
    931 	if (getenv_r("RUMPHIJACK__PWDINRUMP", buf, sizeof(buf)) == 0) {
    932 		pwdinrump = true;
    933 		unsetenv("RUMPHIJACK__PWDINRUMP");
    934 	}
    935 }
    936 
    937 static int
    938 fd_rump2host(int fd)
    939 {
    940 
    941 	if (fd == -1)
    942 		return fd;
    943 	return fd + hijack_fdoff;
    944 }
    945 
    946 static int
    947 fd_rump2host_withdup(int fd)
    948 {
    949 	int hfd;
    950 
    951 	_DIAGASSERT(fd != -1);
    952 	hfd = unmapdup2(fd);
    953 	if (hfd != -1) {
    954 		_DIAGASSERT(hfd <= DUP2HIGH);
    955 		return hfd;
    956 	}
    957 	return fd_rump2host(fd);
    958 }
    959 
    960 static int
    961 fd_host2rump(int fd)
    962 {
    963 
    964 	if (!isdup2d(fd))
    965 		return fd - hijack_fdoff;
    966 	else
    967 		return mapdup2(fd);
    968 }
    969 
    970 static bool
    971 fd_isrump(int fd)
    972 {
    973 
    974 	return isdup2d(fd) || fd >= hijack_fdoff;
    975 }
    976 
    977 #define assertfd(_fd_) assert(ISDUP2D(_fd_) || (_fd_) >= hijack_fdoff)
    978 
    979 static enum pathtype
    980 path_isrump(const char *path)
    981 {
    982 	size_t plen;
    983 	int i;
    984 
    985 	if (rumpprefix == NULL && nblanket == 0)
    986 		return PATH_HOST;
    987 
    988 	if (*path == '/') {
    989 		plen = strlen(path);
    990 		if (rumpprefix && plen >= rumpprefixlen) {
    991 			if (strncmp(path, rumpprefix, rumpprefixlen) == 0
    992 			    && (plen == rumpprefixlen
    993 			      || *(path + rumpprefixlen) == '/')) {
    994 				return PATH_RUMP;
    995 			}
    996 		}
    997 		for (i = 0; i < nblanket; i++) {
    998 			if (strncmp(path, blanket[i].pfx, blanket[i].len) == 0)
    999 				return PATH_RUMPBLANKET;
   1000 		}
   1001 
   1002 		return PATH_HOST;
   1003 	} else {
   1004 		return pwdinrump ? PATH_RUMP : PATH_HOST;
   1005 	}
   1006 }
   1007 
   1008 static const char *rootpath = "/";
   1009 static const char *
   1010 path_host2rump(const char *path)
   1011 {
   1012 	const char *rv;
   1013 
   1014 	if (*path == '/') {
   1015 		rv = path + rumpprefixlen;
   1016 		if (*rv == '\0')
   1017 			rv = rootpath;
   1018 	} else {
   1019 		rv = path;
   1020 	}
   1021 
   1022 	return rv;
   1023 }
   1024 
   1025 static int
   1026 dodup(int oldd, int minfd)
   1027 {
   1028 	int (*op_fcntl)(int, int, ...);
   1029 	int newd;
   1030 	int isrump;
   1031 
   1032 	DPRINTF(("dup -> %d (minfd %d)\n", oldd, minfd));
   1033 	if (fd_isrump(oldd)) {
   1034 		op_fcntl = GETSYSCALL(rump, FCNTL);
   1035 		oldd = fd_host2rump(oldd);
   1036 		if (minfd >= hijack_fdoff)
   1037 			minfd -= hijack_fdoff;
   1038 		isrump = 1;
   1039 	} else {
   1040 		op_fcntl = GETSYSCALL(host, FCNTL);
   1041 		isrump = 0;
   1042 	}
   1043 
   1044 	newd = op_fcntl(oldd, F_DUPFD, minfd);
   1045 
   1046 	if (isrump)
   1047 		newd = fd_rump2host(newd);
   1048 	DPRINTF(("dup <- %d\n", newd));
   1049 
   1050 	return newd;
   1051 }
   1052 
   1053 /*
   1054  * Check that host fd value does not exceed fdoffset and if necessary
   1055  * dup the file descriptor so that it doesn't collide with the dup2mask.
   1056  */
   1057 static int
   1058 fd_host2host(int fd)
   1059 {
   1060 	int (*op_fcntl)(int, int, ...) = GETSYSCALL(host, FCNTL);
   1061 	int (*op_close)(int) = GETSYSCALL(host, CLOSE);
   1062 	int ofd, i;
   1063 
   1064 	if (fd >= hijack_fdoff) {
   1065 		op_close(fd);
   1066 		errno = ENFILE;
   1067 		return -1;
   1068 	}
   1069 
   1070 	for (i = 1; isdup2d(fd); i++) {
   1071 		ofd = fd;
   1072 		fd = op_fcntl(ofd, F_DUPFD, i);
   1073 		op_close(ofd);
   1074 	}
   1075 
   1076 	return fd;
   1077 }
   1078 
   1079 int
   1080 open(const char *path, int flags, ...)
   1081 {
   1082 	int (*op_open)(const char *, int, ...);
   1083 	bool isrump;
   1084 	va_list ap;
   1085 	enum pathtype pt;
   1086 	int fd;
   1087 
   1088 	DPRINTF(("open -> %s (%s)\n", path, whichpath(path)));
   1089 
   1090 	if ((pt = path_isrump(path)) != PATH_HOST) {
   1091 		if (pt == PATH_RUMP)
   1092 			path = path_host2rump(path);
   1093 		op_open = GETSYSCALL(rump, OPEN);
   1094 		isrump = true;
   1095 	} else {
   1096 		op_open = GETSYSCALL(host, OPEN);
   1097 		isrump = false;
   1098 	}
   1099 
   1100 	va_start(ap, flags);
   1101 	fd = op_open(path, flags, va_arg(ap, mode_t));
   1102 	va_end(ap);
   1103 
   1104 	if (isrump)
   1105 		fd = fd_rump2host(fd);
   1106 	else
   1107 		fd = fd_host2host(fd);
   1108 
   1109 	DPRINTF(("open <- %d (%s)\n", fd, whichfd(fd)));
   1110 	return fd;
   1111 }
   1112 
   1113 int
   1114 chdir(const char *path)
   1115 {
   1116 	int (*op_chdir)(const char *);
   1117 	enum pathtype pt;
   1118 	int rv;
   1119 
   1120 	if ((pt = path_isrump(path)) != PATH_HOST) {
   1121 		op_chdir = GETSYSCALL(rump, CHDIR);
   1122 		if (pt == PATH_RUMP)
   1123 			path = path_host2rump(path);
   1124 	} else {
   1125 		op_chdir = GETSYSCALL(host, CHDIR);
   1126 	}
   1127 
   1128 	rv = op_chdir(path);
   1129 	if (rv == 0)
   1130 		pwdinrump = pt != PATH_HOST;
   1131 
   1132 	return rv;
   1133 }
   1134 
   1135 int
   1136 fchdir(int fd)
   1137 {
   1138 	int (*op_fchdir)(int);
   1139 	bool isrump;
   1140 	int rv;
   1141 
   1142 	if (fd_isrump(fd)) {
   1143 		op_fchdir = GETSYSCALL(rump, FCHDIR);
   1144 		isrump = true;
   1145 		fd = fd_host2rump(fd);
   1146 	} else {
   1147 		op_fchdir = GETSYSCALL(host, FCHDIR);
   1148 		isrump = false;
   1149 	}
   1150 
   1151 	rv = op_fchdir(fd);
   1152 	if (rv == 0) {
   1153 		pwdinrump = isrump;
   1154 	}
   1155 
   1156 	return rv;
   1157 }
   1158 
   1159 #ifndef __linux__
   1160 int
   1161 __getcwd(char *bufp, size_t len)
   1162 {
   1163 	int (*op___getcwd)(char *, size_t);
   1164 	size_t prefixgap;
   1165 	bool iamslash;
   1166 	int rv;
   1167 
   1168 	if (pwdinrump && rumpprefix) {
   1169 		if (rumpprefix[rumpprefixlen-1] == '/')
   1170 			iamslash = true;
   1171 		else
   1172 			iamslash = false;
   1173 
   1174 		if (iamslash)
   1175 			prefixgap = rumpprefixlen - 1; /* ``//+path'' */
   1176 		else
   1177 			prefixgap = rumpprefixlen; /* ``/pfx+/path'' */
   1178 		if (len <= prefixgap) {
   1179 			errno = ERANGE;
   1180 			return -1;
   1181 		}
   1182 
   1183 		op___getcwd = GETSYSCALL(rump, __GETCWD);
   1184 		rv = op___getcwd(bufp + prefixgap, len - prefixgap);
   1185 		if (rv == -1)
   1186 			return rv;
   1187 
   1188 		/* augment the "/" part only for a non-root path */
   1189 		memcpy(bufp, rumpprefix, rumpprefixlen);
   1190 
   1191 		/* append / only to non-root cwd */
   1192 		if (rv != 2)
   1193 			bufp[prefixgap] = '/';
   1194 
   1195 		/* don't append extra slash in the purely-slash case */
   1196 		if (rv == 2 && !iamslash)
   1197 			bufp[rumpprefixlen] = '\0';
   1198 	} else if (pwdinrump) {
   1199 		/* assume blanket.  we can't provide a prefix here */
   1200 		op___getcwd = GETSYSCALL(rump, __GETCWD);
   1201 		rv = op___getcwd(bufp, len);
   1202 	} else {
   1203 		op___getcwd = GETSYSCALL(host, __GETCWD);
   1204 		rv = op___getcwd(bufp, len);
   1205 	}
   1206 
   1207 	return rv;
   1208 }
   1209 #endif
   1210 
   1211 static int
   1212 moveish(const char *from, const char *to,
   1213     int (*rump_op)(const char *, const char *),
   1214     int (*host_op)(const char *, const char *))
   1215 {
   1216 	int (*op)(const char *, const char *);
   1217 	enum pathtype ptf, ptt;
   1218 
   1219 	if ((ptf = path_isrump(from)) != PATH_HOST) {
   1220 		if ((ptt = path_isrump(to)) == PATH_HOST) {
   1221 			errno = EXDEV;
   1222 			return -1;
   1223 		}
   1224 
   1225 		if (ptf == PATH_RUMP)
   1226 			from = path_host2rump(from);
   1227 		if (ptt == PATH_RUMP)
   1228 			to = path_host2rump(to);
   1229 		op = rump_op;
   1230 	} else {
   1231 		if (path_isrump(to) != PATH_HOST) {
   1232 			errno = EXDEV;
   1233 			return -1;
   1234 		}
   1235 
   1236 		op = host_op;
   1237 	}
   1238 
   1239 	return op(from, to);
   1240 }
   1241 
   1242 int
   1243 link(const char *from, const char *to)
   1244 {
   1245 	return moveish(from, to,
   1246 	    GETSYSCALL(rump, LINK), GETSYSCALL(host, LINK));
   1247 }
   1248 
   1249 int
   1250 rename(const char *from, const char *to)
   1251 {
   1252 	return moveish(from, to,
   1253 	    GETSYSCALL(rump, RENAME), GETSYSCALL(host, RENAME));
   1254 }
   1255 
   1256 int
   1257 REALSOCKET(int domain, int type, int protocol)
   1258 {
   1259 	int (*op_socket)(int, int, int);
   1260 	int fd;
   1261 	bool isrump;
   1262 
   1263 	isrump = domain < PF_MAX && rumpsockets[domain];
   1264 
   1265 	if (isrump)
   1266 		op_socket = GETSYSCALL(rump, SOCKET);
   1267 	else
   1268 		op_socket = GETSYSCALL(host, SOCKET);
   1269 	fd = op_socket(domain, type, protocol);
   1270 
   1271 	if (isrump)
   1272 		fd = fd_rump2host(fd);
   1273 	else
   1274 		fd = fd_host2host(fd);
   1275 	DPRINTF(("socket <- %d\n", fd));
   1276 
   1277 	return fd;
   1278 }
   1279 
   1280 int
   1281 accept(int s, struct sockaddr *addr, socklen_t *addrlen)
   1282 {
   1283 	int (*op_accept)(int, struct sockaddr *, socklen_t *);
   1284 	int fd;
   1285 	bool isrump;
   1286 
   1287 	isrump = fd_isrump(s);
   1288 
   1289 	DPRINTF(("accept -> %d", s));
   1290 	if (isrump) {
   1291 		op_accept = GETSYSCALL(rump, ACCEPT);
   1292 		s = fd_host2rump(s);
   1293 	} else {
   1294 		op_accept = GETSYSCALL(host, ACCEPT);
   1295 	}
   1296 	fd = op_accept(s, addr, addrlen);
   1297 	if (fd != -1 && isrump)
   1298 		fd = fd_rump2host(fd);
   1299 	else
   1300 		fd = fd_host2host(fd);
   1301 
   1302 	DPRINTF((" <- %d\n", fd));
   1303 
   1304 	return fd;
   1305 }
   1306 
   1307 /*
   1308  * ioctl() and fcntl() are varargs calls and need special treatment.
   1309  */
   1310 
   1311 /*
   1312  * Various [Linux] libc's have various signatures for ioctl so we
   1313  * need to handle the discrepancies.  On NetBSD, we use the
   1314  * one with unsigned long cmd.
   1315  */
   1316 int
   1317 #ifdef HAVE_IOCTL_CMD_INT
   1318 ioctl(int fd, int cmd, ...)
   1319 {
   1320 	int (*op_ioctl)(int, int cmd, ...);
   1321 #else
   1322 ioctl(int fd, unsigned long cmd, ...)
   1323 {
   1324 	int (*op_ioctl)(int, unsigned long cmd, ...);
   1325 #endif
   1326 	va_list ap;
   1327 	int rv;
   1328 
   1329 	DPRINTF(("ioctl -> %d\n", fd));
   1330 	if (fd_isrump(fd)) {
   1331 		fd = fd_host2rump(fd);
   1332 		op_ioctl = GETSYSCALL(rump, IOCTL);
   1333 	} else {
   1334 		op_ioctl = GETSYSCALL(host, IOCTL);
   1335 	}
   1336 
   1337 	va_start(ap, cmd);
   1338 	rv = op_ioctl(fd, cmd, va_arg(ap, void *));
   1339 	va_end(ap);
   1340 	return rv;
   1341 }
   1342 
   1343 int
   1344 fcntl(int fd, int cmd, ...)
   1345 {
   1346 	int (*op_fcntl)(int, int, ...);
   1347 	va_list ap;
   1348 	int rv, minfd;
   1349 
   1350 	DPRINTF(("fcntl -> %d (cmd %d)\n", fd, cmd));
   1351 
   1352 	switch (cmd) {
   1353 	case F_DUPFD:
   1354 		va_start(ap, cmd);
   1355 		minfd = va_arg(ap, int);
   1356 		va_end(ap);
   1357 		return dodup(fd, minfd);
   1358 
   1359 #ifdef F_CLOSEM
   1360 	case F_CLOSEM: {
   1361 		int maxdup2, i;
   1362 
   1363 		/*
   1364 		 * So, if fd < HIJACKOFF, we want to do a host closem.
   1365 		 */
   1366 
   1367 		if (fd < hijack_fdoff) {
   1368 			int closemfd = fd;
   1369 
   1370 			if (rumpclient__closenotify(&closemfd,
   1371 			    RUMPCLIENT_CLOSE_FCLOSEM) == -1)
   1372 				return -1;
   1373 			op_fcntl = GETSYSCALL(host, FCNTL);
   1374 			rv = op_fcntl(closemfd, cmd);
   1375 			if (rv)
   1376 				return rv;
   1377 		}
   1378 
   1379 		/*
   1380 		 * Additionally, we want to do a rump closem, but only
   1381 		 * for the file descriptors not dup2'd.
   1382 		 */
   1383 
   1384 		for (i = 0, maxdup2 = 0; i <= DUP2HIGH; i++) {
   1385 			if (dup2vec[i] & DUP2BIT) {
   1386 				int val;
   1387 
   1388 				val = dup2vec[i] & DUP2FDMASK;
   1389 				maxdup2 = MAX(val, maxdup2);
   1390 			}
   1391 		}
   1392 
   1393 		if (fd >= hijack_fdoff)
   1394 			fd -= hijack_fdoff;
   1395 		else
   1396 			fd = 0;
   1397 		fd = MAX(maxdup2+1, fd);
   1398 
   1399 		/* hmm, maybe we should close rump fd's not within dup2mask? */
   1400 		return rump_sys_fcntl(fd, F_CLOSEM);
   1401 	}
   1402 #endif /* F_CLOSEM */
   1403 
   1404 #ifdef F_MAXFD
   1405 	case F_MAXFD:
   1406 		/*
   1407 		 * For maxfd, if there's a rump kernel fd, return
   1408 		 * it hostified.  Otherwise, return host's MAXFD
   1409 		 * return value.
   1410 		 */
   1411 		if ((rv = rump_sys_fcntl(fd, F_MAXFD)) != -1) {
   1412 			/*
   1413 			 * This might go a little wrong in case
   1414 			 * of dup2 to [012], but I'm not sure if
   1415 			 * there's a justification for tracking
   1416 			 * that info.  Consider e.g.
   1417 			 * dup2(rumpfd, 2) followed by rump_sys_open()
   1418 			 * returning 1.  We should return 1+HIJACKOFF,
   1419 			 * not 2+HIJACKOFF.  However, if [01] is not
   1420 			 * open, the correct return value is 2.
   1421 			 */
   1422 			return fd_rump2host(fd);
   1423 		} else {
   1424 			op_fcntl = GETSYSCALL(host, FCNTL);
   1425 			return op_fcntl(fd, F_MAXFD);
   1426 		}
   1427 		/*NOTREACHED*/
   1428 #endif /* F_MAXFD */
   1429 
   1430 	default:
   1431 		if (fd_isrump(fd)) {
   1432 			fd = fd_host2rump(fd);
   1433 			op_fcntl = GETSYSCALL(rump, FCNTL);
   1434 		} else {
   1435 			op_fcntl = GETSYSCALL(host, FCNTL);
   1436 		}
   1437 
   1438 		va_start(ap, cmd);
   1439 		rv = op_fcntl(fd, cmd, va_arg(ap, void *));
   1440 		va_end(ap);
   1441 		return rv;
   1442 	}
   1443 	/*NOTREACHED*/
   1444 }
   1445 
   1446 int
   1447 close(int fd)
   1448 {
   1449 	int (*op_close)(int);
   1450 	int rv;
   1451 
   1452 	DPRINTF(("close -> %d\n", fd));
   1453 	if (fd_isrump(fd)) {
   1454 		bool undup2 = false;
   1455 		int ofd;
   1456 
   1457 		if (isdup2d(ofd = fd)) {
   1458 			undup2 = true;
   1459 		}
   1460 
   1461 		fd = fd_host2rump(fd);
   1462 		if (!undup2 && killdup2alias(fd)) {
   1463 			return 0;
   1464 		}
   1465 
   1466 		op_close = GETSYSCALL(rump, CLOSE);
   1467 		rv = op_close(fd);
   1468 		if (rv == 0 && undup2) {
   1469 			clrdup2(ofd);
   1470 		}
   1471 	} else {
   1472 		if (rumpclient__closenotify(&fd, RUMPCLIENT_CLOSE_CLOSE) == -1)
   1473 			return -1;
   1474 		op_close = GETSYSCALL(host, CLOSE);
   1475 		rv = op_close(fd);
   1476 	}
   1477 
   1478 	return rv;
   1479 }
   1480 
   1481 /*
   1482  * write cannot issue a standard debug printf due to recursion
   1483  */
   1484 ssize_t
   1485 write(int fd, const void *buf, size_t blen)
   1486 {
   1487 	ssize_t (*op_write)(int, const void *, size_t);
   1488 
   1489 	if (fd_isrump(fd)) {
   1490 		fd = fd_host2rump(fd);
   1491 		op_write = GETSYSCALL(rump, WRITE);
   1492 	} else {
   1493 		op_write = GETSYSCALL(host, WRITE);
   1494 	}
   1495 
   1496 	return op_write(fd, buf, blen);
   1497 }
   1498 
   1499 /*
   1500  * file descriptor passing
   1501  *
   1502  * we intercept sendmsg and recvmsg to convert file descriptors in
   1503  * control messages.  an attempt to send a descriptor from a different kernel
   1504  * is rejected.  (ENOTSUP)
   1505  */
   1506 
   1507 static int
   1508 msg_convert(struct msghdr *msg, int (*func)(int))
   1509 {
   1510 	struct cmsghdr *cmsg;
   1511 
   1512 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
   1513 	    cmsg = CMSG_NXTHDR(msg, cmsg)) {
   1514 		if (cmsg->cmsg_level == SOL_SOCKET &&
   1515 		    cmsg->cmsg_type == SCM_RIGHTS) {
   1516 			int *fdp = (void *)CMSG_DATA(cmsg);
   1517 			const size_t size =
   1518 			    cmsg->cmsg_len - __CMSG_ALIGN(sizeof(*cmsg));
   1519 			const int nfds = (int)(size / sizeof(int));
   1520 			const int * const efdp = fdp + nfds;
   1521 
   1522 			while (fdp < efdp) {
   1523 				const int newval = func(*fdp);
   1524 
   1525 				if (newval < 0) {
   1526 					return ENOTSUP;
   1527 				}
   1528 				*fdp = newval;
   1529 				fdp++;
   1530 			}
   1531 		}
   1532 	}
   1533 	return 0;
   1534 }
   1535 
   1536 ssize_t
   1537 recvmsg(int fd, struct msghdr *msg, int flags)
   1538 {
   1539 	ssize_t (*op_recvmsg)(int, struct msghdr *, int);
   1540 	ssize_t ret;
   1541 	const bool isrump = fd_isrump(fd);
   1542 
   1543 	if (isrump) {
   1544 		fd = fd_host2rump(fd);
   1545 		op_recvmsg = GETSYSCALL(rump, RECVMSG);
   1546 	} else {
   1547 		op_recvmsg = GETSYSCALL(host, RECVMSG);
   1548 	}
   1549 	ret = op_recvmsg(fd, msg, flags);
   1550 	if (ret == -1) {
   1551 		return ret;
   1552 	}
   1553 	/*
   1554 	 * convert descriptors in the message.
   1555 	 */
   1556 	if (isrump) {
   1557 		msg_convert(msg, fd_rump2host);
   1558 	} else {
   1559 		msg_convert(msg, fd_host2host);
   1560 	}
   1561 	return ret;
   1562 }
   1563 
   1564 ssize_t
   1565 recv(int fd, void *buf, size_t len, int flags)
   1566 {
   1567 
   1568 	return recvfrom(fd, buf, len, flags, NULL, NULL);
   1569 }
   1570 
   1571 ssize_t
   1572 send(int fd, const void *buf, size_t len, int flags)
   1573 {
   1574 
   1575 	return sendto(fd, buf, len, flags, NULL, 0);
   1576 }
   1577 
   1578 static int
   1579 fd_check_rump(int fd)
   1580 {
   1581 
   1582 	return fd_isrump(fd) ? 0 : -1;
   1583 }
   1584 
   1585 static int
   1586 fd_check_host(int fd)
   1587 {
   1588 
   1589 	return !fd_isrump(fd) ? 0 : -1;
   1590 }
   1591 
   1592 ssize_t
   1593 sendmsg(int fd, const struct msghdr *msg, int flags)
   1594 {
   1595 	ssize_t (*op_sendmsg)(int, const struct msghdr *, int);
   1596 	const bool isrump = fd_isrump(fd);
   1597 	int error;
   1598 
   1599 	/*
   1600 	 * reject descriptors from a different kernel.
   1601 	 */
   1602 	error = msg_convert(__UNCONST(msg),
   1603 	    isrump ? fd_check_rump: fd_check_host);
   1604 	if (error != 0) {
   1605 		errno = error;
   1606 		return -1;
   1607 	}
   1608 	/*
   1609 	 * convert descriptors in the message to raw values.
   1610 	 */
   1611 	if (isrump) {
   1612 		fd = fd_host2rump(fd);
   1613 		/*
   1614 		 * XXX we directly modify the given message assuming:
   1615 		 * - cmsg is writable (typically on caller's stack)
   1616 		 * - caller don't care cmsg's contents after calling sendmsg.
   1617 		 *   (thus no need to restore values)
   1618 		 *
   1619 		 * it's safer to copy and modify instead.
   1620 		 */
   1621 		msg_convert(__UNCONST(msg), fd_host2rump);
   1622 		op_sendmsg = GETSYSCALL(rump, SENDMSG);
   1623 	} else {
   1624 		op_sendmsg = GETSYSCALL(host, SENDMSG);
   1625 	}
   1626 	return op_sendmsg(fd, msg, flags);
   1627 }
   1628 
   1629 /*
   1630  * dup2 is special.  we allow dup2 of a rump kernel fd to 0-2 since
   1631  * many programs do that.  dup2 of a rump kernel fd to another value
   1632  * not >= fdoff is an error.
   1633  *
   1634  * Note: cannot rump2host newd, because it is often hardcoded.
   1635  */
   1636 int
   1637 dup2(int oldd, int newd)
   1638 {
   1639 	int (*host_dup2)(int, int);
   1640 	int rv;
   1641 
   1642 	DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
   1643 
   1644 	if (fd_isrump(oldd)) {
   1645 		int (*op_close)(int) = GETSYSCALL(host, CLOSE);
   1646 
   1647 		/* only allow fd 0-2 for cross-kernel dup */
   1648 		if (!(newd >= 0 && newd <= 2 && !fd_isrump(newd))) {
   1649 			errno = EBADF;
   1650 			return -1;
   1651 		}
   1652 
   1653 		/* regular dup2? */
   1654 		if (fd_isrump(newd)) {
   1655 			newd = fd_host2rump(newd);
   1656 			rv = rump_sys_dup2(oldd, newd);
   1657 			return fd_rump2host(rv);
   1658 		}
   1659 
   1660 		/*
   1661 		 * dup2 rump => host?  just establish an
   1662 		 * entry in the mapping table.
   1663 		 */
   1664 		op_close(newd);
   1665 		setdup2(newd, fd_host2rump(oldd));
   1666 		rv = 0;
   1667 	} else {
   1668 		host_dup2 = syscalls[DUALCALL_DUP2].bs_host;
   1669 		if (rumpclient__closenotify(&newd, RUMPCLIENT_CLOSE_DUP2) == -1)
   1670 			return -1;
   1671 		rv = host_dup2(oldd, newd);
   1672 	}
   1673 
   1674 	return rv;
   1675 }
   1676 
   1677 int
   1678 dup(int oldd)
   1679 {
   1680 
   1681 	return dodup(oldd, 0);
   1682 }
   1683 
   1684 pid_t
   1685 fork(void)
   1686 {
   1687 	pid_t rv;
   1688 
   1689 	DPRINTF(("fork\n"));
   1690 
   1691 	rv = rumpclient__dofork(host_fork);
   1692 
   1693 	DPRINTF(("fork returns %d\n", rv));
   1694 	return rv;
   1695 }
   1696 #ifdef VFORK
   1697 /* we do not have the luxury of not requiring a stackframe */
   1698 __strong_alias(VFORK,fork);
   1699 #endif
   1700 
   1701 int
   1702 daemon(int nochdir, int noclose)
   1703 {
   1704 	struct rumpclient_fork *rf;
   1705 
   1706 	if ((rf = rumpclient_prefork()) == NULL)
   1707 		return -1;
   1708 
   1709 	if (host_daemon(nochdir, noclose) == -1)
   1710 		return -1;
   1711 
   1712 	if (rumpclient_fork_init(rf) == -1)
   1713 		return -1;
   1714 
   1715 	return 0;
   1716 }
   1717 
   1718 int
   1719 execve(const char *path, char *const argv[], char *const envp[])
   1720 {
   1721 	char buf[128];
   1722 	char *dup2str;
   1723 	const char *pwdinrumpstr;
   1724 	char **newenv;
   1725 	size_t nelem;
   1726 	int rv, sverrno;
   1727 	int bonus = 2, i = 0;
   1728 
   1729 	snprintf(buf, sizeof(buf), "RUMPHIJACK__DUP2INFO=%u,%u,%u",
   1730 	    dup2vec[0], dup2vec[1], dup2vec[2]);
   1731 	dup2str = strdup(buf);
   1732 	if (dup2str == NULL) {
   1733 		errno = ENOMEM;
   1734 		return -1;
   1735 	}
   1736 
   1737 	if (pwdinrump) {
   1738 		pwdinrumpstr = "RUMPHIJACK__PWDINRUMP=true";
   1739 		bonus++;
   1740 	} else {
   1741 		pwdinrumpstr = NULL;
   1742 	}
   1743 
   1744 	for (nelem = 0; envp && envp[nelem]; nelem++)
   1745 		continue;
   1746 	newenv = malloc(sizeof(*newenv) * (nelem+bonus));
   1747 	if (newenv == NULL) {
   1748 		free(dup2str);
   1749 		errno = ENOMEM;
   1750 		return -1;
   1751 	}
   1752 	memcpy(newenv, envp, nelem*sizeof(*newenv));
   1753 	newenv[nelem+i] = dup2str;
   1754 	i++;
   1755 
   1756 	if (pwdinrumpstr) {
   1757 		newenv[nelem+i] = __UNCONST(pwdinrumpstr);
   1758 		i++;
   1759 	}
   1760 	newenv[nelem+i] = NULL;
   1761 	_DIAGASSERT(i < bonus);
   1762 
   1763 	rv = rumpclient_exec(path, argv, newenv);
   1764 
   1765 	_DIAGASSERT(rv != 0);
   1766 	sverrno = errno;
   1767 	free(newenv);
   1768 	free(dup2str);
   1769 	errno = sverrno;
   1770 	return rv;
   1771 }
   1772 
   1773 /*
   1774  * select is done by calling poll.
   1775  */
   1776 int
   1777 REALSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
   1778 	struct timeval *timeout)
   1779 {
   1780 	struct pollfd *pfds;
   1781 	struct timespec ts, *tsp = NULL;
   1782 	nfds_t realnfds;
   1783 	int i, j;
   1784 	int rv, incr;
   1785 
   1786 	DPRINTF(("select %d %p %p %p %p\n", nfds,
   1787 	    readfds, writefds, exceptfds, timeout));
   1788 
   1789 	/*
   1790 	 * Well, first we must scan the fds to figure out how many
   1791 	 * fds there really are.  This is because up to and including
   1792 	 * nb5 poll() silently refuses nfds > process_maxopen_fds.
   1793 	 * Seems to be fixed in current, thank the maker.
   1794 	 * god damn cluster...bomb.
   1795 	 */
   1796 
   1797 	for (i = 0, realnfds = 0; i < nfds; i++) {
   1798 		if (readfds && FD_ISSET(i, readfds)) {
   1799 			realnfds++;
   1800 			continue;
   1801 		}
   1802 		if (writefds && FD_ISSET(i, writefds)) {
   1803 			realnfds++;
   1804 			continue;
   1805 		}
   1806 		if (exceptfds && FD_ISSET(i, exceptfds)) {
   1807 			realnfds++;
   1808 			continue;
   1809 		}
   1810 	}
   1811 
   1812 	if (realnfds) {
   1813 		pfds = calloc(realnfds, sizeof(*pfds));
   1814 		if (!pfds)
   1815 			return -1;
   1816 	} else {
   1817 		pfds = NULL;
   1818 	}
   1819 
   1820 	for (i = 0, j = 0; i < nfds; i++) {
   1821 		incr = 0;
   1822 		if (readfds && FD_ISSET(i, readfds)) {
   1823 			pfds[j].fd = i;
   1824 			pfds[j].events |= POLLIN;
   1825 			incr=1;
   1826 		}
   1827 		if (writefds && FD_ISSET(i, writefds)) {
   1828 			pfds[j].fd = i;
   1829 			pfds[j].events |= POLLOUT;
   1830 			incr=1;
   1831 		}
   1832 		if (exceptfds && FD_ISSET(i, exceptfds)) {
   1833 			pfds[j].fd = i;
   1834 			pfds[j].events |= POLLHUP|POLLERR;
   1835 			incr=1;
   1836 		}
   1837 		if (incr)
   1838 			j++;
   1839 	}
   1840 	assert(j == (int)realnfds);
   1841 
   1842 	if (timeout) {
   1843 		TIMEVAL_TO_TIMESPEC(timeout, &ts);
   1844 		tsp = &ts;
   1845 	}
   1846 	rv = REALPOLLTS(pfds, realnfds, tsp, NULL);
   1847 	/*
   1848 	 * "If select() returns with an error the descriptor sets
   1849 	 * will be unmodified"
   1850 	 */
   1851 	if (rv < 0)
   1852 		goto out;
   1853 
   1854 	/*
   1855 	 * zero out results (can't use FD_ZERO for the
   1856 	 * obvious select-me-not reason).  whee.
   1857 	 *
   1858 	 * We do this here since some software ignores the return
   1859 	 * value of select, and hence if the timeout expires, it may
   1860 	 * assume all input descriptors have activity.
   1861 	 */
   1862 	for (i = 0; i < nfds; i++) {
   1863 		if (readfds)
   1864 			FD_CLR(i, readfds);
   1865 		if (writefds)
   1866 			FD_CLR(i, writefds);
   1867 		if (exceptfds)
   1868 			FD_CLR(i, exceptfds);
   1869 	}
   1870 	if (rv == 0)
   1871 		goto out;
   1872 
   1873 	/*
   1874 	 * We have >0 fds with activity.  Harvest the results.
   1875 	 */
   1876 	for (i = 0; i < (int)realnfds; i++) {
   1877 		if (readfds) {
   1878 			if (pfds[i].revents & POLLIN) {
   1879 				FD_SET(pfds[i].fd, readfds);
   1880 			}
   1881 		}
   1882 		if (writefds) {
   1883 			if (pfds[i].revents & POLLOUT) {
   1884 				FD_SET(pfds[i].fd, writefds);
   1885 			}
   1886 		}
   1887 		if (exceptfds) {
   1888 			if (pfds[i].revents & (POLLHUP|POLLERR)) {
   1889 				FD_SET(pfds[i].fd, exceptfds);
   1890 			}
   1891 		}
   1892 	}
   1893 
   1894  out:
   1895 	free(pfds);
   1896 	return rv;
   1897 }
   1898 
   1899 static void
   1900 checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
   1901 {
   1902 	nfds_t i;
   1903 
   1904 	for (i = 0; i < nfds; i++) {
   1905 		if (fds[i].fd == -1)
   1906 			continue;
   1907 
   1908 		if (fd_isrump(fds[i].fd))
   1909 			(*rumpcall)++;
   1910 		else
   1911 			(*hostcall)++;
   1912 	}
   1913 }
   1914 
   1915 static void
   1916 adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
   1917 {
   1918 	nfds_t i;
   1919 
   1920 	for (i = 0; i < nfds; i++) {
   1921 		fds[i].fd = fdadj(fds[i].fd);
   1922 	}
   1923 }
   1924 
   1925 /*
   1926  * poll is easy as long as the call comes in the fds only in one
   1927  * kernel.  otherwise its quite tricky...
   1928  */
   1929 struct pollarg {
   1930 	struct pollfd *pfds;
   1931 	nfds_t nfds;
   1932 	const struct timespec *ts;
   1933 	const sigset_t *sigmask;
   1934 	int pipefd;
   1935 	int errnum;
   1936 };
   1937 
   1938 static void *
   1939 hostpoll(void *arg)
   1940 {
   1941 	int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
   1942 			 const sigset_t *);
   1943 	struct pollarg *parg = arg;
   1944 	intptr_t rv;
   1945 
   1946 	op_pollts = GETSYSCALL(host, POLLTS);
   1947 	rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
   1948 	if (rv == -1)
   1949 		parg->errnum = errno;
   1950 	rump_sys_write(parg->pipefd, &rv, sizeof(rv));
   1951 
   1952 	return (void *)rv;
   1953 }
   1954 
   1955 int
   1956 REALPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
   1957 	const sigset_t *sigmask)
   1958 {
   1959 	int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
   1960 			 const sigset_t *);
   1961 	int (*host_close)(int);
   1962 	int hostcall = 0, rumpcall = 0;
   1963 	pthread_t pt;
   1964 	nfds_t i;
   1965 	int rv;
   1966 
   1967 	DPRINTF(("poll %p %d %p %p\n", fds, (int)nfds, ts, sigmask));
   1968 	checkpoll(fds, nfds, &hostcall, &rumpcall);
   1969 
   1970 	if (hostcall && rumpcall) {
   1971 		struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
   1972 		int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
   1973 		struct pollarg parg;
   1974 		void *trv_val;
   1975 		int sverrno = 0, rv_rump, rv_host, errno_rump, errno_host;
   1976 
   1977 		/*
   1978 		 * ok, this is where it gets tricky.  We must support
   1979 		 * this since it's a very common operation in certain
   1980 		 * types of software (telnet, netcat, etc).  We allocate
   1981 		 * two vectors and run two poll commands in separate
   1982 		 * threads.  Whichever returns first "wins" and the
   1983 		 * other kernel's fds won't show activity.
   1984 		 */
   1985 		rv = -1;
   1986 
   1987 		/* allocate full vector for O(n) joining after call */
   1988 		pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
   1989 		if (!pfd_host)
   1990 			goto out;
   1991 		pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
   1992 		if (!pfd_rump) {
   1993 			goto out;
   1994 		}
   1995 
   1996 		/*
   1997 		 * then, open two pipes, one for notifications
   1998 		 * to each kernel.
   1999 		 *
   2000 		 * At least the rump pipe should probably be
   2001 		 * cached, along with the helper threads.  This
   2002 		 * should give a microbenchmark improvement (haven't
   2003 		 * experienced a macro-level problem yet, though).
   2004 		 */
   2005 		if ((rv = rump_sys_pipe(rpipe)) == -1) {
   2006 			sverrno = errno;
   2007 		}
   2008 		if (rv == 0 && (rv = pipe(hpipe)) == -1) {
   2009 			sverrno = errno;
   2010 		}
   2011 
   2012 		/* split vectors (or signal errors) */
   2013 		for (i = 0; i < nfds; i++) {
   2014 			int fd;
   2015 
   2016 			fds[i].revents = 0;
   2017 			if (fds[i].fd == -1) {
   2018 				pfd_host[i].fd = -1;
   2019 				pfd_rump[i].fd = -1;
   2020 			} else if (fd_isrump(fds[i].fd)) {
   2021 				pfd_host[i].fd = -1;
   2022 				fd = fd_host2rump(fds[i].fd);
   2023 				if (fd == rpipe[0] || fd == rpipe[1]) {
   2024 					fds[i].revents = POLLNVAL;
   2025 					if (rv != -1)
   2026 						rv++;
   2027 				}
   2028 				pfd_rump[i].fd = fd;
   2029 				pfd_rump[i].events = fds[i].events;
   2030 			} else {
   2031 				pfd_rump[i].fd = -1;
   2032 				fd = fds[i].fd;
   2033 				if (fd == hpipe[0] || fd == hpipe[1]) {
   2034 					fds[i].revents = POLLNVAL;
   2035 					if (rv != -1)
   2036 						rv++;
   2037 				}
   2038 				pfd_host[i].fd = fd;
   2039 				pfd_host[i].events = fds[i].events;
   2040 			}
   2041 			pfd_rump[i].revents = pfd_host[i].revents = 0;
   2042 		}
   2043 		if (rv) {
   2044 			goto out;
   2045 		}
   2046 
   2047 		pfd_host[nfds].fd = hpipe[0];
   2048 		pfd_host[nfds].events = POLLIN;
   2049 		pfd_rump[nfds].fd = rpipe[0];
   2050 		pfd_rump[nfds].events = POLLIN;
   2051 
   2052 		/*
   2053 		 * then, create a thread to do host part and meanwhile
   2054 		 * do rump kernel part right here
   2055 		 */
   2056 
   2057 		parg.pfds = pfd_host;
   2058 		parg.nfds = nfds+1;
   2059 		parg.ts = ts;
   2060 		parg.sigmask = sigmask;
   2061 		parg.pipefd = rpipe[1];
   2062 		pthread_create(&pt, NULL, hostpoll, &parg);
   2063 
   2064 		op_pollts = GETSYSCALL(rump, POLLTS);
   2065 		rv_rump = op_pollts(pfd_rump, nfds+1, ts, NULL);
   2066 		errno_rump = errno;
   2067 		write(hpipe[1], &rv, sizeof(rv));
   2068 		pthread_join(pt, &trv_val);
   2069 		rv_host = (int)(intptr_t)trv_val;
   2070 		errno_host = parg.errnum;
   2071 
   2072 		/* strip cross-thread notification from real results */
   2073 		if (pfd_host[nfds].revents & POLLIN) {
   2074 			assert((pfd_rump[nfds].revents & POLLIN) == 0);
   2075 			assert(rv_host > 0);
   2076 			rv_host--;
   2077 		}
   2078 		if (pfd_rump[nfds].revents & POLLIN) {
   2079 			assert((pfd_host[nfds].revents & POLLIN) == 0);
   2080 			assert(rv_rump > 0);
   2081 			rv_rump--;
   2082 		}
   2083 
   2084 		/* then merge the results into what's reported to the caller */
   2085 		if (rv_rump > 0 || rv_host > 0) {
   2086 			/* SUCCESS */
   2087 
   2088 			rv = 0;
   2089 			if (rv_rump > 0) {
   2090 				for (i = 0; i < nfds; i++) {
   2091 					if (pfd_rump[i].fd != -1)
   2092 						fds[i].revents
   2093 						    = pfd_rump[i].revents;
   2094 				}
   2095 				rv += rv_rump;
   2096 			}
   2097 			if (rv_host > 0) {
   2098 				for (i = 0; i < nfds; i++) {
   2099 					if (pfd_host[i].fd != -1)
   2100 						fds[i].revents
   2101 						    = pfd_host[i].revents;
   2102 				}
   2103 				rv += rv_host;
   2104 			}
   2105 			assert(rv > 0);
   2106 			sverrno = 0;
   2107 		} else if (rv_rump == -1 || rv_host == -1) {
   2108 			/* ERROR */
   2109 
   2110 			/* just pick one kernel at "random" */
   2111 			rv = -1;
   2112 			if (rv_host == -1) {
   2113 				sverrno = errno_host;
   2114 			} else if (rv_rump == -1) {
   2115 				sverrno = errno_rump;
   2116 			}
   2117 		} else {
   2118 			/* TIMEOUT */
   2119 
   2120 			rv = 0;
   2121 			assert(rv_rump == 0 && rv_host == 0);
   2122 		}
   2123 
   2124  out:
   2125 		host_close = GETSYSCALL(host, CLOSE);
   2126 		if (rpipe[0] != -1)
   2127 			rump_sys_close(rpipe[0]);
   2128 		if (rpipe[1] != -1)
   2129 			rump_sys_close(rpipe[1]);
   2130 		if (hpipe[0] != -1)
   2131 			host_close(hpipe[0]);
   2132 		if (hpipe[1] != -1)
   2133 			host_close(hpipe[1]);
   2134 		free(pfd_host);
   2135 		free(pfd_rump);
   2136 		errno = sverrno;
   2137 	} else {
   2138 		if (hostcall) {
   2139 			op_pollts = GETSYSCALL(host, POLLTS);
   2140 		} else {
   2141 			op_pollts = GETSYSCALL(rump, POLLTS);
   2142 			adjustpoll(fds, nfds, fd_host2rump);
   2143 		}
   2144 
   2145 		rv = op_pollts(fds, nfds, ts, sigmask);
   2146 		if (rumpcall)
   2147 			adjustpoll(fds, nfds, fd_rump2host_withdup);
   2148 	}
   2149 
   2150 	return rv;
   2151 }
   2152 
   2153 int
   2154 poll(struct pollfd *fds, nfds_t nfds, int timeout)
   2155 {
   2156 	struct timespec ts;
   2157 	struct timespec *tsp = NULL;
   2158 
   2159 	if (timeout != INFTIM) {
   2160 		ts.tv_sec = timeout / 1000;
   2161 		ts.tv_nsec = (timeout % 1000) * 1000*1000;
   2162 
   2163 		tsp = &ts;
   2164 	}
   2165 
   2166 	return REALPOLLTS(fds, nfds, tsp, NULL);
   2167 }
   2168 
   2169 #ifdef PLATFORM_HAS_KQUEUE
   2170 int
   2171 REALKEVENT(int kq, const struct kevent *changelist, size_t nchanges,
   2172 	struct kevent *eventlist, size_t nevents,
   2173 	const struct timespec *timeout)
   2174 {
   2175 	int (*op_kevent)(int, const struct kevent *, size_t,
   2176 		struct kevent *, size_t, const struct timespec *);
   2177 	const struct kevent *ev;
   2178 	size_t i;
   2179 
   2180 	/*
   2181 	 * Check that we don't attempt to kevent rump kernel fd's.
   2182 	 * That needs similar treatment to select/poll, but is slightly
   2183 	 * trickier since we need to manage to different kq descriptors.
   2184 	 * (TODO, in case you're wondering).
   2185 	 */
   2186 	for (i = 0; i < nchanges; i++) {
   2187 		ev = &changelist[i];
   2188 		if (ev->filter == EVFILT_READ || ev->filter == EVFILT_WRITE ||
   2189 		    ev->filter == EVFILT_VNODE) {
   2190 			if (fd_isrump((int)ev->ident)) {
   2191 				errno = ENOTSUP;
   2192 				return -1;
   2193 			}
   2194 		}
   2195 	}
   2196 
   2197 	op_kevent = GETSYSCALL(host, KEVENT);
   2198 	return op_kevent(kq, changelist, nchanges, eventlist, nevents, timeout);
   2199 }
   2200 #endif /* PLATFORM_HAS_KQUEUE */
   2201 
   2202 /*
   2203  * mmapping from a rump kernel is not supported, so disallow it.
   2204  */
   2205 void *
   2206 mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
   2207 {
   2208 
   2209 	if (flags & MAP_FILE && fd_isrump(fd)) {
   2210 		errno = ENOSYS;
   2211 		return MAP_FAILED;
   2212 	}
   2213 	return host_mmap(addr, len, prot, flags, fd, offset);
   2214 }
   2215 
   2216 #ifdef PLATFORM_HAS_NBSYSCTL
   2217 /*
   2218  * these go to one or the other on a per-process configuration
   2219  */
   2220 int __sysctl(const int *, unsigned int, void *, size_t *, const void *, size_t);
   2221 int
   2222 __sysctl(const int *name, unsigned int namelen, void *old, size_t *oldlenp,
   2223 	const void *new, size_t newlen)
   2224 {
   2225 	int (*op___sysctl)(const int *, unsigned int, void *, size_t *,
   2226 	    const void *, size_t);
   2227 
   2228 	if (rumpsysctl) {
   2229 		op___sysctl = GETSYSCALL(rump, __SYSCTL);
   2230 	} else {
   2231 		op___sysctl = GETSYSCALL(host, __SYSCTL);
   2232 		/* we haven't inited yet */
   2233 		if (__predict_false(op___sysctl == NULL)) {
   2234 			op___sysctl = rumphijack_dlsym(RTLD_NEXT, "__sysctl");
   2235 		}
   2236 	}
   2237 
   2238 	return op___sysctl(name, namelen, old, oldlenp, new, newlen);
   2239 }
   2240 #endif
   2241 
   2242 /*
   2243  * Rest are std type calls.
   2244  */
   2245 
   2246 FDCALL(int, bind, DUALCALL_BIND,					\
   2247 	(int fd, const struct sockaddr *name, socklen_t namelen),	\
   2248 	(int, const struct sockaddr *, socklen_t),			\
   2249 	(fd, name, namelen))
   2250 
   2251 FDCALL(int, connect, DUALCALL_CONNECT,					\
   2252 	(int fd, const struct sockaddr *name, socklen_t namelen),	\
   2253 	(int, const struct sockaddr *, socklen_t),			\
   2254 	(fd, name, namelen))
   2255 
   2256 FDCALL(int, getpeername, DUALCALL_GETPEERNAME,				\
   2257 	(int fd, struct sockaddr *name, socklen_t *namelen),		\
   2258 	(int, struct sockaddr *, socklen_t *),				\
   2259 	(fd, name, namelen))
   2260 
   2261 FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, 				\
   2262 	(int fd, struct sockaddr *name, socklen_t *namelen),		\
   2263 	(int, struct sockaddr *, socklen_t *),				\
   2264 	(fd, name, namelen))
   2265 
   2266 FDCALL(int, listen, DUALCALL_LISTEN,	 				\
   2267 	(int fd, int backlog),						\
   2268 	(int, int),							\
   2269 	(fd, backlog))
   2270 
   2271 FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, 				\
   2272 	(int fd, void *buf, size_t len, int flags,			\
   2273 	    struct sockaddr *from, socklen_t *fromlen),			\
   2274 	(int, void *, size_t, int, struct sockaddr *, socklen_t *),	\
   2275 	(fd, buf, len, flags, from, fromlen))
   2276 
   2277 FDCALL(ssize_t, sendto, DUALCALL_SENDTO, 				\
   2278 	(int fd, const void *buf, size_t len, int flags,		\
   2279 	    const struct sockaddr *to, socklen_t tolen),		\
   2280 	(int, const void *, size_t, int,				\
   2281 	    const struct sockaddr *, socklen_t),			\
   2282 	(fd, buf, len, flags, to, tolen))
   2283 
   2284 FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, 				\
   2285 	(int fd, int level, int optn, void *optval, socklen_t *optlen),	\
   2286 	(int, int, int, void *, socklen_t *),				\
   2287 	(fd, level, optn, optval, optlen))
   2288 
   2289 FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, 				\
   2290 	(int fd, int level, int optn,					\
   2291 	    const void *optval, socklen_t optlen),			\
   2292 	(int, int, int, const void *, socklen_t),			\
   2293 	(fd, level, optn, optval, optlen))
   2294 
   2295 FDCALL(int, shutdown, DUALCALL_SHUTDOWN, 				\
   2296 	(int fd, int how),						\
   2297 	(int, int),							\
   2298 	(fd, how))
   2299 
   2300 FDCALL(ssize_t, REALREAD, DUALCALL_READ,				\
   2301 	(int fd, void *buf, size_t buflen),				\
   2302 	(int, void *, size_t),						\
   2303 	(fd, buf, buflen))
   2304 
   2305 #ifdef __linux__
   2306 ssize_t __read_chk(int, void *, size_t)
   2307     __attribute__((alias("read")));
   2308 #endif
   2309 
   2310 FDCALL(ssize_t, readv, DUALCALL_READV, 					\
   2311 	(int fd, const struct iovec *iov, int iovcnt),			\
   2312 	(int, const struct iovec *, int),				\
   2313 	(fd, iov, iovcnt))
   2314 
   2315 FDCALL(ssize_t, REALPREAD, DUALCALL_PREAD,				\
   2316 	(int fd, void *buf, size_t nbytes, off_t offset),		\
   2317 	(int, void *, size_t, off_t),					\
   2318 	(fd, buf, nbytes, offset))
   2319 
   2320 FDCALL(ssize_t, preadv, DUALCALL_PREADV, 				\
   2321 	(int fd, const struct iovec *iov, int iovcnt, off_t offset),	\
   2322 	(int, const struct iovec *, int, off_t),			\
   2323 	(fd, iov, iovcnt, offset))
   2324 
   2325 FDCALL(ssize_t, writev, DUALCALL_WRITEV, 				\
   2326 	(int fd, const struct iovec *iov, int iovcnt),			\
   2327 	(int, const struct iovec *, int),				\
   2328 	(fd, iov, iovcnt))
   2329 
   2330 FDCALL(ssize_t, REALPWRITE, DUALCALL_PWRITE,				\
   2331 	(int fd, const void *buf, size_t nbytes, off_t offset),		\
   2332 	(int, const void *, size_t, off_t),				\
   2333 	(fd, buf, nbytes, offset))
   2334 
   2335 FDCALL(ssize_t, pwritev, DUALCALL_PWRITEV, 				\
   2336 	(int fd, const struct iovec *iov, int iovcnt, off_t offset),	\
   2337 	(int, const struct iovec *, int, off_t),			\
   2338 	(fd, iov, iovcnt, offset))
   2339 
   2340 #ifndef __linux__
   2341 FDCALL(int, REALFSTAT, DUALCALL_FSTAT,					\
   2342 	(int fd, struct stat *sb),					\
   2343 	(int, struct stat *),						\
   2344 	(fd, sb))
   2345 #endif
   2346 
   2347 #ifdef PLATFORM_HAS_NBVFSSTAT
   2348 FDCALL(int, fstatvfs1, DUALCALL_FSTATVFS1,				\
   2349 	(int fd, struct statvfs *buf, int flags),			\
   2350 	(int, struct statvfs *, int),					\
   2351 	(fd, buf, flags))
   2352 #endif
   2353 
   2354 FDCALL(off_t, lseek, DUALCALL_LSEEK,					\
   2355 	(int fd, off_t offset, int whence),				\
   2356 	(int, off_t, int),						\
   2357 	(fd, offset, whence))
   2358 #ifdef LSEEK_ALIAS
   2359 __strong_alias(LSEEK_ALIAS,lseek);
   2360 #endif
   2361 
   2362 #ifndef __linux__
   2363 FDCALL(int, REALGETDENTS, DUALCALL_GETDENTS,				\
   2364 	(int fd, char *buf, size_t nbytes),				\
   2365 	(int, char *, size_t),						\
   2366 	(fd, buf, nbytes))
   2367 #endif
   2368 
   2369 FDCALL(int, fchown, DUALCALL_FCHOWN,					\
   2370 	(int fd, uid_t owner, gid_t group),				\
   2371 	(int, uid_t, gid_t),						\
   2372 	(fd, owner, group))
   2373 
   2374 FDCALL(int, fchmod, DUALCALL_FCHMOD,					\
   2375 	(int fd, mode_t mode),						\
   2376 	(int, mode_t),							\
   2377 	(fd, mode))
   2378 
   2379 FDCALL(int, ftruncate, DUALCALL_FTRUNCATE,				\
   2380 	(int fd, off_t length),						\
   2381 	(int, off_t),							\
   2382 	(fd, length))
   2383 
   2384 FDCALL(int, fsync, DUALCALL_FSYNC,					\
   2385 	(int fd),							\
   2386 	(int),								\
   2387 	(fd))
   2388 
   2389 #ifdef PLATFORM_HAS_FSYNC_RANGE
   2390 FDCALL(int, fsync_range, DUALCALL_FSYNC_RANGE,				\
   2391 	(int fd, int how, off_t start, off_t length),			\
   2392 	(int, int, off_t, off_t),					\
   2393 	(fd, how, start, length))
   2394 #endif
   2395 
   2396 FDCALL(int, futimes, DUALCALL_FUTIMES,					\
   2397 	(int fd, const struct timeval *tv),				\
   2398 	(int, const struct timeval *),					\
   2399 	(fd, tv))
   2400 
   2401 #ifdef PLATFORM_HAS_CHFLAGS
   2402 FDCALL(int, fchflags, DUALCALL_FCHFLAGS,				\
   2403 	(int fd, u_long flags),						\
   2404 	(int, u_long),							\
   2405 	(fd, flags))
   2406 #endif
   2407 
   2408 /*
   2409  * path-based selectors
   2410  */
   2411 
   2412 #ifndef __linux__
   2413 PATHCALL(int, REALSTAT, DUALCALL_STAT,					\
   2414 	(const char *path, struct stat *sb),				\
   2415 	(const char *, struct stat *),					\
   2416 	(path, sb))
   2417 
   2418 PATHCALL(int, REALLSTAT, DUALCALL_LSTAT,				\
   2419 	(const char *path, struct stat *sb),				\
   2420 	(const char *, struct stat *),					\
   2421 	(path, sb))
   2422 #endif
   2423 
   2424 PATHCALL(int, chown, DUALCALL_CHOWN,					\
   2425 	(const char *path, uid_t owner, gid_t group),			\
   2426 	(const char *, uid_t, gid_t),					\
   2427 	(path, owner, group))
   2428 
   2429 PATHCALL(int, lchown, DUALCALL_LCHOWN,					\
   2430 	(const char *path, uid_t owner, gid_t group),			\
   2431 	(const char *, uid_t, gid_t),					\
   2432 	(path, owner, group))
   2433 
   2434 PATHCALL(int, chmod, DUALCALL_CHMOD,					\
   2435 	(const char *path, mode_t mode),				\
   2436 	(const char *, mode_t),						\
   2437 	(path, mode))
   2438 
   2439 PATHCALL(int, lchmod, DUALCALL_LCHMOD,					\
   2440 	(const char *path, mode_t mode),				\
   2441 	(const char *, mode_t),						\
   2442 	(path, mode))
   2443 
   2444 #ifdef PLATFORM_HAS_NBVFSSTAT
   2445 PATHCALL(int, statvfs1, DUALCALL_STATVFS1,				\
   2446 	(const char *path, struct statvfs *buf, int flags),		\
   2447 	(const char *, struct statvfs *, int),				\
   2448 	(path, buf, flags))
   2449 #endif
   2450 
   2451 PATHCALL(int, unlink, DUALCALL_UNLINK,					\
   2452 	(const char *path),						\
   2453 	(const char *),							\
   2454 	(path))
   2455 
   2456 PATHCALL(int, symlink, DUALCALL_SYMLINK,				\
   2457 	(const char *target, const char *path),				\
   2458 	(const char *, const char *),					\
   2459 	(target, path))
   2460 
   2461 /*
   2462  * readlink() can be called from malloc which can be called
   2463  * from dlsym() during init
   2464  */
   2465 ssize_t
   2466 readlink(const char *path, char *buf, size_t bufsiz)
   2467 {
   2468 	int (*op_readlink)(const char *, char *, size_t);
   2469 	enum pathtype pt;
   2470 
   2471 	if ((pt = path_isrump(path)) != PATH_HOST) {
   2472 		op_readlink = GETSYSCALL(rump, READLINK);
   2473 		if (pt == PATH_RUMP)
   2474 			path = path_host2rump(path);
   2475 	} else {
   2476 		op_readlink = GETSYSCALL(host, READLINK);
   2477 	}
   2478 
   2479 	if (__predict_false(op_readlink == NULL)) {
   2480 		errno = ENOENT;
   2481 		return -1;
   2482 	}
   2483 
   2484 	return op_readlink(path, buf, bufsiz);
   2485 }
   2486 
   2487 PATHCALL(int, mkdir, DUALCALL_MKDIR,					\
   2488 	(const char *path, mode_t mode),				\
   2489 	(const char *, mode_t),						\
   2490 	(path, mode))
   2491 
   2492 PATHCALL(int, rmdir, DUALCALL_RMDIR,					\
   2493 	(const char *path),						\
   2494 	(const char *),							\
   2495 	(path))
   2496 
   2497 PATHCALL(int, utimes, DUALCALL_UTIMES,					\
   2498 	(const char *path, const struct timeval *tv),			\
   2499 	(const char *, const struct timeval *),				\
   2500 	(path, tv))
   2501 
   2502 PATHCALL(int, lutimes, DUALCALL_LUTIMES,				\
   2503 	(const char *path, const struct timeval *tv),			\
   2504 	(const char *, const struct timeval *),				\
   2505 	(path, tv))
   2506 
   2507 #ifdef PLATFORM_HAS_CHFLAGS
   2508 PATHCALL(int, chflags, DUALCALL_CHFLAGS,				\
   2509 	(const char *path, u_long flags),				\
   2510 	(const char *, u_long),						\
   2511 	(path, flags))
   2512 
   2513 PATHCALL(int, lchflags, DUALCALL_LCHFLAGS,				\
   2514 	(const char *path, u_long flags),				\
   2515 	(const char *, u_long),						\
   2516 	(path, flags))
   2517 #endif /* PLATFORM_HAS_CHFLAGS */
   2518 
   2519 PATHCALL(int, truncate, DUALCALL_TRUNCATE,				\
   2520 	(const char *path, off_t length),				\
   2521 	(const char *, off_t),						\
   2522 	(path, length))
   2523 
   2524 PATHCALL(int, access, DUALCALL_ACCESS,					\
   2525 	(const char *path, int mode),					\
   2526 	(const char *, int),						\
   2527 	(path, mode))
   2528 
   2529 #ifndef __linux__
   2530 PATHCALL(int, REALMKNOD, DUALCALL_MKNOD,				\
   2531 	(const char *path, mode_t mode, dev_t dev),			\
   2532 	(const char *, mode_t, dev_t),					\
   2533 	(path, mode, dev))
   2534 #endif
   2535 
   2536 /*
   2537  * Note: with mount the decisive parameter is the mount
   2538  * destination directory.  This is because we don't really know
   2539  * about the "source" directory in a generic call (and besides,
   2540  * it might not even exist, cf. nfs).
   2541  */
   2542 #ifdef PLATFORM_HAS_NBMOUNT
   2543 PATHCALL(int, REALMOUNT, DUALCALL_MOUNT,				\
   2544 	(const char *type, const char *path, int flags,			\
   2545 	    void *data, size_t dlen),					\
   2546 	(const char *, const char *, int, void *, size_t),		\
   2547 	(type, path, flags, data, dlen))
   2548 
   2549 PATHCALL(int, unmount, DUALCALL_UNMOUNT,				\
   2550 	(const char *path, int flags),					\
   2551 	(const char *, int),						\
   2552 	(path, flags))
   2553 #endif /* PLATFORM_HAS_NBMOUNT */
   2554 
   2555 #ifdef PLATFORM_HAS_NBQUOTA
   2556 #if __NetBSD_Prereq__(5,99,63)
   2557 PATHCALL(int, __quotactl, DUALCALL_QUOTACTL,				\
   2558 	(const char *path, struct quotactl_args *args),			\
   2559 	(const char *, struct quotactl_args *),				\
   2560 	(path, args))
   2561 #elif __NetBSD_Prereq__(5,99,48)
   2562 PATHCALL(int, OLDREALQUOTACTL, DUALCALL_QUOTACTL,			\
   2563 	(const char *path, struct plistref *p),				\
   2564 	(const char *, struct plistref *),				\
   2565 	(path, p))
   2566 #endif
   2567 #endif /* PLATFORM_HAS_NBQUOTA */
   2568 
   2569 #ifdef PLATFORM_HAS_NBFILEHANDLE
   2570 PATHCALL(int, REALGETFH, DUALCALL_GETFH,				\
   2571 	(const char *path, void *fhp, size_t *fh_size),			\
   2572 	(const char *, void *, size_t *),				\
   2573 	(path, fhp, fh_size))
   2574 #endif
   2575 
   2576 /*
   2577  * These act different on a per-process vfs configuration
   2578  */
   2579 
   2580 #ifdef PLATFORM_HAS_NBVFSSTAT
   2581 VFSCALL(VFSBIT_GETVFSSTAT, int, getvfsstat, DUALCALL_GETVFSSTAT,	\
   2582 	(struct statvfs *buf, size_t buflen, int flags),		\
   2583 	(struct statvfs *, size_t, int),				\
   2584 	(buf, buflen, flags))
   2585 #endif
   2586 
   2587 #ifdef PLATFORM_HAS_NBFILEHANDLE
   2588 VFSCALL(VFSBIT_FHCALLS, int, REALFHOPEN, DUALCALL_FHOPEN,		\
   2589 	(const void *fhp, size_t fh_size, int flags),			\
   2590 	(const char *, size_t, int),					\
   2591 	(fhp, fh_size, flags))
   2592 
   2593 VFSCALL(VFSBIT_FHCALLS, int, REALFHSTAT, DUALCALL_FHSTAT,		\
   2594 	(const void *fhp, size_t fh_size, struct stat *sb),		\
   2595 	(const char *, size_t, struct stat *),				\
   2596 	(fhp, fh_size, sb))
   2597 
   2598 VFSCALL(VFSBIT_FHCALLS, int, REALFHSTATVFS1, DUALCALL_FHSTATVFS1,	\
   2599 	(const void *fhp, size_t fh_size, struct statvfs *sb, int flgs),\
   2600 	(const char *, size_t, struct statvfs *, int),			\
   2601 	(fhp, fh_size, sb, flgs))
   2602 #endif
   2603 
   2604 
   2605 #ifdef PLATFORM_HAS_NFSSVC
   2606 
   2607 /* finally, put nfssvc here.  "keep the namespace clean" */
   2608 #include <nfs/rpcv2.h>
   2609 #include <nfs/nfs.h>
   2610 
   2611 int
   2612 nfssvc(int flags, void *argstructp)
   2613 {
   2614 	int (*op_nfssvc)(int, void *);
   2615 
   2616 	if (vfsbits & VFSBIT_NFSSVC){
   2617 		struct nfsd_args *nfsdargs;
   2618 
   2619 		/* massage the socket descriptor if necessary */
   2620 		if (flags == NFSSVC_ADDSOCK) {
   2621 			nfsdargs = argstructp;
   2622 			nfsdargs->sock = fd_host2rump(nfsdargs->sock);
   2623 		}
   2624 		op_nfssvc = GETSYSCALL(rump, NFSSVC);
   2625 	} else
   2626 		op_nfssvc = GETSYSCALL(host, NFSSVC);
   2627 
   2628 	return op_nfssvc(flags, argstructp);
   2629 }
   2630 #endif /* PLATFORM_HAS_NFSSVC */
   2631