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