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