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