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