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