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