hijack.c revision 1.63 1 /* $NetBSD: hijack.c,v 1.63 2011/02/21 20:11:56 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.63 2011/02/21 20:11:56 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 if (minfd >= HIJACK_FDOFF)
608 minfd -= HIJACK_FDOFF;
609 isrump = 1;
610 } else {
611 op_fcntl = GETSYSCALL(host, FCNTL);
612 isrump = 0;
613 }
614
615 newd = op_fcntl(oldd, F_DUPFD, minfd);
616
617 if (isrump)
618 newd = fd_rump2host(newd);
619 DPRINTF(("dup <- %d\n", newd));
620
621 return newd;
622 }
623
624 /*
625 * dup a host file descriptor so that it doesn't collide with the dup2mask
626 */
627 static int
628 fd_dupgood(int fd)
629 {
630 int (*op_fcntl)(int, int, ...) = GETSYSCALL(host, FCNTL);
631 int (*op_close)(int) = GETSYSCALL(host, CLOSE);
632 int ofd, i;
633
634 for (i = 1; ISDUP2D(fd); i++) {
635 ofd = fd;
636 fd = op_fcntl(ofd, F_DUPFD, i);
637 op_close(ofd);
638 }
639
640 return fd;
641 }
642
643 int
644 open(const char *path, int flags, ...)
645 {
646 int (*op_open)(const char *, int, ...);
647 bool isrump;
648 va_list ap;
649 int fd;
650
651 if (path_isrump(path)) {
652 path = path_host2rump(path);
653 op_open = GETSYSCALL(rump, OPEN);
654 isrump = true;
655 } else {
656 op_open = GETSYSCALL(host, OPEN);
657 isrump = false;
658 }
659
660 va_start(ap, flags);
661 fd = op_open(path, flags, va_arg(ap, mode_t));
662 va_end(ap);
663
664 if (isrump)
665 fd = fd_rump2host(fd);
666 else
667 fd = fd_dupgood(fd);
668 return fd;
669 }
670
671 int
672 chdir(const char *path)
673 {
674 int (*op_chdir)(const char *);
675 bool isrump;
676 int rv;
677
678 if (path_isrump(path)) {
679 op_chdir = GETSYSCALL(rump, CHDIR);
680 isrump = true;
681 path = path_host2rump(path);
682 } else {
683 op_chdir = GETSYSCALL(host, CHDIR);
684 isrump = false;
685 }
686
687 rv = op_chdir(path);
688 if (rv == 0) {
689 if (isrump)
690 pwdinrump = true;
691 else
692 pwdinrump = false;
693 }
694
695 return rv;
696 }
697
698 int
699 fchdir(int fd)
700 {
701 int (*op_fchdir)(int);
702 bool isrump;
703 int rv;
704
705 if (fd_isrump(fd)) {
706 op_fchdir = GETSYSCALL(rump, FCHDIR);
707 isrump = true;
708 fd = fd_host2rump(fd);
709 } else {
710 op_fchdir = GETSYSCALL(host, FCHDIR);
711 isrump = false;
712 }
713
714 rv = op_fchdir(fd);
715 if (rv == 0) {
716 if (isrump)
717 pwdinrump = true;
718 else
719 pwdinrump = false;
720 }
721
722 return rv;
723 }
724
725 int
726 __getcwd(char *bufp, size_t len)
727 {
728 int (*op___getcwd)(char *, size_t);
729 int rv;
730
731 if (pwdinrump) {
732 size_t prefixgap;
733 bool iamslash;
734
735 if (rumpprefix[rumpprefixlen-1] == '/')
736 iamslash = true;
737 else
738 iamslash = false;
739
740 if (iamslash)
741 prefixgap = rumpprefixlen - 1; /* ``//+path'' */
742 else
743 prefixgap = rumpprefixlen; /* ``/pfx+/path'' */
744 if (len <= prefixgap) {
745 return ERANGE;
746 }
747
748 op___getcwd = GETSYSCALL(rump, __GETCWD);
749 rv = op___getcwd(bufp + prefixgap, len - prefixgap);
750 if (rv == -1)
751 return rv;
752
753 /* augment the "/" part only for a non-root path */
754 memcpy(bufp, rumpprefix, rumpprefixlen);
755
756 /* append / only to non-root cwd */
757 if (rv != 2)
758 bufp[prefixgap] = '/';
759
760 /* don't append extra slash in the purely-slash case */
761 if (rv == 2 && !iamslash)
762 bufp[rumpprefixlen] = '\0';
763
764 return rv;
765 } else {
766 op___getcwd = GETSYSCALL(host, __GETCWD);
767 return op___getcwd(bufp, len);
768 }
769 }
770
771 int
772 rename(const char *from, const char *to)
773 {
774 int (*op_rename)(const char *, const char *);
775
776 if (path_isrump(from)) {
777 if (!path_isrump(to))
778 return EXDEV;
779
780 from = path_host2rump(from);
781 to = path_host2rump(to);
782 op_rename = GETSYSCALL(rump, RENAME);
783 } else {
784 if (path_isrump(to))
785 return EXDEV;
786
787 op_rename = GETSYSCALL(host, RENAME);
788 }
789
790 return op_rename(from, to);
791 }
792
793 int __socket30(int, int, int);
794 int
795 __socket30(int domain, int type, int protocol)
796 {
797 int (*op_socket)(int, int, int);
798 int fd;
799 bool isrump;
800
801 isrump = domain < PF_MAX && rumpsockets[domain];
802
803 if (isrump)
804 op_socket = GETSYSCALL(rump, SOCKET);
805 else
806 op_socket = GETSYSCALL(host, SOCKET);
807 fd = op_socket(domain, type, protocol);
808
809 if (isrump)
810 fd = fd_rump2host(fd);
811 else
812 fd = fd_dupgood(fd);
813 DPRINTF(("socket <- %d\n", fd));
814
815 return fd;
816 }
817
818 int
819 accept(int s, struct sockaddr *addr, socklen_t *addrlen)
820 {
821 int (*op_accept)(int, struct sockaddr *, socklen_t *);
822 int fd;
823 bool isrump;
824
825 isrump = fd_isrump(s);
826
827 DPRINTF(("accept -> %d", s));
828 if (isrump) {
829 op_accept = GETSYSCALL(rump, ACCEPT);
830 s = fd_host2rump(s);
831 } else {
832 op_accept = GETSYSCALL(host, ACCEPT);
833 }
834 fd = op_accept(s, addr, addrlen);
835 if (fd != -1 && isrump)
836 fd = fd_rump2host(fd);
837 else
838 fd = fd_dupgood(fd);
839
840 DPRINTF((" <- %d\n", fd));
841
842 return fd;
843 }
844
845 /*
846 * ioctl and fcntl are varargs calls and need special treatment
847 */
848 int
849 ioctl(int fd, unsigned long cmd, ...)
850 {
851 int (*op_ioctl)(int, unsigned long cmd, ...);
852 va_list ap;
853 int rv;
854
855 DPRINTF(("ioctl -> %d\n", fd));
856 if (fd_isrump(fd)) {
857 fd = fd_host2rump(fd);
858 op_ioctl = GETSYSCALL(rump, IOCTL);
859 } else {
860 op_ioctl = GETSYSCALL(host, IOCTL);
861 }
862
863 va_start(ap, cmd);
864 rv = op_ioctl(fd, cmd, va_arg(ap, void *));
865 va_end(ap);
866 return rv;
867 }
868
869 #include <syslog.h>
870 int
871 fcntl(int fd, int cmd, ...)
872 {
873 int (*op_fcntl)(int, int, ...);
874 va_list ap;
875 int rv, minfd, i;
876
877 DPRINTF(("fcntl -> %d (cmd %d)\n", fd, cmd));
878
879 switch (cmd) {
880 case F_DUPFD:
881 va_start(ap, cmd);
882 minfd = va_arg(ap, int);
883 va_end(ap);
884 return dodup(fd, minfd);
885
886 case F_CLOSEM:
887 /*
888 * So, if fd < HIJACKOFF, we want to do a host closem.
889 */
890
891 if (fd < HIJACK_FDOFF) {
892 int closemfd = fd;
893
894 if (rumpclient__closenotify(&closemfd,
895 RUMPCLIENT_CLOSE_FCLOSEM) == -1)
896 return -1;
897 op_fcntl = GETSYSCALL(host, FCNTL);
898 rv = op_fcntl(closemfd, cmd);
899 if (rv)
900 return rv;
901 }
902
903 /*
904 * Additionally, we want to do a rump closem, but only
905 * for the file descriptors not within the dup2mask.
906 */
907
908 /* why don't we offer fls()? */
909 for (i = 15; i >= 0; i--) {
910 if (ISDUP2D(i))
911 break;
912 }
913
914 if (fd >= HIJACK_FDOFF)
915 fd -= HIJACK_FDOFF;
916 else
917 fd = 0;
918 fd = MAX(i+1, fd);
919
920 /* hmm, maybe we should close rump fd's not within dup2mask? */
921
922 return rump_sys_fcntl(fd, F_CLOSEM);
923
924 case F_MAXFD:
925 /*
926 * For maxfd, if there's a rump kernel fd, return
927 * it hostified. Otherwise, return host's MAXFD
928 * return value.
929 */
930 if ((rv = rump_sys_fcntl(fd, F_MAXFD)) != -1) {
931 /*
932 * This might go a little wrong in case
933 * of dup2 to [012], but I'm not sure if
934 * there's a justification for tracking
935 * that info. Consider e.g.
936 * dup2(rumpfd, 2) followed by rump_sys_open()
937 * returning 1. We should return 1+HIJACKOFF,
938 * not 2+HIJACKOFF. However, if [01] is not
939 * open, the correct return value is 2.
940 */
941 return fd_rump2host(fd);
942 } else {
943 op_fcntl = GETSYSCALL(host, FCNTL);
944 return op_fcntl(fd, F_MAXFD);
945 }
946 /*NOTREACHED*/
947
948 default:
949 if (fd_isrump(fd)) {
950 fd = fd_host2rump(fd);
951 op_fcntl = GETSYSCALL(rump, FCNTL);
952 } else {
953 op_fcntl = GETSYSCALL(host, FCNTL);
954 }
955
956 va_start(ap, cmd);
957 rv = op_fcntl(fd, cmd, va_arg(ap, void *));
958 va_end(ap);
959 return rv;
960 }
961 /*NOTREACHED*/
962 }
963
964 int
965 close(int fd)
966 {
967 int (*op_close)(int);
968 int rv;
969
970 DPRINTF(("close -> %d\n", fd));
971 if (fd_isrump(fd)) {
972 int undup2 = 0;
973
974 fd = fd_host2rump(fd);
975 if (ISDUP2ALIAS(fd)) {
976 _DIAGASSERT(ISDUP2D(fd));
977 CLRDUP2ALIAS(fd);
978 return 0;
979 }
980
981 if (ISDUP2D(fd))
982 undup2 = 1;
983 op_close = GETSYSCALL(rump, CLOSE);
984 rv = op_close(fd);
985 if (rv == 0 && undup2)
986 CLRDUP2(fd);
987 } else {
988 if (rumpclient__closenotify(&fd, RUMPCLIENT_CLOSE_CLOSE) == -1)
989 return -1;
990 op_close = GETSYSCALL(host, CLOSE);
991 rv = op_close(fd);
992 }
993
994 return rv;
995 }
996
997 /*
998 * write cannot issue a standard debug printf due to recursion
999 */
1000 ssize_t
1001 write(int fd, const void *buf, size_t blen)
1002 {
1003 ssize_t (*op_write)(int, const void *, size_t);
1004
1005 if (fd_isrump(fd)) {
1006 fd = fd_host2rump(fd);
1007 op_write = GETSYSCALL(rump, WRITE);
1008 } else {
1009 op_write = GETSYSCALL(host, WRITE);
1010 }
1011
1012 return op_write(fd, buf, blen);
1013 }
1014
1015 /*
1016 * dup2 is special. we allow dup2 of a rump kernel fd to 0-2 since
1017 * many programs do that. dup2 of a rump kernel fd to another value
1018 * not >= fdoff is an error.
1019 *
1020 * Note: cannot rump2host newd, because it is often hardcoded.
1021 */
1022 int
1023 dup2(int oldd, int newd)
1024 {
1025 int (*host_dup2)(int, int);
1026 int rv;
1027
1028 DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
1029
1030 if (fd_isrump(oldd)) {
1031 if (!(newd >= 0 && newd <= 2))
1032 return EBADF;
1033 oldd = fd_host2rump(oldd);
1034 if (oldd == newd) {
1035 SETDUP2(newd);
1036 SETDUP2ALIAS(newd);
1037 return newd;
1038 }
1039 rv = rump_sys_dup2(oldd, newd);
1040 if (rv != -1)
1041 SETDUP2(newd);
1042 } else {
1043 host_dup2 = syscalls[DUALCALL_DUP2].bs_host;
1044 if (rumpclient__closenotify(&newd, RUMPCLIENT_CLOSE_DUP2) == -1)
1045 return -1;
1046 rv = host_dup2(oldd, newd);
1047 }
1048
1049 return rv;
1050 }
1051
1052 int
1053 dup(int oldd)
1054 {
1055
1056 return dodup(oldd, 0);
1057 }
1058
1059 pid_t
1060 fork()
1061 {
1062 pid_t rv;
1063
1064 DPRINTF(("fork\n"));
1065
1066 rv = rumpclient__dofork(host_fork);
1067
1068 DPRINTF(("fork returns %d\n", rv));
1069 return rv;
1070 }
1071 /* we do not have the luxury of not requiring a stackframe */
1072 __strong_alias(__vfork14,fork);
1073
1074 int
1075 daemon(int nochdir, int noclose)
1076 {
1077 struct rumpclient_fork *rf;
1078
1079 if ((rf = rumpclient_prefork()) == NULL)
1080 return -1;
1081
1082 if (host_daemon(nochdir, noclose) == -1)
1083 return -1;
1084
1085 if (rumpclient_fork_init(rf) == -1)
1086 return -1;
1087
1088 return 0;
1089 }
1090
1091 int
1092 execve(const char *path, char *const argv[], char *const envp[])
1093 {
1094 char buf[128];
1095 char *dup2str;
1096 const char *pwdinrumpstr;
1097 char **newenv;
1098 size_t nelem;
1099 int rv, sverrno;
1100 int bonus = 1, i = 0;
1101
1102 if (dup2mask) {
1103 snprintf(buf, sizeof(buf), "RUMPHIJACK__DUP2MASK=%u", dup2mask);
1104 dup2str = malloc(strlen(buf)+1);
1105 if (dup2str == NULL)
1106 return ENOMEM;
1107 strcpy(dup2str, buf);
1108 bonus++;
1109 } else {
1110 dup2str = NULL;
1111 }
1112
1113 if (pwdinrump) {
1114 pwdinrumpstr = "RUMPHIJACK__PWDINRUMP=true";
1115 bonus++;
1116 } else {
1117 pwdinrumpstr = NULL;
1118 }
1119
1120 for (nelem = 0; envp && envp[nelem]; nelem++)
1121 continue;
1122 newenv = malloc(sizeof(*newenv) * nelem+bonus);
1123 if (newenv == NULL) {
1124 free(dup2str);
1125 return ENOMEM;
1126 }
1127 memcpy(newenv, envp, nelem*sizeof(*newenv));
1128 if (dup2str) {
1129 newenv[nelem+i] = dup2str;
1130 i++;
1131 }
1132 if (pwdinrumpstr) {
1133 newenv[nelem+i] = __UNCONST(pwdinrumpstr);
1134 i++;
1135 }
1136 newenv[nelem+i] = NULL;
1137 _DIAGASSERT(i < bonus);
1138
1139 rv = rumpclient_exec(path, argv, newenv);
1140
1141 _DIAGASSERT(rv != 0);
1142 sverrno = errno;
1143 free(newenv);
1144 free(dup2str);
1145 errno = sverrno;
1146 return rv;
1147 }
1148
1149 /*
1150 * select is done by calling poll.
1151 */
1152 int
1153 REALSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
1154 struct timeval *timeout)
1155 {
1156 struct pollfd *pfds;
1157 struct timespec ts, *tsp = NULL;
1158 nfds_t realnfds;
1159 int i, j;
1160 int rv, incr;
1161
1162 DPRINTF(("select\n"));
1163
1164 /*
1165 * Well, first we must scan the fds to figure out how many
1166 * fds there really are. This is because up to and including
1167 * nb5 poll() silently refuses nfds > process_maxopen_fds.
1168 * Seems to be fixed in current, thank the maker.
1169 * god damn cluster...bomb.
1170 */
1171
1172 for (i = 0, realnfds = 0; i < nfds; i++) {
1173 if (readfds && FD_ISSET(i, readfds)) {
1174 realnfds++;
1175 continue;
1176 }
1177 if (writefds && FD_ISSET(i, writefds)) {
1178 realnfds++;
1179 continue;
1180 }
1181 if (exceptfds && FD_ISSET(i, exceptfds)) {
1182 realnfds++;
1183 continue;
1184 }
1185 }
1186
1187 if (realnfds) {
1188 pfds = calloc(realnfds, sizeof(*pfds));
1189 if (!pfds)
1190 return -1;
1191 } else {
1192 pfds = NULL;
1193 }
1194
1195 for (i = 0, j = 0; i < nfds; i++) {
1196 incr = 0;
1197 if (readfds && FD_ISSET(i, readfds)) {
1198 pfds[j].fd = i;
1199 pfds[j].events |= POLLIN;
1200 incr=1;
1201 }
1202 if (writefds && FD_ISSET(i, writefds)) {
1203 pfds[j].fd = i;
1204 pfds[j].events |= POLLOUT;
1205 incr=1;
1206 }
1207 if (exceptfds && FD_ISSET(i, exceptfds)) {
1208 pfds[j].fd = i;
1209 pfds[j].events |= POLLHUP|POLLERR;
1210 incr=1;
1211 }
1212 if (incr)
1213 j++;
1214 }
1215 assert(j == (int)realnfds);
1216
1217 if (timeout) {
1218 TIMEVAL_TO_TIMESPEC(timeout, &ts);
1219 tsp = &ts;
1220 }
1221 rv = REALPOLLTS(pfds, realnfds, tsp, NULL);
1222 /*
1223 * "If select() returns with an error the descriptor sets
1224 * will be unmodified"
1225 */
1226 if (rv < 0)
1227 goto out;
1228
1229 /*
1230 * zero out results (can't use FD_ZERO for the
1231 * obvious select-me-not reason). whee.
1232 *
1233 * We do this here since some software ignores the return
1234 * value of select, and hence if the timeout expires, it may
1235 * assume all input descriptors have activity.
1236 */
1237 for (i = 0; i < nfds; i++) {
1238 if (readfds)
1239 FD_CLR(i, readfds);
1240 if (writefds)
1241 FD_CLR(i, writefds);
1242 if (exceptfds)
1243 FD_CLR(i, exceptfds);
1244 }
1245 if (rv == 0)
1246 goto out;
1247
1248 /*
1249 * We have >0 fds with activity. Harvest the results.
1250 */
1251 for (i = 0; i < (int)realnfds; i++) {
1252 if (readfds) {
1253 if (pfds[i].revents & POLLIN) {
1254 FD_SET(pfds[i].fd, readfds);
1255 }
1256 }
1257 if (writefds) {
1258 if (pfds[i].revents & POLLOUT) {
1259 FD_SET(pfds[i].fd, writefds);
1260 }
1261 }
1262 if (exceptfds) {
1263 if (pfds[i].revents & (POLLHUP|POLLERR)) {
1264 FD_SET(pfds[i].fd, exceptfds);
1265 }
1266 }
1267 }
1268
1269 out:
1270 free(pfds);
1271 return rv;
1272 }
1273
1274 static void
1275 checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
1276 {
1277 nfds_t i;
1278
1279 for (i = 0; i < nfds; i++) {
1280 if (fds[i].fd == -1)
1281 continue;
1282
1283 if (fd_isrump(fds[i].fd))
1284 (*rumpcall)++;
1285 else
1286 (*hostcall)++;
1287 }
1288 }
1289
1290 static void
1291 adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
1292 {
1293 nfds_t i;
1294
1295 for (i = 0; i < nfds; i++) {
1296 fds[i].fd = fdadj(fds[i].fd);
1297 }
1298 }
1299
1300 /*
1301 * poll is easy as long as the call comes in the fds only in one
1302 * kernel. otherwise its quite tricky...
1303 */
1304 struct pollarg {
1305 struct pollfd *pfds;
1306 nfds_t nfds;
1307 const struct timespec *ts;
1308 const sigset_t *sigmask;
1309 int pipefd;
1310 int errnum;
1311 };
1312
1313 static void *
1314 hostpoll(void *arg)
1315 {
1316 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
1317 const sigset_t *);
1318 struct pollarg *parg = arg;
1319 intptr_t rv;
1320
1321 op_pollts = GETSYSCALL(host, POLLTS);
1322 rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
1323 if (rv == -1)
1324 parg->errnum = errno;
1325 rump_sys_write(parg->pipefd, &rv, sizeof(rv));
1326
1327 return (void *)(intptr_t)rv;
1328 }
1329
1330 int
1331 REALPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
1332 const sigset_t *sigmask)
1333 {
1334 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
1335 const sigset_t *);
1336 int (*host_close)(int);
1337 int hostcall = 0, rumpcall = 0;
1338 pthread_t pt;
1339 nfds_t i;
1340 int rv;
1341
1342 DPRINTF(("poll\n"));
1343 checkpoll(fds, nfds, &hostcall, &rumpcall);
1344
1345 if (hostcall && rumpcall) {
1346 struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
1347 int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
1348 struct pollarg parg;
1349 uintptr_t lrv;
1350 int sverrno = 0, trv;
1351
1352 /*
1353 * ok, this is where it gets tricky. We must support
1354 * this since it's a very common operation in certain
1355 * types of software (telnet, netcat, etc). We allocate
1356 * two vectors and run two poll commands in separate
1357 * threads. Whichever returns first "wins" and the
1358 * other kernel's fds won't show activity.
1359 */
1360 rv = -1;
1361
1362 /* allocate full vector for O(n) joining after call */
1363 pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
1364 if (!pfd_host)
1365 goto out;
1366 pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
1367 if (!pfd_rump) {
1368 goto out;
1369 }
1370
1371 /*
1372 * then, open two pipes, one for notifications
1373 * to each kernel.
1374 */
1375 if ((rv = rump_sys_pipe(rpipe)) == -1) {
1376 sverrno = errno;
1377 }
1378 if (rv == 0 && (rv = pipe(hpipe)) == -1) {
1379 sverrno = errno;
1380 }
1381
1382 /* split vectors (or signal errors) */
1383 for (i = 0; i < nfds; i++) {
1384 int fd;
1385
1386 fds[i].revents = 0;
1387 if (fds[i].fd == -1) {
1388 pfd_host[i].fd = -1;
1389 pfd_rump[i].fd = -1;
1390 } else if (fd_isrump(fds[i].fd)) {
1391 pfd_host[i].fd = -1;
1392 fd = fd_host2rump(fds[i].fd);
1393 if (fd == rpipe[0] || fd == rpipe[1]) {
1394 fds[i].revents = POLLNVAL;
1395 if (rv != -1)
1396 rv++;
1397 }
1398 pfd_rump[i].fd = fd;
1399 pfd_rump[i].events = fds[i].events;
1400 } else {
1401 pfd_rump[i].fd = -1;
1402 fd = fds[i].fd;
1403 if (fd == hpipe[0] || fd == hpipe[1]) {
1404 fds[i].revents = POLLNVAL;
1405 if (rv != -1)
1406 rv++;
1407 }
1408 pfd_host[i].fd = fd;
1409 pfd_host[i].events = fds[i].events;
1410 }
1411 pfd_rump[i].revents = pfd_host[i].revents = 0;
1412 }
1413 if (rv) {
1414 goto out;
1415 }
1416
1417 pfd_host[nfds].fd = hpipe[0];
1418 pfd_host[nfds].events = POLLIN;
1419 pfd_rump[nfds].fd = rpipe[0];
1420 pfd_rump[nfds].events = POLLIN;
1421
1422 /*
1423 * then, create a thread to do host part and meanwhile
1424 * do rump kernel part right here
1425 */
1426
1427 parg.pfds = pfd_host;
1428 parg.nfds = nfds+1;
1429 parg.ts = ts;
1430 parg.sigmask = sigmask;
1431 parg.pipefd = rpipe[1];
1432 pthread_create(&pt, NULL, hostpoll, &parg);
1433
1434 op_pollts = GETSYSCALL(rump, POLLTS);
1435 lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
1436 sverrno = errno;
1437 write(hpipe[1], &rv, sizeof(rv));
1438 pthread_join(pt, (void *)&trv);
1439
1440 /* check who "won" and merge results */
1441 if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
1442 rv = trv;
1443
1444 for (i = 0; i < nfds; i++) {
1445 if (pfd_rump[i].fd != -1)
1446 fds[i].revents = pfd_rump[i].revents;
1447 }
1448 sverrno = parg.errnum;
1449 } else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
1450 rv = trv;
1451
1452 for (i = 0; i < nfds; i++) {
1453 if (pfd_host[i].fd != -1)
1454 fds[i].revents = pfd_host[i].revents;
1455 }
1456 } else {
1457 rv = 0;
1458 }
1459
1460 out:
1461 host_close = GETSYSCALL(host, CLOSE);
1462 if (rpipe[0] != -1)
1463 rump_sys_close(rpipe[0]);
1464 if (rpipe[1] != -1)
1465 rump_sys_close(rpipe[1]);
1466 if (hpipe[0] != -1)
1467 host_close(hpipe[0]);
1468 if (hpipe[1] != -1)
1469 host_close(hpipe[1]);
1470 free(pfd_host);
1471 free(pfd_rump);
1472 errno = sverrno;
1473 } else {
1474 if (hostcall) {
1475 op_pollts = GETSYSCALL(host, POLLTS);
1476 } else {
1477 op_pollts = GETSYSCALL(rump, POLLTS);
1478 adjustpoll(fds, nfds, fd_host2rump);
1479 }
1480
1481 rv = op_pollts(fds, nfds, ts, sigmask);
1482 if (rumpcall)
1483 adjustpoll(fds, nfds, fd_rump2host);
1484 }
1485
1486 return rv;
1487 }
1488
1489 int
1490 poll(struct pollfd *fds, nfds_t nfds, int timeout)
1491 {
1492 struct timespec ts;
1493 struct timespec *tsp = NULL;
1494
1495 if (timeout != INFTIM) {
1496 ts.tv_sec = timeout / 1000;
1497 ts.tv_nsec = (timeout % 1000) * 1000*1000;
1498
1499 tsp = &ts;
1500 }
1501
1502 return REALPOLLTS(fds, nfds, tsp, NULL);
1503 }
1504
1505 int
1506 REALKEVENT(int kq, const struct kevent *changelist, size_t nchanges,
1507 struct kevent *eventlist, size_t nevents,
1508 const struct timespec *timeout)
1509 {
1510 int (*op_kevent)(int, const struct kevent *, size_t,
1511 struct kevent *, size_t, const struct timespec *);
1512 const struct kevent *ev;
1513 size_t i;
1514
1515 /*
1516 * Check that we don't attempt to kevent rump kernel fd's.
1517 * That needs similar treatment to select/poll, but is slightly
1518 * trickier since we need to manage to different kq descriptors.
1519 * (TODO, in case you're wondering).
1520 */
1521 for (i = 0; i < nchanges; i++) {
1522 ev = &changelist[i];
1523 if (ev->filter == EVFILT_READ || ev->filter == EVFILT_WRITE ||
1524 ev->filter == EVFILT_VNODE) {
1525 if (fd_isrump((int)ev->ident))
1526 return ENOTSUP;
1527 }
1528 }
1529
1530 op_kevent = GETSYSCALL(host, KEVENT);
1531 return op_kevent(kq, changelist, nchanges, eventlist, nevents, timeout);
1532 }
1533
1534 /*
1535 * mmapping from a rump kernel is not supported, so disallow it.
1536 */
1537 void *
1538 mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
1539 {
1540
1541 if (flags & MAP_FILE && fd_isrump(fd)) {
1542 errno = ENOSYS;
1543 return MAP_FAILED;
1544 }
1545 return host_mmap(addr, len, prot, flags, fd, offset);
1546 }
1547
1548 /*
1549 * Rest are std type calls.
1550 */
1551
1552 FDCALL(int, bind, DUALCALL_BIND, \
1553 (int fd, const struct sockaddr *name, socklen_t namelen), \
1554 (int, const struct sockaddr *, socklen_t), \
1555 (fd, name, namelen))
1556
1557 FDCALL(int, connect, DUALCALL_CONNECT, \
1558 (int fd, const struct sockaddr *name, socklen_t namelen), \
1559 (int, const struct sockaddr *, socklen_t), \
1560 (fd, name, namelen))
1561
1562 FDCALL(int, getpeername, DUALCALL_GETPEERNAME, \
1563 (int fd, struct sockaddr *name, socklen_t *namelen), \
1564 (int, struct sockaddr *, socklen_t *), \
1565 (fd, name, namelen))
1566
1567 FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, \
1568 (int fd, struct sockaddr *name, socklen_t *namelen), \
1569 (int, struct sockaddr *, socklen_t *), \
1570 (fd, name, namelen))
1571
1572 FDCALL(int, listen, DUALCALL_LISTEN, \
1573 (int fd, int backlog), \
1574 (int, int), \
1575 (fd, backlog))
1576
1577 FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, \
1578 (int fd, void *buf, size_t len, int flags, \
1579 struct sockaddr *from, socklen_t *fromlen), \
1580 (int, void *, size_t, int, struct sockaddr *, socklen_t *), \
1581 (fd, buf, len, flags, from, fromlen))
1582
1583 FDCALL(ssize_t, sendto, DUALCALL_SENDTO, \
1584 (int fd, const void *buf, size_t len, int flags, \
1585 const struct sockaddr *to, socklen_t tolen), \
1586 (int, const void *, size_t, int, \
1587 const struct sockaddr *, socklen_t), \
1588 (fd, buf, len, flags, to, tolen))
1589
1590 FDCALL(ssize_t, recvmsg, DUALCALL_RECVMSG, \
1591 (int fd, struct msghdr *msg, int flags), \
1592 (int, struct msghdr *, int), \
1593 (fd, msg, flags))
1594
1595 FDCALL(ssize_t, sendmsg, DUALCALL_SENDMSG, \
1596 (int fd, const struct msghdr *msg, int flags), \
1597 (int, const struct msghdr *, int), \
1598 (fd, msg, flags))
1599
1600 FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, \
1601 (int fd, int level, int optn, void *optval, socklen_t *optlen), \
1602 (int, int, int, void *, socklen_t *), \
1603 (fd, level, optn, optval, optlen))
1604
1605 FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, \
1606 (int fd, int level, int optn, \
1607 const void *optval, socklen_t optlen), \
1608 (int, int, int, const void *, socklen_t), \
1609 (fd, level, optn, optval, optlen))
1610
1611 FDCALL(int, shutdown, DUALCALL_SHUTDOWN, \
1612 (int fd, int how), \
1613 (int, int), \
1614 (fd, how))
1615
1616 #if _FORTIFY_SOURCE > 0
1617 #define STUB(fun) __ssp_weak_name(fun)
1618 ssize_t _sys_readlink(const char * __restrict, char * __restrict, size_t);
1619 ssize_t
1620 STUB(readlink)(const char * __restrict path, char * __restrict buf,
1621 size_t bufsiz)
1622 {
1623 return _sys_readlink(path, buf, bufsiz);
1624 }
1625
1626 char *_sys_getcwd(char *, size_t);
1627 char *
1628 STUB(getcwd)(char *buf, size_t size)
1629 {
1630 return _sys_getcwd(buf, size);
1631 }
1632 #else
1633 #define STUB(fun) fun
1634 #endif
1635
1636 FDCALL(ssize_t, REALREAD, DUALCALL_READ, \
1637 (int fd, void *buf, size_t buflen), \
1638 (int, void *, size_t), \
1639 (fd, buf, buflen))
1640
1641 FDCALL(ssize_t, readv, DUALCALL_READV, \
1642 (int fd, const struct iovec *iov, int iovcnt), \
1643 (int, const struct iovec *, int), \
1644 (fd, iov, iovcnt))
1645
1646 FDCALL(ssize_t, REALPREAD, DUALCALL_PREAD, \
1647 (int fd, void *buf, size_t nbytes, off_t offset), \
1648 (int, void *, size_t, off_t), \
1649 (fd, buf, nbytes, offset))
1650
1651 FDCALL(ssize_t, preadv, DUALCALL_PREADV, \
1652 (int fd, const struct iovec *iov, int iovcnt, off_t offset), \
1653 (int, const struct iovec *, int, off_t), \
1654 (fd, iov, iovcnt, offset))
1655
1656 FDCALL(ssize_t, writev, DUALCALL_WRITEV, \
1657 (int fd, const struct iovec *iov, int iovcnt), \
1658 (int, const struct iovec *, int), \
1659 (fd, iov, iovcnt))
1660
1661 FDCALL(ssize_t, REALPWRITE, DUALCALL_PWRITE, \
1662 (int fd, const void *buf, size_t nbytes, off_t offset), \
1663 (int, const void *, size_t, off_t), \
1664 (fd, buf, nbytes, offset))
1665
1666 FDCALL(ssize_t, pwritev, DUALCALL_PWRITEV, \
1667 (int fd, const struct iovec *iov, int iovcnt, off_t offset), \
1668 (int, const struct iovec *, int, off_t), \
1669 (fd, iov, iovcnt, offset))
1670
1671 FDCALL(int, REALFSTAT, DUALCALL_FSTAT, \
1672 (int fd, struct stat *sb), \
1673 (int, struct stat *), \
1674 (fd, sb))
1675
1676 FDCALL(int, fstatvfs1, DUALCALL_FSTATVFS1, \
1677 (int fd, struct statvfs *buf, int flags), \
1678 (int, struct statvfs *, int), \
1679 (fd, buf, flags))
1680
1681 FDCALL(off_t, lseek, DUALCALL_LSEEK, \
1682 (int fd, off_t offset, int whence), \
1683 (int, off_t, int), \
1684 (fd, offset, whence))
1685 __strong_alias(_lseek,lseek);
1686
1687 FDCALL(int, REALGETDENTS, DUALCALL_GETDENTS, \
1688 (int fd, char *buf, size_t nbytes), \
1689 (int, char *, size_t), \
1690 (fd, buf, nbytes))
1691
1692 FDCALL(int, fchown, DUALCALL_FCHOWN, \
1693 (int fd, uid_t owner, gid_t group), \
1694 (int, uid_t, gid_t), \
1695 (fd, owner, group))
1696
1697 FDCALL(int, fchmod, DUALCALL_FCHMOD, \
1698 (int fd, mode_t mode), \
1699 (int, mode_t), \
1700 (fd, mode))
1701
1702 FDCALL(int, ftruncate, DUALCALL_FTRUNCATE, \
1703 (int fd, off_t length), \
1704 (int, off_t), \
1705 (fd, length))
1706
1707 FDCALL(int, fsync, DUALCALL_FSYNC, \
1708 (int fd), \
1709 (int), \
1710 (fd))
1711
1712 FDCALL(int, fsync_range, DUALCALL_FSYNC_RANGE, \
1713 (int fd, int how, off_t start, off_t length), \
1714 (int, int, off_t, off_t), \
1715 (fd, how, start, length))
1716
1717 FDCALL(int, futimes, DUALCALL_FUTIMES, \
1718 (int fd, const struct timeval *tv), \
1719 (int, const struct timeval *), \
1720 (fd, tv))
1721
1722 FDCALL(int, fchflags, DUALCALL_FCHFLAGS, \
1723 (int fd, u_long flags), \
1724 (int, u_long), \
1725 (fd, flags))
1726
1727 /*
1728 * path-based selectors
1729 */
1730
1731 PATHCALL(int, REALSTAT, DUALCALL_STAT, \
1732 (const char *path, struct stat *sb), \
1733 (const char *, struct stat *), \
1734 (path, sb))
1735
1736 PATHCALL(int, REALLSTAT, DUALCALL_LSTAT, \
1737 (const char *path, struct stat *sb), \
1738 (const char *, struct stat *), \
1739 (path, sb))
1740
1741 PATHCALL(int, chown, DUALCALL_CHOWN, \
1742 (const char *path, uid_t owner, gid_t group), \
1743 (const char *, uid_t, gid_t), \
1744 (path, owner, group))
1745
1746 PATHCALL(int, lchown, DUALCALL_LCHOWN, \
1747 (const char *path, uid_t owner, gid_t group), \
1748 (const char *, uid_t, gid_t), \
1749 (path, owner, group))
1750
1751 PATHCALL(int, chmod, DUALCALL_CHMOD, \
1752 (const char *path, mode_t mode), \
1753 (const char *, mode_t), \
1754 (path, mode))
1755
1756 PATHCALL(int, lchmod, DUALCALL_LCHMOD, \
1757 (const char *path, mode_t mode), \
1758 (const char *, mode_t), \
1759 (path, mode))
1760
1761 PATHCALL(int, statvfs1, DUALCALL_STATVFS1, \
1762 (const char *path, struct statvfs *buf, int flags), \
1763 (const char *, struct statvfs *, int), \
1764 (path, buf, flags))
1765
1766 PATHCALL(int, unlink, DUALCALL_UNLINK, \
1767 (const char *path), \
1768 (const char *), \
1769 (path))
1770
1771 PATHCALL(int, symlink, DUALCALL_SYMLINK, \
1772 (const char *target, const char *path), \
1773 (const char *, const char *), \
1774 (target, path))
1775
1776 PATHCALL(ssize_t, readlink, DUALCALL_READLINK, \
1777 (const char *path, char *buf, size_t bufsiz), \
1778 (const char *, char *, size_t), \
1779 (path, buf, bufsiz))
1780
1781 PATHCALL(int, mkdir, DUALCALL_MKDIR, \
1782 (const char *path, mode_t mode), \
1783 (const char *, mode_t), \
1784 (path, mode))
1785
1786 PATHCALL(int, rmdir, DUALCALL_RMDIR, \
1787 (const char *path), \
1788 (const char *), \
1789 (path))
1790
1791 PATHCALL(int, utimes, DUALCALL_UTIMES, \
1792 (const char *path, const struct timeval *tv), \
1793 (const char *, const struct timeval *), \
1794 (path, tv))
1795
1796 PATHCALL(int, lutimes, DUALCALL_LUTIMES, \
1797 (const char *path, const struct timeval *tv), \
1798 (const char *, const struct timeval *), \
1799 (path, tv))
1800
1801 PATHCALL(int, chflags, DUALCALL_CHFLAGS, \
1802 (const char *path, u_long flags), \
1803 (const char *, u_long), \
1804 (path, flags))
1805
1806 PATHCALL(int, lchflags, DUALCALL_LCHFLAGS, \
1807 (const char *path, u_long flags), \
1808 (const char *, u_long), \
1809 (path, flags))
1810
1811 PATHCALL(int, truncate, DUALCALL_TRUNCATE, \
1812 (const char *path, off_t length), \
1813 (const char *, off_t), \
1814 (path, length))
1815
1816 /*
1817 * Note: with mount the decisive parameter is the mount
1818 * destination directory. This is because we don't really know
1819 * about the "source" directory in a generic call (and besides,
1820 * it might not even exist, cf. nfs).
1821 */
1822 PATHCALL(int, REALMOUNT, DUALCALL_MOUNT, \
1823 (const char *type, const char *path, int flags, \
1824 void *data, size_t dlen), \
1825 (const char *, const char *, int, void *, size_t), \
1826 (type, path, flags, data, dlen))
1827
1828 PATHCALL(int, unmount, DUALCALL_UNMOUNT, \
1829 (const char *path, int flags), \
1830 (const char *, int), \
1831 (path, flags))
1832