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