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