1 /* $NetBSD: hijack.c,v 1.120 2016/08/11 09:48:57 kre 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.120 2016/08/11 09:48:57 kre 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 if (minfd >= hijack_fdoff) { 1068 errno = EINVAL; 1069 return -1; 1070 } 1071 op_fcntl = GETSYSCALL(host, FCNTL); 1072 isrump = 0; 1073 } 1074 1075 newd = op_fcntl(oldd, F_DUPFD, minfd); 1076 1077 if (isrump) 1078 newd = fd_rump2host(newd); 1079 DPRINTF(("dup <- %d\n", newd)); 1080 1081 return newd; 1082 } 1083 1084 /* 1085 * Check that host fd value does not exceed fdoffset and if necessary 1086 * dup the file descriptor so that it doesn't collide with the dup2mask. 1087 */ 1088 static int 1089 fd_host2host(int fd) 1090 { 1091 int (*op_fcntl)(int, int, ...) = GETSYSCALL(host, FCNTL); 1092 int (*op_close)(int) = GETSYSCALL(host, CLOSE); 1093 int ofd, i; 1094 1095 if (fd >= hijack_fdoff) { 1096 op_close(fd); 1097 errno = ENFILE; 1098 return -1; 1099 } 1100 1101 for (i = 1; isdup2d(fd); i++) { 1102 ofd = fd; 1103 fd = op_fcntl(ofd, F_DUPFD, i); 1104 op_close(ofd); 1105 } 1106 1107 return fd; 1108 } 1109 1110 int 1111 open(const char *path, int flags, ...) 1112 { 1113 int (*op_open)(const char *, int, ...); 1114 bool isrump; 1115 va_list ap; 1116 enum pathtype pt; 1117 int fd; 1118 1119 DPRINTF(("open -> %s (%s)\n", path, whichpath(path))); 1120 1121 if ((pt = path_isrump(path)) != PATH_HOST) { 1122 if (pt == PATH_RUMP) 1123 path = path_host2rump(path); 1124 op_open = GETSYSCALL(rump, OPEN); 1125 isrump = true; 1126 } else { 1127 op_open = GETSYSCALL(host, OPEN); 1128 isrump = false; 1129 } 1130 1131 va_start(ap, flags); 1132 fd = op_open(path, flags, va_arg(ap, mode_t)); 1133 va_end(ap); 1134 1135 if (isrump) 1136 fd = fd_rump2host(fd); 1137 else 1138 fd = fd_host2host(fd); 1139 1140 DPRINTF(("open <- %d (%s)\n", fd, whichfd(fd))); 1141 return fd; 1142 } 1143 1144 int 1145 chdir(const char *path) 1146 { 1147 int (*op_chdir)(const char *); 1148 enum pathtype pt; 1149 int rv; 1150 1151 if ((pt = path_isrump(path)) != PATH_HOST) { 1152 op_chdir = GETSYSCALL(rump, CHDIR); 1153 if (pt == PATH_RUMP) 1154 path = path_host2rump(path); 1155 } else { 1156 op_chdir = GETSYSCALL(host, CHDIR); 1157 } 1158 1159 rv = op_chdir(path); 1160 if (rv == 0) 1161 pwdinrump = pt != PATH_HOST; 1162 1163 return rv; 1164 } 1165 1166 int 1167 fchdir(int fd) 1168 { 1169 int (*op_fchdir)(int); 1170 bool isrump; 1171 int rv; 1172 1173 if (fd_isrump(fd)) { 1174 op_fchdir = GETSYSCALL(rump, FCHDIR); 1175 isrump = true; 1176 fd = fd_host2rump(fd); 1177 } else { 1178 op_fchdir = GETSYSCALL(host, FCHDIR); 1179 isrump = false; 1180 } 1181 1182 rv = op_fchdir(fd); 1183 if (rv == 0) { 1184 pwdinrump = isrump; 1185 } 1186 1187 return rv; 1188 } 1189 1190 #ifndef __linux__ 1191 int 1192 __getcwd(char *bufp, size_t len) 1193 { 1194 int (*op___getcwd)(char *, size_t); 1195 size_t prefixgap; 1196 bool iamslash; 1197 int rv; 1198 1199 if (pwdinrump && rumpprefix) { 1200 if (rumpprefix[rumpprefixlen-1] == '/') 1201 iamslash = true; 1202 else 1203 iamslash = false; 1204 1205 if (iamslash) 1206 prefixgap = rumpprefixlen - 1; /* ``//+path'' */ 1207 else 1208 prefixgap = rumpprefixlen; /* ``/pfx+/path'' */ 1209 if (len <= prefixgap) { 1210 errno = ERANGE; 1211 return -1; 1212 } 1213 1214 op___getcwd = GETSYSCALL(rump, __GETCWD); 1215 rv = op___getcwd(bufp + prefixgap, len - prefixgap); 1216 if (rv == -1) 1217 return rv; 1218 1219 /* augment the "/" part only for a non-root path */ 1220 memcpy(bufp, rumpprefix, rumpprefixlen); 1221 1222 /* append / only to non-root cwd */ 1223 if (rv != 2) 1224 bufp[prefixgap] = '/'; 1225 1226 /* don't append extra slash in the purely-slash case */ 1227 if (rv == 2 && !iamslash) 1228 bufp[rumpprefixlen] = '\0'; 1229 } else if (pwdinrump) { 1230 /* assume blanket. we can't provide a prefix here */ 1231 op___getcwd = GETSYSCALL(rump, __GETCWD); 1232 rv = op___getcwd(bufp, len); 1233 } else { 1234 op___getcwd = GETSYSCALL(host, __GETCWD); 1235 rv = op___getcwd(bufp, len); 1236 } 1237 1238 return rv; 1239 } 1240 #endif 1241 1242 static int 1243 moveish(const char *from, const char *to, 1244 int (*rump_op)(const char *, const char *), 1245 int (*host_op)(const char *, const char *)) 1246 { 1247 int (*op)(const char *, const char *); 1248 enum pathtype ptf, ptt; 1249 1250 if ((ptf = path_isrump(from)) != PATH_HOST) { 1251 if ((ptt = path_isrump(to)) == PATH_HOST) { 1252 errno = EXDEV; 1253 return -1; 1254 } 1255 1256 if (ptf == PATH_RUMP) 1257 from = path_host2rump(from); 1258 if (ptt == PATH_RUMP) 1259 to = path_host2rump(to); 1260 op = rump_op; 1261 } else { 1262 if (path_isrump(to) != PATH_HOST) { 1263 errno = EXDEV; 1264 return -1; 1265 } 1266 1267 op = host_op; 1268 } 1269 1270 return op(from, to); 1271 } 1272 1273 int 1274 link(const char *from, const char *to) 1275 { 1276 return moveish(from, to, 1277 GETSYSCALL(rump, LINK), GETSYSCALL(host, LINK)); 1278 } 1279 1280 int 1281 rename(const char *from, const char *to) 1282 { 1283 return moveish(from, to, 1284 GETSYSCALL(rump, RENAME), GETSYSCALL(host, RENAME)); 1285 } 1286 1287 int 1288 REALSOCKET(int domain, int type, int protocol) 1289 { 1290 int (*op_socket)(int, int, int); 1291 int fd; 1292 bool isrump; 1293 1294 isrump = domain < PF_MAX && rumpsockets[domain]; 1295 1296 if (isrump) 1297 op_socket = GETSYSCALL(rump, SOCKET); 1298 else 1299 op_socket = GETSYSCALL(host, SOCKET); 1300 fd = op_socket(domain, type, protocol); 1301 1302 if (isrump) 1303 fd = fd_rump2host(fd); 1304 else 1305 fd = fd_host2host(fd); 1306 DPRINTF(("socket <- %d\n", fd)); 1307 1308 return fd; 1309 } 1310 1311 int 1312 accept(int s, struct sockaddr *addr, socklen_t *addrlen) 1313 { 1314 int (*op_accept)(int, struct sockaddr *, socklen_t *); 1315 int fd; 1316 bool isrump; 1317 1318 isrump = fd_isrump(s); 1319 1320 DPRINTF(("accept -> %d", s)); 1321 if (isrump) { 1322 op_accept = GETSYSCALL(rump, ACCEPT); 1323 s = fd_host2rump(s); 1324 } else { 1325 op_accept = GETSYSCALL(host, ACCEPT); 1326 } 1327 fd = op_accept(s, addr, addrlen); 1328 if (fd != -1 && isrump) 1329 fd = fd_rump2host(fd); 1330 else 1331 fd = fd_host2host(fd); 1332 1333 DPRINTF((" <- %d\n", fd)); 1334 1335 return fd; 1336 } 1337 1338 /* 1339 * ioctl() and fcntl() are varargs calls and need special treatment. 1340 */ 1341 1342 /* 1343 * Various [Linux] libc's have various signatures for ioctl so we 1344 * need to handle the discrepancies. On NetBSD, we use the 1345 * one with unsigned long cmd. 1346 */ 1347 int 1348 #ifdef HAVE_IOCTL_CMD_INT 1349 ioctl(int fd, int cmd, ...) 1350 { 1351 int (*op_ioctl)(int, int cmd, ...); 1352 #else 1353 ioctl(int fd, unsigned long cmd, ...) 1354 { 1355 int (*op_ioctl)(int, unsigned long cmd, ...); 1356 #endif 1357 va_list ap; 1358 int rv; 1359 1360 DPRINTF(("ioctl -> %d\n", fd)); 1361 if (fd_isrump(fd)) { 1362 fd = fd_host2rump(fd); 1363 op_ioctl = GETSYSCALL(rump, IOCTL); 1364 } else { 1365 op_ioctl = GETSYSCALL(host, IOCTL); 1366 } 1367 1368 va_start(ap, cmd); 1369 rv = op_ioctl(fd, cmd, va_arg(ap, void *)); 1370 va_end(ap); 1371 return rv; 1372 } 1373 1374 int 1375 fcntl(int fd, int cmd, ...) 1376 { 1377 int (*op_fcntl)(int, int, ...); 1378 va_list ap; 1379 int rv, minfd; 1380 1381 DPRINTF(("fcntl -> %d (cmd %d)\n", fd, cmd)); 1382 1383 switch (cmd) { 1384 case F_DUPFD: 1385 va_start(ap, cmd); 1386 minfd = va_arg(ap, int); 1387 va_end(ap); 1388 return dodup(fd, minfd); 1389 1390 #ifdef F_CLOSEM 1391 case F_CLOSEM: { 1392 int maxdup2, i; 1393 1394 /* 1395 * So, if fd < HIJACKOFF, we want to do a host closem. 1396 */ 1397 1398 if (fd < hijack_fdoff) { 1399 int closemfd = fd; 1400 1401 if (rumpclient__closenotify(&closemfd, 1402 RUMPCLIENT_CLOSE_FCLOSEM) == -1) 1403 return -1; 1404 op_fcntl = GETSYSCALL(host, FCNTL); 1405 rv = op_fcntl(closemfd, cmd); 1406 if (rv) 1407 return rv; 1408 } 1409 1410 /* 1411 * Additionally, we want to do a rump closem, but only 1412 * for the file descriptors not dup2'd. 1413 */ 1414 1415 for (i = 0, maxdup2 = -1; i <= DUP2HIGH; i++) { 1416 if (dup2vec[i] & DUP2BIT) { 1417 int val; 1418 1419 val = dup2vec[i] & DUP2FDMASK; 1420 maxdup2 = MAX(val, maxdup2); 1421 } 1422 } 1423 1424 if (fd >= hijack_fdoff) 1425 fd -= hijack_fdoff; 1426 else 1427 fd = 0; 1428 fd = MAX(maxdup2+1, fd); 1429 1430 /* hmm, maybe we should close rump fd's not within dup2mask? */ 1431 return rump_sys_fcntl(fd, F_CLOSEM); 1432 } 1433 #endif /* F_CLOSEM */ 1434 1435 #ifdef F_MAXFD 1436 case F_MAXFD: 1437 /* 1438 * For maxfd, if there's a rump kernel fd, return 1439 * it hostified. Otherwise, return host's MAXFD 1440 * return value. 1441 */ 1442 if ((rv = rump_sys_fcntl(fd, F_MAXFD)) != -1) { 1443 /* 1444 * This might go a little wrong in case 1445 * of dup2 to [012], but I'm not sure if 1446 * there's a justification for tracking 1447 * that info. Consider e.g. 1448 * dup2(rumpfd, 2) followed by rump_sys_open() 1449 * returning 1. We should return 1+HIJACKOFF, 1450 * not 2+HIJACKOFF. However, if [01] is not 1451 * open, the correct return value is 2. 1452 */ 1453 return fd_rump2host(fd); 1454 } else { 1455 op_fcntl = GETSYSCALL(host, FCNTL); 1456 return op_fcntl(fd, F_MAXFD); 1457 } 1458 /*NOTREACHED*/ 1459 #endif /* F_MAXFD */ 1460 1461 default: 1462 if (fd_isrump(fd)) { 1463 fd = fd_host2rump(fd); 1464 op_fcntl = GETSYSCALL(rump, FCNTL); 1465 } else { 1466 op_fcntl = GETSYSCALL(host, FCNTL); 1467 } 1468 1469 va_start(ap, cmd); 1470 rv = op_fcntl(fd, cmd, va_arg(ap, void *)); 1471 va_end(ap); 1472 return rv; 1473 } 1474 /*NOTREACHED*/ 1475 } 1476 1477 int 1478 close(int fd) 1479 { 1480 int (*op_close)(int); 1481 int rv; 1482 1483 DPRINTF(("close -> %d\n", fd)); 1484 if (fd_isrump(fd)) { 1485 bool undup2 = false; 1486 int ofd; 1487 1488 if (isdup2d(ofd = fd)) { 1489 undup2 = true; 1490 } 1491 1492 fd = fd_host2rump(fd); 1493 if (!undup2 && killdup2alias(fd)) { 1494 return 0; 1495 } 1496 1497 op_close = GETSYSCALL(rump, CLOSE); 1498 rv = op_close(fd); 1499 if (rv == 0 && undup2) { 1500 clrdup2(ofd); 1501 } 1502 } else { 1503 if (rumpclient__closenotify(&fd, RUMPCLIENT_CLOSE_CLOSE) == -1) 1504 return -1; 1505 op_close = GETSYSCALL(host, CLOSE); 1506 rv = op_close(fd); 1507 } 1508 1509 return rv; 1510 } 1511 1512 /* 1513 * write cannot issue a standard debug printf due to recursion 1514 */ 1515 ssize_t 1516 write(int fd, const void *buf, size_t blen) 1517 { 1518 ssize_t (*op_write)(int, const void *, size_t); 1519 1520 if (fd_isrump(fd)) { 1521 fd = fd_host2rump(fd); 1522 op_write = GETSYSCALL(rump, WRITE); 1523 } else { 1524 op_write = GETSYSCALL(host, WRITE); 1525 } 1526 1527 return op_write(fd, buf, blen); 1528 } 1529 1530 /* 1531 * file descriptor passing 1532 * 1533 * we intercept sendmsg and recvmsg to convert file descriptors in 1534 * control messages. an attempt to send a descriptor from a different kernel 1535 * is rejected. (ENOTSUP) 1536 */ 1537 1538 static int 1539 msg_convert(struct msghdr *msg, int (*func)(int)) 1540 { 1541 struct cmsghdr *cmsg; 1542 1543 for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; 1544 cmsg = CMSG_NXTHDR(msg, cmsg)) { 1545 if (cmsg->cmsg_level == SOL_SOCKET && 1546 cmsg->cmsg_type == SCM_RIGHTS) { 1547 int *fdp = (void *)CMSG_DATA(cmsg); 1548 const size_t size = 1549 cmsg->cmsg_len - __CMSG_ALIGN(sizeof(*cmsg)); 1550 const int nfds = (int)(size / sizeof(int)); 1551 const int * const efdp = fdp + nfds; 1552 1553 while (fdp < efdp) { 1554 const int newval = func(*fdp); 1555 1556 if (newval < 0) { 1557 return ENOTSUP; 1558 } 1559 *fdp = newval; 1560 fdp++; 1561 } 1562 } 1563 } 1564 return 0; 1565 } 1566 1567 ssize_t 1568 recvmsg(int fd, struct msghdr *msg, int flags) 1569 { 1570 ssize_t (*op_recvmsg)(int, struct msghdr *, int); 1571 ssize_t ret; 1572 const bool isrump = fd_isrump(fd); 1573 1574 if (isrump) { 1575 fd = fd_host2rump(fd); 1576 op_recvmsg = GETSYSCALL(rump, RECVMSG); 1577 } else { 1578 op_recvmsg = GETSYSCALL(host, RECVMSG); 1579 } 1580 ret = op_recvmsg(fd, msg, flags); 1581 if (ret == -1) { 1582 return ret; 1583 } 1584 /* 1585 * convert descriptors in the message. 1586 */ 1587 if (isrump) { 1588 msg_convert(msg, fd_rump2host); 1589 } else { 1590 msg_convert(msg, fd_host2host); 1591 } 1592 return ret; 1593 } 1594 1595 ssize_t 1596 recv(int fd, void *buf, size_t len, int flags) 1597 { 1598 1599 return recvfrom(fd, buf, len, flags, NULL, NULL); 1600 } 1601 1602 ssize_t 1603 send(int fd, const void *buf, size_t len, int flags) 1604 { 1605 1606 return sendto(fd, buf, len, flags, NULL, 0); 1607 } 1608 1609 static int 1610 fd_check_rump(int fd) 1611 { 1612 1613 return fd_isrump(fd) ? 0 : -1; 1614 } 1615 1616 static int 1617 fd_check_host(int fd) 1618 { 1619 1620 return !fd_isrump(fd) ? 0 : -1; 1621 } 1622 1623 ssize_t 1624 sendmsg(int fd, const struct msghdr *msg, int flags) 1625 { 1626 ssize_t (*op_sendmsg)(int, const struct msghdr *, int); 1627 const bool isrump = fd_isrump(fd); 1628 int error; 1629 1630 /* 1631 * reject descriptors from a different kernel. 1632 */ 1633 error = msg_convert(__UNCONST(msg), 1634 isrump ? fd_check_rump: fd_check_host); 1635 if (error != 0) { 1636 errno = error; 1637 return -1; 1638 } 1639 /* 1640 * convert descriptors in the message to raw values. 1641 */ 1642 if (isrump) { 1643 fd = fd_host2rump(fd); 1644 /* 1645 * XXX we directly modify the given message assuming: 1646 * - cmsg is writable (typically on caller's stack) 1647 * - caller don't care cmsg's contents after calling sendmsg. 1648 * (thus no need to restore values) 1649 * 1650 * it's safer to copy and modify instead. 1651 */ 1652 msg_convert(__UNCONST(msg), fd_host2rump); 1653 op_sendmsg = GETSYSCALL(rump, SENDMSG); 1654 } else { 1655 op_sendmsg = GETSYSCALL(host, SENDMSG); 1656 } 1657 return op_sendmsg(fd, msg, flags); 1658 } 1659 1660 /* 1661 * dup2 is special. we allow dup2 of a rump kernel fd to 0-2 since 1662 * many programs do that. dup2 of a rump kernel fd to another value 1663 * not >= fdoff is an error. 1664 * 1665 * Note: cannot rump2host newd, because it is often hardcoded. 1666 */ 1667 int 1668 dup2(int oldd, int newd) 1669 { 1670 int (*host_dup2)(int, int); 1671 int rv; 1672 1673 DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd)); 1674 1675 if (fd_isrump(oldd)) { 1676 int (*op_close)(int) = GETSYSCALL(host, CLOSE); 1677 1678 /* only allow fd 0-2 for cross-kernel dup */ 1679 if (!(newd >= 0 && newd <= 2 && !fd_isrump(newd))) { 1680 errno = EBADF; 1681 return -1; 1682 } 1683 1684 /* regular dup2? */ 1685 if (fd_isrump(newd)) { 1686 newd = fd_host2rump(newd); 1687 rv = rump_sys_dup2(oldd, newd); 1688 return fd_rump2host(rv); 1689 } 1690 1691 /* 1692 * dup2 rump => host? just establish an 1693 * entry in the mapping table. 1694 */ 1695 op_close(newd); 1696 setdup2(newd, fd_host2rump(oldd)); 1697 rv = 0; 1698 } else { 1699 host_dup2 = syscalls[DUALCALL_DUP2].bs_host; 1700 if (rumpclient__closenotify(&newd, RUMPCLIENT_CLOSE_DUP2) == -1) 1701 return -1; 1702 rv = host_dup2(oldd, newd); 1703 } 1704 1705 return rv; 1706 } 1707 1708 int 1709 dup(int oldd) 1710 { 1711 1712 return dodup(oldd, 0); 1713 } 1714 1715 pid_t 1716 fork(void) 1717 { 1718 pid_t rv; 1719 1720 DPRINTF(("fork\n")); 1721 1722 rv = rumpclient__dofork(host_fork); 1723 1724 DPRINTF(("fork returns %d\n", rv)); 1725 return rv; 1726 } 1727 #ifdef VFORK 1728 /* we do not have the luxury of not requiring a stackframe */ 1729 #define __strong_alias_macro(m, f) __strong_alias(m, f) 1730 __strong_alias_macro(VFORK,fork); 1731 #endif 1732 1733 int 1734 daemon(int nochdir, int noclose) 1735 { 1736 struct rumpclient_fork *rf; 1737 1738 if ((rf = rumpclient_prefork()) == NULL) 1739 return -1; 1740 1741 if (host_daemon(nochdir, noclose) == -1) 1742 return -1; 1743 1744 if (rumpclient_fork_init(rf) == -1) 1745 return -1; 1746 1747 return 0; 1748 } 1749 1750 int 1751 execve(const char *path, char *const argv[], char *const envp[]) 1752 { 1753 char buf[128]; 1754 char *dup2str; 1755 const char *pwdinrumpstr; 1756 char **newenv; 1757 size_t nelem; 1758 int rv, sverrno; 1759 int bonus = 2, i = 0; 1760 1761 snprintf(buf, sizeof(buf), "RUMPHIJACK__DUP2INFO=%u,%u,%u", 1762 dup2vec[0], dup2vec[1], dup2vec[2]); 1763 dup2str = strdup(buf); 1764 if (dup2str == NULL) { 1765 errno = ENOMEM; 1766 return -1; 1767 } 1768 1769 if (pwdinrump) { 1770 pwdinrumpstr = "RUMPHIJACK__PWDINRUMP=true"; 1771 bonus++; 1772 } else { 1773 pwdinrumpstr = NULL; 1774 } 1775 1776 for (nelem = 0; envp && envp[nelem]; nelem++) 1777 continue; 1778 newenv = malloc(sizeof(*newenv) * (nelem+bonus)); 1779 if (newenv == NULL) { 1780 free(dup2str); 1781 errno = ENOMEM; 1782 return -1; 1783 } 1784 memcpy(newenv, envp, nelem*sizeof(*newenv)); 1785 newenv[nelem+i] = dup2str; 1786 i++; 1787 1788 if (pwdinrumpstr) { 1789 newenv[nelem+i] = __UNCONST(pwdinrumpstr); 1790 i++; 1791 } 1792 newenv[nelem+i] = NULL; 1793 _DIAGASSERT(i < bonus); 1794 1795 rv = rumpclient_exec(path, argv, newenv); 1796 1797 _DIAGASSERT(rv != 0); 1798 sverrno = errno; 1799 free(newenv); 1800 free(dup2str); 1801 errno = sverrno; 1802 return rv; 1803 } 1804 1805 /* 1806 * select is done by calling poll. 1807 */ 1808 int 1809 REALSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, 1810 struct timeval *timeout) 1811 { 1812 struct pollfd *pfds; 1813 struct timespec ts, *tsp = NULL; 1814 nfds_t realnfds; 1815 int i, j; 1816 int rv, incr; 1817 1818 DPRINTF(("select %d %p %p %p %p\n", nfds, 1819 readfds, writefds, exceptfds, timeout)); 1820 1821 /* 1822 * Well, first we must scan the fds to figure out how many 1823 * fds there really are. This is because up to and including 1824 * nb5 poll() silently refuses nfds > process_maxopen_fds. 1825 * Seems to be fixed in current, thank the maker. 1826 * god damn cluster...bomb. 1827 */ 1828 1829 for (i = 0, realnfds = 0; i < nfds; i++) { 1830 if (readfds && FD_ISSET(i, readfds)) { 1831 realnfds++; 1832 continue; 1833 } 1834 if (writefds && FD_ISSET(i, writefds)) { 1835 realnfds++; 1836 continue; 1837 } 1838 if (exceptfds && FD_ISSET(i, exceptfds)) { 1839 realnfds++; 1840 continue; 1841 } 1842 } 1843 1844 if (realnfds) { 1845 pfds = calloc(realnfds, sizeof(*pfds)); 1846 if (!pfds) 1847 return -1; 1848 } else { 1849 pfds = NULL; 1850 } 1851 1852 for (i = 0, j = 0; i < nfds; i++) { 1853 incr = 0; 1854 if (readfds && FD_ISSET(i, readfds)) { 1855 pfds[j].fd = i; 1856 pfds[j].events |= POLLIN; 1857 incr=1; 1858 } 1859 if (writefds && FD_ISSET(i, writefds)) { 1860 pfds[j].fd = i; 1861 pfds[j].events |= POLLOUT; 1862 incr=1; 1863 } 1864 if (exceptfds && FD_ISSET(i, exceptfds)) { 1865 pfds[j].fd = i; 1866 pfds[j].events |= POLLHUP|POLLERR; 1867 incr=1; 1868 } 1869 if (incr) 1870 j++; 1871 } 1872 assert(j == (int)realnfds); 1873 1874 if (timeout) { 1875 TIMEVAL_TO_TIMESPEC(timeout, &ts); 1876 tsp = &ts; 1877 } 1878 rv = REALPOLLTS(pfds, realnfds, tsp, NULL); 1879 /* 1880 * "If select() returns with an error the descriptor sets 1881 * will be unmodified" 1882 */ 1883 if (rv < 0) 1884 goto out; 1885 1886 /* 1887 * zero out results (can't use FD_ZERO for the 1888 * obvious select-me-not reason). whee. 1889 * 1890 * We do this here since some software ignores the return 1891 * value of select, and hence if the timeout expires, it may 1892 * assume all input descriptors have activity. 1893 */ 1894 for (i = 0; i < nfds; i++) { 1895 if (readfds) 1896 FD_CLR(i, readfds); 1897 if (writefds) 1898 FD_CLR(i, writefds); 1899 if (exceptfds) 1900 FD_CLR(i, exceptfds); 1901 } 1902 if (rv == 0) 1903 goto out; 1904 1905 /* 1906 * We have >0 fds with activity. Harvest the results. 1907 */ 1908 for (i = 0; i < (int)realnfds; i++) { 1909 if (readfds) { 1910 if (pfds[i].revents & POLLIN) { 1911 FD_SET(pfds[i].fd, readfds); 1912 } 1913 } 1914 if (writefds) { 1915 if (pfds[i].revents & POLLOUT) { 1916 FD_SET(pfds[i].fd, writefds); 1917 } 1918 } 1919 if (exceptfds) { 1920 if (pfds[i].revents & (POLLHUP|POLLERR)) { 1921 FD_SET(pfds[i].fd, exceptfds); 1922 } 1923 } 1924 } 1925 1926 out: 1927 free(pfds); 1928 return rv; 1929 } 1930 1931 static void 1932 checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall) 1933 { 1934 nfds_t i; 1935 1936 for (i = 0; i < nfds; i++) { 1937 if (fds[i].fd == -1) 1938 continue; 1939 1940 if (fd_isrump(fds[i].fd)) 1941 (*rumpcall)++; 1942 else 1943 (*hostcall)++; 1944 } 1945 } 1946 1947 static void 1948 adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int)) 1949 { 1950 nfds_t i; 1951 1952 for (i = 0; i < nfds; i++) { 1953 fds[i].fd = fdadj(fds[i].fd); 1954 } 1955 } 1956 1957 /* 1958 * poll is easy as long as the call comes in the fds only in one 1959 * kernel. otherwise its quite tricky... 1960 */ 1961 struct pollarg { 1962 struct pollfd *pfds; 1963 nfds_t nfds; 1964 const struct timespec *ts; 1965 const sigset_t *sigmask; 1966 int pipefd; 1967 int errnum; 1968 }; 1969 1970 static void * 1971 hostpoll(void *arg) 1972 { 1973 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *, 1974 const sigset_t *); 1975 struct pollarg *parg = arg; 1976 intptr_t rv; 1977 1978 op_pollts = GETSYSCALL(host, POLLTS); 1979 rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask); 1980 if (rv == -1) 1981 parg->errnum = errno; 1982 rump_sys_write(parg->pipefd, &rv, sizeof(rv)); 1983 1984 return (void *)rv; 1985 } 1986 1987 int 1988 REALPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts, 1989 const sigset_t *sigmask) 1990 { 1991 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *, 1992 const sigset_t *); 1993 int (*host_close)(int); 1994 int hostcall = 0, rumpcall = 0; 1995 pthread_t pt; 1996 nfds_t i; 1997 int rv; 1998 1999 DPRINTF(("poll %p %d %p %p\n", fds, (int)nfds, ts, sigmask)); 2000 checkpoll(fds, nfds, &hostcall, &rumpcall); 2001 2002 if (hostcall && rumpcall) { 2003 struct pollfd *pfd_host = NULL, *pfd_rump = NULL; 2004 int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1}; 2005 struct pollarg parg; 2006 void *trv_val; 2007 int sverrno = 0, rv_rump, rv_host, errno_rump, errno_host; 2008 2009 /* 2010 * ok, this is where it gets tricky. We must support 2011 * this since it's a very common operation in certain 2012 * types of software (telnet, netcat, etc). We allocate 2013 * two vectors and run two poll commands in separate 2014 * threads. Whichever returns first "wins" and the 2015 * other kernel's fds won't show activity. 2016 */ 2017 rv = -1; 2018 2019 /* allocate full vector for O(n) joining after call */ 2020 pfd_host = malloc(sizeof(*pfd_host)*(nfds+1)); 2021 if (!pfd_host) 2022 goto out; 2023 pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1)); 2024 if (!pfd_rump) { 2025 goto out; 2026 } 2027 2028 /* 2029 * then, open two pipes, one for notifications 2030 * to each kernel. 2031 * 2032 * At least the rump pipe should probably be 2033 * cached, along with the helper threads. This 2034 * should give a microbenchmark improvement (haven't 2035 * experienced a macro-level problem yet, though). 2036 */ 2037 if ((rv = rump_sys_pipe(rpipe)) == -1) { 2038 sverrno = errno; 2039 } 2040 if (rv == 0 && (rv = pipe(hpipe)) == -1) { 2041 sverrno = errno; 2042 } 2043 2044 /* split vectors (or signal errors) */ 2045 for (i = 0; i < nfds; i++) { 2046 int fd; 2047 2048 fds[i].revents = 0; 2049 if (fds[i].fd == -1) { 2050 pfd_host[i].fd = -1; 2051 pfd_rump[i].fd = -1; 2052 } else if (fd_isrump(fds[i].fd)) { 2053 pfd_host[i].fd = -1; 2054 fd = fd_host2rump(fds[i].fd); 2055 if (fd == rpipe[0] || fd == rpipe[1]) { 2056 fds[i].revents = POLLNVAL; 2057 if (rv != -1) 2058 rv++; 2059 } 2060 pfd_rump[i].fd = fd; 2061 pfd_rump[i].events = fds[i].events; 2062 } else { 2063 pfd_rump[i].fd = -1; 2064 fd = fds[i].fd; 2065 if (fd == hpipe[0] || fd == hpipe[1]) { 2066 fds[i].revents = POLLNVAL; 2067 if (rv != -1) 2068 rv++; 2069 } 2070 pfd_host[i].fd = fd; 2071 pfd_host[i].events = fds[i].events; 2072 } 2073 pfd_rump[i].revents = pfd_host[i].revents = 0; 2074 } 2075 if (rv) { 2076 goto out; 2077 } 2078 2079 pfd_host[nfds].fd = hpipe[0]; 2080 pfd_host[nfds].events = POLLIN; 2081 pfd_rump[nfds].fd = rpipe[0]; 2082 pfd_rump[nfds].events = POLLIN; 2083 2084 /* 2085 * then, create a thread to do host part and meanwhile 2086 * do rump kernel part right here 2087 */ 2088 2089 parg.pfds = pfd_host; 2090 parg.nfds = nfds+1; 2091 parg.ts = ts; 2092 parg.sigmask = sigmask; 2093 parg.pipefd = rpipe[1]; 2094 pthread_create(&pt, NULL, hostpoll, &parg); 2095 2096 op_pollts = GETSYSCALL(rump, POLLTS); 2097 rv_rump = op_pollts(pfd_rump, nfds+1, ts, NULL); 2098 errno_rump = errno; 2099 write(hpipe[1], &rv, sizeof(rv)); 2100 pthread_join(pt, &trv_val); 2101 rv_host = (int)(intptr_t)trv_val; 2102 errno_host = parg.errnum; 2103 2104 /* strip cross-thread notification from real results */ 2105 if (rv_host > 0 && pfd_host[nfds].revents & POLLIN) { 2106 rv_host--; 2107 } 2108 if (rv_rump > 0 && pfd_rump[nfds].revents & POLLIN) { 2109 rv_rump--; 2110 } 2111 2112 /* then merge the results into what's reported to the caller */ 2113 if (rv_rump > 0 || rv_host > 0) { 2114 /* SUCCESS */ 2115 2116 rv = 0; 2117 if (rv_rump > 0) { 2118 for (i = 0; i < nfds; i++) { 2119 if (pfd_rump[i].fd != -1) 2120 fds[i].revents 2121 = pfd_rump[i].revents; 2122 } 2123 rv += rv_rump; 2124 } 2125 if (rv_host > 0) { 2126 for (i = 0; i < nfds; i++) { 2127 if (pfd_host[i].fd != -1) 2128 fds[i].revents 2129 = pfd_host[i].revents; 2130 } 2131 rv += rv_host; 2132 } 2133 assert(rv > 0); 2134 sverrno = 0; 2135 } else if (rv_rump == -1 || rv_host == -1) { 2136 /* ERROR */ 2137 2138 /* just pick one kernel at "random" */ 2139 rv = -1; 2140 if (rv_host == -1) { 2141 sverrno = errno_host; 2142 } else if (rv_rump == -1) { 2143 sverrno = errno_rump; 2144 } 2145 } else { 2146 /* TIMEOUT */ 2147 2148 rv = 0; 2149 assert(rv_rump == 0 && rv_host == 0); 2150 } 2151 2152 out: 2153 host_close = GETSYSCALL(host, CLOSE); 2154 if (rpipe[0] != -1) 2155 rump_sys_close(rpipe[0]); 2156 if (rpipe[1] != -1) 2157 rump_sys_close(rpipe[1]); 2158 if (hpipe[0] != -1) 2159 host_close(hpipe[0]); 2160 if (hpipe[1] != -1) 2161 host_close(hpipe[1]); 2162 free(pfd_host); 2163 free(pfd_rump); 2164 errno = sverrno; 2165 } else { 2166 if (hostcall) { 2167 op_pollts = GETSYSCALL(host, POLLTS); 2168 } else { 2169 op_pollts = GETSYSCALL(rump, POLLTS); 2170 adjustpoll(fds, nfds, fd_host2rump); 2171 } 2172 2173 rv = op_pollts(fds, nfds, ts, sigmask); 2174 if (rumpcall) 2175 adjustpoll(fds, nfds, fd_rump2host_withdup); 2176 } 2177 2178 return rv; 2179 } 2180 2181 int 2182 poll(struct pollfd *fds, nfds_t nfds, int timeout) 2183 { 2184 struct timespec ts; 2185 struct timespec *tsp = NULL; 2186 2187 if (timeout != INFTIM) { 2188 ts.tv_sec = timeout / 1000; 2189 ts.tv_nsec = (timeout % 1000) * 1000*1000; 2190 2191 tsp = &ts; 2192 } 2193 2194 return REALPOLLTS(fds, nfds, tsp, NULL); 2195 } 2196 2197 #ifdef HAVE_KQUEUE 2198 int 2199 REALKEVENT(int kq, const struct kevent *changelist, size_t nchanges, 2200 struct kevent *eventlist, size_t nevents, 2201 const struct timespec *timeout) 2202 { 2203 int (*op_kevent)(int, const struct kevent *, size_t, 2204 struct kevent *, size_t, const struct timespec *); 2205 const struct kevent *ev; 2206 size_t i; 2207 2208 /* 2209 * Check that we don't attempt to kevent rump kernel fd's. 2210 * That needs similar treatment to select/poll, but is slightly 2211 * trickier since we need to manage to different kq descriptors. 2212 * (TODO, in case you're wondering). 2213 */ 2214 for (i = 0; i < nchanges; i++) { 2215 ev = &changelist[i]; 2216 if (ev->filter == EVFILT_READ || ev->filter == EVFILT_WRITE || 2217 ev->filter == EVFILT_VNODE) { 2218 if (fd_isrump((int)ev->ident)) { 2219 errno = ENOTSUP; 2220 return -1; 2221 } 2222 } 2223 } 2224 2225 op_kevent = GETSYSCALL(host, KEVENT); 2226 return op_kevent(kq, changelist, nchanges, eventlist, nevents, timeout); 2227 } 2228 #endif /* HAVE_KQUEUE */ 2229 2230 /* 2231 * mmapping from a rump kernel is not supported, so disallow it. 2232 */ 2233 void * 2234 mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) 2235 { 2236 2237 if (flags & MAP_FILE && fd_isrump(fd)) { 2238 errno = ENOSYS; 2239 return MAP_FAILED; 2240 } 2241 if (__predict_false(host_mmap == NULL)) { 2242 host_mmap = rumphijack_dlsym(RTLD_NEXT, "mmap"); 2243 } 2244 return host_mmap(addr, len, prot, flags, fd, offset); 2245 } 2246 2247 #ifdef __NetBSD__ 2248 /* 2249 * these go to one or the other on a per-process configuration 2250 */ 2251 int __sysctl(const int *, unsigned int, void *, size_t *, const void *, size_t); 2252 int 2253 __sysctl(const int *name, unsigned int namelen, void *old, size_t *oldlenp, 2254 const void *new, size_t newlen) 2255 { 2256 int (*op___sysctl)(const int *, unsigned int, void *, size_t *, 2257 const void *, size_t); 2258 2259 if (rumpsysctl) { 2260 op___sysctl = GETSYSCALL(rump, __SYSCTL); 2261 } else { 2262 op___sysctl = GETSYSCALL(host, __SYSCTL); 2263 /* we haven't inited yet */ 2264 if (__predict_false(op___sysctl == NULL)) { 2265 op___sysctl = rumphijack_dlsym(RTLD_NEXT, "__sysctl"); 2266 } 2267 } 2268 2269 return op___sysctl(name, namelen, old, oldlenp, new, newlen); 2270 } 2271 #endif 2272 2273 /* 2274 * Rest are std type calls. 2275 */ 2276 2277 #ifdef HAVE_UTIMENSAT 2278 ATCALL(int, utimensat, DUALCALL_UTIMENSAT, \ 2279 (int fd, const char *path, const struct timespec t[2], int f), \ 2280 (int, const char *, const struct timespec [2], int), 2281 (fd, path, t, f)) 2282 #endif 2283 2284 FDCALL(int, bind, DUALCALL_BIND, \ 2285 (int fd, const struct sockaddr *name, socklen_t namelen), \ 2286 (int, const struct sockaddr *, socklen_t), \ 2287 (fd, name, namelen)) 2288 2289 FDCALL(int, connect, DUALCALL_CONNECT, \ 2290 (int fd, const struct sockaddr *name, socklen_t namelen), \ 2291 (int, const struct sockaddr *, socklen_t), \ 2292 (fd, name, namelen)) 2293 2294 FDCALL(int, getpeername, DUALCALL_GETPEERNAME, \ 2295 (int fd, struct sockaddr *name, socklen_t *namelen), \ 2296 (int, struct sockaddr *, socklen_t *), \ 2297 (fd, name, namelen)) 2298 2299 FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, \ 2300 (int fd, struct sockaddr *name, socklen_t *namelen), \ 2301 (int, struct sockaddr *, socklen_t *), \ 2302 (fd, name, namelen)) 2303 2304 FDCALL(int, listen, DUALCALL_LISTEN, \ 2305 (int fd, int backlog), \ 2306 (int, int), \ 2307 (fd, backlog)) 2308 2309 FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, \ 2310 (int fd, void *buf, size_t len, int flags, \ 2311 struct sockaddr *from, socklen_t *fromlen), \ 2312 (int, void *, size_t, int, struct sockaddr *, socklen_t *), \ 2313 (fd, buf, len, flags, from, fromlen)) 2314 2315 FDCALL(ssize_t, sendto, DUALCALL_SENDTO, \ 2316 (int fd, const void *buf, size_t len, int flags, \ 2317 const struct sockaddr *to, socklen_t tolen), \ 2318 (int, const void *, size_t, int, \ 2319 const struct sockaddr *, socklen_t), \ 2320 (fd, buf, len, flags, to, tolen)) 2321 2322 FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, \ 2323 (int fd, int level, int optn, void *optval, socklen_t *optlen), \ 2324 (int, int, int, void *, socklen_t *), \ 2325 (fd, level, optn, optval, optlen)) 2326 2327 FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, \ 2328 (int fd, int level, int optn, \ 2329 const void *optval, socklen_t optlen), \ 2330 (int, int, int, const void *, socklen_t), \ 2331 (fd, level, optn, optval, optlen)) 2332 2333 FDCALL(int, shutdown, DUALCALL_SHUTDOWN, \ 2334 (int fd, int how), \ 2335 (int, int), \ 2336 (fd, how)) 2337 2338 FDCALL(ssize_t, REALREAD, DUALCALL_READ, \ 2339 (int fd, void *buf, size_t buflen), \ 2340 (int, void *, size_t), \ 2341 (fd, buf, buflen)) 2342 2343 #ifdef __linux__ 2344 ssize_t __read_chk(int, void *, size_t) 2345 __attribute__((alias("read"))); 2346 #endif 2347 2348 FDCALL(ssize_t, readv, DUALCALL_READV, \ 2349 (int fd, const struct iovec *iov, int iovcnt), \ 2350 (int, const struct iovec *, int), \ 2351 (fd, iov, iovcnt)) 2352 2353 FDCALL(ssize_t, REALPREAD, DUALCALL_PREAD, \ 2354 (int fd, void *buf, size_t nbytes, off_t offset), \ 2355 (int, void *, size_t, off_t), \ 2356 (fd, buf, nbytes, offset)) 2357 2358 FDCALL(ssize_t, preadv, DUALCALL_PREADV, \ 2359 (int fd, const struct iovec *iov, int iovcnt, off_t offset), \ 2360 (int, const struct iovec *, int, off_t), \ 2361 (fd, iov, iovcnt, offset)) 2362 2363 FDCALL(ssize_t, writev, DUALCALL_WRITEV, \ 2364 (int fd, const struct iovec *iov, int iovcnt), \ 2365 (int, const struct iovec *, int), \ 2366 (fd, iov, iovcnt)) 2367 2368 FDCALL(ssize_t, REALPWRITE, DUALCALL_PWRITE, \ 2369 (int fd, const void *buf, size_t nbytes, off_t offset), \ 2370 (int, const void *, size_t, off_t), \ 2371 (fd, buf, nbytes, offset)) 2372 2373 FDCALL(ssize_t, pwritev, DUALCALL_PWRITEV, \ 2374 (int fd, const struct iovec *iov, int iovcnt, off_t offset), \ 2375 (int, const struct iovec *, int, off_t), \ 2376 (fd, iov, iovcnt, offset)) 2377 2378 #ifndef __linux__ 2379 FDCALL(int, REALFSTAT, DUALCALL_FSTAT, \ 2380 (int fd, struct stat *sb), \ 2381 (int, struct stat *), \ 2382 (fd, sb)) 2383 #endif 2384 2385 #ifdef __NetBSD__ 2386 FDCALL(int, fstatvfs1, DUALCALL_FSTATVFS1, \ 2387 (int fd, struct statvfs *buf, int flags), \ 2388 (int, struct statvfs *, int), \ 2389 (fd, buf, flags)) 2390 #endif 2391 2392 FDCALL(off_t, lseek, DUALCALL_LSEEK, \ 2393 (int fd, off_t offset, int whence), \ 2394 (int, off_t, int), \ 2395 (fd, offset, whence)) 2396 #ifdef LSEEK_ALIAS 2397 __strong_alias(LSEEK_ALIAS,lseek); 2398 #endif 2399 2400 #ifndef __linux__ 2401 FDCALL(int, REALGETDENTS, DUALCALL_GETDENTS, \ 2402 (int fd, char *buf, size_t nbytes), \ 2403 (int, char *, size_t), \ 2404 (fd, buf, nbytes)) 2405 #endif 2406 2407 FDCALL(int, fchown, DUALCALL_FCHOWN, \ 2408 (int fd, uid_t owner, gid_t group), \ 2409 (int, uid_t, gid_t), \ 2410 (fd, owner, group)) 2411 2412 FDCALL(int, fchmod, DUALCALL_FCHMOD, \ 2413 (int fd, mode_t mode), \ 2414 (int, mode_t), \ 2415 (fd, mode)) 2416 2417 FDCALL(int, ftruncate, DUALCALL_FTRUNCATE, \ 2418 (int fd, off_t length), \ 2419 (int, off_t), \ 2420 (fd, length)) 2421 2422 FDCALL(int, fsync, DUALCALL_FSYNC, \ 2423 (int fd), \ 2424 (int), \ 2425 (fd)) 2426 2427 #ifdef HAVE_FSYNC_RANGE 2428 FDCALL(int, fsync_range, DUALCALL_FSYNC_RANGE, \ 2429 (int fd, int how, off_t start, off_t length), \ 2430 (int, int, off_t, off_t), \ 2431 (fd, how, start, length)) 2432 #endif 2433 2434 FDCALL(int, futimes, DUALCALL_FUTIMES, \ 2435 (int fd, const struct timeval *tv), \ 2436 (int, const struct timeval *), \ 2437 (fd, tv)) 2438 2439 FDCALL(int, futimens, DUALCALL_FUTIMENS, \ 2440 (int fd, const struct timespec *ts), \ 2441 (int, const struct timespec *), \ 2442 (fd, ts)) 2443 2444 #ifdef HAVE_CHFLAGS 2445 FDCALL(int, fchflags, DUALCALL_FCHFLAGS, \ 2446 (int fd, u_long flags), \ 2447 (int, u_long), \ 2448 (fd, flags)) 2449 #endif 2450 2451 /* 2452 * path-based selectors 2453 */ 2454 2455 #ifndef __linux__ 2456 PATHCALL(int, REALSTAT, DUALCALL_STAT, \ 2457 (const char *path, struct stat *sb), \ 2458 (const char *, struct stat *), \ 2459 (path, sb)) 2460 2461 PATHCALL(int, REALLSTAT, DUALCALL_LSTAT, \ 2462 (const char *path, struct stat *sb), \ 2463 (const char *, struct stat *), \ 2464 (path, sb)) 2465 #endif 2466 2467 PATHCALL(int, chown, DUALCALL_CHOWN, \ 2468 (const char *path, uid_t owner, gid_t group), \ 2469 (const char *, uid_t, gid_t), \ 2470 (path, owner, group)) 2471 2472 PATHCALL(int, lchown, DUALCALL_LCHOWN, \ 2473 (const char *path, uid_t owner, gid_t group), \ 2474 (const char *, uid_t, gid_t), \ 2475 (path, owner, group)) 2476 2477 PATHCALL(int, chmod, DUALCALL_CHMOD, \ 2478 (const char *path, mode_t mode), \ 2479 (const char *, mode_t), \ 2480 (path, mode)) 2481 2482 PATHCALL(int, lchmod, DUALCALL_LCHMOD, \ 2483 (const char *path, mode_t mode), \ 2484 (const char *, mode_t), \ 2485 (path, mode)) 2486 2487 #ifdef __NetBSD__ 2488 PATHCALL(int, statvfs1, DUALCALL_STATVFS1, \ 2489 (const char *path, struct statvfs *buf, int flags), \ 2490 (const char *, struct statvfs *, int), \ 2491 (path, buf, flags)) 2492 #endif 2493 2494 PATHCALL(int, unlink, DUALCALL_UNLINK, \ 2495 (const char *path), \ 2496 (const char *), \ 2497 (path)) 2498 2499 PATHCALL(int, symlink, DUALCALL_SYMLINK, \ 2500 (const char *target, const char *path), \ 2501 (const char *, const char *), \ 2502 (target, path)) 2503 2504 /* 2505 * readlink() can be called from malloc which can be called 2506 * from dlsym() during init 2507 */ 2508 ssize_t 2509 readlink(const char *path, char *buf, size_t bufsiz) 2510 { 2511 int (*op_readlink)(const char *, char *, size_t); 2512 enum pathtype pt; 2513 2514 if ((pt = path_isrump(path)) != PATH_HOST) { 2515 op_readlink = GETSYSCALL(rump, READLINK); 2516 if (pt == PATH_RUMP) 2517 path = path_host2rump(path); 2518 } else { 2519 op_readlink = GETSYSCALL(host, READLINK); 2520 } 2521 2522 if (__predict_false(op_readlink == NULL)) { 2523 errno = ENOENT; 2524 return -1; 2525 } 2526 2527 return op_readlink(path, buf, bufsiz); 2528 } 2529 2530 PATHCALL(int, mkdir, DUALCALL_MKDIR, \ 2531 (const char *path, mode_t mode), \ 2532 (const char *, mode_t), \ 2533 (path, mode)) 2534 2535 PATHCALL(int, rmdir, DUALCALL_RMDIR, \ 2536 (const char *path), \ 2537 (const char *), \ 2538 (path)) 2539 2540 PATHCALL(int, utimes, DUALCALL_UTIMES, \ 2541 (const char *path, const struct timeval *tv), \ 2542 (const char *, const struct timeval *), \ 2543 (path, tv)) 2544 2545 PATHCALL(int, lutimes, DUALCALL_LUTIMES, \ 2546 (const char *path, const struct timeval *tv), \ 2547 (const char *, const struct timeval *), \ 2548 (path, tv)) 2549 2550 #ifdef HAVE_CHFLAGS 2551 PATHCALL(int, chflags, DUALCALL_CHFLAGS, \ 2552 (const char *path, u_long flags), \ 2553 (const char *, u_long), \ 2554 (path, flags)) 2555 2556 PATHCALL(int, lchflags, DUALCALL_LCHFLAGS, \ 2557 (const char *path, u_long flags), \ 2558 (const char *, u_long), \ 2559 (path, flags)) 2560 #endif /* HAVE_CHFLAGS */ 2561 2562 PATHCALL(int, truncate, DUALCALL_TRUNCATE, \ 2563 (const char *path, off_t length), \ 2564 (const char *, off_t), \ 2565 (path, length)) 2566 2567 PATHCALL(int, access, DUALCALL_ACCESS, \ 2568 (const char *path, int mode), \ 2569 (const char *, int), \ 2570 (path, mode)) 2571 2572 #ifndef __linux__ 2573 PATHCALL(int, REALMKNOD, DUALCALL_MKNOD, \ 2574 (const char *path, mode_t mode, dev_t dev), \ 2575 (const char *, mode_t, dev_t), \ 2576 (path, mode, dev)) 2577 #endif 2578 2579 /* 2580 * Note: with mount the decisive parameter is the mount 2581 * destination directory. This is because we don't really know 2582 * about the "source" directory in a generic call (and besides, 2583 * it might not even exist, cf. nfs). 2584 */ 2585 #ifdef __NetBSD__ 2586 PATHCALL(int, REALMOUNT, DUALCALL_MOUNT, \ 2587 (const char *type, const char *path, int flags, \ 2588 void *data, size_t dlen), \ 2589 (const char *, const char *, int, void *, size_t), \ 2590 (type, path, flags, data, dlen)) 2591 2592 PATHCALL(int, unmount, DUALCALL_UNMOUNT, \ 2593 (const char *path, int flags), \ 2594 (const char *, int), \ 2595 (path, flags)) 2596 #endif /* __NetBSD__ */ 2597 2598 #ifdef HAVE___QUOTACTL 2599 PATHCALL(int, __quotactl, DUALCALL_QUOTACTL, \ 2600 (const char *path, struct quotactl_args *args), \ 2601 (const char *, struct quotactl_args *), \ 2602 (path, args)) 2603 #endif /* HAVE___QUOTACTL */ 2604 2605 #ifdef __NetBSD__ 2606 PATHCALL(int, REALGETFH, DUALCALL_GETFH, \ 2607 (const char *path, void *fhp, size_t *fh_size), \ 2608 (const char *, void *, size_t *), \ 2609 (path, fhp, fh_size)) 2610 #endif 2611 2612 /* 2613 * These act different on a per-process vfs configuration 2614 */ 2615 2616 #ifdef __NetBSD__ 2617 VFSCALL(VFSBIT_GETVFSSTAT, int, getvfsstat, DUALCALL_GETVFSSTAT, \ 2618 (struct statvfs *buf, size_t buflen, int flags), \ 2619 (struct statvfs *, size_t, int), \ 2620 (buf, buflen, flags)) 2621 #endif 2622 2623 #ifdef __NetBSD__ 2624 VFSCALL(VFSBIT_FHCALLS, int, REALFHOPEN, DUALCALL_FHOPEN, \ 2625 (const void *fhp, size_t fh_size, int flags), \ 2626 (const char *, size_t, int), \ 2627 (fhp, fh_size, flags)) 2628 2629 VFSCALL(VFSBIT_FHCALLS, int, REALFHSTAT, DUALCALL_FHSTAT, \ 2630 (const void *fhp, size_t fh_size, struct stat *sb), \ 2631 (const char *, size_t, struct stat *), \ 2632 (fhp, fh_size, sb)) 2633 2634 VFSCALL(VFSBIT_FHCALLS, int, REALFHSTATVFS1, DUALCALL_FHSTATVFS1, \ 2635 (const void *fhp, size_t fh_size, struct statvfs *sb, int flgs),\ 2636 (const char *, size_t, struct statvfs *, int), \ 2637 (fhp, fh_size, sb, flgs)) 2638 #endif 2639 2640 2641 #ifdef __NetBSD__ 2642 2643 /* finally, put nfssvc here. "keep the namespace clean" */ 2644 #include <nfs/rpcv2.h> 2645 #include <nfs/nfs.h> 2646 2647 int 2648 nfssvc(int flags, void *argstructp) 2649 { 2650 int (*op_nfssvc)(int, void *); 2651 2652 if (vfsbits & VFSBIT_NFSSVC){ 2653 struct nfsd_args *nfsdargs; 2654 2655 /* massage the socket descriptor if necessary */ 2656 if (flags == NFSSVC_ADDSOCK) { 2657 nfsdargs = argstructp; 2658 nfsdargs->sock = fd_host2rump(nfsdargs->sock); 2659 } 2660 op_nfssvc = GETSYSCALL(rump, NFSSVC); 2661 } else 2662 op_nfssvc = GETSYSCALL(host, NFSSVC); 2663 2664 return op_nfssvc(flags, argstructp); 2665 } 2666 #endif /* __NetBSD__ */ 2667