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