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