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