hijack.c revision 1.68 1 /* $NetBSD: hijack.c,v 1.68 2011/02/25 18:29:00 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 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: hijack.c,v 1.68 2011/02/25 18:29:00 pooka Exp $");
30
31 #define __ssp_weak_name(fun) _hijack_ ## fun
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/event.h>
36 #include <sys/ioctl.h>
37 #include <sys/mman.h>
38 #include <sys/mount.h>
39 #include <sys/poll.h>
40 #include <sys/socket.h>
41 #include <sys/statvfs.h>
42
43 #include <rump/rumpclient.h>
44 #include <rump/rump_syscalls.h>
45
46 #include <assert.h>
47 #include <dlfcn.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <poll.h>
52 #include <pthread.h>
53 #include <signal.h>
54 #include <stdarg.h>
55 #include <stdbool.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <time.h>
60 #include <unistd.h>
61
62 #include "hijack.h"
63
64 enum dualcall {
65 DUALCALL_WRITE, DUALCALL_WRITEV, DUALCALL_PWRITE, DUALCALL_PWRITEV,
66 DUALCALL_IOCTL, DUALCALL_FCNTL,
67 DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_BIND, DUALCALL_CONNECT,
68 DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN,
69 DUALCALL_RECVFROM, DUALCALL_RECVMSG,
70 DUALCALL_SENDTO, DUALCALL_SENDMSG,
71 DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT,
72 DUALCALL_SHUTDOWN,
73 DUALCALL_READ, DUALCALL_READV, DUALCALL_PREAD, DUALCALL_PREADV,
74 DUALCALL_DUP2,
75 DUALCALL_CLOSE,
76 DUALCALL_POLLTS,
77 DUALCALL_KEVENT,
78 DUALCALL_STAT, DUALCALL_LSTAT, DUALCALL_FSTAT,
79 DUALCALL_CHMOD, DUALCALL_LCHMOD, DUALCALL_FCHMOD,
80 DUALCALL_CHOWN, DUALCALL_LCHOWN, DUALCALL_FCHOWN,
81 DUALCALL_OPEN,
82 DUALCALL_STATVFS1, DUALCALL_FSTATVFS1,
83 DUALCALL_CHDIR, DUALCALL_FCHDIR,
84 DUALCALL_LSEEK,
85 DUALCALL_GETDENTS,
86 DUALCALL_UNLINK, DUALCALL_SYMLINK, DUALCALL_READLINK,
87 DUALCALL_RENAME,
88 DUALCALL_MKDIR, DUALCALL_RMDIR,
89 DUALCALL_UTIMES, DUALCALL_LUTIMES, DUALCALL_FUTIMES,
90 DUALCALL_TRUNCATE, DUALCALL_FTRUNCATE,
91 DUALCALL_FSYNC, DUALCALL_FSYNC_RANGE,
92 DUALCALL_MOUNT, DUALCALL_UNMOUNT,
93 DUALCALL___GETCWD,
94 DUALCALL_CHFLAGS, DUALCALL_LCHFLAGS, DUALCALL_FCHFLAGS,
95 DUALCALL_ACCESS,
96 DUALCALL_MKNOD,
97 DUALCALL__NUM
98 };
99
100 #define RSYS_STRING(a) __STRING(a)
101 #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a))
102
103 /*
104 * Would be nice to get this automatically in sync with libc.
105 * Also, this does not work for compat-using binaries!
106 */
107 #if !__NetBSD_Prereq__(5,99,7)
108 #define REALSELECT select
109 #define REALPOLLTS pollts
110 #define REALKEVENT kevent
111 #define REALSTAT __stat30
112 #define REALLSTAT __lstat30
113 #define REALFSTAT __fstat30
114 #define REALUTIMES utimes
115 #define REALLUTIMES lutimes
116 #define REALFUTIMES futimes
117 #define REALMKNOD mknod
118 #else
119 #define REALSELECT _sys___select50
120 #define REALPOLLTS _sys___pollts50
121 #define REALKEVENT _sys___kevent50
122 #define REALSTAT __stat50
123 #define REALLSTAT __lstat50
124 #define REALFSTAT __fstat50
125 #define REALUTIMES __utimes50
126 #define REALLUTIMES __lutimes50
127 #define REALMKNOD __mknod50
128 #endif
129 #define REALREAD _sys_read
130 #define REALPREAD _sys_pread
131 #define REALPWRITE _sys_pwrite
132 #define REALGETDENTS __getdents30
133 #define REALMOUNT __mount50
134
135 int REALSELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *);
136 int REALPOLLTS(struct pollfd *, nfds_t,
137 const struct timespec *, const sigset_t *);
138 int REALKEVENT(int, const struct kevent *, size_t, struct kevent *, size_t,
139 const struct timespec *);
140 ssize_t REALREAD(int, void *, size_t);
141 ssize_t REALPREAD(int, void *, size_t, off_t);
142 ssize_t REALPWRITE(int, const void *, size_t, off_t);
143 int REALSTAT(const char *, struct stat *);
144 int REALLSTAT(const char *, struct stat *);
145 int REALFSTAT(int, struct stat *);
146 int REALGETDENTS(int, char *, size_t);
147 int REALUTIMES(const char *, const struct timeval [2]);
148 int REALLUTIMES(const char *, const struct timeval [2]);
149 int REALFUTIMES(int, const struct timeval [2]);
150 int REALMOUNT(const char *, const char *, int, void *, size_t);
151 int __getcwd(char *, size_t);
152 int REALMKNOD(const char *, mode_t, dev_t);
153
154 #define S(a) __STRING(a)
155 struct sysnames {
156 enum dualcall scm_callnum;
157 const char *scm_hostname;
158 const char *scm_rumpname;
159 } syscnames[] = {
160 { DUALCALL_SOCKET, "__socket30", RSYS_NAME(SOCKET) },
161 { DUALCALL_ACCEPT, "accept", RSYS_NAME(ACCEPT) },
162 { DUALCALL_BIND, "bind", RSYS_NAME(BIND) },
163 { DUALCALL_CONNECT, "connect", RSYS_NAME(CONNECT) },
164 { DUALCALL_GETPEERNAME, "getpeername", RSYS_NAME(GETPEERNAME) },
165 { DUALCALL_GETSOCKNAME, "getsockname", RSYS_NAME(GETSOCKNAME) },
166 { DUALCALL_LISTEN, "listen", RSYS_NAME(LISTEN) },
167 { DUALCALL_RECVFROM, "recvfrom", RSYS_NAME(RECVFROM) },
168 { DUALCALL_RECVMSG, "recvmsg", RSYS_NAME(RECVMSG) },
169 { DUALCALL_SENDTO, "sendto", RSYS_NAME(SENDTO) },
170 { DUALCALL_SENDMSG, "sendmsg", RSYS_NAME(SENDMSG) },
171 { DUALCALL_GETSOCKOPT, "getsockopt", RSYS_NAME(GETSOCKOPT) },
172 { DUALCALL_SETSOCKOPT, "setsockopt", RSYS_NAME(SETSOCKOPT) },
173 { DUALCALL_SHUTDOWN, "shutdown", RSYS_NAME(SHUTDOWN) },
174 { DUALCALL_READ, S(REALREAD), RSYS_NAME(READ) },
175 { DUALCALL_READV, "readv", RSYS_NAME(READV) },
176 { DUALCALL_PREAD, S(REALPREAD), RSYS_NAME(PREAD) },
177 { DUALCALL_PREADV, "preadv", RSYS_NAME(PREADV) },
178 { DUALCALL_WRITE, "write", RSYS_NAME(WRITE) },
179 { DUALCALL_WRITEV, "writev", RSYS_NAME(WRITEV) },
180 { DUALCALL_PWRITE, S(REALPWRITE), RSYS_NAME(PWRITE) },
181 { DUALCALL_PWRITEV, "pwritev", RSYS_NAME(PWRITEV) },
182 { DUALCALL_IOCTL, "ioctl", RSYS_NAME(IOCTL) },
183 { DUALCALL_FCNTL, "fcntl", RSYS_NAME(FCNTL) },
184 { DUALCALL_DUP2, "dup2", RSYS_NAME(DUP2) },
185 { DUALCALL_CLOSE, "close", RSYS_NAME(CLOSE) },
186 { DUALCALL_POLLTS, S(REALPOLLTS), RSYS_NAME(POLLTS) },
187 { DUALCALL_KEVENT, S(REALKEVENT), RSYS_NAME(KEVENT) },
188 { DUALCALL_STAT, S(REALSTAT), RSYS_NAME(STAT) },
189 { DUALCALL_LSTAT, S(REALLSTAT), RSYS_NAME(LSTAT) },
190 { DUALCALL_FSTAT, S(REALFSTAT), RSYS_NAME(FSTAT) },
191 { DUALCALL_CHOWN, "chown", RSYS_NAME(CHOWN) },
192 { DUALCALL_LCHOWN, "lchown", RSYS_NAME(LCHOWN) },
193 { DUALCALL_FCHOWN, "fchown", RSYS_NAME(FCHOWN) },
194 { DUALCALL_CHMOD, "chmod", RSYS_NAME(CHMOD) },
195 { DUALCALL_LCHMOD, "lchmod", RSYS_NAME(LCHMOD) },
196 { DUALCALL_FCHMOD, "fchmod", RSYS_NAME(FCHMOD) },
197 { DUALCALL_UTIMES, S(REALUTIMES), RSYS_NAME(UTIMES) },
198 { DUALCALL_LUTIMES, S(REALLUTIMES), RSYS_NAME(LUTIMES) },
199 { DUALCALL_FUTIMES, S(REALFUTIMES), RSYS_NAME(FUTIMES) },
200 { DUALCALL_OPEN, "open", RSYS_NAME(OPEN) },
201 { DUALCALL_STATVFS1, "statvfs1", RSYS_NAME(STATVFS1) },
202 { DUALCALL_FSTATVFS1, "fstatvfs1", RSYS_NAME(FSTATVFS1) },
203 { DUALCALL_CHDIR, "chdir", RSYS_NAME(CHDIR) },
204 { DUALCALL_FCHDIR, "fchdir", RSYS_NAME(FCHDIR) },
205 { DUALCALL_LSEEK, "lseek", RSYS_NAME(LSEEK) },
206 { DUALCALL_GETDENTS, "__getdents30", RSYS_NAME(GETDENTS) },
207 { DUALCALL_UNLINK, "unlink", RSYS_NAME(UNLINK) },
208 { DUALCALL_SYMLINK, "symlink", RSYS_NAME(SYMLINK) },
209 { DUALCALL_READLINK, "readlink", RSYS_NAME(READLINK) },
210 { DUALCALL_RENAME, "rename", RSYS_NAME(RENAME) },
211 { DUALCALL_MKDIR, "mkdir", RSYS_NAME(MKDIR) },
212 { DUALCALL_RMDIR, "rmdir", RSYS_NAME(RMDIR) },
213 { DUALCALL_TRUNCATE, "truncate", RSYS_NAME(TRUNCATE) },
214 { DUALCALL_FTRUNCATE, "ftruncate", RSYS_NAME(FTRUNCATE) },
215 { DUALCALL_FSYNC, "fsync", RSYS_NAME(FSYNC) },
216 { DUALCALL_FSYNC_RANGE, "fsync_range", RSYS_NAME(FSYNC_RANGE) },
217 { DUALCALL_MOUNT, S(REALMOUNT), RSYS_NAME(MOUNT) },
218 { DUALCALL_UNMOUNT, "unmount", RSYS_NAME(UNMOUNT) },
219 { DUALCALL___GETCWD, "__getcwd", RSYS_NAME(__GETCWD) },
220 { DUALCALL_CHFLAGS, "chflags", RSYS_NAME(CHFLAGS) },
221 { DUALCALL_LCHFLAGS, "lchflags", RSYS_NAME(LCHFLAGS) },
222 { DUALCALL_FCHFLAGS, "fchflags", RSYS_NAME(FCHFLAGS) },
223 { DUALCALL_ACCESS, "access", RSYS_NAME(ACCESS) },
224 { DUALCALL_MKNOD, S(REALMKNOD), RSYS_NAME(MKNOD) },
225 };
226 #undef S
227
228 struct bothsys {
229 void *bs_host;
230 void *bs_rump;
231 } syscalls[DUALCALL__NUM];
232 #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which
233
234 pid_t (*host_fork)(void);
235 int (*host_daemon)(int, int);
236 int (*host_execve)(const char *, char *const[], char *const[]);
237 void * (*host_mmap)(void *, size_t, int, int, int, off_t);
238
239 /* ok, we need *two* bits per dup2'd fd to track fd+HIJACKOFF aliases */
240 static uint32_t dup2mask;
241 #define ISDUP2D(fd) (((fd) < 16) && (1<<(fd) & dup2mask))
242 #define SETDUP2(fd) \
243 do { if ((fd) < 16) dup2mask |= (1<<(fd)); } while (/*CONSTCOND*/0)
244 #define CLRDUP2(fd) \
245 do { if ((fd) < 16) dup2mask &= ~(1<<(fd)); } while (/*CONSTCOND*/0)
246 #define ISDUP2ALIAS(fd) (((fd) < 16) && (1<<((fd)+16) & dup2mask))
247 #define SETDUP2ALIAS(fd) \
248 do { if ((fd) < 16) dup2mask |= (1<<((fd)+16)); } while (/*CONSTCOND*/0)
249 #define CLRDUP2ALIAS(fd) \
250 do { if ((fd) < 16) dup2mask &= ~(1<<((fd)+16)); } while (/*CONSTCOND*/0)
251
252 //#define DEBUGJACK
253 #ifdef DEBUGJACK
254 #define DPRINTF(x) mydprintf x
255 static void
256 mydprintf(const char *fmt, ...)
257 {
258 va_list ap;
259
260 if (ISDUP2D(STDERR_FILENO))
261 return;
262
263 va_start(ap, fmt);
264 vfprintf(stderr, fmt, ap);
265 va_end(ap);
266 }
267
268 #else
269 #define DPRINTF(x)
270 #endif
271
272 #define FDCALL(type, name, rcname, args, proto, vars) \
273 type name args \
274 { \
275 type (*fun) proto; \
276 \
277 DPRINTF(("%s -> %d\n", __STRING(name), fd)); \
278 if (fd_isrump(fd)) { \
279 fun = syscalls[rcname].bs_rump; \
280 fd = fd_host2rump(fd); \
281 } else { \
282 fun = syscalls[rcname].bs_host; \
283 } \
284 \
285 return fun vars; \
286 }
287
288 #define PATHCALL(type, name, rcname, args, proto, vars) \
289 type name args \
290 { \
291 type (*fun) proto; \
292 \
293 DPRINTF(("%s -> %s\n", __STRING(name), path)); \
294 if (path_isrump(path)) { \
295 fun = syscalls[rcname].bs_rump; \
296 path = path_host2rump(path); \
297 } else { \
298 fun = syscalls[rcname].bs_host; \
299 } \
300 \
301 return fun vars; \
302 }
303
304 /*
305 * This tracks if our process is in a subdirectory of /rump.
306 * It's preserved over exec.
307 */
308 static bool pwdinrump = false;
309
310 /*
311 * These variables are set from the RUMPHIJACK string and control
312 * which operations can product rump kernel file descriptors.
313 * This should be easily extendable for future needs.
314 */
315 #define RUMPHIJACK_DEFAULT "path=/rump,socket=all:nolocal"
316 static bool rumpsockets[PF_MAX];
317 static const char *rumpprefix;
318 static size_t rumpprefixlen;
319
320 static struct {
321 int pf;
322 const char *name;
323 } socketmap[] = {
324 { PF_LOCAL, "local" },
325 { PF_INET, "inet" },
326 { PF_LINK, "link" },
327 #ifdef PF_OROUTE
328 { PF_OROUTE, "oroute" },
329 #endif
330 { PF_ROUTE, "route" },
331 { PF_INET6, "inet6" },
332 #ifdef PF_MPLS
333 { PF_MPLS, "mpls" },
334 #endif
335 { -1, NULL }
336 };
337
338 static void
339 sockparser(char *buf)
340 {
341 char *p, *l;
342 bool value;
343 int i;
344
345 /* if "all" is present, it must be specified first */
346 if (strncmp(buf, "all", strlen("all")) == 0) {
347 for (i = 0; i < (int)__arraycount(rumpsockets); i++) {
348 rumpsockets[i] = true;
349 }
350 buf += strlen("all");
351 if (*buf == ':')
352 buf++;
353 }
354
355 for (p = strtok_r(buf, ":", &l); p; p = strtok_r(NULL, ":", &l)) {
356 value = true;
357 if (strncmp(p, "no", strlen("no")) == 0) {
358 value = false;
359 p += strlen("no");
360 }
361
362 for (i = 0; socketmap[i].name; i++) {
363 if (strcmp(p, socketmap[i].name) == 0) {
364 rumpsockets[socketmap[i].pf] = value;
365 break;
366 }
367 }
368 if (socketmap[i].name == NULL) {
369 warnx("invalid socket specifier %s", p);
370 }
371 }
372 }
373
374 static void
375 pathparser(char *buf)
376 {
377
378 /* sanity-check */
379 if (*buf != '/')
380 errx(1, "hijack path specifier must begin with ``/''");
381 rumpprefixlen = strlen(buf);
382 if (rumpprefixlen < 2)
383 errx(1, "invalid hijack prefix: %s", buf);
384 if (buf[rumpprefixlen-1] == '/' && strspn(buf, "/") != rumpprefixlen)
385 errx(1, "hijack prefix may end in slash only if pure "
386 "slash, gave %s", buf);
387
388 if ((rumpprefix = strdup(buf)) == NULL)
389 err(1, "strdup");
390 rumpprefixlen = strlen(rumpprefix);
391 }
392
393 static struct {
394 void (*parsefn)(char *);
395 const char *name;
396 } hijackparse[] = {
397 { sockparser, "socket" },
398 { pathparser, "path" },
399 { NULL, NULL },
400 };
401
402 static void
403 parsehijack(char *hijack)
404 {
405 char *p, *p2, *l;
406 const char *hijackcopy;
407 int i;
408
409 if ((hijackcopy = strdup(hijack)) == NULL)
410 err(1, "strdup");
411
412 /* disable everything explicitly */
413 for (i = 0; i < PF_MAX; i++)
414 rumpsockets[i] = false;
415
416 for (p = strtok_r(hijack, ",", &l); p; p = strtok_r(NULL, ",", &l)) {
417 p2 = strchr(p, '=');
418 if (!p2)
419 errx(1, "invalid hijack specifier: %s", hijackcopy);
420
421 for (i = 0; hijackparse[i].parsefn; i++) {
422 if (strncmp(hijackparse[i].name, p,
423 (size_t)(p2-p)) == 0) {
424 hijackparse[i].parsefn(p2+1);
425 break;
426 }
427 }
428 }
429
430 }
431
432 static void __attribute__((constructor))
433 rcinit(void)
434 {
435 char buf[1024];
436 unsigned i, j;
437
438 host_fork = dlsym(RTLD_NEXT, "fork");
439 host_daemon = dlsym(RTLD_NEXT, "daemon");
440 host_execve = dlsym(RTLD_NEXT, "execve");
441 host_mmap = dlsym(RTLD_NEXT, "mmap");
442
443 /*
444 * In theory cannot print anything during lookups because
445 * we might not have the call vector set up. so, the errx()
446 * is a bit of a strech, but it might work.
447 */
448
449 for (i = 0; i < DUALCALL__NUM; i++) {
450 /* build runtime O(1) access */
451 for (j = 0; j < __arraycount(syscnames); j++) {
452 if (syscnames[j].scm_callnum == i)
453 break;
454 }
455
456 if (j == __arraycount(syscnames))
457 errx(1, "rumphijack error: syscall pos %d missing", i);
458
459 syscalls[i].bs_host = dlsym(RTLD_NEXT,
460 syscnames[j].scm_hostname);
461 if (syscalls[i].bs_host == NULL)
462 errx(1, "hostcall %s not found missing",
463 syscnames[j].scm_hostname);
464
465 syscalls[i].bs_rump = dlsym(RTLD_NEXT,
466 syscnames[j].scm_rumpname);
467 if (syscalls[i].bs_rump == NULL)
468 errx(1, "rumpcall %s not found missing",
469 syscnames[j].scm_rumpname);
470 }
471
472 if (rumpclient_init() == -1)
473 err(1, "rumpclient init");
474
475 /* check which syscalls we're supposed to hijack */
476 if (getenv_r("RUMPHIJACK", buf, sizeof(buf)) == -1) {
477 strcpy(buf, RUMPHIJACK_DEFAULT);
478 }
479 parsehijack(buf);
480
481 /* set client persistence level */
482 if (getenv_r("RUMPHIJACK_RETRYCONNECT", buf, sizeof(buf)) != -1) {
483 if (strcmp(buf, "die") == 0)
484 rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_DIE);
485 else if (strcmp(buf, "inftime") == 0)
486 rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME);
487 else if (strcmp(buf, "once") == 0)
488 rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_ONCE);
489 else {
490 time_t timeout;
491 char *ep;
492
493 timeout = (time_t)strtoll(buf, &ep, 10);
494 if (timeout <= 0 || ep != buf + strlen(buf))
495 errx(1, "RUMPHIJACK_RETRYCONNECT must be "
496 "keyword or integer, got: %s", buf);
497
498 rumpclient_setconnretry(timeout);
499 }
500 }
501
502 if (getenv_r("RUMPHIJACK__DUP2MASK", buf, sizeof(buf)) == 0) {
503 dup2mask = strtoul(buf, NULL, 10);
504 unsetenv("RUMPHIJACK__DUP2MASK");
505 }
506 if (getenv_r("RUMPHIJACK__PWDINRUMP", buf, sizeof(buf)) == 0) {
507 pwdinrump = true;
508 unsetenv("RUMPHIJACK__PWDINRUMP");
509 }
510 }
511
512 /* XXX: need runtime selection. low for now due to FD_SETSIZE */
513 #define HIJACK_FDOFF 128
514 static int
515 fd_rump2host(int fd)
516 {
517
518 if (fd == -1)
519 return fd;
520
521 if (!ISDUP2D(fd))
522 fd += HIJACK_FDOFF;
523
524 return fd;
525 }
526
527 static int
528 fd_host2rump(int fd)
529 {
530
531 if (!ISDUP2D(fd))
532 fd -= HIJACK_FDOFF;
533 return fd;
534 }
535
536 static bool
537 fd_isrump(int fd)
538 {
539
540 return ISDUP2D(fd) || fd >= HIJACK_FDOFF;
541 }
542
543 #define assertfd(_fd_) assert(ISDUP2D(_fd_) || (_fd_) >= HIJACK_FDOFF)
544
545 static bool
546 path_isrump(const char *path)
547 {
548
549 if (rumpprefix == NULL)
550 return false;
551
552 if (*path == '/') {
553 if (strncmp(path, rumpprefix, rumpprefixlen) == 0)
554 return true;
555 return false;
556 } else {
557 return pwdinrump;
558 }
559 }
560
561 static const char *rootpath = "/";
562 static const char *
563 path_host2rump(const char *path)
564 {
565 const char *rv;
566
567 if (*path == '/') {
568 rv = path + rumpprefixlen;
569 if (*rv == '\0')
570 rv = rootpath;
571 } else {
572 rv = path;
573 }
574
575 return rv;
576 }
577
578 static int
579 dodup(int oldd, int minfd)
580 {
581 int (*op_fcntl)(int, int, ...);
582 int newd;
583 int isrump;
584
585 DPRINTF(("dup -> %d (minfd %d)\n", oldd, minfd));
586 if (fd_isrump(oldd)) {
587 op_fcntl = GETSYSCALL(rump, FCNTL);
588 oldd = fd_host2rump(oldd);
589 if (minfd >= HIJACK_FDOFF)
590 minfd -= HIJACK_FDOFF;
591 isrump = 1;
592 } else {
593 op_fcntl = GETSYSCALL(host, FCNTL);
594 isrump = 0;
595 }
596
597 newd = op_fcntl(oldd, F_DUPFD, minfd);
598
599 if (isrump)
600 newd = fd_rump2host(newd);
601 DPRINTF(("dup <- %d\n", newd));
602
603 return newd;
604 }
605
606 /*
607 * dup a host file descriptor so that it doesn't collide with the dup2mask
608 */
609 static int
610 fd_dupgood(int fd)
611 {
612 int (*op_fcntl)(int, int, ...) = GETSYSCALL(host, FCNTL);
613 int (*op_close)(int) = GETSYSCALL(host, CLOSE);
614 int ofd, i;
615
616 for (i = 1; ISDUP2D(fd); i++) {
617 ofd = fd;
618 fd = op_fcntl(ofd, F_DUPFD, i);
619 op_close(ofd);
620 }
621
622 return fd;
623 }
624
625 int
626 open(const char *path, int flags, ...)
627 {
628 int (*op_open)(const char *, int, ...);
629 bool isrump;
630 va_list ap;
631 int fd;
632
633 if (path_isrump(path)) {
634 path = path_host2rump(path);
635 op_open = GETSYSCALL(rump, OPEN);
636 isrump = true;
637 } else {
638 op_open = GETSYSCALL(host, OPEN);
639 isrump = false;
640 }
641
642 va_start(ap, flags);
643 fd = op_open(path, flags, va_arg(ap, mode_t));
644 va_end(ap);
645
646 if (isrump)
647 fd = fd_rump2host(fd);
648 else
649 fd = fd_dupgood(fd);
650 return fd;
651 }
652
653 int
654 chdir(const char *path)
655 {
656 int (*op_chdir)(const char *);
657 bool isrump;
658 int rv;
659
660 if (path_isrump(path)) {
661 op_chdir = GETSYSCALL(rump, CHDIR);
662 isrump = true;
663 path = path_host2rump(path);
664 } else {
665 op_chdir = GETSYSCALL(host, CHDIR);
666 isrump = false;
667 }
668
669 rv = op_chdir(path);
670 if (rv == 0) {
671 if (isrump)
672 pwdinrump = true;
673 else
674 pwdinrump = false;
675 }
676
677 return rv;
678 }
679
680 int
681 fchdir(int fd)
682 {
683 int (*op_fchdir)(int);
684 bool isrump;
685 int rv;
686
687 if (fd_isrump(fd)) {
688 op_fchdir = GETSYSCALL(rump, FCHDIR);
689 isrump = true;
690 fd = fd_host2rump(fd);
691 } else {
692 op_fchdir = GETSYSCALL(host, FCHDIR);
693 isrump = false;
694 }
695
696 rv = op_fchdir(fd);
697 if (rv == 0) {
698 if (isrump)
699 pwdinrump = true;
700 else
701 pwdinrump = false;
702 }
703
704 return rv;
705 }
706
707 int
708 __getcwd(char *bufp, size_t len)
709 {
710 int (*op___getcwd)(char *, size_t);
711 int rv;
712
713 if (pwdinrump) {
714 size_t prefixgap;
715 bool iamslash;
716
717 if (rumpprefix[rumpprefixlen-1] == '/')
718 iamslash = true;
719 else
720 iamslash = false;
721
722 if (iamslash)
723 prefixgap = rumpprefixlen - 1; /* ``//+path'' */
724 else
725 prefixgap = rumpprefixlen; /* ``/pfx+/path'' */
726 if (len <= prefixgap) {
727 errno = ERANGE;
728 return -1;
729 }
730
731 op___getcwd = GETSYSCALL(rump, __GETCWD);
732 rv = op___getcwd(bufp + prefixgap, len - prefixgap);
733 if (rv == -1)
734 return rv;
735
736 /* augment the "/" part only for a non-root path */
737 memcpy(bufp, rumpprefix, rumpprefixlen);
738
739 /* append / only to non-root cwd */
740 if (rv != 2)
741 bufp[prefixgap] = '/';
742
743 /* don't append extra slash in the purely-slash case */
744 if (rv == 2 && !iamslash)
745 bufp[rumpprefixlen] = '\0';
746
747 return rv;
748 } else {
749 op___getcwd = GETSYSCALL(host, __GETCWD);
750 return op___getcwd(bufp, len);
751 }
752 }
753
754 int
755 rename(const char *from, const char *to)
756 {
757 int (*op_rename)(const char *, const char *);
758
759 if (path_isrump(from)) {
760 if (!path_isrump(to)) {
761 errno = EXDEV;
762 return -1;
763 }
764
765 from = path_host2rump(from);
766 to = path_host2rump(to);
767 op_rename = GETSYSCALL(rump, RENAME);
768 } else {
769 if (path_isrump(to)) {
770 errno = EXDEV;
771 return -1;
772 }
773
774 op_rename = GETSYSCALL(host, RENAME);
775 }
776
777 return op_rename(from, to);
778 }
779
780 int __socket30(int, int, int);
781 int
782 __socket30(int domain, int type, int protocol)
783 {
784 int (*op_socket)(int, int, int);
785 int fd;
786 bool isrump;
787
788 isrump = domain < PF_MAX && rumpsockets[domain];
789
790 if (isrump)
791 op_socket = GETSYSCALL(rump, SOCKET);
792 else
793 op_socket = GETSYSCALL(host, SOCKET);
794 fd = op_socket(domain, type, protocol);
795
796 if (isrump)
797 fd = fd_rump2host(fd);
798 else
799 fd = fd_dupgood(fd);
800 DPRINTF(("socket <- %d\n", fd));
801
802 return fd;
803 }
804
805 int
806 accept(int s, struct sockaddr *addr, socklen_t *addrlen)
807 {
808 int (*op_accept)(int, struct sockaddr *, socklen_t *);
809 int fd;
810 bool isrump;
811
812 isrump = fd_isrump(s);
813
814 DPRINTF(("accept -> %d", s));
815 if (isrump) {
816 op_accept = GETSYSCALL(rump, ACCEPT);
817 s = fd_host2rump(s);
818 } else {
819 op_accept = GETSYSCALL(host, ACCEPT);
820 }
821 fd = op_accept(s, addr, addrlen);
822 if (fd != -1 && isrump)
823 fd = fd_rump2host(fd);
824 else
825 fd = fd_dupgood(fd);
826
827 DPRINTF((" <- %d\n", fd));
828
829 return fd;
830 }
831
832 /*
833 * ioctl and fcntl are varargs calls and need special treatment
834 */
835 int
836 ioctl(int fd, unsigned long cmd, ...)
837 {
838 int (*op_ioctl)(int, unsigned long cmd, ...);
839 va_list ap;
840 int rv;
841
842 DPRINTF(("ioctl -> %d\n", fd));
843 if (fd_isrump(fd)) {
844 fd = fd_host2rump(fd);
845 op_ioctl = GETSYSCALL(rump, IOCTL);
846 } else {
847 op_ioctl = GETSYSCALL(host, IOCTL);
848 }
849
850 va_start(ap, cmd);
851 rv = op_ioctl(fd, cmd, va_arg(ap, void *));
852 va_end(ap);
853 return rv;
854 }
855
856 #include <syslog.h>
857 int
858 fcntl(int fd, int cmd, ...)
859 {
860 int (*op_fcntl)(int, int, ...);
861 va_list ap;
862 int rv, minfd, i;
863
864 DPRINTF(("fcntl -> %d (cmd %d)\n", fd, cmd));
865
866 switch (cmd) {
867 case F_DUPFD:
868 va_start(ap, cmd);
869 minfd = va_arg(ap, int);
870 va_end(ap);
871 return dodup(fd, minfd);
872
873 case F_CLOSEM:
874 /*
875 * So, if fd < HIJACKOFF, we want to do a host closem.
876 */
877
878 if (fd < HIJACK_FDOFF) {
879 int closemfd = fd;
880
881 if (rumpclient__closenotify(&closemfd,
882 RUMPCLIENT_CLOSE_FCLOSEM) == -1)
883 return -1;
884 op_fcntl = GETSYSCALL(host, FCNTL);
885 rv = op_fcntl(closemfd, cmd);
886 if (rv)
887 return rv;
888 }
889
890 /*
891 * Additionally, we want to do a rump closem, but only
892 * for the file descriptors not within the dup2mask.
893 */
894
895 /* why don't we offer fls()? */
896 for (i = 15; i >= 0; i--) {
897 if (ISDUP2D(i))
898 break;
899 }
900
901 if (fd >= HIJACK_FDOFF)
902 fd -= HIJACK_FDOFF;
903 else
904 fd = 0;
905 fd = MAX(i+1, fd);
906
907 /* hmm, maybe we should close rump fd's not within dup2mask? */
908
909 return rump_sys_fcntl(fd, F_CLOSEM);
910
911 case F_MAXFD:
912 /*
913 * For maxfd, if there's a rump kernel fd, return
914 * it hostified. Otherwise, return host's MAXFD
915 * return value.
916 */
917 if ((rv = rump_sys_fcntl(fd, F_MAXFD)) != -1) {
918 /*
919 * This might go a little wrong in case
920 * of dup2 to [012], but I'm not sure if
921 * there's a justification for tracking
922 * that info. Consider e.g.
923 * dup2(rumpfd, 2) followed by rump_sys_open()
924 * returning 1. We should return 1+HIJACKOFF,
925 * not 2+HIJACKOFF. However, if [01] is not
926 * open, the correct return value is 2.
927 */
928 return fd_rump2host(fd);
929 } else {
930 op_fcntl = GETSYSCALL(host, FCNTL);
931 return op_fcntl(fd, F_MAXFD);
932 }
933 /*NOTREACHED*/
934
935 default:
936 if (fd_isrump(fd)) {
937 fd = fd_host2rump(fd);
938 op_fcntl = GETSYSCALL(rump, FCNTL);
939 } else {
940 op_fcntl = GETSYSCALL(host, FCNTL);
941 }
942
943 va_start(ap, cmd);
944 rv = op_fcntl(fd, cmd, va_arg(ap, void *));
945 va_end(ap);
946 return rv;
947 }
948 /*NOTREACHED*/
949 }
950
951 int
952 close(int fd)
953 {
954 int (*op_close)(int);
955 int rv;
956
957 DPRINTF(("close -> %d\n", fd));
958 if (fd_isrump(fd)) {
959 int undup2 = 0;
960
961 fd = fd_host2rump(fd);
962 if (ISDUP2ALIAS(fd)) {
963 _DIAGASSERT(ISDUP2D(fd));
964 CLRDUP2ALIAS(fd);
965 return 0;
966 }
967
968 if (ISDUP2D(fd))
969 undup2 = 1;
970 op_close = GETSYSCALL(rump, CLOSE);
971 rv = op_close(fd);
972 if (rv == 0 && undup2)
973 CLRDUP2(fd);
974 } else {
975 if (rumpclient__closenotify(&fd, RUMPCLIENT_CLOSE_CLOSE) == -1)
976 return -1;
977 op_close = GETSYSCALL(host, CLOSE);
978 rv = op_close(fd);
979 }
980
981 return rv;
982 }
983
984 /*
985 * write cannot issue a standard debug printf due to recursion
986 */
987 ssize_t
988 write(int fd, const void *buf, size_t blen)
989 {
990 ssize_t (*op_write)(int, const void *, size_t);
991
992 if (fd_isrump(fd)) {
993 fd = fd_host2rump(fd);
994 op_write = GETSYSCALL(rump, WRITE);
995 } else {
996 op_write = GETSYSCALL(host, WRITE);
997 }
998
999 return op_write(fd, buf, blen);
1000 }
1001
1002 /*
1003 * dup2 is special. we allow dup2 of a rump kernel fd to 0-2 since
1004 * many programs do that. dup2 of a rump kernel fd to another value
1005 * not >= fdoff is an error.
1006 *
1007 * Note: cannot rump2host newd, because it is often hardcoded.
1008 */
1009 int
1010 dup2(int oldd, int newd)
1011 {
1012 int (*host_dup2)(int, int);
1013 int rv;
1014
1015 DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
1016
1017 if (fd_isrump(oldd)) {
1018 if (!(newd >= 0 && newd <= 2)) {
1019 errno = EBADF;
1020 return -1;
1021 }
1022 oldd = fd_host2rump(oldd);
1023 if (oldd == newd) {
1024 SETDUP2(newd);
1025 SETDUP2ALIAS(newd);
1026 return newd;
1027 }
1028 rv = rump_sys_dup2(oldd, newd);
1029 if (rv != -1)
1030 SETDUP2(newd);
1031 } else {
1032 host_dup2 = syscalls[DUALCALL_DUP2].bs_host;
1033 if (rumpclient__closenotify(&newd, RUMPCLIENT_CLOSE_DUP2) == -1)
1034 return -1;
1035 rv = host_dup2(oldd, newd);
1036 }
1037
1038 return rv;
1039 }
1040
1041 int
1042 dup(int oldd)
1043 {
1044
1045 return dodup(oldd, 0);
1046 }
1047
1048 pid_t
1049 fork()
1050 {
1051 pid_t rv;
1052
1053 DPRINTF(("fork\n"));
1054
1055 rv = rumpclient__dofork(host_fork);
1056
1057 DPRINTF(("fork returns %d\n", rv));
1058 return rv;
1059 }
1060 /* we do not have the luxury of not requiring a stackframe */
1061 __strong_alias(__vfork14,fork);
1062
1063 int
1064 daemon(int nochdir, int noclose)
1065 {
1066 struct rumpclient_fork *rf;
1067
1068 if ((rf = rumpclient_prefork()) == NULL)
1069 return -1;
1070
1071 if (host_daemon(nochdir, noclose) == -1)
1072 return -1;
1073
1074 if (rumpclient_fork_init(rf) == -1)
1075 return -1;
1076
1077 return 0;
1078 }
1079
1080 int
1081 execve(const char *path, char *const argv[], char *const envp[])
1082 {
1083 char buf[128];
1084 char *dup2str;
1085 const char *pwdinrumpstr;
1086 char **newenv;
1087 size_t nelem;
1088 int rv, sverrno;
1089 int bonus = 1, i = 0;
1090
1091 if (dup2mask) {
1092 snprintf(buf, sizeof(buf), "RUMPHIJACK__DUP2MASK=%u", dup2mask);
1093 dup2str = malloc(strlen(buf)+1);
1094 if (dup2str == NULL) {
1095 errno = ENOMEM;
1096 return -1;
1097 }
1098 strcpy(dup2str, buf);
1099 bonus++;
1100 } else {
1101 dup2str = NULL;
1102 }
1103
1104 if (pwdinrump) {
1105 pwdinrumpstr = "RUMPHIJACK__PWDINRUMP=true";
1106 bonus++;
1107 } else {
1108 pwdinrumpstr = NULL;
1109 }
1110
1111 for (nelem = 0; envp && envp[nelem]; nelem++)
1112 continue;
1113 newenv = malloc(sizeof(*newenv) * nelem+bonus);
1114 if (newenv == NULL) {
1115 free(dup2str);
1116 errno = ENOMEM;
1117 return -1;
1118 }
1119 memcpy(newenv, envp, nelem*sizeof(*newenv));
1120 if (dup2str) {
1121 newenv[nelem+i] = dup2str;
1122 i++;
1123 }
1124 if (pwdinrumpstr) {
1125 newenv[nelem+i] = __UNCONST(pwdinrumpstr);
1126 i++;
1127 }
1128 newenv[nelem+i] = NULL;
1129 _DIAGASSERT(i < bonus);
1130
1131 rv = rumpclient_exec(path, argv, newenv);
1132
1133 _DIAGASSERT(rv != 0);
1134 sverrno = errno;
1135 free(newenv);
1136 free(dup2str);
1137 errno = sverrno;
1138 return rv;
1139 }
1140
1141 /*
1142 * select is done by calling poll.
1143 */
1144 int
1145 REALSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
1146 struct timeval *timeout)
1147 {
1148 struct pollfd *pfds;
1149 struct timespec ts, *tsp = NULL;
1150 nfds_t realnfds;
1151 int i, j;
1152 int rv, incr;
1153
1154 DPRINTF(("select\n"));
1155
1156 /*
1157 * Well, first we must scan the fds to figure out how many
1158 * fds there really are. This is because up to and including
1159 * nb5 poll() silently refuses nfds > process_maxopen_fds.
1160 * Seems to be fixed in current, thank the maker.
1161 * god damn cluster...bomb.
1162 */
1163
1164 for (i = 0, realnfds = 0; i < nfds; i++) {
1165 if (readfds && FD_ISSET(i, readfds)) {
1166 realnfds++;
1167 continue;
1168 }
1169 if (writefds && FD_ISSET(i, writefds)) {
1170 realnfds++;
1171 continue;
1172 }
1173 if (exceptfds && FD_ISSET(i, exceptfds)) {
1174 realnfds++;
1175 continue;
1176 }
1177 }
1178
1179 if (realnfds) {
1180 pfds = calloc(realnfds, sizeof(*pfds));
1181 if (!pfds)
1182 return -1;
1183 } else {
1184 pfds = NULL;
1185 }
1186
1187 for (i = 0, j = 0; i < nfds; i++) {
1188 incr = 0;
1189 if (readfds && FD_ISSET(i, readfds)) {
1190 pfds[j].fd = i;
1191 pfds[j].events |= POLLIN;
1192 incr=1;
1193 }
1194 if (writefds && FD_ISSET(i, writefds)) {
1195 pfds[j].fd = i;
1196 pfds[j].events |= POLLOUT;
1197 incr=1;
1198 }
1199 if (exceptfds && FD_ISSET(i, exceptfds)) {
1200 pfds[j].fd = i;
1201 pfds[j].events |= POLLHUP|POLLERR;
1202 incr=1;
1203 }
1204 if (incr)
1205 j++;
1206 }
1207 assert(j == (int)realnfds);
1208
1209 if (timeout) {
1210 TIMEVAL_TO_TIMESPEC(timeout, &ts);
1211 tsp = &ts;
1212 }
1213 rv = REALPOLLTS(pfds, realnfds, tsp, NULL);
1214 /*
1215 * "If select() returns with an error the descriptor sets
1216 * will be unmodified"
1217 */
1218 if (rv < 0)
1219 goto out;
1220
1221 /*
1222 * zero out results (can't use FD_ZERO for the
1223 * obvious select-me-not reason). whee.
1224 *
1225 * We do this here since some software ignores the return
1226 * value of select, and hence if the timeout expires, it may
1227 * assume all input descriptors have activity.
1228 */
1229 for (i = 0; i < nfds; i++) {
1230 if (readfds)
1231 FD_CLR(i, readfds);
1232 if (writefds)
1233 FD_CLR(i, writefds);
1234 if (exceptfds)
1235 FD_CLR(i, exceptfds);
1236 }
1237 if (rv == 0)
1238 goto out;
1239
1240 /*
1241 * We have >0 fds with activity. Harvest the results.
1242 */
1243 for (i = 0; i < (int)realnfds; i++) {
1244 if (readfds) {
1245 if (pfds[i].revents & POLLIN) {
1246 FD_SET(pfds[i].fd, readfds);
1247 }
1248 }
1249 if (writefds) {
1250 if (pfds[i].revents & POLLOUT) {
1251 FD_SET(pfds[i].fd, writefds);
1252 }
1253 }
1254 if (exceptfds) {
1255 if (pfds[i].revents & (POLLHUP|POLLERR)) {
1256 FD_SET(pfds[i].fd, exceptfds);
1257 }
1258 }
1259 }
1260
1261 out:
1262 free(pfds);
1263 return rv;
1264 }
1265
1266 static void
1267 checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
1268 {
1269 nfds_t i;
1270
1271 for (i = 0; i < nfds; i++) {
1272 if (fds[i].fd == -1)
1273 continue;
1274
1275 if (fd_isrump(fds[i].fd))
1276 (*rumpcall)++;
1277 else
1278 (*hostcall)++;
1279 }
1280 }
1281
1282 static void
1283 adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
1284 {
1285 nfds_t i;
1286
1287 for (i = 0; i < nfds; i++) {
1288 fds[i].fd = fdadj(fds[i].fd);
1289 }
1290 }
1291
1292 /*
1293 * poll is easy as long as the call comes in the fds only in one
1294 * kernel. otherwise its quite tricky...
1295 */
1296 struct pollarg {
1297 struct pollfd *pfds;
1298 nfds_t nfds;
1299 const struct timespec *ts;
1300 const sigset_t *sigmask;
1301 int pipefd;
1302 int errnum;
1303 };
1304
1305 static void *
1306 hostpoll(void *arg)
1307 {
1308 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
1309 const sigset_t *);
1310 struct pollarg *parg = arg;
1311 intptr_t rv;
1312
1313 op_pollts = GETSYSCALL(host, POLLTS);
1314 rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
1315 if (rv == -1)
1316 parg->errnum = errno;
1317 rump_sys_write(parg->pipefd, &rv, sizeof(rv));
1318
1319 return (void *)(intptr_t)rv;
1320 }
1321
1322 int
1323 REALPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
1324 const sigset_t *sigmask)
1325 {
1326 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
1327 const sigset_t *);
1328 int (*host_close)(int);
1329 int hostcall = 0, rumpcall = 0;
1330 pthread_t pt;
1331 nfds_t i;
1332 int rv;
1333
1334 DPRINTF(("poll\n"));
1335 checkpoll(fds, nfds, &hostcall, &rumpcall);
1336
1337 if (hostcall && rumpcall) {
1338 struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
1339 int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
1340 struct pollarg parg;
1341 uintptr_t lrv;
1342 int sverrno = 0, trv;
1343
1344 /*
1345 * ok, this is where it gets tricky. We must support
1346 * this since it's a very common operation in certain
1347 * types of software (telnet, netcat, etc). We allocate
1348 * two vectors and run two poll commands in separate
1349 * threads. Whichever returns first "wins" and the
1350 * other kernel's fds won't show activity.
1351 */
1352 rv = -1;
1353
1354 /* allocate full vector for O(n) joining after call */
1355 pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
1356 if (!pfd_host)
1357 goto out;
1358 pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
1359 if (!pfd_rump) {
1360 goto out;
1361 }
1362
1363 /*
1364 * then, open two pipes, one for notifications
1365 * to each kernel.
1366 */
1367 if ((rv = rump_sys_pipe(rpipe)) == -1) {
1368 sverrno = errno;
1369 }
1370 if (rv == 0 && (rv = pipe(hpipe)) == -1) {
1371 sverrno = errno;
1372 }
1373
1374 /* split vectors (or signal errors) */
1375 for (i = 0; i < nfds; i++) {
1376 int fd;
1377
1378 fds[i].revents = 0;
1379 if (fds[i].fd == -1) {
1380 pfd_host[i].fd = -1;
1381 pfd_rump[i].fd = -1;
1382 } else if (fd_isrump(fds[i].fd)) {
1383 pfd_host[i].fd = -1;
1384 fd = fd_host2rump(fds[i].fd);
1385 if (fd == rpipe[0] || fd == rpipe[1]) {
1386 fds[i].revents = POLLNVAL;
1387 if (rv != -1)
1388 rv++;
1389 }
1390 pfd_rump[i].fd = fd;
1391 pfd_rump[i].events = fds[i].events;
1392 } else {
1393 pfd_rump[i].fd = -1;
1394 fd = fds[i].fd;
1395 if (fd == hpipe[0] || fd == hpipe[1]) {
1396 fds[i].revents = POLLNVAL;
1397 if (rv != -1)
1398 rv++;
1399 }
1400 pfd_host[i].fd = fd;
1401 pfd_host[i].events = fds[i].events;
1402 }
1403 pfd_rump[i].revents = pfd_host[i].revents = 0;
1404 }
1405 if (rv) {
1406 goto out;
1407 }
1408
1409 pfd_host[nfds].fd = hpipe[0];
1410 pfd_host[nfds].events = POLLIN;
1411 pfd_rump[nfds].fd = rpipe[0];
1412 pfd_rump[nfds].events = POLLIN;
1413
1414 /*
1415 * then, create a thread to do host part and meanwhile
1416 * do rump kernel part right here
1417 */
1418
1419 parg.pfds = pfd_host;
1420 parg.nfds = nfds+1;
1421 parg.ts = ts;
1422 parg.sigmask = sigmask;
1423 parg.pipefd = rpipe[1];
1424 pthread_create(&pt, NULL, hostpoll, &parg);
1425
1426 op_pollts = GETSYSCALL(rump, POLLTS);
1427 lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
1428 sverrno = errno;
1429 write(hpipe[1], &rv, sizeof(rv));
1430 pthread_join(pt, (void *)&trv);
1431
1432 /* check who "won" and merge results */
1433 if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
1434 rv = trv;
1435
1436 for (i = 0; i < nfds; i++) {
1437 if (pfd_rump[i].fd != -1)
1438 fds[i].revents = pfd_rump[i].revents;
1439 }
1440 sverrno = parg.errnum;
1441 } else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
1442 rv = trv;
1443
1444 for (i = 0; i < nfds; i++) {
1445 if (pfd_host[i].fd != -1)
1446 fds[i].revents = pfd_host[i].revents;
1447 }
1448 } else {
1449 rv = 0;
1450 }
1451
1452 out:
1453 host_close = GETSYSCALL(host, CLOSE);
1454 if (rpipe[0] != -1)
1455 rump_sys_close(rpipe[0]);
1456 if (rpipe[1] != -1)
1457 rump_sys_close(rpipe[1]);
1458 if (hpipe[0] != -1)
1459 host_close(hpipe[0]);
1460 if (hpipe[1] != -1)
1461 host_close(hpipe[1]);
1462 free(pfd_host);
1463 free(pfd_rump);
1464 errno = sverrno;
1465 } else {
1466 if (hostcall) {
1467 op_pollts = GETSYSCALL(host, POLLTS);
1468 } else {
1469 op_pollts = GETSYSCALL(rump, POLLTS);
1470 adjustpoll(fds, nfds, fd_host2rump);
1471 }
1472
1473 rv = op_pollts(fds, nfds, ts, sigmask);
1474 if (rumpcall)
1475 adjustpoll(fds, nfds, fd_rump2host);
1476 }
1477
1478 return rv;
1479 }
1480
1481 int
1482 poll(struct pollfd *fds, nfds_t nfds, int timeout)
1483 {
1484 struct timespec ts;
1485 struct timespec *tsp = NULL;
1486
1487 if (timeout != INFTIM) {
1488 ts.tv_sec = timeout / 1000;
1489 ts.tv_nsec = (timeout % 1000) * 1000*1000;
1490
1491 tsp = &ts;
1492 }
1493
1494 return REALPOLLTS(fds, nfds, tsp, NULL);
1495 }
1496
1497 int
1498 REALKEVENT(int kq, const struct kevent *changelist, size_t nchanges,
1499 struct kevent *eventlist, size_t nevents,
1500 const struct timespec *timeout)
1501 {
1502 int (*op_kevent)(int, const struct kevent *, size_t,
1503 struct kevent *, size_t, const struct timespec *);
1504 const struct kevent *ev;
1505 size_t i;
1506
1507 /*
1508 * Check that we don't attempt to kevent rump kernel fd's.
1509 * That needs similar treatment to select/poll, but is slightly
1510 * trickier since we need to manage to different kq descriptors.
1511 * (TODO, in case you're wondering).
1512 */
1513 for (i = 0; i < nchanges; i++) {
1514 ev = &changelist[i];
1515 if (ev->filter == EVFILT_READ || ev->filter == EVFILT_WRITE ||
1516 ev->filter == EVFILT_VNODE) {
1517 if (fd_isrump((int)ev->ident)) {
1518 errno = ENOTSUP;
1519 return -1;
1520 }
1521 }
1522 }
1523
1524 op_kevent = GETSYSCALL(host, KEVENT);
1525 return op_kevent(kq, changelist, nchanges, eventlist, nevents, timeout);
1526 }
1527
1528 /*
1529 * mmapping from a rump kernel is not supported, so disallow it.
1530 */
1531 void *
1532 mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
1533 {
1534
1535 if (flags & MAP_FILE && fd_isrump(fd)) {
1536 errno = ENOSYS;
1537 return MAP_FAILED;
1538 }
1539 return host_mmap(addr, len, prot, flags, fd, offset);
1540 }
1541
1542 /*
1543 * Rest are std type calls.
1544 */
1545
1546 FDCALL(int, bind, DUALCALL_BIND, \
1547 (int fd, const struct sockaddr *name, socklen_t namelen), \
1548 (int, const struct sockaddr *, socklen_t), \
1549 (fd, name, namelen))
1550
1551 FDCALL(int, connect, DUALCALL_CONNECT, \
1552 (int fd, const struct sockaddr *name, socklen_t namelen), \
1553 (int, const struct sockaddr *, socklen_t), \
1554 (fd, name, namelen))
1555
1556 FDCALL(int, getpeername, DUALCALL_GETPEERNAME, \
1557 (int fd, struct sockaddr *name, socklen_t *namelen), \
1558 (int, struct sockaddr *, socklen_t *), \
1559 (fd, name, namelen))
1560
1561 FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, \
1562 (int fd, struct sockaddr *name, socklen_t *namelen), \
1563 (int, struct sockaddr *, socklen_t *), \
1564 (fd, name, namelen))
1565
1566 FDCALL(int, listen, DUALCALL_LISTEN, \
1567 (int fd, int backlog), \
1568 (int, int), \
1569 (fd, backlog))
1570
1571 FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, \
1572 (int fd, void *buf, size_t len, int flags, \
1573 struct sockaddr *from, socklen_t *fromlen), \
1574 (int, void *, size_t, int, struct sockaddr *, socklen_t *), \
1575 (fd, buf, len, flags, from, fromlen))
1576
1577 FDCALL(ssize_t, sendto, DUALCALL_SENDTO, \
1578 (int fd, const void *buf, size_t len, int flags, \
1579 const struct sockaddr *to, socklen_t tolen), \
1580 (int, const void *, size_t, int, \
1581 const struct sockaddr *, socklen_t), \
1582 (fd, buf, len, flags, to, tolen))
1583
1584 FDCALL(ssize_t, recvmsg, DUALCALL_RECVMSG, \
1585 (int fd, struct msghdr *msg, int flags), \
1586 (int, struct msghdr *, int), \
1587 (fd, msg, flags))
1588
1589 FDCALL(ssize_t, sendmsg, DUALCALL_SENDMSG, \
1590 (int fd, const struct msghdr *msg, int flags), \
1591 (int, const struct msghdr *, int), \
1592 (fd, msg, flags))
1593
1594 FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, \
1595 (int fd, int level, int optn, void *optval, socklen_t *optlen), \
1596 (int, int, int, void *, socklen_t *), \
1597 (fd, level, optn, optval, optlen))
1598
1599 FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, \
1600 (int fd, int level, int optn, \
1601 const void *optval, socklen_t optlen), \
1602 (int, int, int, const void *, socklen_t), \
1603 (fd, level, optn, optval, optlen))
1604
1605 FDCALL(int, shutdown, DUALCALL_SHUTDOWN, \
1606 (int fd, int how), \
1607 (int, int), \
1608 (fd, how))
1609
1610 #if _FORTIFY_SOURCE > 0
1611 #define STUB(fun) __ssp_weak_name(fun)
1612 ssize_t _sys_readlink(const char * __restrict, char * __restrict, size_t);
1613 ssize_t
1614 STUB(readlink)(const char * __restrict path, char * __restrict buf,
1615 size_t bufsiz)
1616 {
1617 return _sys_readlink(path, buf, bufsiz);
1618 }
1619
1620 char *_sys_getcwd(char *, size_t);
1621 char *
1622 STUB(getcwd)(char *buf, size_t size)
1623 {
1624 return _sys_getcwd(buf, size);
1625 }
1626 #else
1627 #define STUB(fun) fun
1628 #endif
1629
1630 FDCALL(ssize_t, REALREAD, DUALCALL_READ, \
1631 (int fd, void *buf, size_t buflen), \
1632 (int, void *, size_t), \
1633 (fd, buf, buflen))
1634
1635 FDCALL(ssize_t, readv, DUALCALL_READV, \
1636 (int fd, const struct iovec *iov, int iovcnt), \
1637 (int, const struct iovec *, int), \
1638 (fd, iov, iovcnt))
1639
1640 FDCALL(ssize_t, REALPREAD, DUALCALL_PREAD, \
1641 (int fd, void *buf, size_t nbytes, off_t offset), \
1642 (int, void *, size_t, off_t), \
1643 (fd, buf, nbytes, offset))
1644
1645 FDCALL(ssize_t, preadv, DUALCALL_PREADV, \
1646 (int fd, const struct iovec *iov, int iovcnt, off_t offset), \
1647 (int, const struct iovec *, int, off_t), \
1648 (fd, iov, iovcnt, offset))
1649
1650 FDCALL(ssize_t, writev, DUALCALL_WRITEV, \
1651 (int fd, const struct iovec *iov, int iovcnt), \
1652 (int, const struct iovec *, int), \
1653 (fd, iov, iovcnt))
1654
1655 FDCALL(ssize_t, REALPWRITE, DUALCALL_PWRITE, \
1656 (int fd, const void *buf, size_t nbytes, off_t offset), \
1657 (int, const void *, size_t, off_t), \
1658 (fd, buf, nbytes, offset))
1659
1660 FDCALL(ssize_t, pwritev, DUALCALL_PWRITEV, \
1661 (int fd, const struct iovec *iov, int iovcnt, off_t offset), \
1662 (int, const struct iovec *, int, off_t), \
1663 (fd, iov, iovcnt, offset))
1664
1665 FDCALL(int, REALFSTAT, DUALCALL_FSTAT, \
1666 (int fd, struct stat *sb), \
1667 (int, struct stat *), \
1668 (fd, sb))
1669
1670 FDCALL(int, fstatvfs1, DUALCALL_FSTATVFS1, \
1671 (int fd, struct statvfs *buf, int flags), \
1672 (int, struct statvfs *, int), \
1673 (fd, buf, flags))
1674
1675 FDCALL(off_t, lseek, DUALCALL_LSEEK, \
1676 (int fd, off_t offset, int whence), \
1677 (int, off_t, int), \
1678 (fd, offset, whence))
1679 __strong_alias(_lseek,lseek);
1680
1681 FDCALL(int, REALGETDENTS, DUALCALL_GETDENTS, \
1682 (int fd, char *buf, size_t nbytes), \
1683 (int, char *, size_t), \
1684 (fd, buf, nbytes))
1685
1686 FDCALL(int, fchown, DUALCALL_FCHOWN, \
1687 (int fd, uid_t owner, gid_t group), \
1688 (int, uid_t, gid_t), \
1689 (fd, owner, group))
1690
1691 FDCALL(int, fchmod, DUALCALL_FCHMOD, \
1692 (int fd, mode_t mode), \
1693 (int, mode_t), \
1694 (fd, mode))
1695
1696 FDCALL(int, ftruncate, DUALCALL_FTRUNCATE, \
1697 (int fd, off_t length), \
1698 (int, off_t), \
1699 (fd, length))
1700
1701 FDCALL(int, fsync, DUALCALL_FSYNC, \
1702 (int fd), \
1703 (int), \
1704 (fd))
1705
1706 FDCALL(int, fsync_range, DUALCALL_FSYNC_RANGE, \
1707 (int fd, int how, off_t start, off_t length), \
1708 (int, int, off_t, off_t), \
1709 (fd, how, start, length))
1710
1711 FDCALL(int, futimes, DUALCALL_FUTIMES, \
1712 (int fd, const struct timeval *tv), \
1713 (int, const struct timeval *), \
1714 (fd, tv))
1715
1716 FDCALL(int, fchflags, DUALCALL_FCHFLAGS, \
1717 (int fd, u_long flags), \
1718 (int, u_long), \
1719 (fd, flags))
1720
1721 /*
1722 * path-based selectors
1723 */
1724
1725 PATHCALL(int, REALSTAT, DUALCALL_STAT, \
1726 (const char *path, struct stat *sb), \
1727 (const char *, struct stat *), \
1728 (path, sb))
1729
1730 PATHCALL(int, REALLSTAT, DUALCALL_LSTAT, \
1731 (const char *path, struct stat *sb), \
1732 (const char *, struct stat *), \
1733 (path, sb))
1734
1735 PATHCALL(int, chown, DUALCALL_CHOWN, \
1736 (const char *path, uid_t owner, gid_t group), \
1737 (const char *, uid_t, gid_t), \
1738 (path, owner, group))
1739
1740 PATHCALL(int, lchown, DUALCALL_LCHOWN, \
1741 (const char *path, uid_t owner, gid_t group), \
1742 (const char *, uid_t, gid_t), \
1743 (path, owner, group))
1744
1745 PATHCALL(int, chmod, DUALCALL_CHMOD, \
1746 (const char *path, mode_t mode), \
1747 (const char *, mode_t), \
1748 (path, mode))
1749
1750 PATHCALL(int, lchmod, DUALCALL_LCHMOD, \
1751 (const char *path, mode_t mode), \
1752 (const char *, mode_t), \
1753 (path, mode))
1754
1755 PATHCALL(int, statvfs1, DUALCALL_STATVFS1, \
1756 (const char *path, struct statvfs *buf, int flags), \
1757 (const char *, struct statvfs *, int), \
1758 (path, buf, flags))
1759
1760 PATHCALL(int, unlink, DUALCALL_UNLINK, \
1761 (const char *path), \
1762 (const char *), \
1763 (path))
1764
1765 PATHCALL(int, symlink, DUALCALL_SYMLINK, \
1766 (const char *target, const char *path), \
1767 (const char *, const char *), \
1768 (target, path))
1769
1770 PATHCALL(ssize_t, readlink, DUALCALL_READLINK, \
1771 (const char *path, char *buf, size_t bufsiz), \
1772 (const char *, char *, size_t), \
1773 (path, buf, bufsiz))
1774
1775 PATHCALL(int, mkdir, DUALCALL_MKDIR, \
1776 (const char *path, mode_t mode), \
1777 (const char *, mode_t), \
1778 (path, mode))
1779
1780 PATHCALL(int, rmdir, DUALCALL_RMDIR, \
1781 (const char *path), \
1782 (const char *), \
1783 (path))
1784
1785 PATHCALL(int, utimes, DUALCALL_UTIMES, \
1786 (const char *path, const struct timeval *tv), \
1787 (const char *, const struct timeval *), \
1788 (path, tv))
1789
1790 PATHCALL(int, lutimes, DUALCALL_LUTIMES, \
1791 (const char *path, const struct timeval *tv), \
1792 (const char *, const struct timeval *), \
1793 (path, tv))
1794
1795 PATHCALL(int, chflags, DUALCALL_CHFLAGS, \
1796 (const char *path, u_long flags), \
1797 (const char *, u_long), \
1798 (path, flags))
1799
1800 PATHCALL(int, lchflags, DUALCALL_LCHFLAGS, \
1801 (const char *path, u_long flags), \
1802 (const char *, u_long), \
1803 (path, flags))
1804
1805 PATHCALL(int, truncate, DUALCALL_TRUNCATE, \
1806 (const char *path, off_t length), \
1807 (const char *, off_t), \
1808 (path, length))
1809
1810 PATHCALL(int, access, DUALCALL_ACCESS, \
1811 (const char *path, int mode), \
1812 (const char *, int), \
1813 (path, mode))
1814
1815 PATHCALL(int, REALMKNOD, DUALCALL_MKNOD, \
1816 (const char *path, mode_t mode, dev_t dev), \
1817 (const char *, mode_t, dev_t), \
1818 (path, mode, dev))
1819
1820 /*
1821 * Note: with mount the decisive parameter is the mount
1822 * destination directory. This is because we don't really know
1823 * about the "source" directory in a generic call (and besides,
1824 * it might not even exist, cf. nfs).
1825 */
1826 PATHCALL(int, REALMOUNT, DUALCALL_MOUNT, \
1827 (const char *type, const char *path, int flags, \
1828 void *data, size_t dlen), \
1829 (const char *, const char *, int, void *, size_t), \
1830 (type, path, flags, data, dlen))
1831
1832 PATHCALL(int, unmount, DUALCALL_UNMOUNT, \
1833 (const char *path, int flags), \
1834 (const char *, int), \
1835 (path, flags))
1836