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