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