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