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