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