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