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