Home | History | Annotate | Line # | Download | only in librumphijack
hijack.c revision 1.20
      1  1.20  pooka /*      $NetBSD: hijack.c,v 1.20 2011/01/25 17:37:00 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.20  pooka __RCSID("$NetBSD: hijack.c,v 1.20 2011/01/25 17:37:00 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.17  pooka enum dualcall {
     57  1.17  pooka 	DUALCALL_WRITE, DUALCALL_WRITEV,
     58  1.17  pooka 	DUALCALL_IOCTL, DUALCALL_FCNTL,
     59  1.17  pooka 	DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_BIND, DUALCALL_CONNECT,
     60  1.17  pooka 	DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN,
     61  1.17  pooka 	DUALCALL_RECVFROM, DUALCALL_RECVMSG,
     62  1.17  pooka 	DUALCALL_SENDTO, DUALCALL_SENDMSG,
     63  1.17  pooka 	DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT,
     64  1.17  pooka 	DUALCALL_SHUTDOWN,
     65  1.17  pooka 	DUALCALL_READ, DUALCALL_READV,
     66  1.17  pooka 	DUALCALL_DUP2, DUALCALL_CLOSE,
     67  1.17  pooka 	DUALCALL_POLLTS,
     68  1.17  pooka 	DUALCALL__NUM
     69   1.1  pooka };
     70   1.1  pooka 
     71   1.8  pooka #define RSYS_STRING(a) __STRING(a)
     72   1.8  pooka #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a))
     73   1.8  pooka 
     74   1.1  pooka /*
     75  1.14  pooka  * Would be nice to get this automatically in sync with libc.
     76  1.14  pooka  * Also, this does not work for compat-using binaries!
     77  1.14  pooka  */
     78  1.14  pooka #if !__NetBSD_Prereq__(5,99,7)
     79  1.17  pooka #define LIBCSELECT select
     80  1.17  pooka #define LIBCPOLLTS pollts
     81  1.17  pooka #define LIBCPOLL poll
     82  1.14  pooka #else
     83  1.17  pooka #define LIBCSELECT __select50
     84  1.17  pooka #define LIBCPOLLTS __pollts50
     85  1.17  pooka #define LIBCPOLL __poll50
     86  1.17  pooka #endif
     87  1.14  pooka 
     88  1.20  pooka int LIBCSELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *);
     89  1.20  pooka int LIBCPOLLTS(struct pollfd *, nfds_t,
     90  1.20  pooka 	       const struct timespec *, const sigset_t *);
     91  1.20  pooka int LIBCPOLL(struct pollfd *, nfds_t, int);
     92  1.17  pooka 
     93  1.17  pooka #define S(a) __STRING(a)
     94  1.17  pooka struct sysnames {
     95  1.17  pooka 	enum dualcall scm_callnum;
     96  1.17  pooka 	const char *scm_hostname;
     97  1.17  pooka 	const char *scm_rumpname;
     98  1.17  pooka } syscnames[] = {
     99  1.17  pooka 	{ DUALCALL_SOCKET,	"__socket30",	RSYS_NAME(SOCKET)	},
    100  1.17  pooka 	{ DUALCALL_ACCEPT,	"accept",	RSYS_NAME(ACCEPT)	},
    101  1.17  pooka 	{ DUALCALL_BIND,	"bind",		RSYS_NAME(BIND)		},
    102  1.17  pooka 	{ DUALCALL_CONNECT,	"connect",	RSYS_NAME(CONNECT)	},
    103  1.17  pooka 	{ DUALCALL_GETPEERNAME,	"getpeername",	RSYS_NAME(GETPEERNAME)	},
    104  1.17  pooka 	{ DUALCALL_GETSOCKNAME,	"getsockname",	RSYS_NAME(GETSOCKNAME)	},
    105  1.17  pooka 	{ DUALCALL_LISTEN,	"listen",	RSYS_NAME(LISTEN)	},
    106  1.17  pooka 	{ DUALCALL_RECVFROM,	"recvfrom",	RSYS_NAME(RECVFROM)	},
    107  1.17  pooka 	{ DUALCALL_RECVMSG,	"recvmsg",	RSYS_NAME(RECVMSG)	},
    108  1.17  pooka 	{ DUALCALL_SENDTO,	"sendto",	RSYS_NAME(SENDTO)	},
    109  1.17  pooka 	{ DUALCALL_SENDMSG,	"sendmsg",	RSYS_NAME(SENDMSG)	},
    110  1.17  pooka 	{ DUALCALL_GETSOCKOPT,	"getsockopt",	RSYS_NAME(GETSOCKOPT)	},
    111  1.17  pooka 	{ DUALCALL_SETSOCKOPT,	"setsockopt",	RSYS_NAME(SETSOCKOPT)	},
    112  1.17  pooka 	{ DUALCALL_SHUTDOWN,	"shutdown",	RSYS_NAME(SHUTDOWN)	},
    113  1.17  pooka 	{ DUALCALL_READ,	"read",		RSYS_NAME(READ)		},
    114  1.17  pooka 	{ DUALCALL_READV,	"readv",	RSYS_NAME(READV)	},
    115  1.17  pooka 	{ DUALCALL_WRITE,	"write",	RSYS_NAME(WRITE)	},
    116  1.17  pooka 	{ DUALCALL_WRITEV,	"writev",	RSYS_NAME(WRITEV)	},
    117  1.17  pooka 	{ DUALCALL_IOCTL,	"ioctl",	RSYS_NAME(IOCTL)	},
    118  1.17  pooka 	{ DUALCALL_FCNTL,	"fcntl",	RSYS_NAME(FCNTL)	},
    119  1.17  pooka 	{ DUALCALL_DUP2,	"dup2",		RSYS_NAME(DUP2)		},
    120  1.17  pooka 	{ DUALCALL_CLOSE,	"close",	RSYS_NAME(CLOSE)	},
    121  1.17  pooka 	{ DUALCALL_POLLTS,	S(LIBCPOLLTS),	RSYS_NAME(POLLTS)	},
    122  1.17  pooka };
    123  1.17  pooka #undef S
    124  1.17  pooka 
    125  1.17  pooka struct bothsys {
    126  1.17  pooka 	void *bs_host;
    127  1.17  pooka 	void *bs_rump;
    128  1.17  pooka } syscalls[DUALCALL__NUM];
    129  1.17  pooka #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which
    130  1.17  pooka 
    131  1.17  pooka pid_t (*host_fork)(void);
    132  1.17  pooka 
    133  1.17  pooka static unsigned dup2mask;
    134  1.17  pooka #define ISDUP2D(fd) (1<<(fd) & dup2mask)
    135  1.17  pooka 
    136  1.17  pooka //#define DEBUGJACK
    137  1.17  pooka #ifdef DEBUGJACK
    138  1.17  pooka #define DPRINTF(x) mydprintf x
    139  1.17  pooka static void
    140  1.17  pooka mydprintf(const char *fmt, ...)
    141  1.17  pooka {
    142  1.17  pooka 	va_list ap;
    143  1.17  pooka 
    144  1.17  pooka 	if (ISDUP2D(STDERR_FILENO))
    145  1.17  pooka 		return;
    146  1.17  pooka 
    147  1.17  pooka 	va_start(ap, fmt);
    148  1.17  pooka 	vfprintf(stderr, fmt, ap);
    149  1.17  pooka 	va_end(ap);
    150  1.17  pooka }
    151  1.17  pooka 
    152  1.17  pooka #else
    153  1.17  pooka #define DPRINTF(x)
    154  1.14  pooka #endif
    155  1.14  pooka 
    156  1.17  pooka #define FDCALL(type, name, rcname, args, proto, vars)			\
    157  1.17  pooka type name args								\
    158  1.17  pooka {									\
    159  1.17  pooka 	type (*fun) proto;						\
    160  1.17  pooka 									\
    161  1.17  pooka 	if (fd_isrump(fd)) {						\
    162  1.17  pooka 		fun = syscalls[rcname].bs_rump;				\
    163  1.17  pooka 		fd = fd_host2rump(fd);					\
    164  1.17  pooka 	} else {							\
    165  1.17  pooka 		fun = syscalls[rcname].bs_host;				\
    166  1.17  pooka 	}								\
    167  1.17  pooka 									\
    168  1.17  pooka 	return fun vars;						\
    169  1.17  pooka }
    170  1.17  pooka 
    171  1.14  pooka /*
    172   1.1  pooka  * This is called from librumpclient in case of LD_PRELOAD.
    173   1.1  pooka  * It ensures correct RTLD_NEXT.
    174   1.1  pooka  */
    175   1.1  pooka static void *
    176   1.1  pooka hijackdlsym(void *handle, const char *symbol)
    177   1.1  pooka {
    178   1.1  pooka 
    179   1.1  pooka 	return dlsym(handle, symbol);
    180   1.1  pooka }
    181   1.1  pooka 
    182   1.7  pooka /* low calorie sockets? */
    183  1.14  pooka static bool hostlocalsockets = true;
    184   1.7  pooka 
    185   1.1  pooka static void __attribute__((constructor))
    186   1.1  pooka rcinit(void)
    187   1.1  pooka {
    188   1.1  pooka 	int (*rumpcinit)(void);
    189   1.1  pooka 	void **rumpcdlsym;
    190   1.1  pooka 	void *hand;
    191  1.19  pooka 	unsigned i, j;
    192   1.1  pooka 
    193   1.1  pooka 	hand = dlopen("librumpclient.so", RTLD_LAZY|RTLD_GLOBAL);
    194   1.1  pooka 	if (!hand)
    195   1.1  pooka 		err(1, "cannot open librumpclient.so");
    196   1.1  pooka 	rumpcinit = dlsym(hand, "rumpclient_init");
    197   1.1  pooka 	_DIAGASSERT(rumpcinit);
    198   1.1  pooka 
    199   1.1  pooka 	rumpcdlsym = dlsym(hand, "rumpclient_dlsym");
    200   1.1  pooka 	*rumpcdlsym = hijackdlsym;
    201  1.17  pooka 	host_fork = dlsym(RTLD_NEXT, "fork");
    202  1.17  pooka 
    203  1.17  pooka 	/*
    204  1.17  pooka 	 * In theory cannot print anything during lookups because
    205  1.17  pooka 	 * we might not have the call vector set up.  so, the errx()
    206  1.17  pooka 	 * is a bit of a strech, but it might work.
    207  1.17  pooka 	 */
    208   1.1  pooka 
    209  1.17  pooka 	for (i = 0; i < DUALCALL__NUM; i++) {
    210  1.17  pooka 		/* build runtime O(1) access */
    211  1.17  pooka 		for (j = 0; j < __arraycount(syscnames); j++) {
    212  1.17  pooka 			if (syscnames[j].scm_callnum == i)
    213  1.17  pooka 				break;
    214  1.17  pooka 		}
    215  1.17  pooka 
    216  1.17  pooka 		if (j == __arraycount(syscnames))
    217  1.17  pooka 			errx(1, "rumphijack error: syscall pos %d missing", i);
    218  1.17  pooka 
    219  1.17  pooka 		syscalls[i].bs_host = dlsym(hand,syscnames[j].scm_hostname);
    220  1.17  pooka 		if (syscalls[i].bs_host == NULL)
    221  1.17  pooka 			errx(1, "hostcall %s not found missing",
    222  1.17  pooka 			    syscnames[j].scm_hostname);
    223  1.17  pooka 
    224  1.17  pooka 		syscalls[i].bs_rump = dlsym(hand,syscnames[j].scm_rumpname);
    225  1.17  pooka 		if (syscalls[i].bs_rump == NULL)
    226  1.17  pooka 			errx(1, "rumpcall %s not found missing",
    227  1.17  pooka 			    syscnames[j].scm_rumpname);
    228   1.1  pooka 	}
    229   1.1  pooka 
    230   1.1  pooka 	if (rumpcinit() == -1)
    231   1.1  pooka 		err(1, "rumpclient init");
    232   1.1  pooka }
    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.17  pooka 	int (*op_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.17  pooka 		op_socket = GETSYSCALL(host, SOCKET);
    282   1.7  pooka 	else
    283  1.17  pooka 		op_socket = GETSYSCALL(rump, SOCKET);
    284  1.17  pooka 	fd = op_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.17  pooka 	int (*op_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.17  pooka 		op_accept = GETSYSCALL(rump, ACCEPT);
    305   1.7  pooka 		s = fd_host2rump(s);
    306   1.7  pooka 	} else {
    307  1.17  pooka 		op_accept = GETSYSCALL(host, ACCEPT);
    308   1.7  pooka 	}
    309  1.17  pooka 	fd = op_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.17  pooka /*
    319  1.17  pooka  * ioctl and fcntl are varargs calls and need special treatment
    320  1.17  pooka  */
    321   1.1  pooka int
    322  1.17  pooka ioctl(int fd, unsigned long cmd, ...)
    323   1.1  pooka {
    324  1.17  pooka 	int (*op_ioctl)(int, unsigned long cmd, ...);
    325  1.17  pooka 	va_list ap;
    326  1.17  pooka 	int rv;
    327   1.1  pooka 
    328  1.17  pooka 	DPRINTF(("ioctl -> %d\n", fd));
    329  1.17  pooka 	if (fd_isrump(fd)) {
    330  1.17  pooka 		fd = fd_host2rump(fd);
    331  1.17  pooka 		op_ioctl = GETSYSCALL(rump, IOCTL);
    332   1.7  pooka 	} else {
    333  1.17  pooka 		op_ioctl = GETSYSCALL(host, IOCTL);
    334   1.7  pooka 	}
    335   1.1  pooka 
    336  1.17  pooka 	va_start(ap, cmd);
    337  1.17  pooka 	rv = op_ioctl(fd, cmd, va_arg(ap, void *));
    338  1.17  pooka 	va_end(ap);
    339  1.17  pooka 	return rv;
    340   1.1  pooka }
    341   1.1  pooka 
    342   1.1  pooka int
    343  1.17  pooka fcntl(int fd, int cmd, ...)
    344   1.1  pooka {
    345  1.17  pooka 	int (*op_fcntl)(int, int, ...);
    346  1.17  pooka 	va_list ap;
    347  1.17  pooka 	int rv;
    348   1.1  pooka 
    349  1.17  pooka 	DPRINTF(("fcntl -> %d\n", fd));
    350  1.17  pooka 	if (fd_isrump(fd)) {
    351  1.17  pooka 		fd = fd_host2rump(fd);
    352  1.17  pooka 		op_fcntl = GETSYSCALL(rump, FCNTL);
    353   1.7  pooka 	} else {
    354  1.17  pooka 		op_fcntl = GETSYSCALL(host, FCNTL);
    355   1.7  pooka 	}
    356   1.1  pooka 
    357  1.17  pooka 	va_start(ap, cmd);
    358  1.17  pooka 	rv = op_fcntl(fd, cmd, va_arg(ap, void *));
    359  1.17  pooka 	va_end(ap);
    360  1.17  pooka 	return rv;
    361   1.1  pooka }
    362   1.1  pooka 
    363  1.17  pooka /*
    364  1.17  pooka  * write cannot issue a standard debug printf due to recursion
    365  1.17  pooka  */
    366   1.1  pooka ssize_t
    367  1.17  pooka write(int fd, const void *buf, size_t blen)
    368   1.1  pooka {
    369  1.17  pooka 	ssize_t (*op_write)(int, const void *, size_t);
    370   1.1  pooka 
    371  1.17  pooka 	if (fd_isrump(fd)) {
    372  1.17  pooka 		fd = fd_host2rump(fd);
    373  1.17  pooka 		op_write = GETSYSCALL(rump, WRITE);
    374  1.16  pooka 	} else {
    375  1.17  pooka 		op_write = GETSYSCALL(host, WRITE);
    376  1.16  pooka 	}
    377   1.1  pooka 
    378  1.17  pooka 	return op_write(fd, buf, blen);
    379   1.2  pooka }
    380   1.2  pooka 
    381   1.2  pooka /*
    382   1.2  pooka  * dup2 is special.  we allow dup2 of a rump kernel fd to 0-2 since
    383   1.2  pooka  * many programs do that.  dup2 of a rump kernel fd to another value
    384   1.2  pooka  * not >= fdoff is an error.
    385   1.2  pooka  *
    386   1.2  pooka  * Note: cannot rump2host newd, because it is often hardcoded.
    387   1.2  pooka  */
    388   1.2  pooka int
    389   1.2  pooka dup2(int oldd, int newd)
    390   1.2  pooka {
    391  1.17  pooka 	int (*host_dup2)(int, int);
    392   1.2  pooka 	int rv;
    393   1.2  pooka 
    394   1.2  pooka 	DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
    395   1.2  pooka 
    396   1.2  pooka 	if (fd_isrump(oldd)) {
    397   1.2  pooka 		if (!(newd >= 0 && newd <= 2))
    398   1.2  pooka 			return EBADF;
    399   1.2  pooka 		oldd = fd_host2rump(oldd);
    400   1.2  pooka 		rv = rump_sys_dup2(oldd, newd);
    401   1.2  pooka 		if (rv != -1)
    402  1.10  pooka 			dup2mask |= 1<<newd;
    403   1.2  pooka 	} else {
    404  1.17  pooka 		host_dup2 = syscalls[DUALCALL_DUP2].bs_host;
    405  1.10  pooka 		rv = host_dup2(oldd, newd);
    406   1.2  pooka 	}
    407  1.10  pooka 
    408  1.10  pooka 	return rv;
    409   1.2  pooka }
    410   1.2  pooka 
    411   1.2  pooka /*
    412   1.2  pooka  * We just wrap fork the appropriate rump client calls to preserve
    413   1.2  pooka  * the file descriptors of the forked parent in the child, but
    414   1.2  pooka  * prevent double use of connection fd.
    415   1.2  pooka  */
    416   1.2  pooka pid_t
    417   1.2  pooka fork()
    418   1.2  pooka {
    419   1.2  pooka 	struct rumpclient_fork *rf;
    420   1.2  pooka 	pid_t rv;
    421   1.2  pooka 
    422   1.2  pooka 	DPRINTF(("fork\n"));
    423   1.2  pooka 
    424   1.2  pooka 	if ((rf = rumpclient_prefork()) == NULL)
    425   1.2  pooka 		return -1;
    426   1.2  pooka 
    427   1.2  pooka 	switch ((rv = host_fork())) {
    428   1.2  pooka 	case -1:
    429   1.2  pooka 		/* XXX: cancel rf */
    430   1.2  pooka 		break;
    431   1.2  pooka 	case 0:
    432   1.2  pooka 		if (rumpclient_fork_init(rf) == -1)
    433   1.2  pooka 			rv = -1;
    434   1.2  pooka 		break;
    435   1.2  pooka 	default:
    436   1.2  pooka 		break;
    437   1.2  pooka 	}
    438   1.2  pooka 
    439   1.2  pooka 	DPRINTF(("fork returns %d\n", rv));
    440   1.2  pooka 	return rv;
    441   1.1  pooka }
    442   1.1  pooka 
    443   1.1  pooka /*
    444  1.17  pooka  * select is done by calling poll.
    445   1.1  pooka  */
    446   1.1  pooka int
    447  1.17  pooka LIBCSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
    448   1.4  pooka 	struct timeval *timeout)
    449   1.1  pooka {
    450   1.4  pooka 	struct pollfd *pfds;
    451   1.4  pooka 	struct timespec ts, *tsp = NULL;
    452  1.19  pooka 	nfds_t realnfds;
    453  1.19  pooka 	int i, j;
    454   1.4  pooka 	int rv, incr;
    455   1.4  pooka 
    456   1.7  pooka 	DPRINTF(("select\n"));
    457   1.7  pooka 
    458   1.4  pooka 	/*
    459   1.4  pooka 	 * Well, first we must scan the fds to figure out how many
    460   1.4  pooka 	 * fds there really are.  This is because up to and including
    461  1.17  pooka 	 * nb5 poll() silently refuses nfds > process_maxopen_fds.
    462   1.4  pooka 	 * Seems to be fixed in current, thank the maker.
    463   1.4  pooka 	 * god damn cluster...bomb.
    464   1.4  pooka 	 */
    465   1.4  pooka 
    466   1.4  pooka 	for (i = 0, realnfds = 0; i < nfds; i++) {
    467   1.4  pooka 		if (readfds && FD_ISSET(i, readfds)) {
    468   1.4  pooka 			realnfds++;
    469   1.4  pooka 			continue;
    470   1.4  pooka 		}
    471   1.4  pooka 		if (writefds && FD_ISSET(i, writefds)) {
    472   1.4  pooka 			realnfds++;
    473   1.4  pooka 			continue;
    474   1.4  pooka 		}
    475   1.4  pooka 		if (exceptfds && FD_ISSET(i, exceptfds)) {
    476   1.4  pooka 			realnfds++;
    477   1.4  pooka 			continue;
    478   1.1  pooka 		}
    479   1.1  pooka 	}
    480   1.1  pooka 
    481   1.6  pooka 	if (realnfds) {
    482   1.6  pooka 		pfds = malloc(sizeof(*pfds) * realnfds);
    483   1.6  pooka 		if (!pfds)
    484   1.6  pooka 			return -1;
    485   1.6  pooka 	} else {
    486   1.6  pooka 		pfds = NULL;
    487   1.6  pooka 	}
    488   1.1  pooka 
    489   1.4  pooka 	for (i = 0, j = 0; i < nfds; i++) {
    490   1.4  pooka 		incr = 0;
    491   1.4  pooka 		pfds[j].events = pfds[j].revents = 0;
    492   1.4  pooka 		if (readfds && FD_ISSET(i, readfds)) {
    493   1.4  pooka 			pfds[j].fd = i;
    494   1.4  pooka 			pfds[j].events |= POLLIN;
    495   1.4  pooka 			incr=1;
    496   1.4  pooka 		}
    497   1.4  pooka 		if (writefds && FD_ISSET(i, writefds)) {
    498   1.4  pooka 			pfds[j].fd = i;
    499   1.4  pooka 			pfds[j].events |= POLLOUT;
    500   1.4  pooka 			incr=1;
    501   1.4  pooka 		}
    502   1.4  pooka 		if (exceptfds && FD_ISSET(i, exceptfds)) {
    503   1.4  pooka 			pfds[j].fd = i;
    504   1.4  pooka 			pfds[j].events |= POLLHUP|POLLERR;
    505   1.4  pooka 			incr=1;
    506   1.1  pooka 		}
    507   1.4  pooka 		if (incr)
    508   1.4  pooka 			j++;
    509   1.1  pooka 	}
    510   1.1  pooka 
    511   1.4  pooka 	if (timeout) {
    512   1.4  pooka 		TIMEVAL_TO_TIMESPEC(timeout, &ts);
    513   1.4  pooka 		tsp = &ts;
    514   1.4  pooka 	}
    515   1.4  pooka 	rv = pollts(pfds, realnfds, tsp, NULL);
    516   1.4  pooka 	if (rv <= 0)
    517   1.4  pooka 		goto out;
    518   1.4  pooka 
    519   1.4  pooka 	/*
    520   1.4  pooka 	 * ok, harvest results.  first zero out entries (can't use
    521   1.4  pooka 	 * FD_ZERO for the obvious select-me-not reason).  whee.
    522   1.4  pooka 	 */
    523   1.4  pooka 	for (i = 0; i < nfds; i++) {
    524   1.4  pooka 		if (readfds)
    525   1.4  pooka 			FD_CLR(i, readfds);
    526   1.4  pooka 		if (writefds)
    527   1.4  pooka 			FD_CLR(i, writefds);
    528   1.4  pooka 		if (exceptfds)
    529   1.4  pooka 			FD_CLR(i, exceptfds);
    530   1.1  pooka 	}
    531   1.1  pooka 
    532   1.4  pooka 	/* and then plug in the results */
    533  1.19  pooka 	for (i = 0; i < (int)realnfds; i++) {
    534   1.4  pooka 		if (readfds) {
    535   1.4  pooka 			if (pfds[i].revents & POLLIN) {
    536   1.4  pooka 				FD_SET(pfds[i].fd, readfds);
    537   1.4  pooka 			}
    538   1.4  pooka 		}
    539   1.4  pooka 		if (writefds) {
    540   1.4  pooka 			if (pfds[i].revents & POLLOUT) {
    541   1.4  pooka 				FD_SET(pfds[i].fd, writefds);
    542   1.4  pooka 			}
    543   1.4  pooka 		}
    544   1.4  pooka 		if (exceptfds) {
    545   1.4  pooka 			if (pfds[i].revents & (POLLHUP|POLLERR)) {
    546   1.4  pooka 				FD_SET(pfds[i].fd, exceptfds);
    547   1.4  pooka 			}
    548   1.4  pooka 		}
    549   1.1  pooka 	}
    550   1.1  pooka 
    551   1.4  pooka  out:
    552   1.4  pooka 	free(pfds);
    553   1.1  pooka 	return rv;
    554   1.1  pooka }
    555   1.1  pooka 
    556   1.1  pooka static void
    557   1.1  pooka checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
    558   1.1  pooka {
    559   1.1  pooka 	nfds_t i;
    560   1.1  pooka 
    561   1.1  pooka 	for (i = 0; i < nfds; i++) {
    562  1.12  pooka 		if (fds[i].fd == -1)
    563  1.12  pooka 			continue;
    564  1.12  pooka 
    565   1.2  pooka 		if (fd_isrump(fds[i].fd))
    566   1.2  pooka 			(*rumpcall)++;
    567   1.2  pooka 		else
    568   1.1  pooka 			(*hostcall)++;
    569   1.1  pooka 	}
    570   1.1  pooka }
    571   1.1  pooka 
    572   1.1  pooka static void
    573   1.2  pooka adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
    574   1.1  pooka {
    575   1.1  pooka 	nfds_t i;
    576   1.1  pooka 
    577   1.1  pooka 	for (i = 0; i < nfds; i++) {
    578   1.2  pooka 		fds[i].fd = fdadj(fds[i].fd);
    579   1.1  pooka 	}
    580   1.1  pooka }
    581   1.1  pooka 
    582   1.1  pooka /*
    583   1.1  pooka  * poll is easy as long as the call comes in the fds only in one
    584   1.1  pooka  * kernel.  otherwise its quite tricky...
    585   1.1  pooka  */
    586   1.1  pooka struct pollarg {
    587   1.1  pooka 	struct pollfd *pfds;
    588   1.1  pooka 	nfds_t nfds;
    589   1.3  pooka 	const struct timespec *ts;
    590   1.3  pooka 	const sigset_t *sigmask;
    591   1.1  pooka 	int pipefd;
    592   1.1  pooka 	int errnum;
    593   1.1  pooka };
    594   1.1  pooka 
    595   1.1  pooka static void *
    596   1.1  pooka hostpoll(void *arg)
    597   1.1  pooka {
    598  1.17  pooka 	int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
    599  1.17  pooka 			 const sigset_t *);
    600   1.1  pooka 	struct pollarg *parg = arg;
    601   1.1  pooka 	intptr_t rv;
    602   1.1  pooka 
    603  1.17  pooka 	op_pollts = syscalls[DUALCALL_POLLTS].bs_host;
    604  1.17  pooka 	rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
    605   1.1  pooka 	if (rv == -1)
    606   1.1  pooka 		parg->errnum = errno;
    607   1.1  pooka 	rump_sys_write(parg->pipefd, &rv, sizeof(rv));
    608   1.1  pooka 
    609   1.1  pooka 	return (void *)(intptr_t)rv;
    610   1.1  pooka }
    611   1.1  pooka 
    612   1.1  pooka int
    613  1.17  pooka LIBCPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
    614   1.3  pooka 	const sigset_t *sigmask)
    615   1.1  pooka {
    616   1.3  pooka 	int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
    617   1.3  pooka 			 const sigset_t *);
    618  1.17  pooka 	int (*host_close)(int);
    619   1.1  pooka 	int hostcall = 0, rumpcall = 0;
    620   1.1  pooka 	pthread_t pt;
    621   1.1  pooka 	nfds_t i;
    622   1.1  pooka 	int rv;
    623   1.1  pooka 
    624   1.2  pooka 	DPRINTF(("poll\n"));
    625   1.1  pooka 	checkpoll(fds, nfds, &hostcall, &rumpcall);
    626   1.1  pooka 
    627   1.1  pooka 	if (hostcall && rumpcall) {
    628   1.1  pooka 		struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
    629   1.1  pooka 		int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
    630   1.1  pooka 		struct pollarg parg;
    631   1.1  pooka 		uintptr_t lrv;
    632   1.1  pooka 		int sverrno = 0, trv;
    633   1.1  pooka 
    634   1.1  pooka 		/*
    635   1.1  pooka 		 * ok, this is where it gets tricky.  We must support
    636   1.1  pooka 		 * this since it's a very common operation in certain
    637   1.1  pooka 		 * types of software (telnet, netcat, etc).  We allocate
    638   1.1  pooka 		 * two vectors and run two poll commands in separate
    639   1.1  pooka 		 * threads.  Whichever returns first "wins" and the
    640   1.1  pooka 		 * other kernel's fds won't show activity.
    641   1.1  pooka 		 */
    642   1.1  pooka 		rv = -1;
    643   1.1  pooka 
    644   1.1  pooka 		/* allocate full vector for O(n) joining after call */
    645   1.1  pooka 		pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
    646   1.1  pooka 		if (!pfd_host)
    647   1.1  pooka 			goto out;
    648   1.1  pooka 		pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
    649   1.1  pooka 		if (!pfd_rump) {
    650   1.1  pooka 			goto out;
    651   1.1  pooka 		}
    652   1.1  pooka 
    653   1.1  pooka 		/* split vectors */
    654   1.1  pooka 		for (i = 0; i < nfds; i++) {
    655   1.3  pooka 			if (fds[i].fd == -1) {
    656   1.3  pooka 				pfd_host[i].fd = -1;
    657   1.3  pooka 				pfd_rump[i].fd = -1;
    658   1.3  pooka 			} else if (fd_isrump(fds[i].fd)) {
    659   1.2  pooka 				pfd_host[i].fd = -1;
    660   1.2  pooka 				pfd_rump[i].fd = fd_host2rump(fds[i].fd);
    661   1.2  pooka 				pfd_rump[i].events = fds[i].events;
    662   1.2  pooka 			} else {
    663   1.2  pooka 				pfd_rump[i].fd = -1;
    664   1.1  pooka 				pfd_host[i].fd = fds[i].fd;
    665   1.1  pooka 				pfd_host[i].events = fds[i].events;
    666   1.1  pooka 			}
    667  1.13  pooka 			fds[i].revents = 0;
    668   1.1  pooka 		}
    669   1.1  pooka 
    670   1.1  pooka 		/*
    671   1.1  pooka 		 * then, open two pipes, one for notifications
    672   1.1  pooka 		 * to each kernel.
    673   1.1  pooka 		 */
    674   1.1  pooka 		if (rump_sys_pipe(rpipe) == -1)
    675   1.1  pooka 			goto out;
    676   1.1  pooka 		if (pipe(hpipe) == -1)
    677   1.1  pooka 			goto out;
    678   1.1  pooka 
    679   1.1  pooka 		pfd_host[nfds].fd = hpipe[0];
    680   1.1  pooka 		pfd_host[nfds].events = POLLIN;
    681   1.1  pooka 		pfd_rump[nfds].fd = rpipe[0];
    682   1.1  pooka 		pfd_rump[nfds].events = POLLIN;
    683   1.1  pooka 
    684   1.1  pooka 		/*
    685   1.1  pooka 		 * then, create a thread to do host part and meanwhile
    686   1.1  pooka 		 * do rump kernel part right here
    687   1.1  pooka 		 */
    688   1.1  pooka 
    689   1.1  pooka 		parg.pfds = pfd_host;
    690   1.1  pooka 		parg.nfds = nfds+1;
    691   1.3  pooka 		parg.ts = ts;
    692   1.3  pooka 		parg.sigmask = sigmask;
    693   1.1  pooka 		parg.pipefd = rpipe[1];
    694   1.1  pooka 		pthread_create(&pt, NULL, hostpoll, &parg);
    695   1.1  pooka 
    696  1.17  pooka 		op_pollts = syscalls[DUALCALL_POLLTS].bs_rump;
    697   1.3  pooka 		lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
    698   1.1  pooka 		sverrno = errno;
    699   1.1  pooka 		write(hpipe[1], &rv, sizeof(rv));
    700   1.1  pooka 		pthread_join(pt, (void *)&trv);
    701   1.1  pooka 
    702   1.1  pooka 		/* check who "won" and merge results */
    703   1.1  pooka 		if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
    704   1.1  pooka 			rv = trv;
    705   1.1  pooka 
    706   1.1  pooka 			for (i = 0; i < nfds; i++) {
    707   1.1  pooka 				if (pfd_rump[i].fd != -1)
    708   1.1  pooka 					fds[i].revents = pfd_rump[i].revents;
    709   1.1  pooka 			}
    710   1.1  pooka 			sverrno = parg.errnum;
    711   1.1  pooka 		} else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
    712   1.1  pooka 			rv = trv;
    713   1.1  pooka 
    714   1.1  pooka 			for (i = 0; i < nfds; i++) {
    715   1.1  pooka 				if (pfd_host[i].fd != -1)
    716   1.1  pooka 					fds[i].revents = pfd_host[i].revents;
    717   1.1  pooka 			}
    718   1.1  pooka 		} else {
    719   1.1  pooka 			rv = 0;
    720   1.1  pooka 		}
    721   1.1  pooka 
    722   1.1  pooka  out:
    723  1.17  pooka 		host_close = syscalls[DUALCALL_CLOSE].bs_host;
    724   1.1  pooka 		if (rpipe[0] != -1)
    725   1.1  pooka 			rump_sys_close(rpipe[0]);
    726   1.1  pooka 		if (rpipe[1] != -1)
    727   1.1  pooka 			rump_sys_close(rpipe[1]);
    728   1.1  pooka 		if (hpipe[0] != -1)
    729   1.9  pooka 			host_close(hpipe[0]);
    730   1.1  pooka 		if (hpipe[1] != -1)
    731   1.9  pooka 			host_close(hpipe[1]);
    732   1.1  pooka 		free(pfd_host);
    733   1.1  pooka 		free(pfd_rump);
    734   1.1  pooka 		errno = sverrno;
    735   1.1  pooka 	} else {
    736   1.1  pooka 		if (hostcall) {
    737  1.17  pooka 			op_pollts = syscalls[DUALCALL_POLLTS].bs_host;
    738   1.1  pooka 		} else {
    739  1.17  pooka 			op_pollts = syscalls[DUALCALL_POLLTS].bs_rump;
    740   1.2  pooka 			adjustpoll(fds, nfds, fd_host2rump);
    741   1.1  pooka 		}
    742   1.1  pooka 
    743   1.3  pooka 		rv = op_pollts(fds, nfds, ts, sigmask);
    744   1.1  pooka 		if (rumpcall)
    745   1.2  pooka 			adjustpoll(fds, nfds, fd_rump2host);
    746   1.1  pooka 	}
    747   1.1  pooka 
    748   1.1  pooka 	return rv;
    749   1.1  pooka }
    750   1.1  pooka 
    751   1.1  pooka int
    752  1.17  pooka LIBCPOLL(struct pollfd *fds, nfds_t nfds, int timeout)
    753   1.1  pooka {
    754   1.3  pooka 	struct timespec ts;
    755   1.3  pooka 	struct timespec *tsp = NULL;
    756   1.3  pooka 
    757   1.3  pooka 	if (timeout != INFTIM) {
    758   1.3  pooka 		ts.tv_sec = timeout / 1000;
    759  1.11  pooka 		ts.tv_nsec = (timeout % 1000) * 1000*1000;
    760   1.3  pooka 
    761   1.3  pooka 		tsp = &ts;
    762   1.3  pooka 	}
    763   1.1  pooka 
    764   1.3  pooka 	return pollts(fds, nfds, tsp, NULL);
    765   1.1  pooka }
    766  1.10  pooka 
    767  1.10  pooka int
    768  1.10  pooka kqueue(void)
    769  1.10  pooka {
    770  1.10  pooka 
    771  1.17  pooka 	fprintf(stderr, "kqueue unsupported");
    772  1.10  pooka 	abort();
    773  1.17  pooka 	/*NOTREACHED*/
    774  1.10  pooka }
    775  1.10  pooka 
    776  1.17  pooka /*ARGSUSED*/
    777  1.10  pooka int
    778  1.10  pooka kevent(int kq, const struct kevent *changelist, size_t nchanges,
    779  1.10  pooka 	struct kevent *eventlist, size_t nevents,
    780  1.10  pooka 	const struct timespec *timeout)
    781  1.10  pooka {
    782  1.10  pooka 
    783  1.17  pooka 	fprintf(stderr, "kqueue unsupported");
    784  1.10  pooka 	abort();
    785  1.17  pooka 	/*NOTREACHED*/
    786  1.10  pooka }
    787  1.17  pooka 
    788  1.17  pooka /*
    789  1.17  pooka  * Rest are std type calls.
    790  1.17  pooka  */
    791  1.17  pooka 
    792  1.17  pooka FDCALL(int, bind, DUALCALL_BIND,					\
    793  1.17  pooka 	(int fd, const struct sockaddr *name, socklen_t namelen),	\
    794  1.17  pooka 	(int, const struct sockaddr *, socklen_t),			\
    795  1.17  pooka 	(fd, name, namelen))
    796  1.17  pooka 
    797  1.17  pooka FDCALL(int, connect, DUALCALL_CONNECT,					\
    798  1.17  pooka 	(int fd, const struct sockaddr *name, socklen_t namelen),	\
    799  1.17  pooka 	(int, const struct sockaddr *, socklen_t),			\
    800  1.17  pooka 	(fd, name, namelen))
    801  1.17  pooka 
    802  1.17  pooka FDCALL(int, getpeername, DUALCALL_GETPEERNAME,				\
    803  1.17  pooka 	(int fd, struct sockaddr *name, socklen_t *namelen),		\
    804  1.17  pooka 	(int, struct sockaddr *, socklen_t *),				\
    805  1.17  pooka 	(fd, name, namelen))
    806  1.17  pooka 
    807  1.17  pooka FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, 				\
    808  1.17  pooka 	(int fd, struct sockaddr *name, socklen_t *namelen),		\
    809  1.17  pooka 	(int, struct sockaddr *, socklen_t *),				\
    810  1.17  pooka 	(fd, name, namelen))
    811  1.17  pooka 
    812  1.17  pooka FDCALL(int, listen, DUALCALL_LISTEN,	 				\
    813  1.17  pooka 	(int fd, int backlog),						\
    814  1.17  pooka 	(int, int),							\
    815  1.17  pooka 	(fd, backlog))
    816  1.17  pooka 
    817  1.17  pooka FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, 				\
    818  1.17  pooka 	(int fd, void *buf, size_t len, int flags,			\
    819  1.17  pooka 	    struct sockaddr *from, socklen_t *fromlen),			\
    820  1.17  pooka 	(int, void *, size_t, int, struct sockaddr *, socklen_t *),	\
    821  1.17  pooka 	(fd, buf, len, flags, from, fromlen))
    822  1.17  pooka 
    823  1.17  pooka FDCALL(ssize_t, sendto, DUALCALL_SENDTO, 				\
    824  1.17  pooka 	(int fd, const void *buf, size_t len, int flags,		\
    825  1.17  pooka 	    const struct sockaddr *to, socklen_t tolen),		\
    826  1.17  pooka 	(int, const void *, size_t, int,				\
    827  1.17  pooka 	    const struct sockaddr *, socklen_t),			\
    828  1.17  pooka 	(fd, buf, len, flags, to, tolen))
    829  1.17  pooka 
    830  1.17  pooka FDCALL(ssize_t, recvmsg, DUALCALL_RECVMSG, 				\
    831  1.17  pooka 	(int fd, struct msghdr *msg, int flags),			\
    832  1.17  pooka 	(int, struct msghdr *, int),					\
    833  1.17  pooka 	(fd, msg, flags))
    834  1.17  pooka 
    835  1.17  pooka FDCALL(ssize_t, sendmsg, DUALCALL_SENDMSG, 				\
    836  1.17  pooka 	(int fd, const struct msghdr *msg, int flags),			\
    837  1.17  pooka 	(int, const struct msghdr *, int),				\
    838  1.17  pooka 	(fd, msg, flags))
    839  1.17  pooka 
    840  1.17  pooka FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, 				\
    841  1.17  pooka 	(int fd, int level, int optn, void *optval, socklen_t *optlen),	\
    842  1.17  pooka 	(int, int, int, void *, socklen_t *),				\
    843  1.17  pooka 	(fd, level, optn, optval, optlen))
    844  1.17  pooka 
    845  1.17  pooka FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, 				\
    846  1.17  pooka 	(int fd, int level, int optn,					\
    847  1.17  pooka 	    const void *optval, socklen_t optlen),			\
    848  1.17  pooka 	(int, int, int, const void *, socklen_t),			\
    849  1.17  pooka 	(fd, level, optn, optval, optlen))
    850  1.17  pooka 
    851  1.17  pooka FDCALL(int, shutdown, DUALCALL_SHUTDOWN, 				\
    852  1.17  pooka 	(int fd, int how),						\
    853  1.17  pooka 	(int, int),							\
    854  1.17  pooka 	(fd, how))
    855  1.17  pooka 
    856  1.17  pooka FDCALL(ssize_t, read, DUALCALL_READ,	 				\
    857  1.17  pooka 	(int fd, void *buf, size_t buflen),				\
    858  1.17  pooka 	(int, void *, size_t),						\
    859  1.17  pooka 	(fd, buf, buflen))
    860  1.17  pooka 
    861  1.18  pooka FDCALL(ssize_t, readv, DUALCALL_READV, 					\
    862  1.17  pooka 	(int fd, const struct iovec *iov, int iovcnt),			\
    863  1.17  pooka 	(int, const struct iovec *, int),				\
    864  1.17  pooka 	(fd, iov, iovcnt))
    865  1.17  pooka 
    866  1.17  pooka FDCALL(ssize_t, writev, DUALCALL_WRITEV, 				\
    867  1.17  pooka 	(int fd, const struct iovec *iov, int iovcnt),			\
    868  1.17  pooka 	(int, const struct iovec *, int),				\
    869  1.17  pooka 	(fd, iov, iovcnt))
    870  1.17  pooka 
    871  1.17  pooka FDCALL(int, close, DUALCALL_CLOSE,	 				\
    872  1.17  pooka 	(int fd),							\
    873  1.17  pooka 	(int),								\
    874  1.17  pooka 	(fd))
    875