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