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