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