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