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