1 /* $NetBSD: hijack.c,v 1.123 2017/06/06 19:48:42 christos 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 /* 29 * XXX: rumphijack sort of works on glibc Linux. But it's not 30 * the same quality working as on NetBSD. 31 * autoconf HAVE_FOO vs. __NetBSD__ / __linux__ could be further 32 * improved. 33 */ 34 #include <rump/rumpuser_port.h> 35 36 #if !defined(lint) 37 __RCSID("$NetBSD: hijack.c,v 1.123 2017/06/06 19:48:42 christos Exp $"); 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/types.h> 42 #include <sys/ioctl.h> 43 #include <sys/mman.h> 44 #include <sys/mount.h> 45 #include <sys/socket.h> 46 #include <sys/stat.h> 47 #include <sys/time.h> 48 #include <sys/uio.h> 49 50 #ifdef __NetBSD__ 51 #include <sys/statvfs.h> 52 #endif 53 54 #ifdef HAVE_KQUEUE 55 #include <sys/event.h> 56 #endif 57 58 #ifdef __NetBSD__ 59 #include <sys/quotactl.h> 60 #endif 61 62 #include <assert.h> 63 #include <dlfcn.h> 64 #include <err.h> 65 #include <errno.h> 66 #include <fcntl.h> 67 #include <poll.h> 68 #include <pthread.h> 69 #include <signal.h> 70 #include <stdarg.h> 71 #include <stdbool.h> 72 #include <stdint.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <time.h> 77 #include <unistd.h> 78 79 #include <rump/rumpclient.h> 80 #include <rump/rump_syscalls.h> 81 82 #include "hijack.h" 83 84 /* 85 * XXX: Consider autogenerating this, syscnames[] and syscalls[] with 86 * a DSL where the tool also checks the symbols exported by this library 87 * to make sure all relevant calls are accounted for. 88 */ 89 enum dualcall { 90 DUALCALL_WRITE, DUALCALL_WRITEV, DUALCALL_PWRITE, DUALCALL_PWRITEV, 91 DUALCALL_IOCTL, DUALCALL_FCNTL, 92 DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_PACCEPT, 93 DUALCALL_BIND, DUALCALL_CONNECT, 94 DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN, 95 DUALCALL_RECVFROM, DUALCALL_RECVMSG, 96 DUALCALL_SENDTO, DUALCALL_SENDMSG, 97 DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT, 98 DUALCALL_SHUTDOWN, 99 DUALCALL_READ, DUALCALL_READV, DUALCALL_PREAD, DUALCALL_PREADV, 100 DUALCALL_DUP2, 101 DUALCALL_CLOSE, 102 DUALCALL_POLLTS, 103 104 #ifndef __linux__ 105 DUALCALL_STAT, DUALCALL_LSTAT, DUALCALL_FSTAT, 106 #endif 107 108 DUALCALL_CHMOD, DUALCALL_LCHMOD, DUALCALL_FCHMOD, 109 DUALCALL_CHOWN, DUALCALL_LCHOWN, DUALCALL_FCHOWN, 110 DUALCALL_OPEN, 111 DUALCALL_CHDIR, DUALCALL_FCHDIR, 112 DUALCALL_LSEEK, 113 DUALCALL_UNLINK, DUALCALL_SYMLINK, DUALCALL_READLINK, 114 DUALCALL_LINK, DUALCALL_RENAME, 115 DUALCALL_MKDIR, DUALCALL_RMDIR, 116 DUALCALL_UTIMES, DUALCALL_LUTIMES, DUALCALL_FUTIMES, 117 DUALCALL_UTIMENSAT, DUALCALL_FUTIMENS, 118 DUALCALL_TRUNCATE, DUALCALL_FTRUNCATE, 119 DUALCALL_FSYNC, 120 DUALCALL_ACCESS, 121 122 #ifndef __linux__ 123 DUALCALL___GETCWD, 124 DUALCALL_GETDENTS, 125 #endif 126 127 #ifndef __linux__ 128 DUALCALL_MKNOD, 129 #endif 130 131 #ifdef __NetBSD__ 132 DUALCALL_GETFH, DUALCALL_FHOPEN, DUALCALL_FHSTAT, DUALCALL_FHSTATVFS1, 133 #endif 134 135 #ifdef HAVE_KQUEUE 136 DUALCALL_KEVENT, 137 #endif 138 139 #ifdef __NetBSD__ 140 DUALCALL___SYSCTL, 141 #endif 142 143 #ifdef __NetBSD__ 144 DUALCALL_NFSSVC, 145 #endif 146 147 #ifdef __NetBSD__ 148 DUALCALL_STATVFS1, DUALCALL_FSTATVFS1, DUALCALL_GETVFSSTAT, 149 #endif 150 151 #ifdef __NetBSD__ 152 DUALCALL_MOUNT, DUALCALL_UNMOUNT, 153 #endif 154 155 #ifdef HAVE_FSYNC_RANGE 156 DUALCALL_FSYNC_RANGE, 157 #endif 158 159 #ifdef HAVE_CHFLAGS 160 DUALCALL_CHFLAGS, DUALCALL_LCHFLAGS, DUALCALL_FCHFLAGS, 161 #endif 162 163 #ifdef HAVE___QUOTACTL 164 DUALCALL_QUOTACTL, 165 #endif 166 #ifdef __NetBSD__ 167 DUALCALL_LINKAT, 168 #endif 169 DUALCALL__NUM 170 }; 171 172 #define RSYS_STRING(a) __STRING(a) 173 #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a)) 174 175 /* 176 * Would be nice to get this automatically in sync with libc. 177 * Also, this does not work for compat-using binaries (we should 178 * provide all previous interfaces, not just the current ones) 179 */ 180 #if defined(__NetBSD__) 181 182 #if !__NetBSD_Prereq__(5,99,7) 183 #define REALSELECT select 184 #define REALPOLLTS pollts 185 #define REALKEVENT kevent 186 #define REALSTAT __stat30 187 #define REALLSTAT __lstat30 188 #define REALFSTAT __fstat30 189 #define REALUTIMES utimes 190 #define REALLUTIMES lutimes 191 #define REALFUTIMES futimes 192 #define REALMKNOD mknod 193 #define REALFHSTAT __fhstat40 194 #else /* >= 5.99.7 */ 195 #define REALSELECT _sys___select50 196 #define REALPOLLTS _sys___pollts50 197 #define REALKEVENT _sys___kevent50 198 #define REALSTAT __stat50 199 #define REALLSTAT __lstat50 200 #define REALFSTAT __fstat50 201 #define REALUTIMES __utimes50 202 #define REALLUTIMES __lutimes50 203 #define REALFUTIMES __futimes50 204 #define REALMKNOD __mknod50 205 #define REALFHSTAT __fhstat50 206 #endif /* < 5.99.7 */ 207 208 #define REALREAD _sys_read 209 #define REALPREAD _sys_pread 210 #define REALPWRITE _sys_pwrite 211 #define REALGETDENTS __getdents30 212 #define REALMOUNT __mount50 213 #define REALGETFH __getfh30 214 #define REALFHOPEN __fhopen40 215 #define REALFHSTATVFS1 __fhstatvfs140 216 #define REALSOCKET __socket30 217 218 #define LSEEK_ALIAS _lseek 219 #define VFORK __vfork14 220 221 int REALSTAT(const char *, struct stat *); 222 int REALLSTAT(const char *, struct stat *); 223 int REALFSTAT(int, struct stat *); 224 int REALMKNOD(const char *, mode_t, dev_t); 225 int REALGETDENTS(int, char *, size_t); 226 227 int __getcwd(char *, size_t); 228 229 #elif defined(__linux__) /* glibc, really */ 230 231 #define REALREAD read 232 #define REALPREAD pread 233 #define REALPWRITE pwrite 234 #define REALSELECT select 235 #define REALPOLLTS ppoll 236 #define REALUTIMES utimes 237 #define REALLUTIMES lutimes 238 #define REALFUTIMES futimes 239 #define REALFHSTAT fhstat 240 #define REALSOCKET socket 241 242 #else /* !NetBSD && !linux */ 243 244 #error platform not supported 245 246 #endif /* platform */ 247 248 int REALSELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *); 249 int REALPOLLTS(struct pollfd *, nfds_t, 250 const struct timespec *, const sigset_t *); 251 int REALKEVENT(int, const struct kevent *, size_t, struct kevent *, size_t, 252 const struct timespec *); 253 ssize_t REALREAD(int, void *, size_t); 254 ssize_t REALPREAD(int, void *, size_t, off_t); 255 ssize_t REALPWRITE(int, const void *, size_t, off_t); 256 int REALUTIMES(const char *, const struct timeval [2]); 257 int REALLUTIMES(const char *, const struct timeval [2]); 258 int REALFUTIMES(int, const struct timeval [2]); 259 int REALMOUNT(const char *, const char *, int, void *, size_t); 260 int REALGETFH(const char *, void *, size_t *); 261 int REALFHOPEN(const void *, size_t, int); 262 int REALFHSTAT(const void *, size_t, struct stat *); 263 int REALFHSTATVFS1(const void *, size_t, struct statvfs *, int); 264 int REALSOCKET(int, int, int); 265 266 #define S(a) __STRING(a) 267 struct sysnames { 268 enum dualcall scm_callnum; 269 const char *scm_hostname; 270 const char *scm_rumpname; 271 } syscnames[] = { 272 { DUALCALL_SOCKET, S(REALSOCKET), RSYS_NAME(SOCKET) }, 273 { DUALCALL_ACCEPT, "accept", RSYS_NAME(ACCEPT) }, 274 { DUALCALL_PACCEPT, "paccept", RSYS_NAME(PACCEPT) }, 275 { DUALCALL_BIND, "bind", RSYS_NAME(BIND) }, 276 { DUALCALL_CONNECT, "connect", RSYS_NAME(CONNECT) }, 277 { DUALCALL_GETPEERNAME, "getpeername", RSYS_NAME(GETPEERNAME) }, 278 { DUALCALL_GETSOCKNAME, "getsockname", RSYS_NAME(GETSOCKNAME) }, 279 { DUALCALL_LISTEN, "listen", RSYS_NAME(LISTEN) }, 280 { DUALCALL_RECVFROM, "recvfrom", RSYS_NAME(RECVFROM) }, 281 { DUALCALL_RECVMSG, "recvmsg", RSYS_NAME(RECVMSG) }, 282 { DUALCALL_SENDTO, "sendto", RSYS_NAME(SENDTO) }, 283 { DUALCALL_SENDMSG, "sendmsg", RSYS_NAME(SENDMSG) }, 284 { DUALCALL_GETSOCKOPT, "getsockopt", RSYS_NAME(GETSOCKOPT) }, 285 { DUALCALL_SETSOCKOPT, "setsockopt", RSYS_NAME(SETSOCKOPT) }, 286 { DUALCALL_SHUTDOWN, "shutdown", RSYS_NAME(SHUTDOWN) }, 287 { DUALCALL_READ, S(REALREAD), RSYS_NAME(READ) }, 288 { DUALCALL_READV, "readv", RSYS_NAME(READV) }, 289 { DUALCALL_PREAD, S(REALPREAD), RSYS_NAME(PREAD) }, 290 { DUALCALL_PREADV, "preadv", RSYS_NAME(PREADV) }, 291 { DUALCALL_WRITE, "write", RSYS_NAME(WRITE) }, 292 { DUALCALL_WRITEV, "writev", RSYS_NAME(WRITEV) }, 293 { DUALCALL_PWRITE, S(REALPWRITE), RSYS_NAME(PWRITE) }, 294 { DUALCALL_PWRITEV, "pwritev", RSYS_NAME(PWRITEV) }, 295 { DUALCALL_IOCTL, "ioctl", RSYS_NAME(IOCTL) }, 296 { DUALCALL_FCNTL, "fcntl", RSYS_NAME(FCNTL) }, 297 { DUALCALL_DUP2, "dup2", RSYS_NAME(DUP2) }, 298 { DUALCALL_CLOSE, "close", RSYS_NAME(CLOSE) }, 299 { DUALCALL_POLLTS, S(REALPOLLTS), RSYS_NAME(POLLTS) }, 300 #ifndef __linux__ 301 { DUALCALL_STAT, S(REALSTAT), RSYS_NAME(STAT) }, 302 { DUALCALL_LSTAT, S(REALLSTAT), RSYS_NAME(LSTAT) }, 303 { DUALCALL_FSTAT, S(REALFSTAT), RSYS_NAME(FSTAT) }, 304 #endif 305 { DUALCALL_CHOWN, "chown", RSYS_NAME(CHOWN) }, 306 { DUALCALL_LCHOWN, "lchown", RSYS_NAME(LCHOWN) }, 307 { DUALCALL_FCHOWN, "fchown", RSYS_NAME(FCHOWN) }, 308 { DUALCALL_CHMOD, "chmod", RSYS_NAME(CHMOD) }, 309 { DUALCALL_LCHMOD, "lchmod", RSYS_NAME(LCHMOD) }, 310 { DUALCALL_FCHMOD, "fchmod", RSYS_NAME(FCHMOD) }, 311 { DUALCALL_UTIMES, S(REALUTIMES), RSYS_NAME(UTIMES) }, 312 { DUALCALL_LUTIMES, S(REALLUTIMES), RSYS_NAME(LUTIMES) }, 313 { DUALCALL_FUTIMES, S(REALFUTIMES), RSYS_NAME(FUTIMES) }, 314 { DUALCALL_UTIMENSAT, "utimensat", RSYS_NAME(UTIMENSAT) }, 315 { DUALCALL_FUTIMENS, "futimens", RSYS_NAME(FUTIMENS) }, 316 { DUALCALL_OPEN, "open", RSYS_NAME(OPEN) }, 317 { DUALCALL_CHDIR, "chdir", RSYS_NAME(CHDIR) }, 318 { DUALCALL_FCHDIR, "fchdir", RSYS_NAME(FCHDIR) }, 319 { DUALCALL_LSEEK, "lseek", RSYS_NAME(LSEEK) }, 320 { DUALCALL_UNLINK, "unlink", RSYS_NAME(UNLINK) }, 321 { DUALCALL_SYMLINK, "symlink", RSYS_NAME(SYMLINK) }, 322 { DUALCALL_READLINK, "readlink", RSYS_NAME(READLINK) }, 323 { DUALCALL_LINK, "link", RSYS_NAME(LINK) }, 324 { DUALCALL_RENAME, "rename", RSYS_NAME(RENAME) }, 325 { DUALCALL_MKDIR, "mkdir", RSYS_NAME(MKDIR) }, 326 { DUALCALL_RMDIR, "rmdir", RSYS_NAME(RMDIR) }, 327 { DUALCALL_TRUNCATE, "truncate", RSYS_NAME(TRUNCATE) }, 328 { DUALCALL_FTRUNCATE, "ftruncate", RSYS_NAME(FTRUNCATE) }, 329 { DUALCALL_FSYNC, "fsync", RSYS_NAME(FSYNC) }, 330 { DUALCALL_ACCESS, "access", RSYS_NAME(ACCESS) }, 331 332 #ifndef __linux__ 333 { DUALCALL___GETCWD, "__getcwd", RSYS_NAME(__GETCWD) }, 334 { DUALCALL_GETDENTS, S(REALGETDENTS),RSYS_NAME(GETDENTS) }, 335 #endif 336 337 #ifndef __linux__ 338 { DUALCALL_MKNOD, S(REALMKNOD), RSYS_NAME(MKNOD) }, 339 #endif 340 341 #ifdef __NetBSD__ 342 { DUALCALL_GETFH, S(REALGETFH), RSYS_NAME(GETFH) }, 343 { DUALCALL_FHOPEN, S(REALFHOPEN), RSYS_NAME(FHOPEN) }, 344 { DUALCALL_FHSTAT, S(REALFHSTAT), RSYS_NAME(FHSTAT) }, 345 { DUALCALL_FHSTATVFS1, S(REALFHSTATVFS1),RSYS_NAME(FHSTATVFS1) }, 346 #endif 347 348 #ifdef HAVE_KQUEUE 349 { DUALCALL_KEVENT, S(REALKEVENT), RSYS_NAME(KEVENT) }, 350 #endif 351 352 #ifdef __NetBSD__ 353 { DUALCALL___SYSCTL, "__sysctl", RSYS_NAME(__SYSCTL) }, 354 #endif 355 356 #ifdef __NetBSD__ 357 { DUALCALL_NFSSVC, "nfssvc", RSYS_NAME(NFSSVC) }, 358 #endif 359 360 #ifdef __NetBSD__ 361 { DUALCALL_STATVFS1, "statvfs1", RSYS_NAME(STATVFS1) }, 362 { DUALCALL_FSTATVFS1, "fstatvfs1", RSYS_NAME(FSTATVFS1) }, 363 { DUALCALL_GETVFSSTAT, "getvfsstat", RSYS_NAME(GETVFSSTAT) }, 364 #endif 365 366 #ifdef __NetBSD__ 367 { DUALCALL_MOUNT, S(REALMOUNT), RSYS_NAME(MOUNT) }, 368 { DUALCALL_UNMOUNT, "unmount", RSYS_NAME(UNMOUNT) }, 369 #endif 370 371 #ifdef HAVE_FSYNC_RANGE 372 { DUALCALL_FSYNC_RANGE, "fsync_range", RSYS_NAME(FSYNC_RANGE) }, 373 #endif 374 375 #ifdef HAVE_CHFLAGS 376 { DUALCALL_CHFLAGS, "chflags", RSYS_NAME(CHFLAGS) }, 377 { DUALCALL_LCHFLAGS, "lchflags", RSYS_NAME(LCHFLAGS) }, 378 { DUALCALL_FCHFLAGS, "fchflags", RSYS_NAME(FCHFLAGS) }, 379 #endif /* HAVE_CHFLAGS */ 380 381 #ifdef HAVE___QUOTACTL 382 { DUALCALL_QUOTACTL, "__quotactl", RSYS_NAME(__QUOTACTL) }, 383 #endif /* HAVE___QUOTACTL */ 384 385 #ifdef __NetBSD__ 386 { DUALCALL_LINKAT, "linkat", RSYS_NAME(LINKAT) }, 387 #endif 388 }; 389 #undef S 390 391 struct bothsys { 392 void *bs_host; 393 void *bs_rump; 394 } syscalls[DUALCALL__NUM]; 395 #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which 396 397 static pid_t (*host_fork)(void); 398 static int (*host_daemon)(int, int); 399 static void * (*host_mmap)(void *, size_t, int, int, int, off_t); 400 401 /* 402 * This tracks if our process is in a subdirectory of /rump. 403 * It's preserved over exec. 404 */ 405 static bool pwdinrump; 406 407 enum pathtype { PATH_HOST, PATH_RUMP, PATH_RUMPBLANKET }; 408 409 static bool fd_isrump(int); 410 static enum pathtype path_isrump(const char *); 411 412 /* default FD_SETSIZE is 256 ==> default fdoff is 128 */ 413 static int hijack_fdoff = FD_SETSIZE/2; 414 415 /* 416 * Maintain a mapping table for the usual dup2 suspects. 417 * Could use atomic ops to operate on dup2vec, but an application 418 * racing there is not well-defined, so don't bother. 419 */ 420 /* note: you cannot change this without editing the env-passing code */ 421 #define DUP2HIGH 2 422 static uint32_t dup2vec[DUP2HIGH+1]; 423 #define DUP2BIT (1<<31) 424 #define DUP2ALIAS (1<<30) 425 #define DUP2FDMASK ((1<<30)-1) 426 427 static bool 428 isdup2d(int fd) 429 { 430 431 return fd <= DUP2HIGH && fd >= 0 && dup2vec[fd] & DUP2BIT; 432 } 433 434 static int 435 mapdup2(int hostfd) 436 { 437 438 _DIAGASSERT(isdup2d(hostfd)); 439 return dup2vec[hostfd] & DUP2FDMASK; 440 } 441 442 static int 443 unmapdup2(int rumpfd) 444 { 445 int i; 446 447 for (i = 0; i <= DUP2HIGH; i++) { 448 if (dup2vec[i] & DUP2BIT && 449 (dup2vec[i] & DUP2FDMASK) == (unsigned)rumpfd) 450 return i; 451 } 452 return -1; 453 } 454 455 static void 456 setdup2(int hostfd, int rumpfd) 457 { 458 459 if (hostfd > DUP2HIGH) { 460 _DIAGASSERT(0); 461 return; 462 } 463 464 dup2vec[hostfd] = DUP2BIT | DUP2ALIAS | rumpfd; 465 } 466 467 static void 468 clrdup2(int hostfd) 469 { 470 471 if (hostfd > DUP2HIGH) { 472 _DIAGASSERT(0); 473 return; 474 } 475 476 dup2vec[hostfd] = 0; 477 } 478 479 static bool 480 killdup2alias(int rumpfd) 481 { 482 int hostfd; 483 484 if ((hostfd = unmapdup2(rumpfd)) == -1) 485 return false; 486 487 if (dup2vec[hostfd] & DUP2ALIAS) { 488 dup2vec[hostfd] &= ~DUP2ALIAS; 489 return true; 490 } 491 return false; 492 } 493 494 //#define DEBUGJACK 495 #ifdef DEBUGJACK 496 #define DPRINTF(x) mydprintf x 497 static void 498 mydprintf(const char *fmt, ...) 499 { 500 va_list ap; 501 502 if (isdup2d(STDERR_FILENO)) 503 return; 504 505 va_start(ap, fmt); 506 vfprintf(stderr, fmt, ap); 507 va_end(ap); 508 } 509 510 static const char * 511 whichfd(int fd) 512 { 513 514 if (fd == -1) 515 return "-1"; 516 else if (fd_isrump(fd)) 517 return "rump"; 518 else 519 return "host"; 520 } 521 522 static const char * 523 whichpath(const char *path) 524 { 525 526 if (path_isrump(path)) 527 return "rump"; 528 else 529 return "host"; 530 } 531 532 #else 533 #define DPRINTF(x) 534 #endif 535 536 #define ATCALL(type, name, rcname, args, proto, vars) \ 537 type name args \ 538 { \ 539 type (*fun) proto; \ 540 int isrump = -1; \ 541 \ 542 if (fd == AT_FDCWD || *path == '/') { \ 543 isrump = path_isrump(path); \ 544 } else { \ 545 isrump = fd_isrump(fd); \ 546 } \ 547 \ 548 DPRINTF(("%s -> %d:%s (%s)\n", __STRING(name), \ 549 fd, path, isrump ? "rump" : "host")); \ 550 \ 551 assert(isrump != -1); \ 552 if (isrump) { \ 553 fun = syscalls[rcname].bs_rump; \ 554 if (fd != AT_FDCWD) \ 555 fd = fd_host2rump(fd); \ 556 path = path_host2rump(path); \ 557 } else { \ 558 fun = syscalls[rcname].bs_host; \ 559 } \ 560 return fun vars; \ 561 } 562 563 #define FDCALL(type, name, rcname, args, proto, vars) \ 564 type name args \ 565 { \ 566 type (*fun) proto; \ 567 \ 568 DPRINTF(("%s -> %d (%s)\n", __STRING(name), fd, whichfd(fd))); \ 569 if (fd_isrump(fd)) { \ 570 fun = syscalls[rcname].bs_rump; \ 571 fd = fd_host2rump(fd); \ 572 } else { \ 573 fun = syscalls[rcname].bs_host; \ 574 } \ 575 \ 576 return fun vars; \ 577 } 578 579 #define PATHCALL(type, name, rcname, args, proto, vars) \ 580 type name args \ 581 { \ 582 type (*fun) proto; \ 583 enum pathtype pt; \ 584 \ 585 DPRINTF(("%s -> %s (%s)\n", __STRING(name), path, \ 586 whichpath(path))); \ 587 if ((pt = path_isrump(path)) != PATH_HOST) { \ 588 fun = syscalls[rcname].bs_rump; \ 589 if (pt == PATH_RUMP) \ 590 path = path_host2rump(path); \ 591 } else { \ 592 fun = syscalls[rcname].bs_host; \ 593 } \ 594 \ 595 return fun vars; \ 596 } 597 598 #define VFSCALL(bit, type, name, rcname, args, proto, vars) \ 599 type name args \ 600 { \ 601 type (*fun) proto; \ 602 \ 603 DPRINTF(("%s (0x%x, 0x%x)\n", __STRING(name), bit, vfsbits)); \ 604 if (vfsbits & bit) { \ 605 fun = syscalls[rcname].bs_rump; \ 606 } else { \ 607 fun = syscalls[rcname].bs_host; \ 608 } \ 609 \ 610 return fun vars; \ 611 } 612 613 /* 614 * These variables are set from the RUMPHIJACK string and control 615 * which operations can product rump kernel file descriptors. 616 * This should be easily extendable for future needs. 617 */ 618 #define RUMPHIJACK_DEFAULT "path=/rump,socket=all:nolocal" 619 static bool rumpsockets[PF_MAX]; 620 static const char *rumpprefix; 621 static size_t rumpprefixlen; 622 623 static struct { 624 int pf; 625 const char *name; 626 } socketmap[] = { 627 { PF_LOCAL, "local" }, 628 { PF_INET, "inet" }, 629 #ifdef PF_LINK 630 { PF_LINK, "link" }, 631 #endif 632 #ifdef PF_OROUTE 633 { PF_OROUTE, "oroute" }, 634 #endif 635 { PF_ROUTE, "route" }, 636 { PF_INET6, "inet6" }, 637 #ifdef PF_MPLS 638 { PF_MPLS, "mpls" }, 639 #endif 640 { -1, NULL } 641 }; 642 643 static void 644 sockparser(char *buf) 645 { 646 char *p, *l = NULL; 647 bool value; 648 int i; 649 650 /* if "all" is present, it must be specified first */ 651 if (strncmp(buf, "all", strlen("all")) == 0) { 652 for (i = 0; i < (int)__arraycount(rumpsockets); i++) { 653 rumpsockets[i] = true; 654 } 655 buf += strlen("all"); 656 if (*buf == ':') 657 buf++; 658 } 659 660 for (p = strtok_r(buf, ":", &l); p; p = strtok_r(NULL, ":", &l)) { 661 value = true; 662 if (strncmp(p, "no", strlen("no")) == 0) { 663 value = false; 664 p += strlen("no"); 665 } 666 667 for (i = 0; socketmap[i].name; i++) { 668 if (strcmp(p, socketmap[i].name) == 0) { 669 rumpsockets[socketmap[i].pf] = value; 670 break; 671 } 672 } 673 if (socketmap[i].name == NULL) { 674 errx(1, "invalid socket specifier %s", p); 675 } 676 } 677 } 678 679 static void 680 pathparser(char *buf) 681 { 682 683 /* sanity-check */ 684 if (*buf != '/') 685 errx(1, "hijack path specifier must begin with ``/''"); 686 rumpprefixlen = strlen(buf); 687 if (rumpprefixlen < 2) 688 errx(1, "invalid hijack prefix: %s", buf); 689 if (buf[rumpprefixlen-1] == '/' && strspn(buf, "/") != rumpprefixlen) 690 errx(1, "hijack prefix may end in slash only if pure " 691 "slash, gave %s", buf); 692 693 if ((rumpprefix = strdup(buf)) == NULL) 694 err(1, "strdup"); 695 rumpprefixlen = strlen(rumpprefix); 696 } 697 698 static struct blanket { 699 const char *pfx; 700 size_t len; 701 } *blanket; 702 static int nblanket; 703 704 static void 705 blanketparser(char *buf) 706 { 707 char *p, *l = NULL; 708 int i; 709 710 for (nblanket = 0, p = buf; p; p = strchr(p+1, ':'), nblanket++) 711 continue; 712 713 blanket = malloc(nblanket * sizeof(*blanket)); 714 if (blanket == NULL) 715 err(1, "alloc blanket %d", nblanket); 716 717 for (p = strtok_r(buf, ":", &l), i = 0; p; 718 p = strtok_r(NULL, ":", &l), i++) { 719 blanket[i].pfx = strdup(p); 720 if (blanket[i].pfx == NULL) 721 err(1, "strdup blanket"); 722 blanket[i].len = strlen(p); 723 724 if (blanket[i].len == 0 || *blanket[i].pfx != '/') 725 errx(1, "invalid blanket specifier %s", p); 726 if (*(blanket[i].pfx + blanket[i].len-1) == '/') 727 errx(1, "invalid blanket specifier %s", p); 728 } 729 } 730 731 #define VFSBIT_NFSSVC 0x01 732 #define VFSBIT_GETVFSSTAT 0x02 733 #define VFSBIT_FHCALLS 0x04 734 static unsigned vfsbits; 735 736 static struct { 737 int bit; 738 const char *name; 739 } vfscalls[] = { 740 { VFSBIT_NFSSVC, "nfssvc" }, 741 { VFSBIT_GETVFSSTAT, "getvfsstat" }, 742 { VFSBIT_FHCALLS, "fhcalls" }, 743 { -1, NULL } 744 }; 745 746 static void 747 vfsparser(char *buf) 748 { 749 char *p, *l = NULL; 750 bool turnon; 751 unsigned int fullmask; 752 int i; 753 754 /* build the full mask and sanity-check while we're at it */ 755 fullmask = 0; 756 for (i = 0; vfscalls[i].name != NULL; i++) { 757 if (fullmask & vfscalls[i].bit) 758 errx(1, "problem exists between vi and chair"); 759 fullmask |= vfscalls[i].bit; 760 } 761 762 763 /* if "all" is present, it must be specified first */ 764 if (strncmp(buf, "all", strlen("all")) == 0) { 765 vfsbits = fullmask; 766 buf += strlen("all"); 767 if (*buf == ':') 768 buf++; 769 } 770 771 for (p = strtok_r(buf, ":", &l); p; p = strtok_r(NULL, ":", &l)) { 772 turnon = true; 773 if (strncmp(p, "no", strlen("no")) == 0) { 774 turnon = false; 775 p += strlen("no"); 776 } 777 778 for (i = 0; vfscalls[i].name; i++) { 779 if (strcmp(p, vfscalls[i].name) == 0) { 780 if (turnon) 781 vfsbits |= vfscalls[i].bit; 782 else 783 vfsbits &= ~vfscalls[i].bit; 784 break; 785 } 786 } 787 if (vfscalls[i].name == NULL) { 788 errx(1, "invalid vfscall specifier %s", p); 789 } 790 } 791 } 792 793 static bool rumpsysctl = false; 794 795 static void 796 sysctlparser(char *buf) 797 { 798 799 if (buf == NULL) { 800 rumpsysctl = true; 801 return; 802 } 803 804 if (strcasecmp(buf, "y") == 0 || strcasecmp(buf, "yes") == 0 || 805 strcasecmp(buf, "yep") == 0 || strcasecmp(buf, "tottakai") == 0) { 806 rumpsysctl = true; 807 return; 808 } 809 if (strcasecmp(buf, "n") == 0 || strcasecmp(buf, "no") == 0) { 810 rumpsysctl = false; 811 return; 812 } 813 814 errx(1, "sysctl value should be y(es)/n(o), gave: %s", buf); 815 } 816 817 static void 818 fdoffparser(char *buf) 819 { 820 unsigned long fdoff; 821 char *ep; 822 823 if (*buf == '-') { 824 errx(1, "fdoff must not be negative"); 825 } 826 fdoff = strtoul(buf, &ep, 10); 827 if (*ep != '\0') 828 errx(1, "invalid fdoff specifier \"%s\"", buf); 829 if (fdoff >= INT_MAX/2 || fdoff < 3) 830 errx(1, "fdoff out of range"); 831 hijack_fdoff = fdoff; 832 } 833 834 static struct { 835 void (*parsefn)(char *); 836 const char *name; 837 bool needvalues; 838 } hijackparse[] = { 839 { sockparser, "socket", true }, 840 { pathparser, "path", true }, 841 { blanketparser, "blanket", true }, 842 { vfsparser, "vfs", true }, 843 { sysctlparser, "sysctl", false }, 844 { fdoffparser, "fdoff", true }, 845 { NULL, NULL, false }, 846 }; 847 848 static void 849 parsehijack(char *hijack) 850 { 851 char *p, *p2, *l; 852 const char *hijackcopy; 853 bool nop2; 854 int i; 855 856 if ((hijackcopy = strdup(hijack)) == NULL) 857 err(1, "strdup"); 858 859 /* disable everything explicitly */ 860 for (i = 0; i < PF_MAX; i++) 861 rumpsockets[i] = false; 862 863 for (p = strtok_r(hijack, ",", &l); p; p = strtok_r(NULL, ",", &l)) { 864 nop2 = false; 865 p2 = strchr(p, '='); 866 if (!p2) { 867 nop2 = true; 868 p2 = p + strlen(p); 869 } 870 871 for (i = 0; hijackparse[i].parsefn; i++) { 872 if (strncmp(hijackparse[i].name, p, 873 (size_t)(p2-p)) == 0) { 874 if (nop2 && hijackparse[i].needvalues) 875 errx(1, "invalid hijack specifier: %s", 876 hijackcopy); 877 hijackparse[i].parsefn(nop2 ? NULL : p2+1); 878 break; 879 } 880 } 881 882 if (hijackparse[i].parsefn == NULL) 883 errx(1, "invalid hijack specifier name in %s", p); 884 } 885 886 } 887 888 static void __attribute__((constructor)) 889 rcinit(void) 890 { 891 char buf[1024]; 892 unsigned i, j; 893 894 host_fork = dlsym(RTLD_NEXT, "fork"); 895 host_daemon = dlsym(RTLD_NEXT, "daemon"); 896 if (host_mmap == NULL) 897 host_mmap = dlsym(RTLD_NEXT, "mmap"); 898 899 /* 900 * In theory cannot print anything during lookups because 901 * we might not have the call vector set up. so, the errx() 902 * is a bit of a strech, but it might work. 903 */ 904 905 for (i = 0; i < DUALCALL__NUM; i++) { 906 /* build runtime O(1) access */ 907 for (j = 0; j < __arraycount(syscnames); j++) { 908 if (syscnames[j].scm_callnum == i) 909 break; 910 } 911 912 if (j == __arraycount(syscnames)) 913 errx(1, "rumphijack error: syscall pos %d missing", i); 914 915 syscalls[i].bs_host = dlsym(RTLD_NEXT, 916 syscnames[j].scm_hostname); 917 if (syscalls[i].bs_host == NULL) 918 errx(1, "hostcall %s not found!", 919 syscnames[j].scm_hostname); 920 921 syscalls[i].bs_rump = dlsym(RTLD_NEXT, 922 syscnames[j].scm_rumpname); 923 if (syscalls[i].bs_rump == NULL) 924 errx(1, "rumpcall %s not found!", 925 syscnames[j].scm_rumpname); 926 } 927 928 if (rumpclient_init() == -1) 929 err(1, "rumpclient init"); 930 931 /* check which syscalls we're supposed to hijack */ 932 if (getenv_r("RUMPHIJACK", buf, sizeof(buf)) == -1) { 933 strcpy(buf, RUMPHIJACK_DEFAULT); 934 } 935 parsehijack(buf); 936 937 /* set client persistence level */ 938 if (getenv_r("RUMPHIJACK_RETRYCONNECT", buf, sizeof(buf)) != -1) { 939 if (strcmp(buf, "die") == 0) 940 rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_DIE); 941 else if (strcmp(buf, "inftime") == 0) 942 rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME); 943 else if (strcmp(buf, "once") == 0) 944 rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_ONCE); 945 else { 946 time_t timeout; 947 char *ep; 948 949 timeout = (time_t)strtoll(buf, &ep, 10); 950 if (timeout <= 0 || ep != buf + strlen(buf)) 951 errx(1, "RUMPHIJACK_RETRYCONNECT must be " 952 "keyword or integer, got: %s", buf); 953 954 rumpclient_setconnretry(timeout); 955 } 956 } 957 958 if (getenv_r("RUMPHIJACK__DUP2INFO", buf, sizeof(buf)) == 0) { 959 if (sscanf(buf, "%u,%u,%u", 960 &dup2vec[0], &dup2vec[1], &dup2vec[2]) != 3) { 961 warnx("invalid dup2mask: %s", buf); 962 memset(dup2vec, 0, sizeof(dup2vec)); 963 } 964 unsetenv("RUMPHIJACK__DUP2INFO"); 965 } 966 if (getenv_r("RUMPHIJACK__PWDINRUMP", buf, sizeof(buf)) == 0) { 967 pwdinrump = true; 968 unsetenv("RUMPHIJACK__PWDINRUMP"); 969 } 970 } 971 972 static int 973 fd_rump2host(int fd) 974 { 975 976 if (fd == -1) 977 return fd; 978 return fd + hijack_fdoff; 979 } 980 981 static int 982 fd_rump2host_withdup(int fd) 983 { 984 int hfd; 985 986 _DIAGASSERT(fd != -1); 987 hfd = unmapdup2(fd); 988 if (hfd != -1) { 989 _DIAGASSERT(hfd <= DUP2HIGH); 990 return hfd; 991 } 992 return fd_rump2host(fd); 993 } 994 995 static int 996 fd_host2rump(int fd) 997 { 998 if (!isdup2d(fd)) 999 return fd - hijack_fdoff; 1000 else 1001 return mapdup2(fd); 1002 } 1003 1004 static bool 1005 fd_isrump(int fd) 1006 { 1007 1008 return isdup2d(fd) || fd >= hijack_fdoff; 1009 } 1010 1011 #define assertfd(_fd_) assert(ISDUP2D(_fd_) || (_fd_) >= hijack_fdoff) 1012 1013 static enum pathtype 1014 path_isrump(const char *path) 1015 { 1016 size_t plen; 1017 int i; 1018 1019 if (rumpprefix == NULL && nblanket == 0) 1020 return PATH_HOST; 1021 1022 if (*path == '/') { 1023 plen = strlen(path); 1024 if (rumpprefix && plen >= rumpprefixlen) { 1025 if (strncmp(path, rumpprefix, rumpprefixlen) == 0 1026 && (plen == rumpprefixlen 1027 || *(path + rumpprefixlen) == '/')) { 1028 return PATH_RUMP; 1029 } 1030 } 1031 for (i = 0; i < nblanket; i++) { 1032 if (strncmp(path, blanket[i].pfx, blanket[i].len) == 0) 1033 return PATH_RUMPBLANKET; 1034 } 1035 1036 return PATH_HOST; 1037 } else { 1038 return pwdinrump ? PATH_RUMP : PATH_HOST; 1039 } 1040 } 1041 1042 static const char *rootpath = "/"; 1043 static const char * 1044 path_host2rump(const char *path) 1045 { 1046 const char *rv; 1047 1048 if (*path == '/') { 1049 rv = path + rumpprefixlen; 1050 if (*rv == '\0') 1051 rv = rootpath; 1052 } else { 1053 rv = path; 1054 } 1055 1056 return rv; 1057 } 1058 1059 static int 1060 dodup(int oldd, int minfd) 1061 { 1062 int (*op_fcntl)(int, int, ...); 1063 int newd; 1064 int isrump; 1065 1066 DPRINTF(("dup -> %d (minfd %d)\n", oldd, minfd)); 1067 if (fd_isrump(oldd)) { 1068 op_fcntl = GETSYSCALL(rump, FCNTL); 1069 oldd = fd_host2rump(oldd); 1070 if (minfd >= hijack_fdoff) 1071 minfd -= hijack_fdoff; 1072 isrump = 1; 1073 } else { 1074 if (minfd >= hijack_fdoff) { 1075 errno = EINVAL; 1076 return -1; 1077 } 1078 op_fcntl = GETSYSCALL(host, FCNTL); 1079 isrump = 0; 1080 } 1081 1082 newd = op_fcntl(oldd, F_DUPFD, minfd); 1083 1084 if (isrump) 1085 newd = fd_rump2host(newd); 1086 DPRINTF(("dup <- %d\n", newd)); 1087 1088 return newd; 1089 } 1090 1091 /* 1092 * Check that host fd value does not exceed fdoffset and if necessary 1093 * dup the file descriptor so that it doesn't collide with the dup2mask. 1094 */ 1095 static int 1096 fd_host2host(int fd) 1097 { 1098 int (*op_fcntl)(int, int, ...) = GETSYSCALL(host, FCNTL); 1099 int (*op_close)(int) = GETSYSCALL(host, CLOSE); 1100 int ofd, i; 1101 1102 if (fd >= hijack_fdoff) { 1103 op_close(fd); 1104 errno = ENFILE; 1105 return -1; 1106 } 1107 1108 for (i = 1; isdup2d(fd); i++) { 1109 ofd = fd; 1110 fd = op_fcntl(ofd, F_DUPFD, i); 1111 op_close(ofd); 1112 } 1113 1114 return fd; 1115 } 1116 1117 int 1118 open(const char *path, int flags, ...) 1119 { 1120 int (*op_open)(const char *, int, ...); 1121 bool isrump; 1122 va_list ap; 1123 enum pathtype pt; 1124 int fd; 1125 1126 DPRINTF(("open -> %s (%s)\n", path, whichpath(path))); 1127 1128 if ((pt = path_isrump(path)) != PATH_HOST) { 1129 if (pt == PATH_RUMP) 1130 path = path_host2rump(path); 1131 op_open = GETSYSCALL(rump, OPEN); 1132 isrump = true; 1133 } else { 1134 op_open = GETSYSCALL(host, OPEN); 1135 isrump = false; 1136 } 1137 1138 va_start(ap, flags); 1139 fd = op_open(path, flags, va_arg(ap, mode_t)); 1140 va_end(ap); 1141 1142 if (isrump) 1143 fd = fd_rump2host(fd); 1144 else 1145 fd = fd_host2host(fd); 1146 1147 DPRINTF(("open <- %d (%s)\n", fd, whichfd(fd))); 1148 return fd; 1149 } 1150 1151 int 1152 chdir(const char *path) 1153 { 1154 int (*op_chdir)(const char *); 1155 enum pathtype pt; 1156 int rv; 1157 1158 if ((pt = path_isrump(path)) != PATH_HOST) { 1159 op_chdir = GETSYSCALL(rump, CHDIR); 1160 if (pt == PATH_RUMP) 1161 path = path_host2rump(path); 1162 } else { 1163 op_chdir = GETSYSCALL(host, CHDIR); 1164 } 1165 1166 rv = op_chdir(path); 1167 if (rv == 0) 1168 pwdinrump = pt != PATH_HOST; 1169 1170 return rv; 1171 } 1172 1173 int 1174 fchdir(int fd) 1175 { 1176 int (*op_fchdir)(int); 1177 bool isrump; 1178 int rv; 1179 1180 if (fd_isrump(fd)) { 1181 op_fchdir = GETSYSCALL(rump, FCHDIR); 1182 isrump = true; 1183 fd = fd_host2rump(fd); 1184 } else { 1185 op_fchdir = GETSYSCALL(host, FCHDIR); 1186 isrump = false; 1187 } 1188 1189 rv = op_fchdir(fd); 1190 if (rv == 0) { 1191 pwdinrump = isrump; 1192 } 1193 1194 return rv; 1195 } 1196 1197 #ifndef __linux__ 1198 int 1199 __getcwd(char *bufp, size_t len) 1200 { 1201 int (*op___getcwd)(char *, size_t); 1202 size_t prefixgap; 1203 bool iamslash; 1204 int rv; 1205 1206 if (pwdinrump && rumpprefix) { 1207 if (rumpprefix[rumpprefixlen-1] == '/') 1208 iamslash = true; 1209 else 1210 iamslash = false; 1211 1212 if (iamslash) 1213 prefixgap = rumpprefixlen - 1; /* ``//+path'' */ 1214 else 1215 prefixgap = rumpprefixlen; /* ``/pfx+/path'' */ 1216 if (len <= prefixgap) { 1217 errno = ERANGE; 1218 return -1; 1219 } 1220 1221 op___getcwd = GETSYSCALL(rump, __GETCWD); 1222 rv = op___getcwd(bufp + prefixgap, len - prefixgap); 1223 if (rv == -1) 1224 return rv; 1225 1226 /* augment the "/" part only for a non-root path */ 1227 memcpy(bufp, rumpprefix, rumpprefixlen); 1228 1229 /* append / only to non-root cwd */ 1230 if (rv != 2) 1231 bufp[prefixgap] = '/'; 1232 1233 /* don't append extra slash in the purely-slash case */ 1234 if (rv == 2 && !iamslash) 1235 bufp[rumpprefixlen] = '\0'; 1236 } else if (pwdinrump) { 1237 /* assume blanket. we can't provide a prefix here */ 1238 op___getcwd = GETSYSCALL(rump, __GETCWD); 1239 rv = op___getcwd(bufp, len); 1240 } else { 1241 op___getcwd = GETSYSCALL(host, __GETCWD); 1242 rv = op___getcwd(bufp, len); 1243 } 1244 1245 return rv; 1246 } 1247 #endif 1248 1249 static int 1250 moveish(const char *from, const char *to, 1251 int (*rump_op)(const char *, const char *), 1252 int (*host_op)(const char *, const char *)) 1253 { 1254 int (*op)(const char *, const char *); 1255 enum pathtype ptf, ptt; 1256 1257 if ((ptf = path_isrump(from)) != PATH_HOST) { 1258 if ((ptt = path_isrump(to)) == PATH_HOST) { 1259 errno = EXDEV; 1260 return -1; 1261 } 1262 1263 if (ptf == PATH_RUMP) 1264 from = path_host2rump(from); 1265 if (ptt == PATH_RUMP) 1266 to = path_host2rump(to); 1267 op = rump_op; 1268 } else { 1269 if (path_isrump(to) != PATH_HOST) { 1270 errno = EXDEV; 1271 return -1; 1272 } 1273 1274 op = host_op; 1275 } 1276 1277 return op(from, to); 1278 } 1279 1280 #ifdef __NetBSD__ 1281 int 1282 linkat(int fromfd, const char *from, int tofd, const char *to, int flags) 1283 { 1284 if (fromfd != AT_FDCWD || tofd != AT_FDCWD 1285 || flags != AT_SYMLINK_FOLLOW) 1286 return ENOSYS; 1287 1288 return moveish(from, to, 1289 GETSYSCALL(rump, LINK), GETSYSCALL(host, LINK)); 1290 } 1291 #endif 1292 1293 int 1294 link(const char *from, const char *to) 1295 { 1296 return moveish(from, to, 1297 GETSYSCALL(rump, LINK), GETSYSCALL(host, LINK)); 1298 } 1299 1300 int 1301 rename(const char *from, const char *to) 1302 { 1303 return moveish(from, to, 1304 GETSYSCALL(rump, RENAME), GETSYSCALL(host, RENAME)); 1305 } 1306 1307 int 1308 REALSOCKET(int domain, int type, int protocol) 1309 { 1310 int (*op_socket)(int, int, int); 1311 int fd; 1312 bool isrump; 1313 1314 isrump = domain < PF_MAX && rumpsockets[domain]; 1315 1316 if (isrump) 1317 op_socket = GETSYSCALL(rump, SOCKET); 1318 else 1319 op_socket = GETSYSCALL(host, SOCKET); 1320 fd = op_socket(domain, type, protocol); 1321 1322 if (isrump) 1323 fd = fd_rump2host(fd); 1324 else 1325 fd = fd_host2host(fd); 1326 DPRINTF(("socket <- %d\n", fd)); 1327 1328 return fd; 1329 } 1330 1331 int 1332 accept(int s, struct sockaddr *addr, socklen_t *addrlen) 1333 { 1334 int (*op_accept)(int, struct sockaddr *, socklen_t *); 1335 int fd; 1336 bool isrump; 1337 1338 isrump = fd_isrump(s); 1339 1340 DPRINTF(("accept -> %d", s)); 1341 if (isrump) { 1342 op_accept = GETSYSCALL(rump, ACCEPT); 1343 s = fd_host2rump(s); 1344 } else { 1345 op_accept = GETSYSCALL(host, ACCEPT); 1346 } 1347 fd = op_accept(s, addr, addrlen); 1348 if (fd != -1 && isrump) 1349 fd = fd_rump2host(fd); 1350 else 1351 fd = fd_host2host(fd); 1352 1353 DPRINTF((" <- %d\n", fd)); 1354 1355 return fd; 1356 } 1357 1358 int 1359 paccept(int s, struct sockaddr *addr, socklen_t *addrlen, 1360 const sigset_t * restrict sigmask, int flags) 1361 { 1362 int (*op_paccept)(int, struct sockaddr *, socklen_t *, 1363 const sigset_t * restrict, int); 1364 int fd; 1365 bool isrump; 1366 1367 isrump = fd_isrump(s); 1368 1369 DPRINTF(("paccept -> %d", s)); 1370 if (isrump) { 1371 op_paccept = GETSYSCALL(rump, PACCEPT); 1372 s = fd_host2rump(s); 1373 } else { 1374 op_paccept = GETSYSCALL(host, PACCEPT); 1375 } 1376 fd = op_paccept(s, addr, addrlen, sigmask, flags); 1377 if (fd != -1 && isrump) 1378 fd = fd_rump2host(fd); 1379 else 1380 fd = fd_host2host(fd); 1381 1382 DPRINTF((" <- %d\n", fd)); 1383 1384 return fd; 1385 } 1386 1387 /* 1388 * ioctl() and fcntl() are varargs calls and need special treatment. 1389 */ 1390 1391 /* 1392 * Various [Linux] libc's have various signatures for ioctl so we 1393 * need to handle the discrepancies. On NetBSD, we use the 1394 * one with unsigned long cmd. 1395 */ 1396 int 1397 #ifdef HAVE_IOCTL_CMD_INT 1398 ioctl(int fd, int cmd, ...) 1399 { 1400 int (*op_ioctl)(int, int cmd, ...); 1401 #else 1402 ioctl(int fd, unsigned long cmd, ...) 1403 { 1404 int (*op_ioctl)(int, unsigned long cmd, ...); 1405 #endif 1406 va_list ap; 1407 int rv; 1408 1409 DPRINTF(("ioctl -> %d\n", fd)); 1410 if (fd_isrump(fd)) { 1411 fd = fd_host2rump(fd); 1412 op_ioctl = GETSYSCALL(rump, IOCTL); 1413 } else { 1414 op_ioctl = GETSYSCALL(host, IOCTL); 1415 } 1416 1417 va_start(ap, cmd); 1418 rv = op_ioctl(fd, cmd, va_arg(ap, void *)); 1419 va_end(ap); 1420 return rv; 1421 } 1422 1423 int 1424 fcntl(int fd, int cmd, ...) 1425 { 1426 int (*op_fcntl)(int, int, ...); 1427 va_list ap; 1428 int rv, minfd; 1429 1430 DPRINTF(("fcntl -> %d (cmd %d)\n", fd, cmd)); 1431 1432 switch (cmd) { 1433 case F_DUPFD_CLOEXEC: /* Ignore CLOEXEC bit for now */ 1434 case F_DUPFD: 1435 va_start(ap, cmd); 1436 minfd = va_arg(ap, int); 1437 va_end(ap); 1438 return dodup(fd, minfd); 1439 1440 #ifdef F_CLOSEM 1441 case F_CLOSEM: { 1442 int maxdup2, i; 1443 1444 /* 1445 * So, if fd < HIJACKOFF, we want to do a host closem. 1446 */ 1447 1448 if (fd < hijack_fdoff) { 1449 int closemfd = fd; 1450 1451 if (rumpclient__closenotify(&closemfd, 1452 RUMPCLIENT_CLOSE_FCLOSEM) == -1) 1453 return -1; 1454 op_fcntl = GETSYSCALL(host, FCNTL); 1455 rv = op_fcntl(closemfd, cmd); 1456 if (rv) 1457 return rv; 1458 } 1459 1460 /* 1461 * Additionally, we want to do a rump closem, but only 1462 * for the file descriptors not dup2'd. 1463 */ 1464 1465 for (i = 0, maxdup2 = -1; i <= DUP2HIGH; i++) { 1466 if (dup2vec[i] & DUP2BIT) { 1467 int val; 1468 1469 val = dup2vec[i] & DUP2FDMASK; 1470 maxdup2 = MAX(val, maxdup2); 1471 } 1472 } 1473 1474 if (fd >= hijack_fdoff) 1475 fd -= hijack_fdoff; 1476 else 1477 fd = 0; 1478 fd = MAX(maxdup2+1, fd); 1479 1480 /* hmm, maybe we should close rump fd's not within dup2mask? */ 1481 return rump_sys_fcntl(fd, F_CLOSEM); 1482 } 1483 #endif /* F_CLOSEM */ 1484 1485 #ifdef F_MAXFD 1486 case F_MAXFD: 1487 /* 1488 * For maxfd, if there's a rump kernel fd, return 1489 * it hostified. Otherwise, return host's MAXFD 1490 * return value. 1491 */ 1492 if ((rv = rump_sys_fcntl(fd, F_MAXFD)) != -1) { 1493 /* 1494 * This might go a little wrong in case 1495 * of dup2 to [012], but I'm not sure if 1496 * there's a justification for tracking 1497 * that info. Consider e.g. 1498 * dup2(rumpfd, 2) followed by rump_sys_open() 1499 * returning 1. We should return 1+HIJACKOFF, 1500 * not 2+HIJACKOFF. However, if [01] is not 1501 * open, the correct return value is 2. 1502 */ 1503 return fd_rump2host(fd); 1504 } else { 1505 op_fcntl = GETSYSCALL(host, FCNTL); 1506 return op_fcntl(fd, F_MAXFD); 1507 } 1508 /*NOTREACHED*/ 1509 #endif /* F_MAXFD */ 1510 1511 default: 1512 if (fd_isrump(fd)) { 1513 fd = fd_host2rump(fd); 1514 op_fcntl = GETSYSCALL(rump, FCNTL); 1515 } else { 1516 op_fcntl = GETSYSCALL(host, FCNTL); 1517 } 1518 1519 va_start(ap, cmd); 1520 rv = op_fcntl(fd, cmd, va_arg(ap, void *)); 1521 va_end(ap); 1522 return rv; 1523 } 1524 /*NOTREACHED*/ 1525 } 1526 1527 int 1528 close(int fd) 1529 { 1530 int (*op_close)(int); 1531 int rv; 1532 1533 DPRINTF(("close -> %d\n", fd)); 1534 if (fd_isrump(fd)) { 1535 bool undup2 = false; 1536 int ofd; 1537 1538 if (isdup2d(ofd = fd)) { 1539 undup2 = true; 1540 } 1541 1542 fd = fd_host2rump(fd); 1543 if (!undup2 && killdup2alias(fd)) { 1544 return 0; 1545 } 1546 1547 op_close = GETSYSCALL(rump, CLOSE); 1548 rv = op_close(fd); 1549 if (rv == 0 && undup2) { 1550 clrdup2(ofd); 1551 } 1552 } else { 1553 if (rumpclient__closenotify(&fd, RUMPCLIENT_CLOSE_CLOSE) == -1) 1554 return -1; 1555 op_close = GETSYSCALL(host, CLOSE); 1556 rv = op_close(fd); 1557 } 1558 1559 return rv; 1560 } 1561 1562 /* 1563 * write cannot issue a standard debug printf due to recursion 1564 */ 1565 ssize_t 1566 write(int fd, const void *buf, size_t blen) 1567 { 1568 ssize_t (*op_write)(int, const void *, size_t); 1569 1570 if (fd_isrump(fd)) { 1571 fd = fd_host2rump(fd); 1572 op_write = GETSYSCALL(rump, WRITE); 1573 } else { 1574 op_write = GETSYSCALL(host, WRITE); 1575 } 1576 1577 return op_write(fd, buf, blen); 1578 } 1579 1580 /* 1581 * file descriptor passing 1582 * 1583 * we intercept sendmsg and recvmsg to convert file descriptors in 1584 * control messages. an attempt to send a descriptor from a different kernel 1585 * is rejected. (ENOTSUP) 1586 */ 1587 1588 static int 1589 msg_convert(struct msghdr *msg, int (*func)(int)) 1590 { 1591 struct cmsghdr *cmsg; 1592 1593 for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; 1594 cmsg = CMSG_NXTHDR(msg, cmsg)) { 1595 if (cmsg->cmsg_level == SOL_SOCKET && 1596 cmsg->cmsg_type == SCM_RIGHTS) { 1597 int *fdp = (void *)CMSG_DATA(cmsg); 1598 const size_t size = 1599 cmsg->cmsg_len - __CMSG_ALIGN(sizeof(*cmsg)); 1600 const int nfds = (int)(size / sizeof(int)); 1601 const int * const efdp = fdp + nfds; 1602 1603 while (fdp < efdp) { 1604 const int newval = func(*fdp); 1605 1606 if (newval < 0) { 1607 return ENOTSUP; 1608 } 1609 *fdp = newval; 1610 fdp++; 1611 } 1612 } 1613 } 1614 return 0; 1615 } 1616 1617 ssize_t 1618 recvmsg(int fd, struct msghdr *msg, int flags) 1619 { 1620 ssize_t (*op_recvmsg)(int, struct msghdr *, int); 1621 ssize_t ret; 1622 const bool isrump = fd_isrump(fd); 1623 1624 if (isrump) { 1625 fd = fd_host2rump(fd); 1626 op_recvmsg = GETSYSCALL(rump, RECVMSG); 1627 } else { 1628 op_recvmsg = GETSYSCALL(host, RECVMSG); 1629 } 1630 ret = op_recvmsg(fd, msg, flags); 1631 if (ret == -1) { 1632 return ret; 1633 } 1634 /* 1635 * convert descriptors in the message. 1636 */ 1637 if (isrump) { 1638 msg_convert(msg, fd_rump2host); 1639 } else { 1640 msg_convert(msg, fd_host2host); 1641 } 1642 return ret; 1643 } 1644 1645 ssize_t 1646 recv(int fd, void *buf, size_t len, int flags) 1647 { 1648 1649 return recvfrom(fd, buf, len, flags, NULL, NULL); 1650 } 1651 1652 ssize_t 1653 send(int fd, const void *buf, size_t len, int flags) 1654 { 1655 1656 return sendto(fd, buf, len, flags, NULL, 0); 1657 } 1658 1659 static int 1660 fd_check_rump(int fd) 1661 { 1662 1663 return fd_isrump(fd) ? 0 : -1; 1664 } 1665 1666 static int 1667 fd_check_host(int fd) 1668 { 1669 1670 return !fd_isrump(fd) ? 0 : -1; 1671 } 1672 1673 ssize_t 1674 sendmsg(int fd, const struct msghdr *msg, int flags) 1675 { 1676 ssize_t (*op_sendmsg)(int, const struct msghdr *, int); 1677 const bool isrump = fd_isrump(fd); 1678 int error; 1679 1680 /* 1681 * reject descriptors from a different kernel. 1682 */ 1683 error = msg_convert(__UNCONST(msg), 1684 isrump ? fd_check_rump: fd_check_host); 1685 if (error != 0) { 1686 errno = error; 1687 return -1; 1688 } 1689 /* 1690 * convert descriptors in the message to raw values. 1691 */ 1692 if (isrump) { 1693 fd = fd_host2rump(fd); 1694 /* 1695 * XXX we directly modify the given message assuming: 1696 * - cmsg is writable (typically on caller's stack) 1697 * - caller don't care cmsg's contents after calling sendmsg. 1698 * (thus no need to restore values) 1699 * 1700 * it's safer to copy and modify instead. 1701 */ 1702 msg_convert(__UNCONST(msg), fd_host2rump); 1703 op_sendmsg = GETSYSCALL(rump, SENDMSG); 1704 } else { 1705 op_sendmsg = GETSYSCALL(host, SENDMSG); 1706 } 1707 return op_sendmsg(fd, msg, flags); 1708 } 1709 1710 /* 1711 * dup2 is special. we allow dup2 of a rump kernel fd to 0-2 since 1712 * many programs do that. dup2 of a rump kernel fd to another value 1713 * not >= fdoff is an error. 1714 * 1715 * Note: cannot rump2host newd, because it is often hardcoded. 1716 */ 1717 int 1718 dup2(int oldd, int newd) 1719 { 1720 int (*host_dup2)(int, int); 1721 int rv; 1722 1723 DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd)); 1724 1725 if (fd_isrump(oldd)) { 1726 int (*op_close)(int) = GETSYSCALL(host, CLOSE); 1727 1728 /* only allow fd 0-2 for cross-kernel dup */ 1729 if (!(newd >= 0 && newd <= 2 && !fd_isrump(newd))) { 1730 errno = EBADF; 1731 return -1; 1732 } 1733 1734 /* regular dup2? */ 1735 if (fd_isrump(newd)) { 1736 newd = fd_host2rump(newd); 1737 rv = rump_sys_dup2(oldd, newd); 1738 return fd_rump2host(rv); 1739 } 1740 1741 /* 1742 * dup2 rump => host? just establish an 1743 * entry in the mapping table. 1744 */ 1745 op_close(newd); 1746 setdup2(newd, fd_host2rump(oldd)); 1747 rv = 0; 1748 } else { 1749 host_dup2 = syscalls[DUALCALL_DUP2].bs_host; 1750 if (rumpclient__closenotify(&newd, RUMPCLIENT_CLOSE_DUP2) == -1) 1751 return -1; 1752 rv = host_dup2(oldd, newd); 1753 } 1754 1755 return rv; 1756 } 1757 1758 int 1759 dup(int oldd) 1760 { 1761 1762 return dodup(oldd, 0); 1763 } 1764 1765 pid_t 1766 fork(void) 1767 { 1768 pid_t rv; 1769 1770 DPRINTF(("fork\n")); 1771 1772 rv = rumpclient__dofork(host_fork); 1773 1774 DPRINTF(("fork returns %d\n", rv)); 1775 return rv; 1776 } 1777 #ifdef VFORK 1778 /* we do not have the luxury of not requiring a stackframe */ 1779 #define __strong_alias_macro(m, f) __strong_alias(m, f) 1780 __strong_alias_macro(VFORK,fork); 1781 #endif 1782 1783 int 1784 daemon(int nochdir, int noclose) 1785 { 1786 struct rumpclient_fork *rf; 1787 1788 if ((rf = rumpclient_prefork()) == NULL) 1789 return -1; 1790 1791 if (host_daemon(nochdir, noclose) == -1) 1792 return -1; 1793 1794 if (rumpclient_fork_init(rf) == -1) 1795 return -1; 1796 1797 return 0; 1798 } 1799 1800 int 1801 execve(const char *path, char *const argv[], char *const envp[]) 1802 { 1803 char buf[128]; 1804 char *dup2str; 1805 const char *pwdinrumpstr; 1806 char **newenv; 1807 size_t nelem; 1808 int rv, sverrno; 1809 int bonus = 2, i = 0; 1810 1811 snprintf(buf, sizeof(buf), "RUMPHIJACK__DUP2INFO=%u,%u,%u", 1812 dup2vec[0], dup2vec[1], dup2vec[2]); 1813 dup2str = strdup(buf); 1814 if (dup2str == NULL) { 1815 errno = ENOMEM; 1816 return -1; 1817 } 1818 1819 if (pwdinrump) { 1820 pwdinrumpstr = "RUMPHIJACK__PWDINRUMP=true"; 1821 bonus++; 1822 } else { 1823 pwdinrumpstr = NULL; 1824 } 1825 1826 for (nelem = 0; envp && envp[nelem]; nelem++) 1827 continue; 1828 newenv = malloc(sizeof(*newenv) * (nelem+bonus)); 1829 if (newenv == NULL) { 1830 free(dup2str); 1831 errno = ENOMEM; 1832 return -1; 1833 } 1834 memcpy(newenv, envp, nelem*sizeof(*newenv)); 1835 newenv[nelem+i] = dup2str; 1836 i++; 1837 1838 if (pwdinrumpstr) { 1839 newenv[nelem+i] = __UNCONST(pwdinrumpstr); 1840 i++; 1841 } 1842 newenv[nelem+i] = NULL; 1843 _DIAGASSERT(i < bonus); 1844 1845 rv = rumpclient_exec(path, argv, newenv); 1846 1847 _DIAGASSERT(rv != 0); 1848 sverrno = errno; 1849 free(newenv); 1850 free(dup2str); 1851 errno = sverrno; 1852 return rv; 1853 } 1854 1855 /* 1856 * select is done by calling poll. 1857 */ 1858 int 1859 REALSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, 1860 struct timeval *timeout) 1861 { 1862 struct pollfd *pfds; 1863 struct timespec ts, *tsp = NULL; 1864 nfds_t realnfds; 1865 int i, j; 1866 int rv, incr; 1867 1868 DPRINTF(("select %d %p %p %p %p\n", nfds, 1869 readfds, writefds, exceptfds, timeout)); 1870 1871 /* 1872 * Well, first we must scan the fds to figure out how many 1873 * fds there really are. This is because up to and including 1874 * nb5 poll() silently refuses nfds > process_maxopen_fds. 1875 * Seems to be fixed in current, thank the maker. 1876 * god damn cluster...bomb. 1877 */ 1878 1879 for (i = 0, realnfds = 0; i < nfds; i++) { 1880 if (readfds && FD_ISSET(i, readfds)) { 1881 realnfds++; 1882 continue; 1883 } 1884 if (writefds && FD_ISSET(i, writefds)) { 1885 realnfds++; 1886 continue; 1887 } 1888 if (exceptfds && FD_ISSET(i, exceptfds)) { 1889 realnfds++; 1890 continue; 1891 } 1892 } 1893 1894 if (realnfds) { 1895 pfds = calloc(realnfds, sizeof(*pfds)); 1896 if (!pfds) 1897 return -1; 1898 } else { 1899 pfds = NULL; 1900 } 1901 1902 for (i = 0, j = 0; i < nfds; i++) { 1903 incr = 0; 1904 if (readfds && FD_ISSET(i, readfds)) { 1905 pfds[j].fd = i; 1906 pfds[j].events |= POLLIN; 1907 incr=1; 1908 } 1909 if (writefds && FD_ISSET(i, writefds)) { 1910 pfds[j].fd = i; 1911 pfds[j].events |= POLLOUT; 1912 incr=1; 1913 } 1914 if (exceptfds && FD_ISSET(i, exceptfds)) { 1915 pfds[j].fd = i; 1916 pfds[j].events |= POLLHUP|POLLERR; 1917 incr=1; 1918 } 1919 if (incr) 1920 j++; 1921 } 1922 assert(j == (int)realnfds); 1923 1924 if (timeout) { 1925 TIMEVAL_TO_TIMESPEC(timeout, &ts); 1926 tsp = &ts; 1927 } 1928 rv = REALPOLLTS(pfds, realnfds, tsp, NULL); 1929 /* 1930 * "If select() returns with an error the descriptor sets 1931 * will be unmodified" 1932 */ 1933 if (rv < 0) 1934 goto out; 1935 1936 /* 1937 * zero out results (can't use FD_ZERO for the 1938 * obvious select-me-not reason). whee. 1939 * 1940 * We do this here since some software ignores the return 1941 * value of select, and hence if the timeout expires, it may 1942 * assume all input descriptors have activity. 1943 */ 1944 for (i = 0; i < nfds; i++) { 1945 if (readfds) 1946 FD_CLR(i, readfds); 1947 if (writefds) 1948 FD_CLR(i, writefds); 1949 if (exceptfds) 1950 FD_CLR(i, exceptfds); 1951 } 1952 if (rv == 0) 1953 goto out; 1954 1955 /* 1956 * We have >0 fds with activity. Harvest the results. 1957 */ 1958 for (i = 0; i < (int)realnfds; i++) { 1959 if (readfds) { 1960 if (pfds[i].revents & POLLIN) { 1961 FD_SET(pfds[i].fd, readfds); 1962 } 1963 } 1964 if (writefds) { 1965 if (pfds[i].revents & POLLOUT) { 1966 FD_SET(pfds[i].fd, writefds); 1967 } 1968 } 1969 if (exceptfds) { 1970 if (pfds[i].revents & (POLLHUP|POLLERR)) { 1971 FD_SET(pfds[i].fd, exceptfds); 1972 } 1973 } 1974 } 1975 1976 out: 1977 free(pfds); 1978 return rv; 1979 } 1980 1981 static void 1982 checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall) 1983 { 1984 nfds_t i; 1985 1986 for (i = 0; i < nfds; i++) { 1987 if (fds[i].fd == -1) 1988 continue; 1989 1990 if (fd_isrump(fds[i].fd)) 1991 (*rumpcall)++; 1992 else 1993 (*hostcall)++; 1994 } 1995 } 1996 1997 static void 1998 adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int)) 1999 { 2000 nfds_t i; 2001 2002 for (i = 0; i < nfds; i++) { 2003 fds[i].fd = fdadj(fds[i].fd); 2004 } 2005 } 2006 2007 /* 2008 * poll is easy as long as the call comes in the fds only in one 2009 * kernel. otherwise its quite tricky... 2010 */ 2011 struct pollarg { 2012 struct pollfd *pfds; 2013 nfds_t nfds; 2014 const struct timespec *ts; 2015 const sigset_t *sigmask; 2016 int pipefd; 2017 int errnum; 2018 }; 2019 2020 static void * 2021 hostpoll(void *arg) 2022 { 2023 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *, 2024 const sigset_t *); 2025 struct pollarg *parg = arg; 2026 intptr_t rv; 2027 2028 op_pollts = GETSYSCALL(host, POLLTS); 2029 rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask); 2030 if (rv == -1) 2031 parg->errnum = errno; 2032 rump_sys_write(parg->pipefd, &rv, sizeof(rv)); 2033 2034 return (void *)rv; 2035 } 2036 2037 int 2038 REALPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts, 2039 const sigset_t *sigmask) 2040 { 2041 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *, 2042 const sigset_t *); 2043 int (*host_close)(int); 2044 int hostcall = 0, rumpcall = 0; 2045 pthread_t pt; 2046 nfds_t i; 2047 int rv; 2048 2049 DPRINTF(("poll %p %d %p %p\n", fds, (int)nfds, ts, sigmask)); 2050 checkpoll(fds, nfds, &hostcall, &rumpcall); 2051 2052 if (hostcall && rumpcall) { 2053 struct pollfd *pfd_host = NULL, *pfd_rump = NULL; 2054 int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1}; 2055 struct pollarg parg; 2056 void *trv_val; 2057 int sverrno = 0, rv_rump, rv_host, errno_rump, errno_host; 2058 2059 /* 2060 * ok, this is where it gets tricky. We must support 2061 * this since it's a very common operation in certain 2062 * types of software (telnet, netcat, etc). We allocate 2063 * two vectors and run two poll commands in separate 2064 * threads. Whichever returns first "wins" and the 2065 * other kernel's fds won't show activity. 2066 */ 2067 rv = -1; 2068 2069 /* allocate full vector for O(n) joining after call */ 2070 pfd_host = malloc(sizeof(*pfd_host)*(nfds+1)); 2071 if (!pfd_host) 2072 goto out; 2073 pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1)); 2074 if (!pfd_rump) { 2075 goto out; 2076 } 2077 2078 /* 2079 * then, open two pipes, one for notifications 2080 * to each kernel. 2081 * 2082 * At least the rump pipe should probably be 2083 * cached, along with the helper threads. This 2084 * should give a microbenchmark improvement (haven't 2085 * experienced a macro-level problem yet, though). 2086 */ 2087 if ((rv = rump_sys_pipe(rpipe)) == -1) { 2088 sverrno = errno; 2089 } 2090 if (rv == 0 && (rv = pipe(hpipe)) == -1) { 2091 sverrno = errno; 2092 } 2093 2094 /* split vectors (or signal errors) */ 2095 for (i = 0; i < nfds; i++) { 2096 int fd; 2097 2098 fds[i].revents = 0; 2099 if (fds[i].fd == -1) { 2100 pfd_host[i].fd = -1; 2101 pfd_rump[i].fd = -1; 2102 } else if (fd_isrump(fds[i].fd)) { 2103 pfd_host[i].fd = -1; 2104 fd = fd_host2rump(fds[i].fd); 2105 if (fd == rpipe[0] || fd == rpipe[1]) { 2106 fds[i].revents = POLLNVAL; 2107 if (rv != -1) 2108 rv++; 2109 } 2110 pfd_rump[i].fd = fd; 2111 pfd_rump[i].events = fds[i].events; 2112 } else { 2113 pfd_rump[i].fd = -1; 2114 fd = fds[i].fd; 2115 if (fd == hpipe[0] || fd == hpipe[1]) { 2116 fds[i].revents = POLLNVAL; 2117 if (rv != -1) 2118 rv++; 2119 } 2120 pfd_host[i].fd = fd; 2121 pfd_host[i].events = fds[i].events; 2122 } 2123 pfd_rump[i].revents = pfd_host[i].revents = 0; 2124 } 2125 if (rv) { 2126 goto out; 2127 } 2128 2129 pfd_host[nfds].fd = hpipe[0]; 2130 pfd_host[nfds].events = POLLIN; 2131 pfd_rump[nfds].fd = rpipe[0]; 2132 pfd_rump[nfds].events = POLLIN; 2133 2134 /* 2135 * then, create a thread to do host part and meanwhile 2136 * do rump kernel part right here 2137 */ 2138 2139 parg.pfds = pfd_host; 2140 parg.nfds = nfds+1; 2141 parg.ts = ts; 2142 parg.sigmask = sigmask; 2143 parg.pipefd = rpipe[1]; 2144 pthread_create(&pt, NULL, hostpoll, &parg); 2145 2146 op_pollts = GETSYSCALL(rump, POLLTS); 2147 rv_rump = op_pollts(pfd_rump, nfds+1, ts, NULL); 2148 errno_rump = errno; 2149 write(hpipe[1], &rv, sizeof(rv)); 2150 pthread_join(pt, &trv_val); 2151 rv_host = (int)(intptr_t)trv_val; 2152 errno_host = parg.errnum; 2153 2154 /* strip cross-thread notification from real results */ 2155 if (rv_host > 0 && pfd_host[nfds].revents & POLLIN) { 2156 rv_host--; 2157 } 2158 if (rv_rump > 0 && pfd_rump[nfds].revents & POLLIN) { 2159 rv_rump--; 2160 } 2161 2162 /* then merge the results into what's reported to the caller */ 2163 if (rv_rump > 0 || rv_host > 0) { 2164 /* SUCCESS */ 2165 2166 rv = 0; 2167 if (rv_rump > 0) { 2168 for (i = 0; i < nfds; i++) { 2169 if (pfd_rump[i].fd != -1) 2170 fds[i].revents 2171 = pfd_rump[i].revents; 2172 } 2173 rv += rv_rump; 2174 } 2175 if (rv_host > 0) { 2176 for (i = 0; i < nfds; i++) { 2177 if (pfd_host[i].fd != -1) 2178 fds[i].revents 2179 = pfd_host[i].revents; 2180 } 2181 rv += rv_host; 2182 } 2183 assert(rv > 0); 2184 sverrno = 0; 2185 } else if (rv_rump == -1 || rv_host == -1) { 2186 /* ERROR */ 2187 2188 /* just pick one kernel at "random" */ 2189 rv = -1; 2190 if (rv_host == -1) { 2191 sverrno = errno_host; 2192 } else if (rv_rump == -1) { 2193 sverrno = errno_rump; 2194 } 2195 } else { 2196 /* TIMEOUT */ 2197 2198 rv = 0; 2199 assert(rv_rump == 0 && rv_host == 0); 2200 } 2201 2202 out: 2203 host_close = GETSYSCALL(host, CLOSE); 2204 if (rpipe[0] != -1) 2205 rump_sys_close(rpipe[0]); 2206 if (rpipe[1] != -1) 2207 rump_sys_close(rpipe[1]); 2208 if (hpipe[0] != -1) 2209 host_close(hpipe[0]); 2210 if (hpipe[1] != -1) 2211 host_close(hpipe[1]); 2212 free(pfd_host); 2213 free(pfd_rump); 2214 errno = sverrno; 2215 } else { 2216 if (hostcall) { 2217 op_pollts = GETSYSCALL(host, POLLTS); 2218 } else { 2219 op_pollts = GETSYSCALL(rump, POLLTS); 2220 adjustpoll(fds, nfds, fd_host2rump); 2221 } 2222 2223 rv = op_pollts(fds, nfds, ts, sigmask); 2224 if (rumpcall) 2225 adjustpoll(fds, nfds, fd_rump2host_withdup); 2226 } 2227 2228 return rv; 2229 } 2230 2231 int 2232 poll(struct pollfd *fds, nfds_t nfds, int timeout) 2233 { 2234 struct timespec ts; 2235 struct timespec *tsp = NULL; 2236 2237 if (timeout != INFTIM) { 2238 ts.tv_sec = timeout / 1000; 2239 ts.tv_nsec = (timeout % 1000) * 1000*1000; 2240 2241 tsp = &ts; 2242 } 2243 2244 return REALPOLLTS(fds, nfds, tsp, NULL); 2245 } 2246 2247 #ifdef HAVE_KQUEUE 2248 int 2249 REALKEVENT(int kq, const struct kevent *changelist, size_t nchanges, 2250 struct kevent *eventlist, size_t nevents, 2251 const struct timespec *timeout) 2252 { 2253 int (*op_kevent)(int, const struct kevent *, size_t, 2254 struct kevent *, size_t, const struct timespec *); 2255 const struct kevent *ev; 2256 size_t i; 2257 2258 /* 2259 * Check that we don't attempt to kevent rump kernel fd's. 2260 * That needs similar treatment to select/poll, but is slightly 2261 * trickier since we need to manage to different kq descriptors. 2262 * (TODO, in case you're wondering). 2263 */ 2264 for (i = 0; i < nchanges; i++) { 2265 ev = &changelist[i]; 2266 if (ev->filter == EVFILT_READ || ev->filter == EVFILT_WRITE || 2267 ev->filter == EVFILT_VNODE) { 2268 if (fd_isrump((int)ev->ident)) { 2269 errno = ENOTSUP; 2270 return -1; 2271 } 2272 } 2273 } 2274 2275 op_kevent = GETSYSCALL(host, KEVENT); 2276 return op_kevent(kq, changelist, nchanges, eventlist, nevents, timeout); 2277 } 2278 #endif /* HAVE_KQUEUE */ 2279 2280 /* 2281 * mmapping from a rump kernel is not supported, so disallow it. 2282 */ 2283 void * 2284 mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) 2285 { 2286 2287 if (flags & MAP_FILE && fd_isrump(fd)) { 2288 errno = ENOSYS; 2289 return MAP_FAILED; 2290 } 2291 if (__predict_false(host_mmap == NULL)) { 2292 host_mmap = rumphijack_dlsym(RTLD_NEXT, "mmap"); 2293 } 2294 return host_mmap(addr, len, prot, flags, fd, offset); 2295 } 2296 2297 #ifdef __NetBSD__ 2298 /* 2299 * these go to one or the other on a per-process configuration 2300 */ 2301 int __sysctl(const int *, unsigned int, void *, size_t *, const void *, size_t); 2302 int 2303 __sysctl(const int *name, unsigned int namelen, void *old, size_t *oldlenp, 2304 const void *new, size_t newlen) 2305 { 2306 int (*op___sysctl)(const int *, unsigned int, void *, size_t *, 2307 const void *, size_t); 2308 2309 if (rumpsysctl) { 2310 op___sysctl = GETSYSCALL(rump, __SYSCTL); 2311 } else { 2312 op___sysctl = GETSYSCALL(host, __SYSCTL); 2313 /* we haven't inited yet */ 2314 if (__predict_false(op___sysctl == NULL)) { 2315 op___sysctl = rumphijack_dlsym(RTLD_NEXT, "__sysctl"); 2316 } 2317 } 2318 2319 return op___sysctl(name, namelen, old, oldlenp, new, newlen); 2320 } 2321 #endif 2322 2323 /* 2324 * Rest are std type calls. 2325 */ 2326 2327 #ifdef HAVE_UTIMENSAT 2328 ATCALL(int, utimensat, DUALCALL_UTIMENSAT, \ 2329 (int fd, const char *path, const struct timespec t[2], int f), \ 2330 (int, const char *, const struct timespec [2], int), 2331 (fd, path, t, f)) 2332 #endif 2333 2334 FDCALL(int, bind, DUALCALL_BIND, \ 2335 (int fd, const struct sockaddr *name, socklen_t namelen), \ 2336 (int, const struct sockaddr *, socklen_t), \ 2337 (fd, name, namelen)) 2338 2339 FDCALL(int, connect, DUALCALL_CONNECT, \ 2340 (int fd, const struct sockaddr *name, socklen_t namelen), \ 2341 (int, const struct sockaddr *, socklen_t), \ 2342 (fd, name, namelen)) 2343 2344 FDCALL(int, getpeername, DUALCALL_GETPEERNAME, \ 2345 (int fd, struct sockaddr *name, socklen_t *namelen), \ 2346 (int, struct sockaddr *, socklen_t *), \ 2347 (fd, name, namelen)) 2348 2349 FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, \ 2350 (int fd, struct sockaddr *name, socklen_t *namelen), \ 2351 (int, struct sockaddr *, socklen_t *), \ 2352 (fd, name, namelen)) 2353 2354 FDCALL(int, listen, DUALCALL_LISTEN, \ 2355 (int fd, int backlog), \ 2356 (int, int), \ 2357 (fd, backlog)) 2358 2359 FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, \ 2360 (int fd, void *buf, size_t len, int flags, \ 2361 struct sockaddr *from, socklen_t *fromlen), \ 2362 (int, void *, size_t, int, struct sockaddr *, socklen_t *), \ 2363 (fd, buf, len, flags, from, fromlen)) 2364 2365 FDCALL(ssize_t, sendto, DUALCALL_SENDTO, \ 2366 (int fd, const void *buf, size_t len, int flags, \ 2367 const struct sockaddr *to, socklen_t tolen), \ 2368 (int, const void *, size_t, int, \ 2369 const struct sockaddr *, socklen_t), \ 2370 (fd, buf, len, flags, to, tolen)) 2371 2372 FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, \ 2373 (int fd, int level, int optn, void *optval, socklen_t *optlen), \ 2374 (int, int, int, void *, socklen_t *), \ 2375 (fd, level, optn, optval, optlen)) 2376 2377 FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, \ 2378 (int fd, int level, int optn, \ 2379 const void *optval, socklen_t optlen), \ 2380 (int, int, int, const void *, socklen_t), \ 2381 (fd, level, optn, optval, optlen)) 2382 2383 FDCALL(int, shutdown, DUALCALL_SHUTDOWN, \ 2384 (int fd, int how), \ 2385 (int, int), \ 2386 (fd, how)) 2387 2388 FDCALL(ssize_t, REALREAD, DUALCALL_READ, \ 2389 (int fd, void *buf, size_t buflen), \ 2390 (int, void *, size_t), \ 2391 (fd, buf, buflen)) 2392 2393 #ifdef __linux__ 2394 ssize_t __read_chk(int, void *, size_t) 2395 __attribute__((alias("read"))); 2396 #endif 2397 2398 FDCALL(ssize_t, readv, DUALCALL_READV, \ 2399 (int fd, const struct iovec *iov, int iovcnt), \ 2400 (int, const struct iovec *, int), \ 2401 (fd, iov, iovcnt)) 2402 2403 FDCALL(ssize_t, REALPREAD, DUALCALL_PREAD, \ 2404 (int fd, void *buf, size_t nbytes, off_t offset), \ 2405 (int, void *, size_t, off_t), \ 2406 (fd, buf, nbytes, offset)) 2407 2408 FDCALL(ssize_t, preadv, DUALCALL_PREADV, \ 2409 (int fd, const struct iovec *iov, int iovcnt, off_t offset), \ 2410 (int, const struct iovec *, int, off_t), \ 2411 (fd, iov, iovcnt, offset)) 2412 2413 FDCALL(ssize_t, writev, DUALCALL_WRITEV, \ 2414 (int fd, const struct iovec *iov, int iovcnt), \ 2415 (int, const struct iovec *, int), \ 2416 (fd, iov, iovcnt)) 2417 2418 FDCALL(ssize_t, REALPWRITE, DUALCALL_PWRITE, \ 2419 (int fd, const void *buf, size_t nbytes, off_t offset), \ 2420 (int, const void *, size_t, off_t), \ 2421 (fd, buf, nbytes, offset)) 2422 2423 FDCALL(ssize_t, pwritev, DUALCALL_PWRITEV, \ 2424 (int fd, const struct iovec *iov, int iovcnt, off_t offset), \ 2425 (int, const struct iovec *, int, off_t), \ 2426 (fd, iov, iovcnt, offset)) 2427 2428 #ifndef __linux__ 2429 FDCALL(int, REALFSTAT, DUALCALL_FSTAT, \ 2430 (int fd, struct stat *sb), \ 2431 (int, struct stat *), \ 2432 (fd, sb)) 2433 #endif 2434 2435 #ifdef __NetBSD__ 2436 FDCALL(int, fstatvfs1, DUALCALL_FSTATVFS1, \ 2437 (int fd, struct statvfs *buf, int flags), \ 2438 (int, struct statvfs *, int), \ 2439 (fd, buf, flags)) 2440 #endif 2441 2442 FDCALL(off_t, lseek, DUALCALL_LSEEK, \ 2443 (int fd, off_t offset, int whence), \ 2444 (int, off_t, int), \ 2445 (fd, offset, whence)) 2446 #ifdef LSEEK_ALIAS 2447 __strong_alias(LSEEK_ALIAS,lseek); 2448 #endif 2449 2450 #ifndef __linux__ 2451 FDCALL(int, REALGETDENTS, DUALCALL_GETDENTS, \ 2452 (int fd, char *buf, size_t nbytes), \ 2453 (int, char *, size_t), \ 2454 (fd, buf, nbytes)) 2455 #endif 2456 2457 FDCALL(int, fchown, DUALCALL_FCHOWN, \ 2458 (int fd, uid_t owner, gid_t group), \ 2459 (int, uid_t, gid_t), \ 2460 (fd, owner, group)) 2461 2462 FDCALL(int, fchmod, DUALCALL_FCHMOD, \ 2463 (int fd, mode_t mode), \ 2464 (int, mode_t), \ 2465 (fd, mode)) 2466 2467 FDCALL(int, ftruncate, DUALCALL_FTRUNCATE, \ 2468 (int fd, off_t length), \ 2469 (int, off_t), \ 2470 (fd, length)) 2471 2472 FDCALL(int, fsync, DUALCALL_FSYNC, \ 2473 (int fd), \ 2474 (int), \ 2475 (fd)) 2476 2477 #ifdef HAVE_FSYNC_RANGE 2478 FDCALL(int, fsync_range, DUALCALL_FSYNC_RANGE, \ 2479 (int fd, int how, off_t start, off_t length), \ 2480 (int, int, off_t, off_t), \ 2481 (fd, how, start, length)) 2482 #endif 2483 2484 FDCALL(int, futimes, DUALCALL_FUTIMES, \ 2485 (int fd, const struct timeval *tv), \ 2486 (int, const struct timeval *), \ 2487 (fd, tv)) 2488 2489 FDCALL(int, futimens, DUALCALL_FUTIMENS, \ 2490 (int fd, const struct timespec *ts), \ 2491 (int, const struct timespec *), \ 2492 (fd, ts)) 2493 2494 #ifdef HAVE_CHFLAGS 2495 FDCALL(int, fchflags, DUALCALL_FCHFLAGS, \ 2496 (int fd, u_long flags), \ 2497 (int, u_long), \ 2498 (fd, flags)) 2499 #endif 2500 2501 /* 2502 * path-based selectors 2503 */ 2504 2505 #ifndef __linux__ 2506 PATHCALL(int, REALSTAT, DUALCALL_STAT, \ 2507 (const char *path, struct stat *sb), \ 2508 (const char *, struct stat *), \ 2509 (path, sb)) 2510 2511 PATHCALL(int, REALLSTAT, DUALCALL_LSTAT, \ 2512 (const char *path, struct stat *sb), \ 2513 (const char *, struct stat *), \ 2514 (path, sb)) 2515 #endif 2516 2517 PATHCALL(int, chown, DUALCALL_CHOWN, \ 2518 (const char *path, uid_t owner, gid_t group), \ 2519 (const char *, uid_t, gid_t), \ 2520 (path, owner, group)) 2521 2522 PATHCALL(int, lchown, DUALCALL_LCHOWN, \ 2523 (const char *path, uid_t owner, gid_t group), \ 2524 (const char *, uid_t, gid_t), \ 2525 (path, owner, group)) 2526 2527 PATHCALL(int, chmod, DUALCALL_CHMOD, \ 2528 (const char *path, mode_t mode), \ 2529 (const char *, mode_t), \ 2530 (path, mode)) 2531 2532 PATHCALL(int, lchmod, DUALCALL_LCHMOD, \ 2533 (const char *path, mode_t mode), \ 2534 (const char *, mode_t), \ 2535 (path, mode)) 2536 2537 #ifdef __NetBSD__ 2538 PATHCALL(int, statvfs1, DUALCALL_STATVFS1, \ 2539 (const char *path, struct statvfs *buf, int flags), \ 2540 (const char *, struct statvfs *, int), \ 2541 (path, buf, flags)) 2542 #endif 2543 2544 PATHCALL(int, unlink, DUALCALL_UNLINK, \ 2545 (const char *path), \ 2546 (const char *), \ 2547 (path)) 2548 2549 PATHCALL(int, symlink, DUALCALL_SYMLINK, \ 2550 (const char *target, const char *path), \ 2551 (const char *, const char *), \ 2552 (target, path)) 2553 2554 /* 2555 * readlink() can be called from malloc which can be called 2556 * from dlsym() during init 2557 */ 2558 ssize_t 2559 readlink(const char *path, char *buf, size_t bufsiz) 2560 { 2561 int (*op_readlink)(const char *, char *, size_t); 2562 enum pathtype pt; 2563 2564 if ((pt = path_isrump(path)) != PATH_HOST) { 2565 op_readlink = GETSYSCALL(rump, READLINK); 2566 if (pt == PATH_RUMP) 2567 path = path_host2rump(path); 2568 } else { 2569 op_readlink = GETSYSCALL(host, READLINK); 2570 } 2571 2572 if (__predict_false(op_readlink == NULL)) { 2573 errno = ENOENT; 2574 return -1; 2575 } 2576 2577 return op_readlink(path, buf, bufsiz); 2578 } 2579 2580 PATHCALL(int, mkdir, DUALCALL_MKDIR, \ 2581 (const char *path, mode_t mode), \ 2582 (const char *, mode_t), \ 2583 (path, mode)) 2584 2585 PATHCALL(int, rmdir, DUALCALL_RMDIR, \ 2586 (const char *path), \ 2587 (const char *), \ 2588 (path)) 2589 2590 PATHCALL(int, utimes, DUALCALL_UTIMES, \ 2591 (const char *path, const struct timeval *tv), \ 2592 (const char *, const struct timeval *), \ 2593 (path, tv)) 2594 2595 PATHCALL(int, lutimes, DUALCALL_LUTIMES, \ 2596 (const char *path, const struct timeval *tv), \ 2597 (const char *, const struct timeval *), \ 2598 (path, tv)) 2599 2600 #ifdef HAVE_CHFLAGS 2601 PATHCALL(int, chflags, DUALCALL_CHFLAGS, \ 2602 (const char *path, u_long flags), \ 2603 (const char *, u_long), \ 2604 (path, flags)) 2605 2606 PATHCALL(int, lchflags, DUALCALL_LCHFLAGS, \ 2607 (const char *path, u_long flags), \ 2608 (const char *, u_long), \ 2609 (path, flags)) 2610 #endif /* HAVE_CHFLAGS */ 2611 2612 PATHCALL(int, truncate, DUALCALL_TRUNCATE, \ 2613 (const char *path, off_t length), \ 2614 (const char *, off_t), \ 2615 (path, length)) 2616 2617 PATHCALL(int, access, DUALCALL_ACCESS, \ 2618 (const char *path, int mode), \ 2619 (const char *, int), \ 2620 (path, mode)) 2621 2622 #ifndef __linux__ 2623 PATHCALL(int, REALMKNOD, DUALCALL_MKNOD, \ 2624 (const char *path, mode_t mode, dev_t dev), \ 2625 (const char *, mode_t, dev_t), \ 2626 (path, mode, dev)) 2627 #endif 2628 2629 /* 2630 * Note: with mount the decisive parameter is the mount 2631 * destination directory. This is because we don't really know 2632 * about the "source" directory in a generic call (and besides, 2633 * it might not even exist, cf. nfs). 2634 */ 2635 #ifdef __NetBSD__ 2636 PATHCALL(int, REALMOUNT, DUALCALL_MOUNT, \ 2637 (const char *type, const char *path, int flags, \ 2638 void *data, size_t dlen), \ 2639 (const char *, const char *, int, void *, size_t), \ 2640 (type, path, flags, data, dlen)) 2641 2642 PATHCALL(int, unmount, DUALCALL_UNMOUNT, \ 2643 (const char *path, int flags), \ 2644 (const char *, int), \ 2645 (path, flags)) 2646 #endif /* __NetBSD__ */ 2647 2648 #ifdef HAVE___QUOTACTL 2649 PATHCALL(int, __quotactl, DUALCALL_QUOTACTL, \ 2650 (const char *path, struct quotactl_args *args), \ 2651 (const char *, struct quotactl_args *), \ 2652 (path, args)) 2653 #endif /* HAVE___QUOTACTL */ 2654 2655 #ifdef __NetBSD__ 2656 PATHCALL(int, REALGETFH, DUALCALL_GETFH, \ 2657 (const char *path, void *fhp, size_t *fh_size), \ 2658 (const char *, void *, size_t *), \ 2659 (path, fhp, fh_size)) 2660 #endif 2661 2662 /* 2663 * These act different on a per-process vfs configuration 2664 */ 2665 2666 #ifdef __NetBSD__ 2667 VFSCALL(VFSBIT_GETVFSSTAT, int, getvfsstat, DUALCALL_GETVFSSTAT, \ 2668 (struct statvfs *buf, size_t buflen, int flags), \ 2669 (struct statvfs *, size_t, int), \ 2670 (buf, buflen, flags)) 2671 #endif 2672 2673 #ifdef __NetBSD__ 2674 VFSCALL(VFSBIT_FHCALLS, int, REALFHOPEN, DUALCALL_FHOPEN, \ 2675 (const void *fhp, size_t fh_size, int flags), \ 2676 (const char *, size_t, int), \ 2677 (fhp, fh_size, flags)) 2678 2679 VFSCALL(VFSBIT_FHCALLS, int, REALFHSTAT, DUALCALL_FHSTAT, \ 2680 (const void *fhp, size_t fh_size, struct stat *sb), \ 2681 (const char *, size_t, struct stat *), \ 2682 (fhp, fh_size, sb)) 2683 2684 VFSCALL(VFSBIT_FHCALLS, int, REALFHSTATVFS1, DUALCALL_FHSTATVFS1, \ 2685 (const void *fhp, size_t fh_size, struct statvfs *sb, int flgs),\ 2686 (const char *, size_t, struct statvfs *, int), \ 2687 (fhp, fh_size, sb, flgs)) 2688 #endif 2689 2690 2691 #ifdef __NetBSD__ 2692 2693 /* finally, put nfssvc here. "keep the namespace clean" */ 2694 #include <nfs/rpcv2.h> 2695 #include <nfs/nfs.h> 2696 2697 int 2698 nfssvc(int flags, void *argstructp) 2699 { 2700 int (*op_nfssvc)(int, void *); 2701 2702 if (vfsbits & VFSBIT_NFSSVC){ 2703 struct nfsd_args *nfsdargs; 2704 2705 /* massage the socket descriptor if necessary */ 2706 if (flags == NFSSVC_ADDSOCK) { 2707 nfsdargs = argstructp; 2708 nfsdargs->sock = fd_host2rump(nfsdargs->sock); 2709 } 2710 op_nfssvc = GETSYSCALL(rump, NFSSVC); 2711 } else 2712 op_nfssvc = GETSYSCALL(host, NFSSVC); 2713 2714 return op_nfssvc(flags, argstructp); 2715 } 2716 #endif /* __NetBSD__ */ 2717