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