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