Home | History | Annotate | Line # | Download | only in librumphijack
hijack.c revision 1.41
      1  1.41     pooka /*      $NetBSD: hijack.c,v 1.41 2011/02/15 14:01:52 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.41     pooka __RCSID("$NetBSD: hijack.c,v 1.41 2011/02/15 14:01:52 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.1     pooka #include <sys/socket.h>
     38   1.1     pooka #include <sys/poll.h>
     39   1.1     pooka 
     40   1.1     pooka #include <rump/rumpclient.h>
     41   1.1     pooka #include <rump/rump_syscalls.h>
     42   1.1     pooka 
     43   1.1     pooka #include <assert.h>
     44   1.1     pooka #include <dlfcn.h>
     45   1.1     pooka #include <err.h>
     46   1.1     pooka #include <errno.h>
     47   1.1     pooka #include <fcntl.h>
     48   1.1     pooka #include <poll.h>
     49   1.1     pooka #include <pthread.h>
     50   1.3     pooka #include <signal.h>
     51   1.1     pooka #include <stdarg.h>
     52   1.8     pooka #include <stdbool.h>
     53   1.1     pooka #include <stdio.h>
     54   1.1     pooka #include <stdlib.h>
     55  1.28     pooka #include <string.h>
     56   1.3     pooka #include <time.h>
     57   1.1     pooka #include <unistd.h>
     58   1.1     pooka 
     59  1.17     pooka enum dualcall {
     60  1.17     pooka 	DUALCALL_WRITE, DUALCALL_WRITEV,
     61  1.17     pooka 	DUALCALL_IOCTL, DUALCALL_FCNTL,
     62  1.17     pooka 	DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_BIND, DUALCALL_CONNECT,
     63  1.17     pooka 	DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN,
     64  1.17     pooka 	DUALCALL_RECVFROM, DUALCALL_RECVMSG,
     65  1.17     pooka 	DUALCALL_SENDTO, DUALCALL_SENDMSG,
     66  1.17     pooka 	DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT,
     67  1.17     pooka 	DUALCALL_SHUTDOWN,
     68  1.17     pooka 	DUALCALL_READ, DUALCALL_READV,
     69  1.41     pooka 	DUALCALL_DUP2,
     70  1.34     pooka 	DUALCALL_CLOSE,
     71  1.17     pooka 	DUALCALL_POLLTS,
     72  1.34     pooka 	DUALCALL_KEVENT,
     73  1.17     pooka 	DUALCALL__NUM
     74   1.1     pooka };
     75   1.1     pooka 
     76   1.8     pooka #define RSYS_STRING(a) __STRING(a)
     77   1.8     pooka #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a))
     78   1.8     pooka 
     79   1.1     pooka /*
     80  1.14     pooka  * Would be nice to get this automatically in sync with libc.
     81  1.14     pooka  * Also, this does not work for compat-using binaries!
     82  1.14     pooka  */
     83  1.14     pooka #if !__NetBSD_Prereq__(5,99,7)
     84  1.29     pooka #define REALSELECT select
     85  1.29     pooka #define REALPOLLTS pollts
     86  1.34     pooka #define REALKEVENT kevent
     87  1.14     pooka #else
     88  1.29     pooka #define REALSELECT _sys___select50
     89  1.29     pooka #define REALPOLLTS _sys___pollts50
     90  1.34     pooka #define REALKEVENT _sys___kevent50
     91  1.17     pooka #endif
     92  1.31     pooka #define REALREAD _sys_read
     93  1.14     pooka 
     94  1.29     pooka int REALSELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *);
     95  1.29     pooka int REALPOLLTS(struct pollfd *, nfds_t,
     96  1.20     pooka 	       const struct timespec *, const sigset_t *);
     97  1.34     pooka int REALKEVENT(int, const struct kevent *, size_t, struct kevent *, size_t,
     98  1.34     pooka 	       const struct timespec *);
     99  1.31     pooka ssize_t REALREAD(int, void *, size_t);
    100  1.17     pooka 
    101  1.17     pooka #define S(a) __STRING(a)
    102  1.17     pooka struct sysnames {
    103  1.17     pooka 	enum dualcall scm_callnum;
    104  1.17     pooka 	const char *scm_hostname;
    105  1.17     pooka 	const char *scm_rumpname;
    106  1.17     pooka } syscnames[] = {
    107  1.17     pooka 	{ DUALCALL_SOCKET,	"__socket30",	RSYS_NAME(SOCKET)	},
    108  1.17     pooka 	{ DUALCALL_ACCEPT,	"accept",	RSYS_NAME(ACCEPT)	},
    109  1.17     pooka 	{ DUALCALL_BIND,	"bind",		RSYS_NAME(BIND)		},
    110  1.17     pooka 	{ DUALCALL_CONNECT,	"connect",	RSYS_NAME(CONNECT)	},
    111  1.17     pooka 	{ DUALCALL_GETPEERNAME,	"getpeername",	RSYS_NAME(GETPEERNAME)	},
    112  1.17     pooka 	{ DUALCALL_GETSOCKNAME,	"getsockname",	RSYS_NAME(GETSOCKNAME)	},
    113  1.17     pooka 	{ DUALCALL_LISTEN,	"listen",	RSYS_NAME(LISTEN)	},
    114  1.17     pooka 	{ DUALCALL_RECVFROM,	"recvfrom",	RSYS_NAME(RECVFROM)	},
    115  1.17     pooka 	{ DUALCALL_RECVMSG,	"recvmsg",	RSYS_NAME(RECVMSG)	},
    116  1.17     pooka 	{ DUALCALL_SENDTO,	"sendto",	RSYS_NAME(SENDTO)	},
    117  1.17     pooka 	{ DUALCALL_SENDMSG,	"sendmsg",	RSYS_NAME(SENDMSG)	},
    118  1.17     pooka 	{ DUALCALL_GETSOCKOPT,	"getsockopt",	RSYS_NAME(GETSOCKOPT)	},
    119  1.17     pooka 	{ DUALCALL_SETSOCKOPT,	"setsockopt",	RSYS_NAME(SETSOCKOPT)	},
    120  1.17     pooka 	{ DUALCALL_SHUTDOWN,	"shutdown",	RSYS_NAME(SHUTDOWN)	},
    121  1.31     pooka 	{ DUALCALL_READ,	S(REALREAD),	RSYS_NAME(READ)		},
    122  1.17     pooka 	{ DUALCALL_READV,	"readv",	RSYS_NAME(READV)	},
    123  1.17     pooka 	{ DUALCALL_WRITE,	"write",	RSYS_NAME(WRITE)	},
    124  1.17     pooka 	{ DUALCALL_WRITEV,	"writev",	RSYS_NAME(WRITEV)	},
    125  1.17     pooka 	{ DUALCALL_IOCTL,	"ioctl",	RSYS_NAME(IOCTL)	},
    126  1.17     pooka 	{ DUALCALL_FCNTL,	"fcntl",	RSYS_NAME(FCNTL)	},
    127  1.17     pooka 	{ DUALCALL_DUP2,	"dup2",		RSYS_NAME(DUP2)		},
    128  1.17     pooka 	{ DUALCALL_CLOSE,	"close",	RSYS_NAME(CLOSE)	},
    129  1.29     pooka 	{ DUALCALL_POLLTS,	S(REALPOLLTS),	RSYS_NAME(POLLTS)	},
    130  1.34     pooka 	{ DUALCALL_KEVENT,	S(REALKEVENT),	RSYS_NAME(KEVENT)	},
    131  1.17     pooka };
    132  1.17     pooka #undef S
    133  1.17     pooka 
    134  1.17     pooka struct bothsys {
    135  1.17     pooka 	void *bs_host;
    136  1.17     pooka 	void *bs_rump;
    137  1.17     pooka } syscalls[DUALCALL__NUM];
    138  1.17     pooka #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which
    139  1.17     pooka 
    140  1.25     pooka pid_t	(*host_fork)(void);
    141  1.25     pooka int	(*host_daemon)(int, int);
    142  1.39     pooka int	(*host_execve)(const char *, char *const[], char *const[]);
    143  1.17     pooka 
    144  1.40     pooka static uint32_t dup2mask;
    145  1.40     pooka #define ISDUP2D(fd) (((fd) < 32) && (1<<(fd) & dup2mask))
    146  1.40     pooka #define SETDUP2(fd) \
    147  1.40     pooka     do { if ((fd) < 32) dup2mask |= (1<<(fd)); } while (/*CONSTCOND*/0)
    148  1.40     pooka #define CLRDUP2(fd) \
    149  1.40     pooka     do { if ((fd) < 32) dup2mask &= ~(1<<(fd)); } while (/*CONSTCOND*/0)
    150  1.17     pooka 
    151  1.17     pooka //#define DEBUGJACK
    152  1.17     pooka #ifdef DEBUGJACK
    153  1.17     pooka #define DPRINTF(x) mydprintf x
    154  1.17     pooka static void
    155  1.17     pooka mydprintf(const char *fmt, ...)
    156  1.17     pooka {
    157  1.17     pooka 	va_list ap;
    158  1.17     pooka 
    159  1.17     pooka 	if (ISDUP2D(STDERR_FILENO))
    160  1.17     pooka 		return;
    161  1.17     pooka 
    162  1.17     pooka 	va_start(ap, fmt);
    163  1.17     pooka 	vfprintf(stderr, fmt, ap);
    164  1.17     pooka 	va_end(ap);
    165  1.17     pooka }
    166  1.17     pooka 
    167  1.17     pooka #else
    168  1.17     pooka #define DPRINTF(x)
    169  1.14     pooka #endif
    170  1.14     pooka 
    171  1.17     pooka #define FDCALL(type, name, rcname, args, proto, vars)			\
    172  1.17     pooka type name args								\
    173  1.17     pooka {									\
    174  1.17     pooka 	type (*fun) proto;						\
    175  1.17     pooka 									\
    176  1.33     pooka 	DPRINTF(("%s -> %d\n", __STRING(name), fd));			\
    177  1.17     pooka 	if (fd_isrump(fd)) {						\
    178  1.17     pooka 		fun = syscalls[rcname].bs_rump;				\
    179  1.17     pooka 		fd = fd_host2rump(fd);					\
    180  1.17     pooka 	} else {							\
    181  1.17     pooka 		fun = syscalls[rcname].bs_host;				\
    182  1.17     pooka 	}								\
    183  1.17     pooka 									\
    184  1.17     pooka 	return fun vars;						\
    185  1.17     pooka }
    186  1.17     pooka 
    187  1.14     pooka /*
    188   1.1     pooka  * This is called from librumpclient in case of LD_PRELOAD.
    189   1.1     pooka  * It ensures correct RTLD_NEXT.
    190  1.32     pooka  *
    191  1.32     pooka  * ... except, it's apparently extremely difficult to force
    192  1.32     pooka  * at least gcc to generate an actual stack frame here.  So
    193  1.32     pooka  * sprinkle some volatile foobar and baz to throw the optimizer
    194  1.32     pooka  * off the scent and generate a variable assignment with the
    195  1.32     pooka  * return value.  The posterboy for this meltdown is amd64
    196  1.32     pooka  * with -O2.  At least with gcc 4.1.3 i386 works regardless of
    197  1.32     pooka  * optimization.
    198   1.1     pooka  */
    199  1.32     pooka volatile int rumphijack_unrope; /* there, unhang yourself */
    200   1.1     pooka static void *
    201   1.1     pooka hijackdlsym(void *handle, const char *symbol)
    202   1.1     pooka {
    203  1.32     pooka 	void *rv;
    204  1.32     pooka 
    205  1.32     pooka 	rv = dlsym(handle, symbol);
    206  1.32     pooka 	rumphijack_unrope = *(volatile int *)rv;
    207   1.1     pooka 
    208  1.32     pooka 	return (void *)rv;
    209   1.1     pooka }
    210   1.1     pooka 
    211   1.7     pooka /* low calorie sockets? */
    212  1.14     pooka static bool hostlocalsockets = true;
    213   1.7     pooka 
    214   1.1     pooka static void __attribute__((constructor))
    215   1.1     pooka rcinit(void)
    216   1.1     pooka {
    217  1.28     pooka 	char buf[64];
    218  1.23     pooka 	extern void *(*rumpclient_dlsym)(void *, const char *);
    219  1.19     pooka 	unsigned i, j;
    220   1.1     pooka 
    221  1.23     pooka 	rumpclient_dlsym = hijackdlsym;
    222  1.17     pooka 	host_fork = dlsym(RTLD_NEXT, "fork");
    223  1.25     pooka 	host_daemon = dlsym(RTLD_NEXT, "daemon");
    224  1.39     pooka 	host_execve = dlsym(RTLD_NEXT, "execve");
    225  1.17     pooka 
    226  1.17     pooka 	/*
    227  1.17     pooka 	 * In theory cannot print anything during lookups because
    228  1.17     pooka 	 * we might not have the call vector set up.  so, the errx()
    229  1.17     pooka 	 * is a bit of a strech, but it might work.
    230  1.17     pooka 	 */
    231   1.1     pooka 
    232  1.17     pooka 	for (i = 0; i < DUALCALL__NUM; i++) {
    233  1.17     pooka 		/* build runtime O(1) access */
    234  1.17     pooka 		for (j = 0; j < __arraycount(syscnames); j++) {
    235  1.17     pooka 			if (syscnames[j].scm_callnum == i)
    236  1.17     pooka 				break;
    237  1.17     pooka 		}
    238  1.17     pooka 
    239  1.17     pooka 		if (j == __arraycount(syscnames))
    240  1.17     pooka 			errx(1, "rumphijack error: syscall pos %d missing", i);
    241  1.17     pooka 
    242  1.23     pooka 		syscalls[i].bs_host = dlsym(RTLD_NEXT,
    243  1.23     pooka 		    syscnames[j].scm_hostname);
    244  1.17     pooka 		if (syscalls[i].bs_host == NULL)
    245  1.17     pooka 			errx(1, "hostcall %s not found missing",
    246  1.17     pooka 			    syscnames[j].scm_hostname);
    247  1.17     pooka 
    248  1.23     pooka 		syscalls[i].bs_rump = dlsym(RTLD_NEXT,
    249  1.23     pooka 		    syscnames[j].scm_rumpname);
    250  1.17     pooka 		if (syscalls[i].bs_rump == NULL)
    251  1.17     pooka 			errx(1, "rumpcall %s not found missing",
    252  1.17     pooka 			    syscnames[j].scm_rumpname);
    253   1.1     pooka 	}
    254   1.1     pooka 
    255  1.22     pooka 	if (rumpclient_init() == -1)
    256   1.1     pooka 		err(1, "rumpclient init");
    257  1.28     pooka 
    258  1.28     pooka 	/* set client persistence level */
    259  1.28     pooka 	if (getenv_r("RUMPHIJACK_RETRY", buf, sizeof(buf)) == -1) {
    260  1.28     pooka 		if (errno == ERANGE)
    261  1.28     pooka 			err(1, "invalid RUMPHIJACK_RETRY");
    262  1.28     pooka 		rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME);
    263  1.28     pooka 	} else {
    264  1.28     pooka 		if (strcmp(buf, "die") == 0)
    265  1.28     pooka 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_DIE);
    266  1.28     pooka 		else if (strcmp(buf, "inftime") == 0)
    267  1.28     pooka 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME);
    268  1.28     pooka 		else if (strcmp(buf, "once") == 0)
    269  1.28     pooka 			rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_ONCE);
    270  1.28     pooka 		else {
    271  1.28     pooka 			time_t timeout;
    272  1.28     pooka 
    273  1.28     pooka 			timeout = (time_t)strtoll(buf, NULL, 10);
    274  1.28     pooka 			if (timeout <= 0)
    275  1.28     pooka 				errx(1, "RUMPHIJACK_RETRY must be keyword "
    276  1.28     pooka 				    "or a positive integer, got: %s", buf);
    277  1.28     pooka 
    278  1.28     pooka 			rumpclient_setconnretry(timeout);
    279  1.28     pooka 		}
    280  1.28     pooka 	}
    281  1.39     pooka 
    282  1.39     pooka 	if (getenv_r("RUMPHIJACK__DUP2MASK", buf, sizeof(buf)) == 0) {
    283  1.40     pooka 		dup2mask = strtoul(buf, NULL, 10);
    284  1.39     pooka 	}
    285   1.1     pooka }
    286   1.1     pooka 
    287   1.2     pooka /* XXX: need runtime selection.  low for now due to FD_SETSIZE */
    288   1.2     pooka #define HIJACK_FDOFF 128
    289   1.2     pooka static int
    290   1.2     pooka fd_rump2host(int fd)
    291   1.2     pooka {
    292   1.2     pooka 
    293   1.2     pooka 	if (fd == -1)
    294   1.2     pooka 		return fd;
    295   1.2     pooka 
    296   1.2     pooka 	if (!ISDUP2D(fd))
    297   1.2     pooka 		fd += HIJACK_FDOFF;
    298   1.2     pooka 
    299   1.2     pooka 	return fd;
    300   1.2     pooka }
    301   1.2     pooka 
    302   1.2     pooka static int
    303   1.2     pooka fd_host2rump(int fd)
    304   1.2     pooka {
    305   1.2     pooka 
    306   1.2     pooka 	if (!ISDUP2D(fd))
    307   1.2     pooka 		fd -= HIJACK_FDOFF;
    308   1.2     pooka 	return fd;
    309   1.2     pooka }
    310   1.2     pooka 
    311   1.2     pooka static bool
    312   1.2     pooka fd_isrump(int fd)
    313   1.2     pooka {
    314   1.2     pooka 
    315   1.2     pooka 	return ISDUP2D(fd) || fd >= HIJACK_FDOFF;
    316   1.2     pooka }
    317   1.2     pooka 
    318  1.40     pooka #define assertfd(_fd_) assert(ISDUP2D(_fd_) || (_fd_) >= HIJACK_FDOFF)
    319  1.40     pooka 
    320  1.40     pooka static int
    321  1.40     pooka dodup(int oldd, int minfd)
    322  1.40     pooka {
    323  1.40     pooka 	int (*op_fcntl)(int, int, ...);
    324  1.40     pooka 	int newd;
    325  1.40     pooka 	int isrump;
    326  1.40     pooka 
    327  1.40     pooka 	DPRINTF(("dup -> %d (minfd %d)\n", oldd, minfd));
    328  1.40     pooka 	if (fd_isrump(oldd)) {
    329  1.40     pooka 		op_fcntl = GETSYSCALL(rump, FCNTL);
    330  1.40     pooka 		oldd = fd_host2rump(oldd);
    331  1.40     pooka 		isrump = 1;
    332  1.40     pooka 	} else {
    333  1.40     pooka 		op_fcntl = GETSYSCALL(host, FCNTL);
    334  1.40     pooka 		isrump = 0;
    335  1.40     pooka 	}
    336  1.40     pooka 
    337  1.40     pooka 	newd = op_fcntl(oldd, F_DUPFD, minfd);
    338  1.40     pooka 
    339  1.40     pooka 	if (isrump)
    340  1.40     pooka 		newd = fd_rump2host(newd);
    341  1.40     pooka 	DPRINTF(("dup <- %d\n", newd));
    342  1.40     pooka 
    343  1.40     pooka 	return newd;
    344  1.40     pooka }
    345   1.2     pooka 
    346   1.1     pooka int __socket30(int, int, int);
    347   1.1     pooka int
    348   1.1     pooka __socket30(int domain, int type, int protocol)
    349   1.1     pooka {
    350  1.17     pooka 	int (*op_socket)(int, int, int);
    351   1.1     pooka 	int fd;
    352   1.7     pooka 	bool dohost;
    353   1.7     pooka 
    354   1.7     pooka 	dohost = hostlocalsockets && (domain == AF_LOCAL);
    355   1.1     pooka 
    356   1.7     pooka 	if (dohost)
    357  1.17     pooka 		op_socket = GETSYSCALL(host, SOCKET);
    358   1.7     pooka 	else
    359  1.17     pooka 		op_socket = GETSYSCALL(rump, SOCKET);
    360  1.17     pooka 	fd = op_socket(domain, type, protocol);
    361   1.2     pooka 
    362   1.7     pooka 	if (!dohost)
    363   1.7     pooka 		fd = fd_rump2host(fd);
    364   1.7     pooka 	DPRINTF(("socket <- %d\n", fd));
    365   1.2     pooka 
    366   1.7     pooka 	return fd;
    367   1.1     pooka }
    368   1.1     pooka 
    369   1.1     pooka int
    370   1.1     pooka accept(int s, struct sockaddr *addr, socklen_t *addrlen)
    371   1.1     pooka {
    372  1.17     pooka 	int (*op_accept)(int, struct sockaddr *, socklen_t *);
    373   1.1     pooka 	int fd;
    374   1.7     pooka 	bool isrump;
    375   1.7     pooka 
    376   1.7     pooka 	isrump = fd_isrump(s);
    377   1.1     pooka 
    378   1.2     pooka 	DPRINTF(("accept -> %d", s));
    379   1.7     pooka 	if (isrump) {
    380  1.17     pooka 		op_accept = GETSYSCALL(rump, ACCEPT);
    381   1.7     pooka 		s = fd_host2rump(s);
    382   1.7     pooka 	} else {
    383  1.17     pooka 		op_accept = GETSYSCALL(host, ACCEPT);
    384   1.7     pooka 	}
    385  1.17     pooka 	fd = op_accept(s, addr, addrlen);
    386   1.7     pooka 	if (fd != -1 && isrump)
    387   1.7     pooka 		fd = fd_rump2host(fd);
    388   1.7     pooka 
    389   1.7     pooka 	DPRINTF((" <- %d\n", fd));
    390   1.2     pooka 
    391   1.7     pooka 	return fd;
    392   1.1     pooka }
    393   1.1     pooka 
    394  1.17     pooka /*
    395  1.17     pooka  * ioctl and fcntl are varargs calls and need special treatment
    396  1.17     pooka  */
    397   1.1     pooka int
    398  1.17     pooka ioctl(int fd, unsigned long cmd, ...)
    399   1.1     pooka {
    400  1.17     pooka 	int (*op_ioctl)(int, unsigned long cmd, ...);
    401  1.17     pooka 	va_list ap;
    402  1.17     pooka 	int rv;
    403   1.1     pooka 
    404  1.17     pooka 	DPRINTF(("ioctl -> %d\n", fd));
    405  1.17     pooka 	if (fd_isrump(fd)) {
    406  1.17     pooka 		fd = fd_host2rump(fd);
    407  1.17     pooka 		op_ioctl = GETSYSCALL(rump, IOCTL);
    408   1.7     pooka 	} else {
    409  1.17     pooka 		op_ioctl = GETSYSCALL(host, IOCTL);
    410   1.7     pooka 	}
    411   1.1     pooka 
    412  1.17     pooka 	va_start(ap, cmd);
    413  1.17     pooka 	rv = op_ioctl(fd, cmd, va_arg(ap, void *));
    414  1.17     pooka 	va_end(ap);
    415  1.17     pooka 	return rv;
    416   1.1     pooka }
    417   1.1     pooka 
    418  1.40     pooka #include <syslog.h>
    419   1.1     pooka int
    420  1.17     pooka fcntl(int fd, int cmd, ...)
    421   1.1     pooka {
    422  1.17     pooka 	int (*op_fcntl)(int, int, ...);
    423  1.17     pooka 	va_list ap;
    424  1.40     pooka 	int rv, minfd, i;
    425  1.40     pooka 
    426  1.40     pooka 	DPRINTF(("fcntl -> %d (cmd %d)\n", fd, cmd));
    427  1.40     pooka 
    428  1.40     pooka 	switch (cmd) {
    429  1.40     pooka 	case F_DUPFD:
    430  1.40     pooka 		va_start(ap, cmd);
    431  1.40     pooka 		minfd = va_arg(ap, int);
    432  1.40     pooka 		va_end(ap);
    433  1.40     pooka 		return dodup(fd, minfd);
    434  1.40     pooka 
    435  1.40     pooka 	case F_CLOSEM:
    436  1.40     pooka 		/*
    437  1.40     pooka 		 * So, if fd < HIJACKOFF, we want to do a host closem.
    438  1.40     pooka 		 */
    439  1.40     pooka 
    440  1.40     pooka 		if (fd < HIJACK_FDOFF) {
    441  1.40     pooka 			int closemfd = fd;
    442   1.1     pooka 
    443  1.40     pooka 			if (rumpclient__closenotify(&closemfd,
    444  1.39     pooka 			    RUMPCLIENT_CLOSE_FCLOSEM) == -1)
    445  1.39     pooka 				return -1;
    446  1.40     pooka 			op_fcntl = GETSYSCALL(host, FCNTL);
    447  1.40     pooka 			rv = op_fcntl(closemfd, cmd);
    448  1.40     pooka 			if (rv)
    449  1.40     pooka 				return rv;
    450  1.40     pooka 		}
    451  1.40     pooka 
    452  1.40     pooka 		/*
    453  1.40     pooka 		 * Additionally, we want to do a rump closem, but only
    454  1.40     pooka 		 * for the file descriptors not within the dup2mask.
    455  1.40     pooka 		 */
    456  1.40     pooka 
    457  1.40     pooka 		/* why don't we offer fls()? */
    458  1.40     pooka 		for (i = 31; i >= 0; i--) {
    459  1.40     pooka 			if (dup2mask & 1<<i)
    460  1.40     pooka 				break;
    461  1.40     pooka 		}
    462  1.40     pooka 
    463  1.40     pooka 		if (fd >= HIJACK_FDOFF)
    464  1.40     pooka 			fd -= HIJACK_FDOFF;
    465  1.40     pooka 		else
    466  1.40     pooka 			fd = 0;
    467  1.40     pooka 		fd = MAX(i+1, fd);
    468  1.40     pooka 
    469  1.40     pooka 		/* hmm, maybe we should close rump fd's not within dup2mask? */
    470  1.40     pooka 
    471  1.40     pooka 		return rump_sys_fcntl(fd, F_CLOSEM);
    472  1.40     pooka 
    473  1.40     pooka 	case F_MAXFD:
    474  1.40     pooka 		/*
    475  1.40     pooka 		 * For maxfd, if there's a rump kernel fd, return
    476  1.40     pooka 		 * it hostified.  Otherwise, return host's MAXFD
    477  1.40     pooka 		 * return value.
    478  1.40     pooka 		 */
    479  1.40     pooka 		if ((rv = rump_sys_fcntl(fd, F_MAXFD)) != -1) {
    480  1.40     pooka 			/*
    481  1.40     pooka 			 * This might go a little wrong in case
    482  1.40     pooka 			 * of dup2 to [012], but I'm not sure if
    483  1.40     pooka 			 * there's a justification for tracking
    484  1.40     pooka 			 * that info.  Consider e.g.
    485  1.40     pooka 			 * dup2(rumpfd, 2) followed by rump_sys_open()
    486  1.40     pooka 			 * returning 1.  We should return 1+HIJACKOFF,
    487  1.40     pooka 			 * not 2+HIJACKOFF.  However, if [01] is not
    488  1.40     pooka 			 * open, the correct return value is 2.
    489  1.40     pooka 			 */
    490  1.40     pooka 			return fd_rump2host(fd);
    491  1.40     pooka 		} else {
    492  1.40     pooka 			op_fcntl = GETSYSCALL(host, FCNTL);
    493  1.40     pooka 			return op_fcntl(fd, F_MAXFD);
    494  1.40     pooka 		}
    495  1.40     pooka 		/*NOTREACHED*/
    496  1.40     pooka 
    497  1.40     pooka 	default:
    498  1.40     pooka 		if (fd_isrump(fd)) {
    499  1.40     pooka 			fd = fd_host2rump(fd);
    500  1.40     pooka 			op_fcntl = GETSYSCALL(rump, FCNTL);
    501  1.40     pooka 		} else {
    502  1.40     pooka 			op_fcntl = GETSYSCALL(host, FCNTL);
    503  1.40     pooka 		}
    504  1.40     pooka 
    505  1.40     pooka 		va_start(ap, cmd);
    506  1.40     pooka 		rv = op_fcntl(fd, cmd, va_arg(ap, void *));
    507  1.40     pooka 		va_end(ap);
    508  1.40     pooka 		return rv;
    509   1.7     pooka 	}
    510  1.40     pooka 	/*NOTREACHED*/
    511   1.1     pooka }
    512   1.1     pooka 
    513  1.39     pooka int
    514  1.39     pooka close(int fd)
    515  1.39     pooka {
    516  1.39     pooka 	int (*op_close)(int);
    517  1.39     pooka 	int rv;
    518  1.39     pooka 
    519  1.39     pooka 	DPRINTF(("close -> %d\n", fd));
    520  1.39     pooka 	if (fd_isrump(fd)) {
    521  1.39     pooka 		int undup2 = 0;
    522  1.39     pooka 
    523  1.39     pooka 		if (ISDUP2D(fd))
    524  1.39     pooka 			undup2 = 1;
    525  1.39     pooka 		fd = fd_host2rump(fd);
    526  1.39     pooka 		op_close = GETSYSCALL(rump, CLOSE);
    527  1.39     pooka 		rv = op_close(fd);
    528  1.39     pooka 		if (rv == 0 && undup2)
    529  1.40     pooka 			CLRDUP2(fd);
    530  1.39     pooka 	} else {
    531  1.39     pooka 		if (rumpclient__closenotify(&fd, RUMPCLIENT_CLOSE_CLOSE) == -1)
    532  1.39     pooka 			return -1;
    533  1.39     pooka 		op_close = GETSYSCALL(host, CLOSE);
    534  1.39     pooka 		rv = op_close(fd);
    535  1.39     pooka 	}
    536  1.39     pooka 
    537  1.39     pooka 	return rv;
    538  1.39     pooka }
    539  1.39     pooka 
    540  1.17     pooka /*
    541  1.17     pooka  * write cannot issue a standard debug printf due to recursion
    542  1.17     pooka  */
    543   1.1     pooka ssize_t
    544  1.17     pooka write(int fd, const void *buf, size_t blen)
    545   1.1     pooka {
    546  1.17     pooka 	ssize_t (*op_write)(int, const void *, size_t);
    547   1.1     pooka 
    548  1.17     pooka 	if (fd_isrump(fd)) {
    549  1.17     pooka 		fd = fd_host2rump(fd);
    550  1.17     pooka 		op_write = GETSYSCALL(rump, WRITE);
    551  1.16     pooka 	} else {
    552  1.17     pooka 		op_write = GETSYSCALL(host, WRITE);
    553  1.16     pooka 	}
    554   1.1     pooka 
    555  1.17     pooka 	return op_write(fd, buf, blen);
    556   1.2     pooka }
    557   1.2     pooka 
    558   1.2     pooka /*
    559   1.2     pooka  * dup2 is special.  we allow dup2 of a rump kernel fd to 0-2 since
    560   1.2     pooka  * many programs do that.  dup2 of a rump kernel fd to another value
    561   1.2     pooka  * not >= fdoff is an error.
    562   1.2     pooka  *
    563   1.2     pooka  * Note: cannot rump2host newd, because it is often hardcoded.
    564   1.2     pooka  */
    565   1.2     pooka int
    566   1.2     pooka dup2(int oldd, int newd)
    567   1.2     pooka {
    568  1.17     pooka 	int (*host_dup2)(int, int);
    569   1.2     pooka 	int rv;
    570   1.2     pooka 
    571   1.2     pooka 	DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
    572   1.2     pooka 
    573   1.2     pooka 	if (fd_isrump(oldd)) {
    574   1.2     pooka 		if (!(newd >= 0 && newd <= 2))
    575   1.2     pooka 			return EBADF;
    576   1.2     pooka 		oldd = fd_host2rump(oldd);
    577   1.2     pooka 		rv = rump_sys_dup2(oldd, newd);
    578   1.2     pooka 		if (rv != -1)
    579  1.40     pooka 			SETDUP2(newd);
    580   1.2     pooka 	} else {
    581  1.17     pooka 		host_dup2 = syscalls[DUALCALL_DUP2].bs_host;
    582  1.39     pooka 		if (rumpclient__closenotify(&newd, RUMPCLIENT_CLOSE_DUP2) == -1)
    583  1.39     pooka 			return -1;
    584  1.10     pooka 		rv = host_dup2(oldd, newd);
    585   1.2     pooka 	}
    586  1.10     pooka 
    587  1.10     pooka 	return rv;
    588   1.2     pooka }
    589   1.2     pooka 
    590  1.34     pooka int
    591  1.34     pooka dup(int oldd)
    592  1.34     pooka {
    593  1.34     pooka 
    594  1.40     pooka 	return dodup(oldd, 0);
    595  1.34     pooka }
    596  1.34     pooka 
    597   1.2     pooka /*
    598   1.2     pooka  * We just wrap fork the appropriate rump client calls to preserve
    599   1.2     pooka  * the file descriptors of the forked parent in the child, but
    600   1.2     pooka  * prevent double use of connection fd.
    601   1.2     pooka  */
    602   1.2     pooka pid_t
    603   1.2     pooka fork()
    604   1.2     pooka {
    605   1.2     pooka 	struct rumpclient_fork *rf;
    606   1.2     pooka 	pid_t rv;
    607   1.2     pooka 
    608   1.2     pooka 	DPRINTF(("fork\n"));
    609   1.2     pooka 
    610   1.2     pooka 	if ((rf = rumpclient_prefork()) == NULL)
    611   1.2     pooka 		return -1;
    612   1.2     pooka 
    613   1.2     pooka 	switch ((rv = host_fork())) {
    614   1.2     pooka 	case -1:
    615   1.2     pooka 		/* XXX: cancel rf */
    616   1.2     pooka 		break;
    617   1.2     pooka 	case 0:
    618   1.2     pooka 		if (rumpclient_fork_init(rf) == -1)
    619  1.26     pooka 			rv = -1;
    620   1.2     pooka 		break;
    621   1.2     pooka 	default:
    622   1.2     pooka 		break;
    623   1.2     pooka 	}
    624   1.2     pooka 
    625   1.2     pooka 	DPRINTF(("fork returns %d\n", rv));
    626   1.2     pooka 	return rv;
    627   1.1     pooka }
    628   1.1     pooka 
    629  1.25     pooka int
    630  1.25     pooka daemon(int nochdir, int noclose)
    631  1.25     pooka {
    632  1.25     pooka 	struct rumpclient_fork *rf;
    633  1.25     pooka 
    634  1.25     pooka 	if ((rf = rumpclient_prefork()) == NULL)
    635  1.25     pooka 		return -1;
    636  1.25     pooka 
    637  1.25     pooka 	if (host_daemon(nochdir, noclose) == -1)
    638  1.25     pooka 		return -1;
    639  1.25     pooka 
    640  1.25     pooka 	if (rumpclient_fork_init(rf) == -1)
    641  1.25     pooka 		return -1;
    642  1.25     pooka 
    643  1.25     pooka 	return 0;
    644  1.25     pooka }
    645  1.25     pooka 
    646  1.39     pooka int
    647  1.39     pooka execve(const char *path, char *const argv[], char *const oenvp[])
    648  1.39     pooka {
    649  1.39     pooka 	char buf[128];
    650  1.39     pooka 	char **env;
    651  1.39     pooka 	char *dup2maskenv[2];
    652  1.39     pooka 	char *dup2str;
    653  1.39     pooka 	int rv;
    654  1.39     pooka 
    655  1.40     pooka 	snprintf(buf, sizeof(buf), "RUMPHIJACK__DUP2MASK=%u", dup2mask);
    656  1.39     pooka 	dup2str = malloc(strlen(buf)+1);
    657  1.39     pooka 	if (dup2str == NULL)
    658  1.39     pooka 		return ENOMEM;
    659  1.39     pooka 	strcpy(dup2str, buf);
    660  1.39     pooka 	dup2maskenv[0] = dup2str;
    661  1.39     pooka 	dup2maskenv[1] = NULL;
    662  1.39     pooka 
    663  1.39     pooka 	rv = rumpclient__exec_augmentenv(oenvp, dup2maskenv, &env);
    664  1.39     pooka 	if (rv)
    665  1.39     pooka 		return rv;
    666  1.39     pooka 
    667  1.39     pooka 	rv = host_execve(path, argv, env);
    668  1.39     pooka 	if (rv != 0) {
    669  1.39     pooka 		free(dup2str);
    670  1.39     pooka 		free(env); /* XXX missing some strings within env */
    671  1.39     pooka 	}
    672  1.39     pooka 	return rv;
    673  1.39     pooka }
    674  1.39     pooka 
    675   1.1     pooka /*
    676  1.17     pooka  * select is done by calling poll.
    677   1.1     pooka  */
    678   1.1     pooka int
    679  1.29     pooka REALSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
    680   1.4     pooka 	struct timeval *timeout)
    681   1.1     pooka {
    682   1.4     pooka 	struct pollfd *pfds;
    683   1.4     pooka 	struct timespec ts, *tsp = NULL;
    684  1.19     pooka 	nfds_t realnfds;
    685  1.19     pooka 	int i, j;
    686   1.4     pooka 	int rv, incr;
    687   1.4     pooka 
    688   1.7     pooka 	DPRINTF(("select\n"));
    689   1.7     pooka 
    690   1.4     pooka 	/*
    691   1.4     pooka 	 * Well, first we must scan the fds to figure out how many
    692   1.4     pooka 	 * fds there really are.  This is because up to and including
    693  1.17     pooka 	 * nb5 poll() silently refuses nfds > process_maxopen_fds.
    694   1.4     pooka 	 * Seems to be fixed in current, thank the maker.
    695   1.4     pooka 	 * god damn cluster...bomb.
    696   1.4     pooka 	 */
    697   1.4     pooka 
    698   1.4     pooka 	for (i = 0, realnfds = 0; i < nfds; i++) {
    699   1.4     pooka 		if (readfds && FD_ISSET(i, readfds)) {
    700   1.4     pooka 			realnfds++;
    701   1.4     pooka 			continue;
    702   1.4     pooka 		}
    703   1.4     pooka 		if (writefds && FD_ISSET(i, writefds)) {
    704   1.4     pooka 			realnfds++;
    705   1.4     pooka 			continue;
    706   1.4     pooka 		}
    707   1.4     pooka 		if (exceptfds && FD_ISSET(i, exceptfds)) {
    708   1.4     pooka 			realnfds++;
    709   1.4     pooka 			continue;
    710   1.1     pooka 		}
    711   1.1     pooka 	}
    712   1.1     pooka 
    713   1.6     pooka 	if (realnfds) {
    714  1.38     pooka 		pfds = calloc(realnfds, sizeof(*pfds));
    715   1.6     pooka 		if (!pfds)
    716   1.6     pooka 			return -1;
    717   1.6     pooka 	} else {
    718   1.6     pooka 		pfds = NULL;
    719   1.6     pooka 	}
    720   1.1     pooka 
    721   1.4     pooka 	for (i = 0, j = 0; i < nfds; i++) {
    722   1.4     pooka 		incr = 0;
    723   1.4     pooka 		if (readfds && FD_ISSET(i, readfds)) {
    724   1.4     pooka 			pfds[j].fd = i;
    725   1.4     pooka 			pfds[j].events |= POLLIN;
    726   1.4     pooka 			incr=1;
    727   1.4     pooka 		}
    728   1.4     pooka 		if (writefds && FD_ISSET(i, writefds)) {
    729   1.4     pooka 			pfds[j].fd = i;
    730   1.4     pooka 			pfds[j].events |= POLLOUT;
    731   1.4     pooka 			incr=1;
    732   1.4     pooka 		}
    733   1.4     pooka 		if (exceptfds && FD_ISSET(i, exceptfds)) {
    734   1.4     pooka 			pfds[j].fd = i;
    735   1.4     pooka 			pfds[j].events |= POLLHUP|POLLERR;
    736   1.4     pooka 			incr=1;
    737   1.1     pooka 		}
    738   1.4     pooka 		if (incr)
    739   1.4     pooka 			j++;
    740   1.1     pooka 	}
    741  1.37     pooka 	assert(j == (int)realnfds);
    742   1.1     pooka 
    743   1.4     pooka 	if (timeout) {
    744   1.4     pooka 		TIMEVAL_TO_TIMESPEC(timeout, &ts);
    745   1.4     pooka 		tsp = &ts;
    746   1.4     pooka 	}
    747  1.29     pooka 	rv = REALPOLLTS(pfds, realnfds, tsp, NULL);
    748  1.36     pooka 	/*
    749  1.36     pooka 	 * "If select() returns with an error the descriptor sets
    750  1.36     pooka 	 * will be unmodified"
    751  1.36     pooka 	 */
    752  1.36     pooka 	if (rv < 0)
    753   1.4     pooka 		goto out;
    754   1.4     pooka 
    755   1.4     pooka 	/*
    756  1.36     pooka 	 * zero out results (can't use FD_ZERO for the
    757  1.36     pooka 	 * obvious select-me-not reason).  whee.
    758  1.36     pooka 	 *
    759  1.36     pooka 	 * We do this here since some software ignores the return
    760  1.36     pooka 	 * value of select, and hence if the timeout expires, it may
    761  1.36     pooka 	 * assume all input descriptors have activity.
    762   1.4     pooka 	 */
    763   1.4     pooka 	for (i = 0; i < nfds; i++) {
    764   1.4     pooka 		if (readfds)
    765   1.4     pooka 			FD_CLR(i, readfds);
    766   1.4     pooka 		if (writefds)
    767   1.4     pooka 			FD_CLR(i, writefds);
    768   1.4     pooka 		if (exceptfds)
    769   1.4     pooka 			FD_CLR(i, exceptfds);
    770   1.1     pooka 	}
    771  1.36     pooka 	if (rv == 0)
    772  1.36     pooka 		goto out;
    773   1.1     pooka 
    774  1.36     pooka 	/*
    775  1.36     pooka 	 * We have >0 fds with activity.  Harvest the results.
    776  1.36     pooka 	 */
    777  1.19     pooka 	for (i = 0; i < (int)realnfds; i++) {
    778   1.4     pooka 		if (readfds) {
    779   1.4     pooka 			if (pfds[i].revents & POLLIN) {
    780   1.4     pooka 				FD_SET(pfds[i].fd, readfds);
    781   1.4     pooka 			}
    782   1.4     pooka 		}
    783   1.4     pooka 		if (writefds) {
    784   1.4     pooka 			if (pfds[i].revents & POLLOUT) {
    785   1.4     pooka 				FD_SET(pfds[i].fd, writefds);
    786   1.4     pooka 			}
    787   1.4     pooka 		}
    788   1.4     pooka 		if (exceptfds) {
    789   1.4     pooka 			if (pfds[i].revents & (POLLHUP|POLLERR)) {
    790   1.4     pooka 				FD_SET(pfds[i].fd, exceptfds);
    791   1.4     pooka 			}
    792   1.4     pooka 		}
    793   1.1     pooka 	}
    794   1.1     pooka 
    795   1.4     pooka  out:
    796   1.4     pooka 	free(pfds);
    797   1.1     pooka 	return rv;
    798   1.1     pooka }
    799   1.1     pooka 
    800   1.1     pooka static void
    801   1.1     pooka checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
    802   1.1     pooka {
    803   1.1     pooka 	nfds_t i;
    804   1.1     pooka 
    805   1.1     pooka 	for (i = 0; i < nfds; i++) {
    806  1.12     pooka 		if (fds[i].fd == -1)
    807  1.12     pooka 			continue;
    808  1.12     pooka 
    809   1.2     pooka 		if (fd_isrump(fds[i].fd))
    810   1.2     pooka 			(*rumpcall)++;
    811   1.2     pooka 		else
    812   1.1     pooka 			(*hostcall)++;
    813   1.1     pooka 	}
    814   1.1     pooka }
    815   1.1     pooka 
    816   1.1     pooka static void
    817   1.2     pooka adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
    818   1.1     pooka {
    819   1.1     pooka 	nfds_t i;
    820   1.1     pooka 
    821   1.1     pooka 	for (i = 0; i < nfds; i++) {
    822   1.2     pooka 		fds[i].fd = fdadj(fds[i].fd);
    823   1.1     pooka 	}
    824   1.1     pooka }
    825   1.1     pooka 
    826   1.1     pooka /*
    827   1.1     pooka  * poll is easy as long as the call comes in the fds only in one
    828   1.1     pooka  * kernel.  otherwise its quite tricky...
    829   1.1     pooka  */
    830   1.1     pooka struct pollarg {
    831   1.1     pooka 	struct pollfd *pfds;
    832   1.1     pooka 	nfds_t nfds;
    833   1.3     pooka 	const struct timespec *ts;
    834   1.3     pooka 	const sigset_t *sigmask;
    835   1.1     pooka 	int pipefd;
    836   1.1     pooka 	int errnum;
    837   1.1     pooka };
    838   1.1     pooka 
    839   1.1     pooka static void *
    840   1.1     pooka hostpoll(void *arg)
    841   1.1     pooka {
    842  1.17     pooka 	int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
    843  1.17     pooka 			 const sigset_t *);
    844   1.1     pooka 	struct pollarg *parg = arg;
    845   1.1     pooka 	intptr_t rv;
    846   1.1     pooka 
    847  1.35     pooka 	op_pollts = GETSYSCALL(host, POLLTS);
    848  1.17     pooka 	rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
    849   1.1     pooka 	if (rv == -1)
    850   1.1     pooka 		parg->errnum = errno;
    851   1.1     pooka 	rump_sys_write(parg->pipefd, &rv, sizeof(rv));
    852   1.1     pooka 
    853   1.1     pooka 	return (void *)(intptr_t)rv;
    854   1.1     pooka }
    855   1.1     pooka 
    856   1.1     pooka int
    857  1.29     pooka REALPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
    858   1.3     pooka 	const sigset_t *sigmask)
    859   1.1     pooka {
    860   1.3     pooka 	int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
    861   1.3     pooka 			 const sigset_t *);
    862  1.17     pooka 	int (*host_close)(int);
    863   1.1     pooka 	int hostcall = 0, rumpcall = 0;
    864   1.1     pooka 	pthread_t pt;
    865   1.1     pooka 	nfds_t i;
    866   1.1     pooka 	int rv;
    867   1.1     pooka 
    868   1.2     pooka 	DPRINTF(("poll\n"));
    869   1.1     pooka 	checkpoll(fds, nfds, &hostcall, &rumpcall);
    870   1.1     pooka 
    871   1.1     pooka 	if (hostcall && rumpcall) {
    872   1.1     pooka 		struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
    873   1.1     pooka 		int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
    874   1.1     pooka 		struct pollarg parg;
    875   1.1     pooka 		uintptr_t lrv;
    876   1.1     pooka 		int sverrno = 0, trv;
    877   1.1     pooka 
    878   1.1     pooka 		/*
    879   1.1     pooka 		 * ok, this is where it gets tricky.  We must support
    880   1.1     pooka 		 * this since it's a very common operation in certain
    881   1.1     pooka 		 * types of software (telnet, netcat, etc).  We allocate
    882   1.1     pooka 		 * two vectors and run two poll commands in separate
    883   1.1     pooka 		 * threads.  Whichever returns first "wins" and the
    884   1.1     pooka 		 * other kernel's fds won't show activity.
    885   1.1     pooka 		 */
    886   1.1     pooka 		rv = -1;
    887   1.1     pooka 
    888   1.1     pooka 		/* allocate full vector for O(n) joining after call */
    889   1.1     pooka 		pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
    890   1.1     pooka 		if (!pfd_host)
    891   1.1     pooka 			goto out;
    892   1.1     pooka 		pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
    893   1.1     pooka 		if (!pfd_rump) {
    894   1.1     pooka 			goto out;
    895   1.1     pooka 		}
    896   1.1     pooka 
    897   1.1     pooka 		/* split vectors */
    898   1.1     pooka 		for (i = 0; i < nfds; i++) {
    899   1.3     pooka 			if (fds[i].fd == -1) {
    900   1.3     pooka 				pfd_host[i].fd = -1;
    901   1.3     pooka 				pfd_rump[i].fd = -1;
    902   1.3     pooka 			} else if (fd_isrump(fds[i].fd)) {
    903   1.2     pooka 				pfd_host[i].fd = -1;
    904   1.2     pooka 				pfd_rump[i].fd = fd_host2rump(fds[i].fd);
    905   1.2     pooka 				pfd_rump[i].events = fds[i].events;
    906   1.2     pooka 			} else {
    907   1.2     pooka 				pfd_rump[i].fd = -1;
    908   1.1     pooka 				pfd_host[i].fd = fds[i].fd;
    909   1.1     pooka 				pfd_host[i].events = fds[i].events;
    910   1.1     pooka 			}
    911  1.39     pooka 			pfd_rump[i].revents = pfd_host[i].revents = 0;
    912  1.13     pooka 			fds[i].revents = 0;
    913   1.1     pooka 		}
    914   1.1     pooka 
    915   1.1     pooka 		/*
    916   1.1     pooka 		 * then, open two pipes, one for notifications
    917   1.1     pooka 		 * to each kernel.
    918   1.1     pooka 		 */
    919   1.1     pooka 		if (rump_sys_pipe(rpipe) == -1)
    920   1.1     pooka 			goto out;
    921   1.1     pooka 		if (pipe(hpipe) == -1)
    922   1.1     pooka 			goto out;
    923   1.1     pooka 
    924   1.1     pooka 		pfd_host[nfds].fd = hpipe[0];
    925   1.1     pooka 		pfd_host[nfds].events = POLLIN;
    926   1.1     pooka 		pfd_rump[nfds].fd = rpipe[0];
    927   1.1     pooka 		pfd_rump[nfds].events = POLLIN;
    928   1.1     pooka 
    929   1.1     pooka 		/*
    930   1.1     pooka 		 * then, create a thread to do host part and meanwhile
    931   1.1     pooka 		 * do rump kernel part right here
    932   1.1     pooka 		 */
    933   1.1     pooka 
    934   1.1     pooka 		parg.pfds = pfd_host;
    935   1.1     pooka 		parg.nfds = nfds+1;
    936   1.3     pooka 		parg.ts = ts;
    937   1.3     pooka 		parg.sigmask = sigmask;
    938   1.1     pooka 		parg.pipefd = rpipe[1];
    939   1.1     pooka 		pthread_create(&pt, NULL, hostpoll, &parg);
    940   1.1     pooka 
    941  1.35     pooka 		op_pollts = GETSYSCALL(rump, POLLTS);
    942   1.3     pooka 		lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
    943   1.1     pooka 		sverrno = errno;
    944   1.1     pooka 		write(hpipe[1], &rv, sizeof(rv));
    945   1.1     pooka 		pthread_join(pt, (void *)&trv);
    946   1.1     pooka 
    947   1.1     pooka 		/* check who "won" and merge results */
    948   1.1     pooka 		if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
    949   1.1     pooka 			rv = trv;
    950   1.1     pooka 
    951   1.1     pooka 			for (i = 0; i < nfds; i++) {
    952   1.1     pooka 				if (pfd_rump[i].fd != -1)
    953   1.1     pooka 					fds[i].revents = pfd_rump[i].revents;
    954   1.1     pooka 			}
    955   1.1     pooka 			sverrno = parg.errnum;
    956   1.1     pooka 		} else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
    957   1.1     pooka 			rv = trv;
    958   1.1     pooka 
    959   1.1     pooka 			for (i = 0; i < nfds; i++) {
    960   1.1     pooka 				if (pfd_host[i].fd != -1)
    961   1.1     pooka 					fds[i].revents = pfd_host[i].revents;
    962   1.1     pooka 			}
    963   1.1     pooka 		} else {
    964   1.1     pooka 			rv = 0;
    965   1.1     pooka 		}
    966   1.1     pooka 
    967   1.1     pooka  out:
    968  1.35     pooka 		host_close = GETSYSCALL(host, CLOSE);
    969   1.1     pooka 		if (rpipe[0] != -1)
    970   1.1     pooka 			rump_sys_close(rpipe[0]);
    971   1.1     pooka 		if (rpipe[1] != -1)
    972   1.1     pooka 			rump_sys_close(rpipe[1]);
    973   1.1     pooka 		if (hpipe[0] != -1)
    974   1.9     pooka 			host_close(hpipe[0]);
    975   1.1     pooka 		if (hpipe[1] != -1)
    976   1.9     pooka 			host_close(hpipe[1]);
    977   1.1     pooka 		free(pfd_host);
    978   1.1     pooka 		free(pfd_rump);
    979   1.1     pooka 		errno = sverrno;
    980   1.1     pooka 	} else {
    981   1.1     pooka 		if (hostcall) {
    982  1.35     pooka 			op_pollts = GETSYSCALL(host, POLLTS);
    983   1.1     pooka 		} else {
    984  1.35     pooka 			op_pollts = GETSYSCALL(rump, POLLTS);
    985   1.2     pooka 			adjustpoll(fds, nfds, fd_host2rump);
    986   1.1     pooka 		}
    987   1.1     pooka 
    988   1.3     pooka 		rv = op_pollts(fds, nfds, ts, sigmask);
    989   1.1     pooka 		if (rumpcall)
    990   1.2     pooka 			adjustpoll(fds, nfds, fd_rump2host);
    991   1.1     pooka 	}
    992   1.1     pooka 
    993   1.1     pooka 	return rv;
    994   1.1     pooka }
    995   1.1     pooka 
    996   1.1     pooka int
    997  1.24     pooka poll(struct pollfd *fds, nfds_t nfds, int timeout)
    998   1.1     pooka {
    999   1.3     pooka 	struct timespec ts;
   1000   1.3     pooka 	struct timespec *tsp = NULL;
   1001   1.3     pooka 
   1002   1.3     pooka 	if (timeout != INFTIM) {
   1003   1.3     pooka 		ts.tv_sec = timeout / 1000;
   1004  1.11     pooka 		ts.tv_nsec = (timeout % 1000) * 1000*1000;
   1005   1.3     pooka 
   1006   1.3     pooka 		tsp = &ts;
   1007   1.3     pooka 	}
   1008   1.1     pooka 
   1009  1.29     pooka 	return REALPOLLTS(fds, nfds, tsp, NULL);
   1010   1.1     pooka }
   1011  1.10     pooka 
   1012  1.10     pooka int
   1013  1.34     pooka REALKEVENT(int kq, const struct kevent *changelist, size_t nchanges,
   1014  1.34     pooka 	struct kevent *eventlist, size_t nevents,
   1015  1.34     pooka 	const struct timespec *timeout)
   1016  1.10     pooka {
   1017  1.34     pooka 	int (*op_kevent)(int, const struct kevent *, size_t,
   1018  1.34     pooka 		struct kevent *, size_t, const struct timespec *);
   1019  1.34     pooka 	const struct kevent *ev;
   1020  1.34     pooka 	size_t i;
   1021  1.10     pooka 
   1022  1.34     pooka 	/*
   1023  1.34     pooka 	 * Check that we don't attempt to kevent rump kernel fd's.
   1024  1.34     pooka 	 * That needs similar treatment to select/poll, but is slightly
   1025  1.34     pooka 	 * trickier since we need to manage to different kq descriptors.
   1026  1.34     pooka 	 * (TODO, in case you're wondering).
   1027  1.34     pooka 	 */
   1028  1.34     pooka 	for (i = 0; i < nchanges; i++) {
   1029  1.34     pooka 		ev = &changelist[i];
   1030  1.34     pooka 		if (ev->filter == EVFILT_READ || ev->filter == EVFILT_WRITE ||
   1031  1.34     pooka 		    ev->filter == EVFILT_VNODE) {
   1032  1.40     pooka 			if (fd_isrump((int)ev->ident))
   1033  1.34     pooka 				return ENOTSUP;
   1034  1.34     pooka 		}
   1035  1.27     pooka 	}
   1036  1.10     pooka 
   1037  1.35     pooka 	op_kevent = GETSYSCALL(host, KEVENT);
   1038  1.34     pooka 	return op_kevent(kq, changelist, nchanges, eventlist, nevents, timeout);
   1039  1.10     pooka }
   1040  1.17     pooka 
   1041  1.17     pooka /*
   1042  1.17     pooka  * Rest are std type calls.
   1043  1.17     pooka  */
   1044  1.17     pooka 
   1045  1.17     pooka FDCALL(int, bind, DUALCALL_BIND,					\
   1046  1.17     pooka 	(int fd, const struct sockaddr *name, socklen_t namelen),	\
   1047  1.17     pooka 	(int, const struct sockaddr *, socklen_t),			\
   1048  1.17     pooka 	(fd, name, namelen))
   1049  1.17     pooka 
   1050  1.17     pooka FDCALL(int, connect, DUALCALL_CONNECT,					\
   1051  1.17     pooka 	(int fd, const struct sockaddr *name, socklen_t namelen),	\
   1052  1.17     pooka 	(int, const struct sockaddr *, socklen_t),			\
   1053  1.17     pooka 	(fd, name, namelen))
   1054  1.17     pooka 
   1055  1.17     pooka FDCALL(int, getpeername, DUALCALL_GETPEERNAME,				\
   1056  1.17     pooka 	(int fd, struct sockaddr *name, socklen_t *namelen),		\
   1057  1.17     pooka 	(int, struct sockaddr *, socklen_t *),				\
   1058  1.17     pooka 	(fd, name, namelen))
   1059  1.17     pooka 
   1060  1.17     pooka FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, 				\
   1061  1.17     pooka 	(int fd, struct sockaddr *name, socklen_t *namelen),		\
   1062  1.17     pooka 	(int, struct sockaddr *, socklen_t *),				\
   1063  1.17     pooka 	(fd, name, namelen))
   1064  1.17     pooka 
   1065  1.17     pooka FDCALL(int, listen, DUALCALL_LISTEN,	 				\
   1066  1.17     pooka 	(int fd, int backlog),						\
   1067  1.17     pooka 	(int, int),							\
   1068  1.17     pooka 	(fd, backlog))
   1069  1.17     pooka 
   1070  1.17     pooka FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, 				\
   1071  1.17     pooka 	(int fd, void *buf, size_t len, int flags,			\
   1072  1.17     pooka 	    struct sockaddr *from, socklen_t *fromlen),			\
   1073  1.17     pooka 	(int, void *, size_t, int, struct sockaddr *, socklen_t *),	\
   1074  1.17     pooka 	(fd, buf, len, flags, from, fromlen))
   1075  1.17     pooka 
   1076  1.17     pooka FDCALL(ssize_t, sendto, DUALCALL_SENDTO, 				\
   1077  1.17     pooka 	(int fd, const void *buf, size_t len, int flags,		\
   1078  1.17     pooka 	    const struct sockaddr *to, socklen_t tolen),		\
   1079  1.17     pooka 	(int, const void *, size_t, int,				\
   1080  1.17     pooka 	    const struct sockaddr *, socklen_t),			\
   1081  1.17     pooka 	(fd, buf, len, flags, to, tolen))
   1082  1.17     pooka 
   1083  1.17     pooka FDCALL(ssize_t, recvmsg, DUALCALL_RECVMSG, 				\
   1084  1.17     pooka 	(int fd, struct msghdr *msg, int flags),			\
   1085  1.17     pooka 	(int, struct msghdr *, int),					\
   1086  1.17     pooka 	(fd, msg, flags))
   1087  1.17     pooka 
   1088  1.17     pooka FDCALL(ssize_t, sendmsg, DUALCALL_SENDMSG, 				\
   1089  1.17     pooka 	(int fd, const struct msghdr *msg, int flags),			\
   1090  1.17     pooka 	(int, const struct msghdr *, int),				\
   1091  1.17     pooka 	(fd, msg, flags))
   1092  1.17     pooka 
   1093  1.17     pooka FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, 				\
   1094  1.17     pooka 	(int fd, int level, int optn, void *optval, socklen_t *optlen),	\
   1095  1.17     pooka 	(int, int, int, void *, socklen_t *),				\
   1096  1.17     pooka 	(fd, level, optn, optval, optlen))
   1097  1.17     pooka 
   1098  1.17     pooka FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, 				\
   1099  1.17     pooka 	(int fd, int level, int optn,					\
   1100  1.17     pooka 	    const void *optval, socklen_t optlen),			\
   1101  1.17     pooka 	(int, int, int, const void *, socklen_t),			\
   1102  1.17     pooka 	(fd, level, optn, optval, optlen))
   1103  1.17     pooka 
   1104  1.17     pooka FDCALL(int, shutdown, DUALCALL_SHUTDOWN, 				\
   1105  1.17     pooka 	(int fd, int how),						\
   1106  1.17     pooka 	(int, int),							\
   1107  1.17     pooka 	(fd, how))
   1108  1.17     pooka 
   1109  1.21  christos #if _FORTIFY_SOURCE > 0
   1110  1.21  christos #define STUB(fun) __ssp_weak_name(fun)
   1111  1.21  christos ssize_t _sys_readlink(const char * __restrict, char * __restrict, size_t);
   1112  1.21  christos ssize_t
   1113  1.21  christos STUB(readlink)(const char * __restrict path, char * __restrict buf,
   1114  1.21  christos     size_t bufsiz)
   1115  1.21  christos {
   1116  1.21  christos 	return _sys_readlink(path, buf, bufsiz);
   1117  1.21  christos }
   1118  1.21  christos 
   1119  1.21  christos char *_sys_getcwd(char *, size_t);
   1120  1.21  christos char *
   1121  1.21  christos STUB(getcwd)(char *buf, size_t size)
   1122  1.21  christos {
   1123  1.21  christos 	return _sys_getcwd(buf, size);
   1124  1.21  christos }
   1125  1.21  christos #else
   1126  1.21  christos #define STUB(fun) fun
   1127  1.21  christos #endif
   1128  1.21  christos 
   1129  1.31     pooka FDCALL(ssize_t, REALREAD, DUALCALL_READ,				\
   1130  1.17     pooka 	(int fd, void *buf, size_t buflen),				\
   1131  1.17     pooka 	(int, void *, size_t),						\
   1132  1.17     pooka 	(fd, buf, buflen))
   1133  1.17     pooka 
   1134  1.18     pooka FDCALL(ssize_t, readv, DUALCALL_READV, 					\
   1135  1.17     pooka 	(int fd, const struct iovec *iov, int iovcnt),			\
   1136  1.17     pooka 	(int, const struct iovec *, int),				\
   1137  1.17     pooka 	(fd, iov, iovcnt))
   1138  1.17     pooka 
   1139  1.17     pooka FDCALL(ssize_t, writev, DUALCALL_WRITEV, 				\
   1140  1.17     pooka 	(int fd, const struct iovec *iov, int iovcnt),			\
   1141  1.17     pooka 	(int, const struct iovec *, int),				\
   1142  1.17     pooka 	(fd, iov, iovcnt))
   1143