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