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