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