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