Home | History | Annotate | Line # | Download | only in librumpclient
rumpclient.c revision 1.16.2.1
      1  1.16.2.1  bouyer /*      $NetBSD: rumpclient.c,v 1.16.2.1 2011/02/08 16:19:04 bouyer Exp $	*/
      2       1.1   pooka 
      3       1.1   pooka /*
      4      1.11   pooka  * Copyright (c) 2010, 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 /*
     29       1.1   pooka  * Client side routines for rump syscall proxy.
     30       1.1   pooka  */
     31       1.1   pooka 
     32       1.1   pooka #include <sys/cdefs.h>
     33       1.1   pooka __RCSID("$NetBSD");
     34       1.1   pooka 
     35       1.5   pooka #include <sys/param.h>
     36      1.15   pooka #include <sys/event.h>
     37       1.1   pooka #include <sys/mman.h>
     38       1.1   pooka #include <sys/socket.h>
     39       1.1   pooka 
     40       1.1   pooka #include <arpa/inet.h>
     41       1.1   pooka #include <netinet/in.h>
     42       1.1   pooka #include <netinet/tcp.h>
     43       1.1   pooka 
     44       1.1   pooka #include <assert.h>
     45      1.13   pooka #include <dlfcn.h>
     46       1.1   pooka #include <errno.h>
     47       1.1   pooka #include <fcntl.h>
     48      1.13   pooka #include <link.h>
     49       1.1   pooka #include <poll.h>
     50       1.1   pooka #include <pthread.h>
     51      1.11   pooka #include <signal.h>
     52       1.1   pooka #include <stdarg.h>
     53  1.16.2.1  bouyer #include <stdbool.h>
     54       1.1   pooka #include <stdio.h>
     55       1.1   pooka #include <stdlib.h>
     56       1.1   pooka #include <string.h>
     57       1.1   pooka #include <unistd.h>
     58       1.1   pooka 
     59       1.1   pooka #include <rump/rumpclient.h>
     60       1.1   pooka 
     61      1.13   pooka #define HOSTOPS
     62      1.13   pooka int	(*host_socket)(int, int, int);
     63      1.13   pooka int	(*host_close)(int);
     64      1.13   pooka int	(*host_connect)(int, const struct sockaddr *, socklen_t);
     65      1.15   pooka int	(*host_fcntl)(int, int, ...);
     66      1.13   pooka int	(*host_poll)(struct pollfd *, nfds_t, int);
     67      1.13   pooka ssize_t	(*host_read)(int, void *, size_t);
     68      1.13   pooka ssize_t (*host_sendto)(int, const void *, size_t, int,
     69      1.13   pooka 		       const struct sockaddr *, socklen_t);
     70      1.13   pooka int	(*host_setsockopt)(int, int, int, const void *, socklen_t);
     71      1.13   pooka 
     72  1.16.2.1  bouyer int	(*host_kqueue)(void);
     73  1.16.2.1  bouyer int	(*host_kevent)(int, const struct kevent *, size_t,
     74  1.16.2.1  bouyer 		       struct kevent *, size_t, const struct timespec *);
     75  1.16.2.1  bouyer 
     76       1.1   pooka #include "sp_common.c"
     77       1.1   pooka 
     78      1.11   pooka static struct spclient clispc = {
     79      1.11   pooka 	.spc_fd = -1,
     80      1.11   pooka };
     81       1.1   pooka 
     82  1.16.2.1  bouyer static int kq = -1;
     83      1.15   pooka static sigset_t fullset;
     84      1.12   pooka 
     85  1.16.2.1  bouyer static int doconnect(bool);
     86  1.16.2.1  bouyer static int handshake_req(struct spclient *, uint32_t *, int, bool);
     87  1.16.2.1  bouyer 
     88  1.16.2.1  bouyer time_t retrytimo = RUMPCLIENT_RETRYCONN_ONCE;
     89  1.16.2.1  bouyer 
     90      1.12   pooka static int
     91  1.16.2.1  bouyer send_with_recon(struct spclient *spc, const void *data, size_t dlen)
     92      1.12   pooka {
     93  1.16.2.1  bouyer 	struct timeval starttime, curtime;
     94  1.16.2.1  bouyer 	time_t prevreconmsg;
     95  1.16.2.1  bouyer 	unsigned reconretries;
     96  1.16.2.1  bouyer 	int rv;
     97  1.16.2.1  bouyer 
     98  1.16.2.1  bouyer 	for (prevreconmsg = 0, reconretries = 0;;) {
     99  1.16.2.1  bouyer 		rv = dosend(spc, data, dlen);
    100  1.16.2.1  bouyer 		if (__predict_false(rv == ENOTCONN || rv == EBADF)) {
    101  1.16.2.1  bouyer 			/* no persistent connections */
    102  1.16.2.1  bouyer 			if (retrytimo == 0)
    103  1.16.2.1  bouyer 				break;
    104  1.16.2.1  bouyer 			if (retrytimo == RUMPCLIENT_RETRYCONN_DIE)
    105  1.16.2.1  bouyer 				exit(1);
    106  1.16.2.1  bouyer 
    107  1.16.2.1  bouyer 			if (!prevreconmsg) {
    108  1.16.2.1  bouyer 				prevreconmsg = time(NULL);
    109  1.16.2.1  bouyer 				gettimeofday(&starttime, NULL);
    110  1.16.2.1  bouyer 			}
    111  1.16.2.1  bouyer 			if (reconretries == 1) {
    112  1.16.2.1  bouyer 				if (retrytimo == RUMPCLIENT_RETRYCONN_ONCE) {
    113  1.16.2.1  bouyer 					rv = ENOTCONN;
    114  1.16.2.1  bouyer 					break;
    115  1.16.2.1  bouyer 				}
    116  1.16.2.1  bouyer 				fprintf(stderr, "rump_sp: connection to "
    117  1.16.2.1  bouyer 				    "kernel lost, trying to reconnect ...\n");
    118  1.16.2.1  bouyer 			} else if (time(NULL) - prevreconmsg > 120) {
    119  1.16.2.1  bouyer 				fprintf(stderr, "rump_sp: still trying to "
    120  1.16.2.1  bouyer 				    "reconnect ...\n");
    121  1.16.2.1  bouyer 				prevreconmsg = time(NULL);
    122  1.16.2.1  bouyer 			}
    123  1.16.2.1  bouyer 
    124  1.16.2.1  bouyer 			/* check that we aren't over the limit */
    125  1.16.2.1  bouyer 			if (retrytimo > 0) {
    126  1.16.2.1  bouyer 				struct timeval tmp;
    127  1.16.2.1  bouyer 
    128  1.16.2.1  bouyer 				gettimeofday(&curtime, NULL);
    129  1.16.2.1  bouyer 				timersub(&curtime, &starttime, &tmp);
    130  1.16.2.1  bouyer 				if (tmp.tv_sec >= retrytimo) {
    131  1.16.2.1  bouyer 					fprintf(stderr, "rump_sp: reconnect "
    132  1.16.2.1  bouyer 					    "failed, %lld second timeout\n",
    133  1.16.2.1  bouyer 					    (long long)retrytimo);
    134  1.16.2.1  bouyer 					return ENOTCONN;
    135  1.16.2.1  bouyer 				}
    136  1.16.2.1  bouyer 			}
    137  1.16.2.1  bouyer 
    138  1.16.2.1  bouyer 			/* adhoc backoff timer */
    139  1.16.2.1  bouyer 			if (reconretries < 10) {
    140  1.16.2.1  bouyer 				usleep(100000 * reconretries);
    141  1.16.2.1  bouyer 			} else {
    142  1.16.2.1  bouyer 				sleep(MIN(10, reconretries-9));
    143  1.16.2.1  bouyer 			}
    144  1.16.2.1  bouyer 			reconretries++;
    145  1.16.2.1  bouyer 
    146  1.16.2.1  bouyer 			if ((rv = doconnect(false)) != 0)
    147  1.16.2.1  bouyer 				continue;
    148  1.16.2.1  bouyer 			if ((rv = handshake_req(&clispc, NULL, 0, true)) != 0)
    149  1.16.2.1  bouyer 				continue;
    150  1.16.2.1  bouyer 
    151  1.16.2.1  bouyer 			/*
    152  1.16.2.1  bouyer 			 * ok, reconnect succesful.  we need to return to
    153  1.16.2.1  bouyer 			 * the upper layer to get the entire PDU resent.
    154  1.16.2.1  bouyer 			 */
    155  1.16.2.1  bouyer 			if (reconretries != 1)
    156  1.16.2.1  bouyer 				fprintf(stderr, "rump_sp: reconnected!\n");
    157  1.16.2.1  bouyer 			rv = EAGAIN;
    158  1.16.2.1  bouyer 			break;
    159  1.16.2.1  bouyer 		} else {
    160  1.16.2.1  bouyer 			_DIAGASSERT(errno != EAGAIN);
    161  1.16.2.1  bouyer 			break;
    162  1.16.2.1  bouyer 		}
    163  1.16.2.1  bouyer 	}
    164  1.16.2.1  bouyer 
    165  1.16.2.1  bouyer 	return rv;
    166  1.16.2.1  bouyer }
    167  1.16.2.1  bouyer 
    168  1.16.2.1  bouyer static int
    169  1.16.2.1  bouyer cliwaitresp(struct spclient *spc, struct respwait *rw, sigset_t *mask,
    170  1.16.2.1  bouyer 	bool keeplock)
    171  1.16.2.1  bouyer {
    172  1.16.2.1  bouyer 	uint64_t mygen;
    173  1.16.2.1  bouyer 	bool imalive = true;
    174      1.12   pooka 
    175      1.15   pooka 	pthread_mutex_lock(&spc->spc_mtx);
    176  1.16.2.1  bouyer 	if (!keeplock)
    177  1.16.2.1  bouyer 		sendunlockl(spc);
    178  1.16.2.1  bouyer 	mygen = spc->spc_generation;
    179      1.12   pooka 
    180      1.12   pooka 	rw->rw_error = 0;
    181  1.16.2.1  bouyer 	while (!rw->rw_done && rw->rw_error == 0) {
    182  1.16.2.1  bouyer 		if (__predict_false(spc->spc_generation != mygen || !imalive))
    183  1.16.2.1  bouyer 			break;
    184  1.16.2.1  bouyer 
    185      1.12   pooka 		/* are we free to receive? */
    186      1.12   pooka 		if (spc->spc_istatus == SPCSTATUS_FREE) {
    187      1.15   pooka 			struct kevent kev[8];
    188      1.15   pooka 			int gotresp, dosig, rv, i;
    189      1.15   pooka 
    190      1.12   pooka 			spc->spc_istatus = SPCSTATUS_BUSY;
    191      1.12   pooka 			pthread_mutex_unlock(&spc->spc_mtx);
    192      1.12   pooka 
    193      1.15   pooka 			dosig = 0;
    194      1.15   pooka 			for (gotresp = 0; !gotresp; ) {
    195      1.15   pooka 				switch (readframe(spc)) {
    196      1.15   pooka 				case 0:
    197  1.16.2.1  bouyer 					rv = host_kevent(kq, NULL, 0,
    198      1.15   pooka 					    kev, __arraycount(kev), NULL);
    199  1.16.2.1  bouyer 
    200  1.16.2.1  bouyer 					/*
    201  1.16.2.1  bouyer 					 * XXX: don't know how this can
    202  1.16.2.1  bouyer 					 * happen (timeout cannot expire
    203  1.16.2.1  bouyer 					 * since there isn't one), but
    204  1.16.2.1  bouyer 					 * it does happen
    205  1.16.2.1  bouyer 					 */
    206  1.16.2.1  bouyer 					if (__predict_false(rv == 0))
    207  1.16.2.1  bouyer 						continue;
    208  1.16.2.1  bouyer 
    209      1.15   pooka 					for (i = 0; i < rv; i++) {
    210      1.15   pooka 						if (kev[i].filter
    211      1.15   pooka 						    == EVFILT_SIGNAL)
    212      1.15   pooka 							dosig++;
    213      1.15   pooka 					}
    214      1.15   pooka 					if (dosig)
    215      1.15   pooka 						goto cleanup;
    216      1.15   pooka 
    217      1.15   pooka 					continue;
    218      1.15   pooka 				case -1:
    219  1.16.2.1  bouyer 					imalive = false;
    220      1.15   pooka 					goto cleanup;
    221      1.15   pooka 				default:
    222      1.15   pooka 					break;
    223      1.15   pooka 				}
    224      1.12   pooka 
    225      1.15   pooka 				switch (spc->spc_hdr.rsp_class) {
    226      1.12   pooka 				case RUMPSP_RESP:
    227      1.12   pooka 				case RUMPSP_ERROR:
    228      1.12   pooka 					kickwaiter(spc);
    229      1.15   pooka 					gotresp = spc->spc_hdr.rsp_reqno ==
    230      1.15   pooka 					    rw->rw_reqno;
    231      1.12   pooka 					break;
    232      1.12   pooka 				case RUMPSP_REQ:
    233      1.12   pooka 					handlereq(spc);
    234      1.12   pooka 					break;
    235      1.12   pooka 				default:
    236      1.12   pooka 					/* panic */
    237      1.12   pooka 					break;
    238      1.15   pooka 				}
    239      1.12   pooka 			}
    240      1.12   pooka 
    241      1.15   pooka  cleanup:
    242      1.15   pooka 			pthread_mutex_lock(&spc->spc_mtx);
    243      1.15   pooka 			if (spc->spc_istatus == SPCSTATUS_WANTED)
    244      1.15   pooka 				kickall(spc);
    245      1.15   pooka 			spc->spc_istatus = SPCSTATUS_FREE;
    246      1.15   pooka 
    247      1.15   pooka 			/* take one for the team */
    248      1.15   pooka 			if (dosig) {
    249      1.15   pooka 				pthread_mutex_unlock(&spc->spc_mtx);
    250      1.15   pooka 				pthread_sigmask(SIG_SETMASK, mask, NULL);
    251      1.15   pooka 				pthread_sigmask(SIG_SETMASK, &fullset, NULL);
    252      1.15   pooka 				pthread_mutex_lock(&spc->spc_mtx);
    253      1.15   pooka 			}
    254      1.12   pooka 		} else {
    255      1.12   pooka 			spc->spc_istatus = SPCSTATUS_WANTED;
    256      1.12   pooka 			pthread_cond_wait(&rw->rw_cv, &spc->spc_mtx);
    257      1.12   pooka 		}
    258      1.12   pooka 	}
    259      1.12   pooka 	TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
    260      1.12   pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    261      1.12   pooka 	pthread_cond_destroy(&rw->rw_cv);
    262      1.12   pooka 
    263  1.16.2.1  bouyer 	if (spc->spc_generation != mygen || !imalive) {
    264      1.12   pooka 		return ENOTCONN;
    265  1.16.2.1  bouyer 	}
    266      1.12   pooka 	return rw->rw_error;
    267      1.12   pooka }
    268      1.12   pooka 
    269       1.1   pooka static int
    270  1.16.2.1  bouyer syscall_req(struct spclient *spc, sigset_t *omask, int sysnum,
    271       1.3   pooka 	const void *data, size_t dlen, void **resp)
    272       1.1   pooka {
    273       1.1   pooka 	struct rsp_hdr rhdr;
    274       1.3   pooka 	struct respwait rw;
    275       1.3   pooka 	int rv;
    276       1.1   pooka 
    277       1.1   pooka 	rhdr.rsp_len = sizeof(rhdr) + dlen;
    278       1.3   pooka 	rhdr.rsp_class = RUMPSP_REQ;
    279       1.3   pooka 	rhdr.rsp_type = RUMPSP_SYSCALL;
    280       1.1   pooka 	rhdr.rsp_sysnum = sysnum;
    281       1.1   pooka 
    282       1.6   pooka 	do {
    283       1.6   pooka 		putwait(spc, &rw, &rhdr);
    284  1.16.2.1  bouyer 		if ((rv = send_with_recon(spc, &rhdr, sizeof(rhdr))) != 0) {
    285  1.16.2.1  bouyer 			unputwait(spc, &rw);
    286  1.16.2.1  bouyer 			continue;
    287  1.16.2.1  bouyer 		}
    288  1.16.2.1  bouyer 		if ((rv = send_with_recon(spc, data, dlen)) != 0) {
    289       1.6   pooka 			unputwait(spc, &rw);
    290  1.16.2.1  bouyer 			continue;
    291       1.6   pooka 		}
    292       1.6   pooka 
    293  1.16.2.1  bouyer 		rv = cliwaitresp(spc, &rw, omask, false);
    294  1.16.2.1  bouyer 		if (rv == ENOTCONN)
    295  1.16.2.1  bouyer 			rv = EAGAIN;
    296       1.6   pooka 	} while (rv == EAGAIN);
    297       1.3   pooka 
    298       1.3   pooka 	*resp = rw.rw_data;
    299       1.3   pooka 	return rv;
    300       1.1   pooka }
    301       1.1   pooka 
    302       1.1   pooka static int
    303  1.16.2.1  bouyer handshake_req(struct spclient *spc, uint32_t *auth, int cancel, bool haslock)
    304      1.10   pooka {
    305      1.11   pooka 	struct handshake_fork rf;
    306      1.10   pooka 	struct rsp_hdr rhdr;
    307      1.10   pooka 	struct respwait rw;
    308      1.12   pooka 	sigset_t omask;
    309  1.16.2.1  bouyer 	size_t bonus;
    310      1.10   pooka 	int rv;
    311      1.10   pooka 
    312  1.16.2.1  bouyer 	if (auth) {
    313  1.16.2.1  bouyer 		bonus = sizeof(rf);
    314  1.16.2.1  bouyer 	} else {
    315  1.16.2.1  bouyer 		bonus = strlen(getprogname())+1;
    316  1.16.2.1  bouyer 	}
    317  1.16.2.1  bouyer 
    318      1.10   pooka 	/* performs server handshake */
    319  1.16.2.1  bouyer 	rhdr.rsp_len = sizeof(rhdr) + bonus;
    320      1.10   pooka 	rhdr.rsp_class = RUMPSP_REQ;
    321      1.10   pooka 	rhdr.rsp_type = RUMPSP_HANDSHAKE;
    322      1.11   pooka 	if (auth)
    323      1.11   pooka 		rhdr.rsp_handshake = HANDSHAKE_FORK;
    324      1.11   pooka 	else
    325      1.11   pooka 		rhdr.rsp_handshake = HANDSHAKE_GUEST;
    326      1.10   pooka 
    327      1.12   pooka 	pthread_sigmask(SIG_SETMASK, &fullset, &omask);
    328  1.16.2.1  bouyer 	if (haslock)
    329  1.16.2.1  bouyer 		putwait_locked(spc, &rw, &rhdr);
    330  1.16.2.1  bouyer 	else
    331  1.16.2.1  bouyer 		putwait(spc, &rw, &rhdr);
    332      1.10   pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    333      1.11   pooka 	if (auth) {
    334      1.11   pooka 		memcpy(rf.rf_auth, auth, AUTHLEN*sizeof(*auth));
    335      1.11   pooka 		rf.rf_cancel = cancel;
    336  1.16.2.1  bouyer 		rv = send_with_recon(spc, &rf, sizeof(rf));
    337  1.16.2.1  bouyer 	} else {
    338  1.16.2.1  bouyer 		rv = dosend(spc, getprogname(), strlen(getprogname())+1);
    339  1.16.2.1  bouyer 	}
    340  1.16.2.1  bouyer 	if (rv || cancel) {
    341  1.16.2.1  bouyer 		if (haslock)
    342  1.16.2.1  bouyer 			unputwait_locked(spc, &rw);
    343  1.16.2.1  bouyer 		else
    344  1.16.2.1  bouyer 			unputwait(spc, &rw);
    345  1.16.2.1  bouyer 		if (cancel) {
    346  1.16.2.1  bouyer 			goto out;
    347  1.16.2.1  bouyer 		}
    348  1.16.2.1  bouyer 	} else {
    349  1.16.2.1  bouyer 		rv = cliwaitresp(spc, &rw, &omask, haslock);
    350      1.10   pooka 	}
    351      1.10   pooka 	if (rv)
    352  1.16.2.1  bouyer 		goto out;
    353      1.10   pooka 
    354      1.10   pooka 	rv = *(int *)rw.rw_data;
    355      1.10   pooka 	free(rw.rw_data);
    356      1.10   pooka 
    357  1.16.2.1  bouyer  out:
    358  1.16.2.1  bouyer 	pthread_sigmask(SIG_SETMASK, &omask, NULL);
    359      1.10   pooka 	return rv;
    360      1.10   pooka }
    361      1.10   pooka 
    362      1.10   pooka static int
    363  1.16.2.1  bouyer prefork_req(struct spclient *spc, sigset_t *omask, void **resp)
    364      1.11   pooka {
    365      1.11   pooka 	struct rsp_hdr rhdr;
    366      1.11   pooka 	struct respwait rw;
    367      1.11   pooka 	int rv;
    368      1.11   pooka 
    369      1.11   pooka 	rhdr.rsp_len = sizeof(rhdr);
    370      1.11   pooka 	rhdr.rsp_class = RUMPSP_REQ;
    371      1.11   pooka 	rhdr.rsp_type = RUMPSP_PREFORK;
    372      1.11   pooka 	rhdr.rsp_error = 0;
    373      1.11   pooka 
    374  1.16.2.1  bouyer 	do {
    375  1.16.2.1  bouyer 		putwait(spc, &rw, &rhdr);
    376  1.16.2.1  bouyer 		rv = send_with_recon(spc, &rhdr, sizeof(rhdr));
    377  1.16.2.1  bouyer 		if (rv != 0) {
    378  1.16.2.1  bouyer 			unputwait(spc, &rw);
    379  1.16.2.1  bouyer 			continue;
    380  1.16.2.1  bouyer 		}
    381  1.16.2.1  bouyer 
    382  1.16.2.1  bouyer 		rv = cliwaitresp(spc, &rw, omask, false);
    383  1.16.2.1  bouyer 		if (rv == ENOTCONN)
    384  1.16.2.1  bouyer 			rv = EAGAIN;
    385  1.16.2.1  bouyer 	} while (rv == EAGAIN);
    386      1.11   pooka 
    387      1.11   pooka 	*resp = rw.rw_data;
    388      1.11   pooka 	return rv;
    389      1.11   pooka }
    390      1.11   pooka 
    391  1.16.2.1  bouyer /*
    392  1.16.2.1  bouyer  * prevent response code from deadlocking with reconnect code
    393  1.16.2.1  bouyer  */
    394      1.11   pooka static int
    395  1.16.2.1  bouyer resp_sendlock(struct spclient *spc)
    396  1.16.2.1  bouyer {
    397  1.16.2.1  bouyer 	int rv = 0;
    398  1.16.2.1  bouyer 
    399  1.16.2.1  bouyer 	pthread_mutex_lock(&spc->spc_mtx);
    400  1.16.2.1  bouyer 	while (spc->spc_ostatus != SPCSTATUS_FREE) {
    401  1.16.2.1  bouyer 		if (__predict_false(spc->spc_reconnecting)) {
    402  1.16.2.1  bouyer 			rv = EBUSY;
    403  1.16.2.1  bouyer 			goto out;
    404  1.16.2.1  bouyer 		}
    405  1.16.2.1  bouyer 		spc->spc_ostatus = SPCSTATUS_WANTED;
    406  1.16.2.1  bouyer 		pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
    407  1.16.2.1  bouyer 	}
    408  1.16.2.1  bouyer 	spc->spc_ostatus = SPCSTATUS_BUSY;
    409  1.16.2.1  bouyer 
    410  1.16.2.1  bouyer  out:
    411  1.16.2.1  bouyer 	pthread_mutex_unlock(&spc->spc_mtx);
    412  1.16.2.1  bouyer 	return rv;
    413  1.16.2.1  bouyer }
    414  1.16.2.1  bouyer 
    415  1.16.2.1  bouyer static void
    416       1.5   pooka send_copyin_resp(struct spclient *spc, uint64_t reqno, void *data, size_t dlen,
    417       1.5   pooka 	int wantstr)
    418       1.1   pooka {
    419       1.1   pooka 	struct rsp_hdr rhdr;
    420       1.1   pooka 
    421       1.5   pooka 	if (wantstr)
    422       1.5   pooka 		dlen = MIN(dlen, strlen(data)+1);
    423       1.5   pooka 
    424       1.1   pooka 	rhdr.rsp_len = sizeof(rhdr) + dlen;
    425       1.1   pooka 	rhdr.rsp_reqno = reqno;
    426       1.3   pooka 	rhdr.rsp_class = RUMPSP_RESP;
    427       1.3   pooka 	rhdr.rsp_type = RUMPSP_COPYIN;
    428       1.1   pooka 	rhdr.rsp_sysnum = 0;
    429       1.1   pooka 
    430  1.16.2.1  bouyer 	if (resp_sendlock(spc) != 0)
    431  1.16.2.1  bouyer 		return;
    432  1.16.2.1  bouyer 	(void)dosend(spc, &rhdr, sizeof(rhdr));
    433  1.16.2.1  bouyer 	(void)dosend(spc, data, dlen);
    434       1.3   pooka 	sendunlock(spc);
    435       1.1   pooka }
    436       1.1   pooka 
    437  1.16.2.1  bouyer static void
    438       1.1   pooka send_anonmmap_resp(struct spclient *spc, uint64_t reqno, void *addr)
    439       1.1   pooka {
    440       1.1   pooka 	struct rsp_hdr rhdr;
    441       1.1   pooka 
    442       1.1   pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(addr);
    443       1.1   pooka 	rhdr.rsp_reqno = reqno;
    444       1.3   pooka 	rhdr.rsp_class = RUMPSP_RESP;
    445       1.3   pooka 	rhdr.rsp_type = RUMPSP_ANONMMAP;
    446       1.1   pooka 	rhdr.rsp_sysnum = 0;
    447       1.1   pooka 
    448  1.16.2.1  bouyer 	if (resp_sendlock(spc) != 0)
    449  1.16.2.1  bouyer 		return;
    450  1.16.2.1  bouyer 	(void)dosend(spc, &rhdr, sizeof(rhdr));
    451  1.16.2.1  bouyer 	(void)dosend(spc, &addr, sizeof(addr));
    452       1.3   pooka 	sendunlock(spc);
    453       1.1   pooka }
    454       1.1   pooka 
    455       1.1   pooka int
    456       1.1   pooka rumpclient_syscall(int sysnum, const void *data, size_t dlen,
    457       1.1   pooka 	register_t *retval)
    458       1.1   pooka {
    459       1.1   pooka 	struct rsp_sysresp *resp;
    460  1.16.2.1  bouyer 	sigset_t omask;
    461       1.3   pooka 	void *rdata;
    462       1.3   pooka 	int rv;
    463       1.3   pooka 
    464  1.16.2.1  bouyer 	pthread_sigmask(SIG_SETMASK, &fullset, &omask);
    465  1.16.2.1  bouyer 
    466       1.3   pooka 	DPRINTF(("rumpsp syscall_req: syscall %d with %p/%zu\n",
    467       1.3   pooka 	    sysnum, data, dlen));
    468       1.3   pooka 
    469  1.16.2.1  bouyer 	rv = syscall_req(&clispc, &omask, sysnum, data, dlen, &rdata);
    470       1.3   pooka 	if (rv)
    471  1.16.2.1  bouyer 		goto out;
    472       1.3   pooka 
    473       1.3   pooka 	resp = rdata;
    474       1.3   pooka 	DPRINTF(("rumpsp syscall_resp: syscall %d error %d, rv: %d/%d\n",
    475       1.3   pooka 	    sysnum, rv, resp->rsys_retval[0], resp->rsys_retval[1]));
    476       1.1   pooka 
    477       1.3   pooka 	memcpy(retval, &resp->rsys_retval, sizeof(resp->rsys_retval));
    478       1.3   pooka 	rv = resp->rsys_error;
    479       1.3   pooka 	free(rdata);
    480       1.1   pooka 
    481  1.16.2.1  bouyer  out:
    482  1.16.2.1  bouyer 	pthread_sigmask(SIG_SETMASK, &omask, NULL);
    483       1.3   pooka 	return rv;
    484       1.3   pooka }
    485       1.1   pooka 
    486       1.3   pooka static void
    487       1.3   pooka handlereq(struct spclient *spc)
    488       1.3   pooka {
    489       1.3   pooka 	struct rsp_copydata *copydata;
    490      1.16   pooka 	struct rsp_hdr *rhdr = &spc->spc_hdr;
    491       1.3   pooka 	void *mapaddr;
    492       1.3   pooka 	size_t maplen;
    493       1.5   pooka 	int reqtype = spc->spc_hdr.rsp_type;
    494       1.1   pooka 
    495       1.5   pooka 	switch (reqtype) {
    496       1.3   pooka 	case RUMPSP_COPYIN:
    497       1.5   pooka 	case RUMPSP_COPYINSTR:
    498       1.3   pooka 		/*LINTED*/
    499       1.3   pooka 		copydata = (struct rsp_copydata *)spc->spc_buf;
    500       1.3   pooka 		DPRINTF(("rump_sp handlereq: copyin request: %p/%zu\n",
    501       1.3   pooka 		    copydata->rcp_addr, copydata->rcp_len));
    502       1.3   pooka 		send_copyin_resp(spc, spc->spc_hdr.rsp_reqno,
    503       1.5   pooka 		    copydata->rcp_addr, copydata->rcp_len,
    504       1.5   pooka 		    reqtype == RUMPSP_COPYINSTR);
    505       1.3   pooka 		break;
    506       1.3   pooka 	case RUMPSP_COPYOUT:
    507       1.5   pooka 	case RUMPSP_COPYOUTSTR:
    508       1.3   pooka 		/*LINTED*/
    509       1.3   pooka 		copydata = (struct rsp_copydata *)spc->spc_buf;
    510       1.3   pooka 		DPRINTF(("rump_sp handlereq: copyout request: %p/%zu\n",
    511       1.3   pooka 		    copydata->rcp_addr, copydata->rcp_len));
    512       1.3   pooka 		/*LINTED*/
    513       1.3   pooka 		memcpy(copydata->rcp_addr, copydata->rcp_data,
    514       1.3   pooka 		    copydata->rcp_len);
    515       1.3   pooka 		break;
    516       1.3   pooka 	case RUMPSP_ANONMMAP:
    517       1.3   pooka 		/*LINTED*/
    518       1.3   pooka 		maplen = *(size_t *)spc->spc_buf;
    519       1.3   pooka 		mapaddr = mmap(NULL, maplen, PROT_READ|PROT_WRITE,
    520       1.3   pooka 		    MAP_ANON, -1, 0);
    521       1.3   pooka 		if (mapaddr == MAP_FAILED)
    522       1.3   pooka 			mapaddr = NULL;
    523       1.3   pooka 		DPRINTF(("rump_sp handlereq: anonmmap: %p\n", mapaddr));
    524       1.3   pooka 		send_anonmmap_resp(spc, spc->spc_hdr.rsp_reqno, mapaddr);
    525       1.3   pooka 		break;
    526      1.16   pooka 	case RUMPSP_RAISE:
    527      1.16   pooka 		DPRINTF(("rump_sp handlereq: raise sig %d\n", rhdr->rsp_signo));
    528  1.16.2.1  bouyer 		raise((int)rhdr->rsp_signo);
    529      1.16   pooka 		/*
    530      1.16   pooka 		 * We most likely have signals blocked, but the signal
    531      1.16   pooka 		 * will be handled soon enough when we return.
    532      1.16   pooka 		 */
    533      1.16   pooka 		break;
    534       1.3   pooka 	default:
    535      1.12   pooka 		printf("PANIC: INVALID TYPE %d\n", reqtype);
    536       1.3   pooka 		abort();
    537       1.3   pooka 		break;
    538       1.1   pooka 	}
    539       1.1   pooka 
    540       1.6   pooka 	spcfreebuf(spc);
    541       1.1   pooka }
    542       1.1   pooka 
    543      1.11   pooka static unsigned ptab_idx;
    544      1.11   pooka static struct sockaddr *serv_sa;
    545      1.11   pooka 
    546      1.11   pooka static int
    547  1.16.2.1  bouyer doconnect(bool noisy)
    548       1.1   pooka {
    549  1.16.2.1  bouyer 	struct respwait rw;
    550  1.16.2.1  bouyer 	struct rsp_hdr rhdr;
    551      1.15   pooka 	struct kevent kev[NSIG+1];
    552       1.9   pooka 	char banner[MAXBANNER];
    553  1.16.2.1  bouyer 	struct pollfd pfd;
    554      1.15   pooka 	int s, error, flags, i;
    555       1.9   pooka 	ssize_t n;
    556       1.1   pooka 
    557  1.16.2.1  bouyer 	if (kq != -1)
    558  1.16.2.1  bouyer 		host_close(kq);
    559  1.16.2.1  bouyer 	kq = -1;
    560  1.16.2.1  bouyer 	s = -1;
    561  1.16.2.1  bouyer 
    562  1.16.2.1  bouyer 	if (clispc.spc_fd != -1)
    563  1.16.2.1  bouyer 		host_close(clispc.spc_fd);
    564  1.16.2.1  bouyer 	clispc.spc_fd = -1;
    565  1.16.2.1  bouyer 
    566  1.16.2.1  bouyer 	/*
    567  1.16.2.1  bouyer 	 * for reconnect, gate everyone out of the receiver code
    568  1.16.2.1  bouyer 	 */
    569  1.16.2.1  bouyer 	putwait_locked(&clispc, &rw, &rhdr);
    570  1.16.2.1  bouyer 
    571  1.16.2.1  bouyer 	pthread_mutex_lock(&clispc.spc_mtx);
    572  1.16.2.1  bouyer 	clispc.spc_reconnecting = 1;
    573  1.16.2.1  bouyer 	pthread_cond_broadcast(&clispc.spc_cv);
    574  1.16.2.1  bouyer 	clispc.spc_generation++;
    575  1.16.2.1  bouyer 	while (clispc.spc_istatus != SPCSTATUS_FREE) {
    576  1.16.2.1  bouyer 		clispc.spc_istatus = SPCSTATUS_WANTED;
    577  1.16.2.1  bouyer 		pthread_cond_wait(&rw.rw_cv, &clispc.spc_mtx);
    578  1.16.2.1  bouyer 	}
    579  1.16.2.1  bouyer 	kickall(&clispc);
    580  1.16.2.1  bouyer 
    581  1.16.2.1  bouyer 	/*
    582  1.16.2.1  bouyer 	 * we can release it already since we hold the
    583  1.16.2.1  bouyer 	 * send lock during reconnect
    584  1.16.2.1  bouyer 	 * XXX: assert it
    585  1.16.2.1  bouyer 	 */
    586  1.16.2.1  bouyer 	clispc.spc_istatus = SPCSTATUS_FREE;
    587  1.16.2.1  bouyer 	pthread_mutex_unlock(&clispc.spc_mtx);
    588  1.16.2.1  bouyer 	unputwait_locked(&clispc, &rw);
    589  1.16.2.1  bouyer 
    590  1.16.2.1  bouyer 	free(clispc.spc_buf);
    591  1.16.2.1  bouyer 	clispc.spc_off = 0;
    592  1.16.2.1  bouyer 
    593      1.13   pooka 	s = host_socket(parsetab[ptab_idx].domain, SOCK_STREAM, 0);
    594      1.11   pooka 	if (s == -1)
    595       1.2   pooka 		return -1;
    596       1.1   pooka 
    597  1.16.2.1  bouyer 	pfd.fd = s;
    598  1.16.2.1  bouyer 	pfd.events = POLLIN;
    599  1.16.2.1  bouyer 	while (host_connect(s, serv_sa, (socklen_t)serv_sa->sa_len) == -1) {
    600  1.16.2.1  bouyer 		if (errno == EINTR)
    601  1.16.2.1  bouyer 			continue;
    602      1.11   pooka 		error = errno;
    603  1.16.2.1  bouyer 		if (noisy)
    604  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: client connect failed: %s\n",
    605  1.16.2.1  bouyer 			    strerror(errno));
    606       1.2   pooka 		errno = error;
    607       1.2   pooka 		return -1;
    608       1.2   pooka 	}
    609       1.1   pooka 
    610      1.11   pooka 	if ((error = parsetab[ptab_idx].connhook(s)) != 0) {
    611       1.2   pooka 		error = errno;
    612  1.16.2.1  bouyer 		if (noisy)
    613  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: connect hook failed\n");
    614       1.2   pooka 		errno = error;
    615       1.2   pooka 		return -1;
    616       1.1   pooka 	}
    617       1.4   pooka 
    618      1.13   pooka 	if ((n = host_read(s, banner, sizeof(banner)-1)) < 0) {
    619       1.2   pooka 		error = errno;
    620  1.16.2.1  bouyer 		if (noisy)
    621  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: failed to read banner\n");
    622       1.2   pooka 		errno = error;
    623       1.2   pooka 		return -1;
    624       1.1   pooka 	}
    625       1.9   pooka 
    626       1.9   pooka 	if (banner[n-1] != '\n') {
    627  1.16.2.1  bouyer 		if (noisy)
    628  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: invalid banner\n");
    629       1.9   pooka 		errno = EINVAL;
    630       1.9   pooka 		return -1;
    631       1.9   pooka 	}
    632       1.9   pooka 	banner[n] = '\0';
    633  1.16.2.1  bouyer 	/* parse the banner some day */
    634       1.9   pooka 
    635      1.15   pooka 	flags = host_fcntl(s, F_GETFL, 0);
    636      1.15   pooka 	if (host_fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) {
    637  1.16.2.1  bouyer 		if (noisy)
    638  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: socket fd NONBLOCK: %s\n",
    639  1.16.2.1  bouyer 			    strerror(errno));
    640      1.15   pooka 		errno = EINVAL;
    641      1.15   pooka 		return -1;
    642      1.15   pooka 	}
    643  1.16.2.1  bouyer 	clispc.spc_fd = s;
    644  1.16.2.1  bouyer 	clispc.spc_state = SPCSTATE_RUNNING;
    645  1.16.2.1  bouyer 	clispc.spc_reconnecting = 0;
    646       1.9   pooka 
    647      1.15   pooka 	/* setup kqueue, we want all signals and the fd */
    648  1.16.2.1  bouyer 	if ((kq = host_kqueue()) == -1) {
    649      1.15   pooka 		error = errno;
    650  1.16.2.1  bouyer 		if (noisy)
    651  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: cannot setup kqueue");
    652      1.15   pooka 		errno = error;
    653      1.15   pooka 		return -1;
    654      1.15   pooka 	}
    655      1.15   pooka 
    656      1.15   pooka 	for (i = 0; i < NSIG; i++) {
    657      1.15   pooka 		EV_SET(&kev[i], i+1, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0, 0, 0);
    658      1.15   pooka 	}
    659  1.16.2.1  bouyer 	EV_SET(&kev[NSIG], clispc.spc_fd,
    660  1.16.2.1  bouyer 	    EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
    661  1.16.2.1  bouyer 	if (host_kevent(kq, kev, NSIG+1, NULL, 0, NULL) == -1) {
    662      1.15   pooka 		error = errno;
    663  1.16.2.1  bouyer 		if (noisy)
    664  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: kevent() failed");
    665      1.15   pooka 		errno = error;
    666      1.15   pooka 		return -1;
    667      1.15   pooka 	}
    668      1.15   pooka 
    669  1.16.2.1  bouyer 	return 0;
    670  1.16.2.1  bouyer }
    671  1.16.2.1  bouyer 
    672  1.16.2.1  bouyer static int
    673  1.16.2.1  bouyer doinit(void)
    674  1.16.2.1  bouyer {
    675  1.16.2.1  bouyer 
    676      1.11   pooka 	TAILQ_INIT(&clispc.spc_respwait);
    677      1.11   pooka 	pthread_mutex_init(&clispc.spc_mtx, NULL);
    678      1.11   pooka 	pthread_cond_init(&clispc.spc_cv, NULL);
    679      1.11   pooka 
    680      1.11   pooka 	return 0;
    681      1.11   pooka }
    682      1.11   pooka 
    683      1.13   pooka void *(*rumpclient_dlsym)(void *, const char *);
    684      1.13   pooka 
    685      1.11   pooka int
    686      1.11   pooka rumpclient_init()
    687      1.11   pooka {
    688      1.11   pooka 	char *p;
    689      1.11   pooka 	int error;
    690      1.11   pooka 
    691  1.16.2.1  bouyer 	sigfillset(&fullset);
    692  1.16.2.1  bouyer 
    693      1.13   pooka 	/* dlsym overrided by rumphijack? */
    694      1.13   pooka 	if (!rumpclient_dlsym)
    695      1.13   pooka 		rumpclient_dlsym = dlsym;
    696      1.13   pooka 
    697      1.13   pooka 	/*
    698      1.13   pooka 	 * sag mir, wo die symbol sind.  zogen fort, der krieg beginnt.
    699      1.13   pooka 	 * wann wird man je verstehen?  wann wird man je verstehen?
    700      1.13   pooka 	 */
    701      1.13   pooka #define FINDSYM2(_name_,_syscall_)					\
    702      1.13   pooka 	if ((host_##_name_ = rumpclient_dlsym(RTLD_NEXT,		\
    703      1.13   pooka 	    #_syscall_)) == NULL)					\
    704      1.13   pooka 		/* host_##_name_ = _syscall_ */;
    705      1.13   pooka #define FINDSYM(_name_) FINDSYM2(_name_,_name_)
    706      1.13   pooka 	FINDSYM2(socket,__socket30);
    707      1.13   pooka 	FINDSYM(close);
    708      1.13   pooka 	FINDSYM(connect);
    709      1.15   pooka 	FINDSYM(fcntl);
    710      1.13   pooka 	FINDSYM(poll);
    711      1.13   pooka 	FINDSYM(read);
    712      1.13   pooka 	FINDSYM(sendto);
    713      1.13   pooka 	FINDSYM(setsockopt);
    714  1.16.2.1  bouyer 	FINDSYM(kqueue);
    715  1.16.2.1  bouyer #if !__NetBSD_Prereq__(5,99,7)
    716  1.16.2.1  bouyer 	FINDSYM(kevent);
    717  1.16.2.1  bouyer #else
    718  1.16.2.1  bouyer 	FINDSYM2(kevent,_sys___kevent50);
    719  1.16.2.1  bouyer #endif
    720      1.13   pooka #undef	FINDSYM
    721      1.13   pooka #undef	FINDSY2
    722      1.13   pooka 
    723      1.11   pooka 	if ((p = getenv("RUMP_SERVER")) == NULL) {
    724      1.11   pooka 		errno = ENOENT;
    725      1.11   pooka 		return -1;
    726      1.11   pooka 	}
    727      1.11   pooka 
    728      1.11   pooka 	if ((error = parseurl(p, &serv_sa, &ptab_idx, 0)) != 0) {
    729      1.11   pooka 		errno = error;
    730      1.11   pooka 		return -1;
    731      1.11   pooka 	}
    732      1.11   pooka 
    733  1.16.2.1  bouyer 	if (doinit() == -1)
    734  1.16.2.1  bouyer 		return -1;
    735  1.16.2.1  bouyer 	if (doconnect(true) == -1)
    736      1.11   pooka 		return -1;
    737      1.11   pooka 
    738  1.16.2.1  bouyer 	error = handshake_req(&clispc, NULL, 0, false);
    739      1.11   pooka 	if (error) {
    740      1.11   pooka 		pthread_mutex_destroy(&clispc.spc_mtx);
    741      1.11   pooka 		pthread_cond_destroy(&clispc.spc_cv);
    742  1.16.2.1  bouyer 		if (clispc.spc_fd != -1)
    743  1.16.2.1  bouyer 			host_close(clispc.spc_fd);
    744      1.10   pooka 		errno = error;
    745      1.10   pooka 		return -1;
    746      1.10   pooka 	}
    747      1.10   pooka 
    748      1.11   pooka 	return 0;
    749      1.11   pooka }
    750      1.11   pooka 
    751      1.11   pooka struct rumpclient_fork {
    752      1.11   pooka 	uint32_t fork_auth[AUTHLEN];
    753      1.11   pooka };
    754      1.11   pooka 
    755      1.11   pooka struct rumpclient_fork *
    756      1.11   pooka rumpclient_prefork(void)
    757      1.11   pooka {
    758      1.11   pooka 	struct rumpclient_fork *rpf;
    759  1.16.2.1  bouyer 	sigset_t omask;
    760      1.11   pooka 	void *resp;
    761      1.11   pooka 	int rv;
    762      1.11   pooka 
    763  1.16.2.1  bouyer 	pthread_sigmask(SIG_SETMASK, &fullset, &omask);
    764      1.11   pooka 	rpf = malloc(sizeof(*rpf));
    765      1.11   pooka 	if (rpf == NULL)
    766      1.11   pooka 		return NULL;
    767      1.11   pooka 
    768  1.16.2.1  bouyer 	if ((rv = prefork_req(&clispc, &omask, &resp)) != 0) {
    769      1.11   pooka 		free(rpf);
    770      1.11   pooka 		errno = rv;
    771  1.16.2.1  bouyer 		rpf = NULL;
    772  1.16.2.1  bouyer 		goto out;
    773      1.11   pooka 	}
    774      1.11   pooka 
    775      1.11   pooka 	memcpy(rpf->fork_auth, resp, sizeof(rpf->fork_auth));
    776      1.11   pooka 	free(resp);
    777      1.11   pooka 
    778  1.16.2.1  bouyer  out:
    779  1.16.2.1  bouyer 	pthread_sigmask(SIG_SETMASK, &omask, NULL);
    780      1.11   pooka 	return rpf;
    781      1.11   pooka }
    782      1.11   pooka 
    783      1.11   pooka int
    784      1.11   pooka rumpclient_fork_init(struct rumpclient_fork *rpf)
    785      1.11   pooka {
    786      1.11   pooka 	int error;
    787  1.16.2.1  bouyer 	int osock;
    788      1.11   pooka 
    789  1.16.2.1  bouyer 	osock = clispc.spc_fd;
    790      1.11   pooka 	memset(&clispc, 0, sizeof(clispc));
    791  1.16.2.1  bouyer 	clispc.spc_fd = osock;
    792  1.16.2.1  bouyer 
    793  1.16.2.1  bouyer 	kq = -1; /* kqueue descriptor is not copied over fork() */
    794      1.11   pooka 
    795  1.16.2.1  bouyer 	if (doinit() == -1)
    796  1.16.2.1  bouyer 		return -1;
    797  1.16.2.1  bouyer 	if (doconnect(false) == -1)
    798      1.11   pooka 		return -1;
    799      1.10   pooka 
    800  1.16.2.1  bouyer 	error = handshake_req(&clispc, rpf->fork_auth, 0, false);
    801      1.10   pooka 	if (error) {
    802      1.10   pooka 		pthread_mutex_destroy(&clispc.spc_mtx);
    803      1.10   pooka 		pthread_cond_destroy(&clispc.spc_cv);
    804      1.11   pooka 		errno = error;
    805      1.11   pooka 		return -1;
    806      1.10   pooka 	}
    807      1.11   pooka 
    808      1.11   pooka 	return 0;
    809       1.1   pooka }
    810  1.16.2.1  bouyer 
    811  1.16.2.1  bouyer void
    812  1.16.2.1  bouyer rumpclient_setconnretry(time_t timeout)
    813  1.16.2.1  bouyer {
    814  1.16.2.1  bouyer 
    815  1.16.2.1  bouyer 	if (timeout < RUMPCLIENT_RETRYCONN_DIE)
    816  1.16.2.1  bouyer 		return; /* gigo */
    817  1.16.2.1  bouyer 
    818  1.16.2.1  bouyer 	retrytimo = timeout;
    819  1.16.2.1  bouyer }
    820