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