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