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