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