Home | History | Annotate | Line # | Download | only in librumpclient
rumpclient.c revision 1.16.2.2
      1  1.16.2.2  bouyer /*      $NetBSD: rumpclient.c,v 1.16.2.2 2011/02/17 11:59:23 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.16.2.2  bouyer int	(*host_dup)(int);
     72      1.13   pooka 
     73  1.16.2.1  bouyer int	(*host_kqueue)(void);
     74  1.16.2.1  bouyer int	(*host_kevent)(int, const struct kevent *, size_t,
     75  1.16.2.1  bouyer 		       struct kevent *, size_t, const struct timespec *);
     76  1.16.2.1  bouyer 
     77  1.16.2.2  bouyer int	(*host_execve)(const char *, char *const[], char *const[]);
     78  1.16.2.2  bouyer 
     79       1.1   pooka #include "sp_common.c"
     80       1.1   pooka 
     81      1.11   pooka static struct spclient clispc = {
     82      1.11   pooka 	.spc_fd = -1,
     83      1.11   pooka };
     84       1.1   pooka 
     85  1.16.2.1  bouyer static int kq = -1;
     86      1.15   pooka static sigset_t fullset;
     87      1.12   pooka 
     88  1.16.2.1  bouyer static int doconnect(bool);
     89  1.16.2.2  bouyer static int handshake_req(struct spclient *, int, void *, int, bool);
     90  1.16.2.1  bouyer 
     91  1.16.2.2  bouyer /*
     92  1.16.2.2  bouyer  * Default: don't retry.  Most clients can't handle it
     93  1.16.2.2  bouyer  * (consider e.g. fds suddenly going missing).
     94  1.16.2.2  bouyer  */
     95  1.16.2.2  bouyer static time_t retrytimo = 0;
     96  1.16.2.1  bouyer 
     97      1.12   pooka static int
     98  1.16.2.1  bouyer send_with_recon(struct spclient *spc, const void *data, size_t dlen)
     99      1.12   pooka {
    100  1.16.2.1  bouyer 	struct timeval starttime, curtime;
    101  1.16.2.1  bouyer 	time_t prevreconmsg;
    102  1.16.2.1  bouyer 	unsigned reconretries;
    103  1.16.2.1  bouyer 	int rv;
    104  1.16.2.1  bouyer 
    105  1.16.2.1  bouyer 	for (prevreconmsg = 0, reconretries = 0;;) {
    106  1.16.2.1  bouyer 		rv = dosend(spc, data, dlen);
    107  1.16.2.1  bouyer 		if (__predict_false(rv == ENOTCONN || rv == EBADF)) {
    108  1.16.2.1  bouyer 			/* no persistent connections */
    109  1.16.2.2  bouyer 			if (retrytimo == 0) {
    110  1.16.2.2  bouyer 				rv = ENOTCONN;
    111  1.16.2.1  bouyer 				break;
    112  1.16.2.2  bouyer 			}
    113  1.16.2.1  bouyer 			if (retrytimo == RUMPCLIENT_RETRYCONN_DIE)
    114  1.16.2.1  bouyer 				exit(1);
    115  1.16.2.1  bouyer 
    116  1.16.2.1  bouyer 			if (!prevreconmsg) {
    117  1.16.2.1  bouyer 				prevreconmsg = time(NULL);
    118  1.16.2.1  bouyer 				gettimeofday(&starttime, NULL);
    119  1.16.2.1  bouyer 			}
    120  1.16.2.1  bouyer 			if (reconretries == 1) {
    121  1.16.2.1  bouyer 				if (retrytimo == RUMPCLIENT_RETRYCONN_ONCE) {
    122  1.16.2.1  bouyer 					rv = ENOTCONN;
    123  1.16.2.1  bouyer 					break;
    124  1.16.2.1  bouyer 				}
    125  1.16.2.1  bouyer 				fprintf(stderr, "rump_sp: connection to "
    126  1.16.2.1  bouyer 				    "kernel lost, trying to reconnect ...\n");
    127  1.16.2.1  bouyer 			} else if (time(NULL) - prevreconmsg > 120) {
    128  1.16.2.1  bouyer 				fprintf(stderr, "rump_sp: still trying to "
    129  1.16.2.1  bouyer 				    "reconnect ...\n");
    130  1.16.2.1  bouyer 				prevreconmsg = time(NULL);
    131  1.16.2.1  bouyer 			}
    132  1.16.2.1  bouyer 
    133  1.16.2.1  bouyer 			/* check that we aren't over the limit */
    134  1.16.2.1  bouyer 			if (retrytimo > 0) {
    135  1.16.2.1  bouyer 				struct timeval tmp;
    136  1.16.2.1  bouyer 
    137  1.16.2.1  bouyer 				gettimeofday(&curtime, NULL);
    138  1.16.2.1  bouyer 				timersub(&curtime, &starttime, &tmp);
    139  1.16.2.1  bouyer 				if (tmp.tv_sec >= retrytimo) {
    140  1.16.2.1  bouyer 					fprintf(stderr, "rump_sp: reconnect "
    141  1.16.2.1  bouyer 					    "failed, %lld second timeout\n",
    142  1.16.2.1  bouyer 					    (long long)retrytimo);
    143  1.16.2.1  bouyer 					return ENOTCONN;
    144  1.16.2.1  bouyer 				}
    145  1.16.2.1  bouyer 			}
    146  1.16.2.1  bouyer 
    147  1.16.2.1  bouyer 			/* adhoc backoff timer */
    148  1.16.2.1  bouyer 			if (reconretries < 10) {
    149  1.16.2.1  bouyer 				usleep(100000 * reconretries);
    150  1.16.2.1  bouyer 			} else {
    151  1.16.2.1  bouyer 				sleep(MIN(10, reconretries-9));
    152  1.16.2.1  bouyer 			}
    153  1.16.2.1  bouyer 			reconretries++;
    154  1.16.2.1  bouyer 
    155  1.16.2.1  bouyer 			if ((rv = doconnect(false)) != 0)
    156  1.16.2.1  bouyer 				continue;
    157  1.16.2.2  bouyer 			if ((rv = handshake_req(&clispc, HANDSHAKE_GUEST,
    158  1.16.2.2  bouyer 			    NULL, 0, true)) != 0)
    159  1.16.2.1  bouyer 				continue;
    160  1.16.2.1  bouyer 
    161  1.16.2.1  bouyer 			/*
    162  1.16.2.1  bouyer 			 * ok, reconnect succesful.  we need to return to
    163  1.16.2.1  bouyer 			 * the upper layer to get the entire PDU resent.
    164  1.16.2.1  bouyer 			 */
    165  1.16.2.1  bouyer 			if (reconretries != 1)
    166  1.16.2.1  bouyer 				fprintf(stderr, "rump_sp: reconnected!\n");
    167  1.16.2.1  bouyer 			rv = EAGAIN;
    168  1.16.2.1  bouyer 			break;
    169  1.16.2.1  bouyer 		} else {
    170  1.16.2.1  bouyer 			_DIAGASSERT(errno != EAGAIN);
    171  1.16.2.1  bouyer 			break;
    172  1.16.2.1  bouyer 		}
    173  1.16.2.1  bouyer 	}
    174  1.16.2.1  bouyer 
    175  1.16.2.1  bouyer 	return rv;
    176  1.16.2.1  bouyer }
    177  1.16.2.1  bouyer 
    178  1.16.2.1  bouyer static int
    179  1.16.2.1  bouyer cliwaitresp(struct spclient *spc, struct respwait *rw, sigset_t *mask,
    180  1.16.2.1  bouyer 	bool keeplock)
    181  1.16.2.1  bouyer {
    182  1.16.2.1  bouyer 	uint64_t mygen;
    183  1.16.2.1  bouyer 	bool imalive = true;
    184      1.12   pooka 
    185      1.15   pooka 	pthread_mutex_lock(&spc->spc_mtx);
    186  1.16.2.1  bouyer 	if (!keeplock)
    187  1.16.2.1  bouyer 		sendunlockl(spc);
    188  1.16.2.1  bouyer 	mygen = spc->spc_generation;
    189      1.12   pooka 
    190      1.12   pooka 	rw->rw_error = 0;
    191  1.16.2.1  bouyer 	while (!rw->rw_done && rw->rw_error == 0) {
    192  1.16.2.1  bouyer 		if (__predict_false(spc->spc_generation != mygen || !imalive))
    193  1.16.2.1  bouyer 			break;
    194  1.16.2.1  bouyer 
    195      1.12   pooka 		/* are we free to receive? */
    196      1.12   pooka 		if (spc->spc_istatus == SPCSTATUS_FREE) {
    197      1.15   pooka 			struct kevent kev[8];
    198      1.15   pooka 			int gotresp, dosig, rv, i;
    199      1.15   pooka 
    200      1.12   pooka 			spc->spc_istatus = SPCSTATUS_BUSY;
    201      1.12   pooka 			pthread_mutex_unlock(&spc->spc_mtx);
    202      1.12   pooka 
    203      1.15   pooka 			dosig = 0;
    204      1.15   pooka 			for (gotresp = 0; !gotresp; ) {
    205      1.15   pooka 				switch (readframe(spc)) {
    206      1.15   pooka 				case 0:
    207  1.16.2.1  bouyer 					rv = host_kevent(kq, NULL, 0,
    208      1.15   pooka 					    kev, __arraycount(kev), NULL);
    209  1.16.2.1  bouyer 
    210  1.16.2.2  bouyer 					if (__predict_false(rv == -1)) {
    211  1.16.2.2  bouyer 						goto cleanup;
    212  1.16.2.2  bouyer 					}
    213  1.16.2.2  bouyer 
    214  1.16.2.1  bouyer 					/*
    215  1.16.2.1  bouyer 					 * XXX: don't know how this can
    216  1.16.2.1  bouyer 					 * happen (timeout cannot expire
    217  1.16.2.1  bouyer 					 * since there isn't one), but
    218  1.16.2.1  bouyer 					 * it does happen
    219  1.16.2.1  bouyer 					 */
    220  1.16.2.1  bouyer 					if (__predict_false(rv == 0))
    221  1.16.2.1  bouyer 						continue;
    222  1.16.2.1  bouyer 
    223      1.15   pooka 					for (i = 0; i < rv; i++) {
    224      1.15   pooka 						if (kev[i].filter
    225      1.15   pooka 						    == EVFILT_SIGNAL)
    226      1.15   pooka 							dosig++;
    227      1.15   pooka 					}
    228      1.15   pooka 					if (dosig)
    229      1.15   pooka 						goto cleanup;
    230      1.15   pooka 
    231      1.15   pooka 					continue;
    232      1.15   pooka 				case -1:
    233  1.16.2.1  bouyer 					imalive = false;
    234      1.15   pooka 					goto cleanup;
    235      1.15   pooka 				default:
    236      1.15   pooka 					break;
    237      1.15   pooka 				}
    238      1.12   pooka 
    239      1.15   pooka 				switch (spc->spc_hdr.rsp_class) {
    240      1.12   pooka 				case RUMPSP_RESP:
    241      1.12   pooka 				case RUMPSP_ERROR:
    242      1.12   pooka 					kickwaiter(spc);
    243      1.15   pooka 					gotresp = spc->spc_hdr.rsp_reqno ==
    244      1.15   pooka 					    rw->rw_reqno;
    245      1.12   pooka 					break;
    246      1.12   pooka 				case RUMPSP_REQ:
    247      1.12   pooka 					handlereq(spc);
    248      1.12   pooka 					break;
    249      1.12   pooka 				default:
    250      1.12   pooka 					/* panic */
    251      1.12   pooka 					break;
    252      1.15   pooka 				}
    253      1.12   pooka 			}
    254      1.12   pooka 
    255      1.15   pooka  cleanup:
    256      1.15   pooka 			pthread_mutex_lock(&spc->spc_mtx);
    257      1.15   pooka 			if (spc->spc_istatus == SPCSTATUS_WANTED)
    258      1.15   pooka 				kickall(spc);
    259      1.15   pooka 			spc->spc_istatus = SPCSTATUS_FREE;
    260      1.15   pooka 
    261      1.15   pooka 			/* take one for the team */
    262      1.15   pooka 			if (dosig) {
    263      1.15   pooka 				pthread_mutex_unlock(&spc->spc_mtx);
    264      1.15   pooka 				pthread_sigmask(SIG_SETMASK, mask, NULL);
    265      1.15   pooka 				pthread_sigmask(SIG_SETMASK, &fullset, NULL);
    266      1.15   pooka 				pthread_mutex_lock(&spc->spc_mtx);
    267      1.15   pooka 			}
    268      1.12   pooka 		} else {
    269      1.12   pooka 			spc->spc_istatus = SPCSTATUS_WANTED;
    270      1.12   pooka 			pthread_cond_wait(&rw->rw_cv, &spc->spc_mtx);
    271      1.12   pooka 		}
    272      1.12   pooka 	}
    273      1.12   pooka 	TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
    274      1.12   pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    275      1.12   pooka 	pthread_cond_destroy(&rw->rw_cv);
    276      1.12   pooka 
    277  1.16.2.1  bouyer 	if (spc->spc_generation != mygen || !imalive) {
    278      1.12   pooka 		return ENOTCONN;
    279  1.16.2.1  bouyer 	}
    280      1.12   pooka 	return rw->rw_error;
    281      1.12   pooka }
    282      1.12   pooka 
    283       1.1   pooka static int
    284  1.16.2.1  bouyer syscall_req(struct spclient *spc, sigset_t *omask, int sysnum,
    285       1.3   pooka 	const void *data, size_t dlen, void **resp)
    286       1.1   pooka {
    287       1.1   pooka 	struct rsp_hdr rhdr;
    288       1.3   pooka 	struct respwait rw;
    289       1.3   pooka 	int rv;
    290       1.1   pooka 
    291       1.1   pooka 	rhdr.rsp_len = sizeof(rhdr) + dlen;
    292       1.3   pooka 	rhdr.rsp_class = RUMPSP_REQ;
    293       1.3   pooka 	rhdr.rsp_type = RUMPSP_SYSCALL;
    294       1.1   pooka 	rhdr.rsp_sysnum = sysnum;
    295       1.1   pooka 
    296       1.6   pooka 	do {
    297       1.6   pooka 		putwait(spc, &rw, &rhdr);
    298  1.16.2.1  bouyer 		if ((rv = send_with_recon(spc, &rhdr, sizeof(rhdr))) != 0) {
    299  1.16.2.1  bouyer 			unputwait(spc, &rw);
    300  1.16.2.1  bouyer 			continue;
    301  1.16.2.1  bouyer 		}
    302  1.16.2.1  bouyer 		if ((rv = send_with_recon(spc, data, dlen)) != 0) {
    303       1.6   pooka 			unputwait(spc, &rw);
    304  1.16.2.1  bouyer 			continue;
    305       1.6   pooka 		}
    306       1.6   pooka 
    307  1.16.2.1  bouyer 		rv = cliwaitresp(spc, &rw, omask, false);
    308  1.16.2.1  bouyer 		if (rv == ENOTCONN)
    309  1.16.2.1  bouyer 			rv = EAGAIN;
    310       1.6   pooka 	} while (rv == EAGAIN);
    311       1.3   pooka 
    312       1.3   pooka 	*resp = rw.rw_data;
    313       1.3   pooka 	return rv;
    314       1.1   pooka }
    315       1.1   pooka 
    316       1.1   pooka static int
    317  1.16.2.2  bouyer handshake_req(struct spclient *spc, int type, void *data,
    318  1.16.2.2  bouyer 	int cancel, bool haslock)
    319      1.10   pooka {
    320      1.11   pooka 	struct handshake_fork rf;
    321      1.10   pooka 	struct rsp_hdr rhdr;
    322      1.10   pooka 	struct respwait rw;
    323      1.12   pooka 	sigset_t omask;
    324  1.16.2.1  bouyer 	size_t bonus;
    325      1.10   pooka 	int rv;
    326      1.10   pooka 
    327  1.16.2.2  bouyer 	if (type == HANDSHAKE_FORK) {
    328  1.16.2.1  bouyer 		bonus = sizeof(rf);
    329  1.16.2.1  bouyer 	} else {
    330  1.16.2.1  bouyer 		bonus = strlen(getprogname())+1;
    331  1.16.2.1  bouyer 	}
    332  1.16.2.1  bouyer 
    333      1.10   pooka 	/* performs server handshake */
    334  1.16.2.1  bouyer 	rhdr.rsp_len = sizeof(rhdr) + bonus;
    335      1.10   pooka 	rhdr.rsp_class = RUMPSP_REQ;
    336      1.10   pooka 	rhdr.rsp_type = RUMPSP_HANDSHAKE;
    337  1.16.2.2  bouyer 	rhdr.rsp_handshake = type;
    338      1.10   pooka 
    339      1.12   pooka 	pthread_sigmask(SIG_SETMASK, &fullset, &omask);
    340  1.16.2.1  bouyer 	if (haslock)
    341  1.16.2.1  bouyer 		putwait_locked(spc, &rw, &rhdr);
    342  1.16.2.1  bouyer 	else
    343  1.16.2.1  bouyer 		putwait(spc, &rw, &rhdr);
    344      1.10   pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    345  1.16.2.2  bouyer 	if (type == HANDSHAKE_FORK) {
    346  1.16.2.2  bouyer 		memcpy(rf.rf_auth, data, sizeof(rf.rf_auth)); /* uh, why? */
    347      1.11   pooka 		rf.rf_cancel = cancel;
    348  1.16.2.1  bouyer 		rv = send_with_recon(spc, &rf, sizeof(rf));
    349  1.16.2.1  bouyer 	} else {
    350  1.16.2.1  bouyer 		rv = dosend(spc, getprogname(), strlen(getprogname())+1);
    351  1.16.2.1  bouyer 	}
    352  1.16.2.1  bouyer 	if (rv || cancel) {
    353  1.16.2.1  bouyer 		if (haslock)
    354  1.16.2.1  bouyer 			unputwait_locked(spc, &rw);
    355  1.16.2.1  bouyer 		else
    356  1.16.2.1  bouyer 			unputwait(spc, &rw);
    357  1.16.2.1  bouyer 		if (cancel) {
    358  1.16.2.1  bouyer 			goto out;
    359  1.16.2.1  bouyer 		}
    360  1.16.2.1  bouyer 	} else {
    361  1.16.2.1  bouyer 		rv = cliwaitresp(spc, &rw, &omask, haslock);
    362      1.10   pooka 	}
    363      1.10   pooka 	if (rv)
    364  1.16.2.1  bouyer 		goto out;
    365      1.10   pooka 
    366      1.10   pooka 	rv = *(int *)rw.rw_data;
    367      1.10   pooka 	free(rw.rw_data);
    368      1.10   pooka 
    369  1.16.2.1  bouyer  out:
    370  1.16.2.1  bouyer 	pthread_sigmask(SIG_SETMASK, &omask, NULL);
    371      1.10   pooka 	return rv;
    372      1.10   pooka }
    373      1.10   pooka 
    374      1.10   pooka static int
    375  1.16.2.1  bouyer prefork_req(struct spclient *spc, sigset_t *omask, void **resp)
    376      1.11   pooka {
    377      1.11   pooka 	struct rsp_hdr rhdr;
    378      1.11   pooka 	struct respwait rw;
    379      1.11   pooka 	int rv;
    380      1.11   pooka 
    381      1.11   pooka 	rhdr.rsp_len = sizeof(rhdr);
    382      1.11   pooka 	rhdr.rsp_class = RUMPSP_REQ;
    383      1.11   pooka 	rhdr.rsp_type = RUMPSP_PREFORK;
    384      1.11   pooka 	rhdr.rsp_error = 0;
    385      1.11   pooka 
    386  1.16.2.1  bouyer 	do {
    387  1.16.2.1  bouyer 		putwait(spc, &rw, &rhdr);
    388  1.16.2.1  bouyer 		rv = send_with_recon(spc, &rhdr, sizeof(rhdr));
    389  1.16.2.1  bouyer 		if (rv != 0) {
    390  1.16.2.1  bouyer 			unputwait(spc, &rw);
    391  1.16.2.1  bouyer 			continue;
    392  1.16.2.1  bouyer 		}
    393  1.16.2.1  bouyer 
    394  1.16.2.1  bouyer 		rv = cliwaitresp(spc, &rw, omask, false);
    395  1.16.2.1  bouyer 		if (rv == ENOTCONN)
    396  1.16.2.1  bouyer 			rv = EAGAIN;
    397  1.16.2.1  bouyer 	} while (rv == EAGAIN);
    398      1.11   pooka 
    399      1.11   pooka 	*resp = rw.rw_data;
    400      1.11   pooka 	return rv;
    401      1.11   pooka }
    402      1.11   pooka 
    403  1.16.2.1  bouyer /*
    404  1.16.2.1  bouyer  * prevent response code from deadlocking with reconnect code
    405  1.16.2.1  bouyer  */
    406      1.11   pooka static int
    407  1.16.2.1  bouyer resp_sendlock(struct spclient *spc)
    408  1.16.2.1  bouyer {
    409  1.16.2.1  bouyer 	int rv = 0;
    410  1.16.2.1  bouyer 
    411  1.16.2.1  bouyer 	pthread_mutex_lock(&spc->spc_mtx);
    412  1.16.2.1  bouyer 	while (spc->spc_ostatus != SPCSTATUS_FREE) {
    413  1.16.2.1  bouyer 		if (__predict_false(spc->spc_reconnecting)) {
    414  1.16.2.1  bouyer 			rv = EBUSY;
    415  1.16.2.1  bouyer 			goto out;
    416  1.16.2.1  bouyer 		}
    417  1.16.2.1  bouyer 		spc->spc_ostatus = SPCSTATUS_WANTED;
    418  1.16.2.1  bouyer 		pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
    419  1.16.2.1  bouyer 	}
    420  1.16.2.1  bouyer 	spc->spc_ostatus = SPCSTATUS_BUSY;
    421  1.16.2.1  bouyer 
    422  1.16.2.1  bouyer  out:
    423  1.16.2.1  bouyer 	pthread_mutex_unlock(&spc->spc_mtx);
    424  1.16.2.1  bouyer 	return rv;
    425  1.16.2.1  bouyer }
    426  1.16.2.1  bouyer 
    427  1.16.2.1  bouyer static void
    428       1.5   pooka send_copyin_resp(struct spclient *spc, uint64_t reqno, void *data, size_t dlen,
    429       1.5   pooka 	int wantstr)
    430       1.1   pooka {
    431       1.1   pooka 	struct rsp_hdr rhdr;
    432       1.1   pooka 
    433       1.5   pooka 	if (wantstr)
    434       1.5   pooka 		dlen = MIN(dlen, strlen(data)+1);
    435       1.5   pooka 
    436       1.1   pooka 	rhdr.rsp_len = sizeof(rhdr) + dlen;
    437       1.1   pooka 	rhdr.rsp_reqno = reqno;
    438       1.3   pooka 	rhdr.rsp_class = RUMPSP_RESP;
    439       1.3   pooka 	rhdr.rsp_type = RUMPSP_COPYIN;
    440       1.1   pooka 	rhdr.rsp_sysnum = 0;
    441       1.1   pooka 
    442  1.16.2.1  bouyer 	if (resp_sendlock(spc) != 0)
    443  1.16.2.1  bouyer 		return;
    444  1.16.2.1  bouyer 	(void)dosend(spc, &rhdr, sizeof(rhdr));
    445  1.16.2.1  bouyer 	(void)dosend(spc, data, dlen);
    446       1.3   pooka 	sendunlock(spc);
    447       1.1   pooka }
    448       1.1   pooka 
    449  1.16.2.1  bouyer static void
    450       1.1   pooka send_anonmmap_resp(struct spclient *spc, uint64_t reqno, void *addr)
    451       1.1   pooka {
    452       1.1   pooka 	struct rsp_hdr rhdr;
    453       1.1   pooka 
    454       1.1   pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(addr);
    455       1.1   pooka 	rhdr.rsp_reqno = reqno;
    456       1.3   pooka 	rhdr.rsp_class = RUMPSP_RESP;
    457       1.3   pooka 	rhdr.rsp_type = RUMPSP_ANONMMAP;
    458       1.1   pooka 	rhdr.rsp_sysnum = 0;
    459       1.1   pooka 
    460  1.16.2.1  bouyer 	if (resp_sendlock(spc) != 0)
    461  1.16.2.1  bouyer 		return;
    462  1.16.2.1  bouyer 	(void)dosend(spc, &rhdr, sizeof(rhdr));
    463  1.16.2.1  bouyer 	(void)dosend(spc, &addr, sizeof(addr));
    464       1.3   pooka 	sendunlock(spc);
    465       1.1   pooka }
    466       1.1   pooka 
    467       1.1   pooka int
    468       1.1   pooka rumpclient_syscall(int sysnum, const void *data, size_t dlen,
    469       1.1   pooka 	register_t *retval)
    470       1.1   pooka {
    471       1.1   pooka 	struct rsp_sysresp *resp;
    472  1.16.2.1  bouyer 	sigset_t omask;
    473       1.3   pooka 	void *rdata;
    474       1.3   pooka 	int rv;
    475       1.3   pooka 
    476  1.16.2.1  bouyer 	pthread_sigmask(SIG_SETMASK, &fullset, &omask);
    477  1.16.2.1  bouyer 
    478       1.3   pooka 	DPRINTF(("rumpsp syscall_req: syscall %d with %p/%zu\n",
    479       1.3   pooka 	    sysnum, data, dlen));
    480       1.3   pooka 
    481  1.16.2.1  bouyer 	rv = syscall_req(&clispc, &omask, sysnum, data, dlen, &rdata);
    482       1.3   pooka 	if (rv)
    483  1.16.2.1  bouyer 		goto out;
    484       1.3   pooka 
    485       1.3   pooka 	resp = rdata;
    486       1.3   pooka 	DPRINTF(("rumpsp syscall_resp: syscall %d error %d, rv: %d/%d\n",
    487       1.3   pooka 	    sysnum, rv, resp->rsys_retval[0], resp->rsys_retval[1]));
    488       1.1   pooka 
    489       1.3   pooka 	memcpy(retval, &resp->rsys_retval, sizeof(resp->rsys_retval));
    490       1.3   pooka 	rv = resp->rsys_error;
    491       1.3   pooka 	free(rdata);
    492       1.1   pooka 
    493  1.16.2.1  bouyer  out:
    494  1.16.2.1  bouyer 	pthread_sigmask(SIG_SETMASK, &omask, NULL);
    495       1.3   pooka 	return rv;
    496       1.3   pooka }
    497       1.1   pooka 
    498       1.3   pooka static void
    499       1.3   pooka handlereq(struct spclient *spc)
    500       1.3   pooka {
    501       1.3   pooka 	struct rsp_copydata *copydata;
    502      1.16   pooka 	struct rsp_hdr *rhdr = &spc->spc_hdr;
    503       1.3   pooka 	void *mapaddr;
    504       1.3   pooka 	size_t maplen;
    505       1.5   pooka 	int reqtype = spc->spc_hdr.rsp_type;
    506       1.1   pooka 
    507       1.5   pooka 	switch (reqtype) {
    508       1.3   pooka 	case RUMPSP_COPYIN:
    509       1.5   pooka 	case RUMPSP_COPYINSTR:
    510       1.3   pooka 		/*LINTED*/
    511       1.3   pooka 		copydata = (struct rsp_copydata *)spc->spc_buf;
    512       1.3   pooka 		DPRINTF(("rump_sp handlereq: copyin request: %p/%zu\n",
    513       1.3   pooka 		    copydata->rcp_addr, copydata->rcp_len));
    514       1.3   pooka 		send_copyin_resp(spc, spc->spc_hdr.rsp_reqno,
    515       1.5   pooka 		    copydata->rcp_addr, copydata->rcp_len,
    516       1.5   pooka 		    reqtype == RUMPSP_COPYINSTR);
    517       1.3   pooka 		break;
    518       1.3   pooka 	case RUMPSP_COPYOUT:
    519       1.5   pooka 	case RUMPSP_COPYOUTSTR:
    520       1.3   pooka 		/*LINTED*/
    521       1.3   pooka 		copydata = (struct rsp_copydata *)spc->spc_buf;
    522       1.3   pooka 		DPRINTF(("rump_sp handlereq: copyout request: %p/%zu\n",
    523       1.3   pooka 		    copydata->rcp_addr, copydata->rcp_len));
    524       1.3   pooka 		/*LINTED*/
    525       1.3   pooka 		memcpy(copydata->rcp_addr, copydata->rcp_data,
    526       1.3   pooka 		    copydata->rcp_len);
    527       1.3   pooka 		break;
    528       1.3   pooka 	case RUMPSP_ANONMMAP:
    529       1.3   pooka 		/*LINTED*/
    530       1.3   pooka 		maplen = *(size_t *)spc->spc_buf;
    531       1.3   pooka 		mapaddr = mmap(NULL, maplen, PROT_READ|PROT_WRITE,
    532       1.3   pooka 		    MAP_ANON, -1, 0);
    533       1.3   pooka 		if (mapaddr == MAP_FAILED)
    534       1.3   pooka 			mapaddr = NULL;
    535       1.3   pooka 		DPRINTF(("rump_sp handlereq: anonmmap: %p\n", mapaddr));
    536       1.3   pooka 		send_anonmmap_resp(spc, spc->spc_hdr.rsp_reqno, mapaddr);
    537       1.3   pooka 		break;
    538      1.16   pooka 	case RUMPSP_RAISE:
    539      1.16   pooka 		DPRINTF(("rump_sp handlereq: raise sig %d\n", rhdr->rsp_signo));
    540  1.16.2.1  bouyer 		raise((int)rhdr->rsp_signo);
    541      1.16   pooka 		/*
    542      1.16   pooka 		 * We most likely have signals blocked, but the signal
    543      1.16   pooka 		 * will be handled soon enough when we return.
    544      1.16   pooka 		 */
    545      1.16   pooka 		break;
    546       1.3   pooka 	default:
    547      1.12   pooka 		printf("PANIC: INVALID TYPE %d\n", reqtype);
    548       1.3   pooka 		abort();
    549       1.3   pooka 		break;
    550       1.1   pooka 	}
    551       1.1   pooka 
    552       1.6   pooka 	spcfreebuf(spc);
    553       1.1   pooka }
    554       1.1   pooka 
    555      1.11   pooka static unsigned ptab_idx;
    556      1.11   pooka static struct sockaddr *serv_sa;
    557      1.11   pooka 
    558  1.16.2.2  bouyer /* dup until we get a "good" fd which does not collide with stdio */
    559  1.16.2.2  bouyer static int
    560  1.16.2.2  bouyer dupgood(int myfd, int mustchange)
    561  1.16.2.2  bouyer {
    562  1.16.2.2  bouyer 	int ofds[4];
    563  1.16.2.2  bouyer 	int i;
    564  1.16.2.2  bouyer 
    565  1.16.2.2  bouyer 	for (i = 0; (myfd <= 2 || mustchange) && myfd != -1; i++) {
    566  1.16.2.2  bouyer 		assert(i < __arraycount(ofds));
    567  1.16.2.2  bouyer 		ofds[i] = myfd;
    568  1.16.2.2  bouyer 		myfd = host_dup(myfd);
    569  1.16.2.2  bouyer 		if (mustchange) {
    570  1.16.2.2  bouyer 			i--; /* prevent closing old fd */
    571  1.16.2.2  bouyer 			mustchange = 0;
    572  1.16.2.2  bouyer 		}
    573  1.16.2.2  bouyer 	}
    574  1.16.2.2  bouyer 
    575  1.16.2.2  bouyer 	for (i--; i >= 0; i--) {
    576  1.16.2.2  bouyer 		host_close(ofds[i]);
    577  1.16.2.2  bouyer 	}
    578  1.16.2.2  bouyer 
    579  1.16.2.2  bouyer 	return myfd;
    580  1.16.2.2  bouyer }
    581  1.16.2.2  bouyer 
    582      1.11   pooka static int
    583  1.16.2.1  bouyer doconnect(bool noisy)
    584       1.1   pooka {
    585  1.16.2.1  bouyer 	struct respwait rw;
    586  1.16.2.1  bouyer 	struct rsp_hdr rhdr;
    587      1.15   pooka 	struct kevent kev[NSIG+1];
    588       1.9   pooka 	char banner[MAXBANNER];
    589  1.16.2.1  bouyer 	struct pollfd pfd;
    590      1.15   pooka 	int s, error, flags, i;
    591       1.9   pooka 	ssize_t n;
    592       1.1   pooka 
    593  1.16.2.1  bouyer 	if (kq != -1)
    594  1.16.2.1  bouyer 		host_close(kq);
    595  1.16.2.1  bouyer 	kq = -1;
    596  1.16.2.1  bouyer 	s = -1;
    597  1.16.2.1  bouyer 
    598  1.16.2.1  bouyer 	if (clispc.spc_fd != -1)
    599  1.16.2.1  bouyer 		host_close(clispc.spc_fd);
    600  1.16.2.1  bouyer 	clispc.spc_fd = -1;
    601  1.16.2.1  bouyer 
    602  1.16.2.1  bouyer 	/*
    603  1.16.2.1  bouyer 	 * for reconnect, gate everyone out of the receiver code
    604  1.16.2.1  bouyer 	 */
    605  1.16.2.1  bouyer 	putwait_locked(&clispc, &rw, &rhdr);
    606  1.16.2.1  bouyer 
    607  1.16.2.1  bouyer 	pthread_mutex_lock(&clispc.spc_mtx);
    608  1.16.2.1  bouyer 	clispc.spc_reconnecting = 1;
    609  1.16.2.1  bouyer 	pthread_cond_broadcast(&clispc.spc_cv);
    610  1.16.2.1  bouyer 	clispc.spc_generation++;
    611  1.16.2.1  bouyer 	while (clispc.spc_istatus != SPCSTATUS_FREE) {
    612  1.16.2.1  bouyer 		clispc.spc_istatus = SPCSTATUS_WANTED;
    613  1.16.2.1  bouyer 		pthread_cond_wait(&rw.rw_cv, &clispc.spc_mtx);
    614  1.16.2.1  bouyer 	}
    615  1.16.2.1  bouyer 	kickall(&clispc);
    616  1.16.2.1  bouyer 
    617  1.16.2.1  bouyer 	/*
    618  1.16.2.1  bouyer 	 * we can release it already since we hold the
    619  1.16.2.1  bouyer 	 * send lock during reconnect
    620  1.16.2.1  bouyer 	 * XXX: assert it
    621  1.16.2.1  bouyer 	 */
    622  1.16.2.1  bouyer 	clispc.spc_istatus = SPCSTATUS_FREE;
    623  1.16.2.1  bouyer 	pthread_mutex_unlock(&clispc.spc_mtx);
    624  1.16.2.1  bouyer 	unputwait_locked(&clispc, &rw);
    625  1.16.2.1  bouyer 
    626  1.16.2.1  bouyer 	free(clispc.spc_buf);
    627  1.16.2.1  bouyer 	clispc.spc_off = 0;
    628  1.16.2.1  bouyer 
    629  1.16.2.2  bouyer 	s = dupgood(host_socket(parsetab[ptab_idx].domain, SOCK_STREAM, 0), 0);
    630      1.11   pooka 	if (s == -1)
    631       1.2   pooka 		return -1;
    632       1.1   pooka 
    633  1.16.2.1  bouyer 	pfd.fd = s;
    634  1.16.2.1  bouyer 	pfd.events = POLLIN;
    635  1.16.2.1  bouyer 	while (host_connect(s, serv_sa, (socklen_t)serv_sa->sa_len) == -1) {
    636  1.16.2.1  bouyer 		if (errno == EINTR)
    637  1.16.2.1  bouyer 			continue;
    638      1.11   pooka 		error = errno;
    639  1.16.2.1  bouyer 		if (noisy)
    640  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: client connect failed: %s\n",
    641  1.16.2.1  bouyer 			    strerror(errno));
    642       1.2   pooka 		errno = error;
    643       1.2   pooka 		return -1;
    644       1.2   pooka 	}
    645       1.1   pooka 
    646      1.11   pooka 	if ((error = parsetab[ptab_idx].connhook(s)) != 0) {
    647       1.2   pooka 		error = errno;
    648  1.16.2.1  bouyer 		if (noisy)
    649  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: connect hook failed\n");
    650       1.2   pooka 		errno = error;
    651       1.2   pooka 		return -1;
    652       1.1   pooka 	}
    653       1.4   pooka 
    654      1.13   pooka 	if ((n = host_read(s, banner, sizeof(banner)-1)) < 0) {
    655       1.2   pooka 		error = errno;
    656  1.16.2.1  bouyer 		if (noisy)
    657  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: failed to read banner\n");
    658       1.2   pooka 		errno = error;
    659       1.2   pooka 		return -1;
    660       1.1   pooka 	}
    661       1.9   pooka 
    662       1.9   pooka 	if (banner[n-1] != '\n') {
    663  1.16.2.1  bouyer 		if (noisy)
    664  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: invalid banner\n");
    665       1.9   pooka 		errno = EINVAL;
    666       1.9   pooka 		return -1;
    667       1.9   pooka 	}
    668       1.9   pooka 	banner[n] = '\0';
    669  1.16.2.1  bouyer 	/* parse the banner some day */
    670       1.9   pooka 
    671      1.15   pooka 	flags = host_fcntl(s, F_GETFL, 0);
    672      1.15   pooka 	if (host_fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) {
    673  1.16.2.1  bouyer 		if (noisy)
    674  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: socket fd NONBLOCK: %s\n",
    675  1.16.2.1  bouyer 			    strerror(errno));
    676      1.15   pooka 		errno = EINVAL;
    677      1.15   pooka 		return -1;
    678      1.15   pooka 	}
    679  1.16.2.1  bouyer 	clispc.spc_fd = s;
    680  1.16.2.1  bouyer 	clispc.spc_state = SPCSTATE_RUNNING;
    681  1.16.2.1  bouyer 	clispc.spc_reconnecting = 0;
    682       1.9   pooka 
    683      1.15   pooka 	/* setup kqueue, we want all signals and the fd */
    684  1.16.2.2  bouyer 	if ((kq = dupgood(host_kqueue(), 0)) == -1) {
    685      1.15   pooka 		error = errno;
    686  1.16.2.1  bouyer 		if (noisy)
    687  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: cannot setup kqueue");
    688      1.15   pooka 		errno = error;
    689      1.15   pooka 		return -1;
    690      1.15   pooka 	}
    691      1.15   pooka 
    692      1.15   pooka 	for (i = 0; i < NSIG; i++) {
    693      1.15   pooka 		EV_SET(&kev[i], i+1, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0, 0, 0);
    694      1.15   pooka 	}
    695  1.16.2.1  bouyer 	EV_SET(&kev[NSIG], clispc.spc_fd,
    696  1.16.2.1  bouyer 	    EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
    697  1.16.2.1  bouyer 	if (host_kevent(kq, kev, NSIG+1, NULL, 0, NULL) == -1) {
    698      1.15   pooka 		error = errno;
    699  1.16.2.1  bouyer 		if (noisy)
    700  1.16.2.1  bouyer 			fprintf(stderr, "rump_sp: kevent() failed");
    701      1.15   pooka 		errno = error;
    702      1.15   pooka 		return -1;
    703      1.15   pooka 	}
    704      1.15   pooka 
    705  1.16.2.1  bouyer 	return 0;
    706  1.16.2.1  bouyer }
    707  1.16.2.1  bouyer 
    708  1.16.2.1  bouyer static int
    709  1.16.2.1  bouyer doinit(void)
    710  1.16.2.1  bouyer {
    711  1.16.2.1  bouyer 
    712      1.11   pooka 	TAILQ_INIT(&clispc.spc_respwait);
    713      1.11   pooka 	pthread_mutex_init(&clispc.spc_mtx, NULL);
    714      1.11   pooka 	pthread_cond_init(&clispc.spc_cv, NULL);
    715      1.11   pooka 
    716      1.11   pooka 	return 0;
    717      1.11   pooka }
    718      1.11   pooka 
    719      1.13   pooka void *(*rumpclient_dlsym)(void *, const char *);
    720  1.16.2.2  bouyer static int init_done = 0;
    721      1.13   pooka 
    722      1.11   pooka int
    723      1.11   pooka rumpclient_init()
    724      1.11   pooka {
    725      1.11   pooka 	char *p;
    726      1.11   pooka 	int error;
    727  1.16.2.2  bouyer 	int rv = -1;
    728  1.16.2.2  bouyer 	int hstype;
    729  1.16.2.2  bouyer 
    730  1.16.2.2  bouyer 	if (init_done)
    731  1.16.2.2  bouyer 		return 0;
    732  1.16.2.2  bouyer 	init_done = 1;
    733      1.11   pooka 
    734  1.16.2.1  bouyer 	sigfillset(&fullset);
    735  1.16.2.1  bouyer 
    736      1.13   pooka 	/* dlsym overrided by rumphijack? */
    737      1.13   pooka 	if (!rumpclient_dlsym)
    738      1.13   pooka 		rumpclient_dlsym = dlsym;
    739      1.13   pooka 
    740      1.13   pooka 	/*
    741      1.13   pooka 	 * sag mir, wo die symbol sind.  zogen fort, der krieg beginnt.
    742      1.13   pooka 	 * wann wird man je verstehen?  wann wird man je verstehen?
    743      1.13   pooka 	 */
    744      1.13   pooka #define FINDSYM2(_name_,_syscall_)					\
    745      1.13   pooka 	if ((host_##_name_ = rumpclient_dlsym(RTLD_NEXT,		\
    746      1.13   pooka 	    #_syscall_)) == NULL)					\
    747      1.13   pooka 		/* host_##_name_ = _syscall_ */;
    748      1.13   pooka #define FINDSYM(_name_) FINDSYM2(_name_,_name_)
    749      1.13   pooka 	FINDSYM2(socket,__socket30);
    750      1.13   pooka 	FINDSYM(close);
    751      1.13   pooka 	FINDSYM(connect);
    752      1.15   pooka 	FINDSYM(fcntl);
    753      1.13   pooka 	FINDSYM(poll);
    754      1.13   pooka 	FINDSYM(read);
    755      1.13   pooka 	FINDSYM(sendto);
    756      1.13   pooka 	FINDSYM(setsockopt);
    757  1.16.2.2  bouyer 	FINDSYM(dup);
    758  1.16.2.1  bouyer 	FINDSYM(kqueue);
    759  1.16.2.2  bouyer 	FINDSYM(execve);
    760  1.16.2.1  bouyer #if !__NetBSD_Prereq__(5,99,7)
    761  1.16.2.1  bouyer 	FINDSYM(kevent);
    762  1.16.2.1  bouyer #else
    763  1.16.2.1  bouyer 	FINDSYM2(kevent,_sys___kevent50);
    764  1.16.2.1  bouyer #endif
    765      1.13   pooka #undef	FINDSYM
    766      1.13   pooka #undef	FINDSY2
    767      1.13   pooka 
    768  1.16.2.2  bouyer 	if ((p = getenv("RUMP__PARSEDSERVER")) == NULL) {
    769  1.16.2.2  bouyer 		if ((p = getenv("RUMP_SERVER")) == NULL) {
    770  1.16.2.2  bouyer 			errno = ENOENT;
    771  1.16.2.2  bouyer 			goto out;
    772  1.16.2.2  bouyer 		}
    773      1.11   pooka 	}
    774      1.11   pooka 
    775      1.11   pooka 	if ((error = parseurl(p, &serv_sa, &ptab_idx, 0)) != 0) {
    776      1.11   pooka 		errno = error;
    777  1.16.2.2  bouyer 		goto out;
    778      1.11   pooka 	}
    779      1.11   pooka 
    780  1.16.2.1  bouyer 	if (doinit() == -1)
    781  1.16.2.2  bouyer 		goto out;
    782  1.16.2.2  bouyer 
    783  1.16.2.2  bouyer 	if ((p = getenv("RUMPCLIENT__EXECFD")) != NULL) {
    784  1.16.2.2  bouyer 		sscanf(p, "%d,%d", &clispc.spc_fd, &kq);
    785  1.16.2.2  bouyer 		unsetenv("RUMPCLIENT__EXECFD");
    786  1.16.2.2  bouyer 		hstype = HANDSHAKE_EXEC;
    787  1.16.2.2  bouyer 	} else {
    788  1.16.2.2  bouyer 		if (doconnect(true) == -1)
    789  1.16.2.2  bouyer 			goto out;
    790  1.16.2.2  bouyer 		hstype = HANDSHAKE_GUEST;
    791  1.16.2.2  bouyer 	}
    792      1.11   pooka 
    793  1.16.2.2  bouyer 	error = handshake_req(&clispc, hstype, NULL, 0, false);
    794      1.11   pooka 	if (error) {
    795      1.11   pooka 		pthread_mutex_destroy(&clispc.spc_mtx);
    796      1.11   pooka 		pthread_cond_destroy(&clispc.spc_cv);
    797  1.16.2.1  bouyer 		if (clispc.spc_fd != -1)
    798  1.16.2.1  bouyer 			host_close(clispc.spc_fd);
    799      1.10   pooka 		errno = error;
    800  1.16.2.2  bouyer 		goto out;
    801      1.10   pooka 	}
    802  1.16.2.2  bouyer 	rv = 0;
    803      1.10   pooka 
    804  1.16.2.2  bouyer  out:
    805  1.16.2.2  bouyer 	if (rv == -1)
    806  1.16.2.2  bouyer 		init_done = 0;
    807  1.16.2.2  bouyer 	return rv;
    808      1.11   pooka }
    809      1.11   pooka 
    810      1.11   pooka struct rumpclient_fork {
    811      1.11   pooka 	uint32_t fork_auth[AUTHLEN];
    812  1.16.2.2  bouyer 	struct spclient fork_spc;
    813  1.16.2.2  bouyer 	int fork_kq;
    814      1.11   pooka };
    815      1.11   pooka 
    816      1.11   pooka struct rumpclient_fork *
    817      1.11   pooka rumpclient_prefork(void)
    818      1.11   pooka {
    819      1.11   pooka 	struct rumpclient_fork *rpf;
    820  1.16.2.1  bouyer 	sigset_t omask;
    821      1.11   pooka 	void *resp;
    822      1.11   pooka 	int rv;
    823      1.11   pooka 
    824  1.16.2.1  bouyer 	pthread_sigmask(SIG_SETMASK, &fullset, &omask);
    825      1.11   pooka 	rpf = malloc(sizeof(*rpf));
    826      1.11   pooka 	if (rpf == NULL)
    827  1.16.2.2  bouyer 		goto out;
    828      1.11   pooka 
    829  1.16.2.1  bouyer 	if ((rv = prefork_req(&clispc, &omask, &resp)) != 0) {
    830      1.11   pooka 		free(rpf);
    831      1.11   pooka 		errno = rv;
    832  1.16.2.1  bouyer 		rpf = NULL;
    833  1.16.2.1  bouyer 		goto out;
    834      1.11   pooka 	}
    835      1.11   pooka 
    836      1.11   pooka 	memcpy(rpf->fork_auth, resp, sizeof(rpf->fork_auth));
    837      1.11   pooka 	free(resp);
    838      1.11   pooka 
    839  1.16.2.2  bouyer 	rpf->fork_spc = clispc;
    840  1.16.2.2  bouyer 	rpf->fork_kq = kq;
    841  1.16.2.2  bouyer 
    842  1.16.2.1  bouyer  out:
    843  1.16.2.1  bouyer 	pthread_sigmask(SIG_SETMASK, &omask, NULL);
    844      1.11   pooka 	return rpf;
    845      1.11   pooka }
    846      1.11   pooka 
    847      1.11   pooka int
    848      1.11   pooka rumpclient_fork_init(struct rumpclient_fork *rpf)
    849      1.11   pooka {
    850      1.11   pooka 	int error;
    851  1.16.2.1  bouyer 	int osock;
    852      1.11   pooka 
    853  1.16.2.1  bouyer 	osock = clispc.spc_fd;
    854      1.11   pooka 	memset(&clispc, 0, sizeof(clispc));
    855  1.16.2.1  bouyer 	clispc.spc_fd = osock;
    856  1.16.2.1  bouyer 
    857  1.16.2.1  bouyer 	kq = -1; /* kqueue descriptor is not copied over fork() */
    858      1.11   pooka 
    859  1.16.2.1  bouyer 	if (doinit() == -1)
    860  1.16.2.1  bouyer 		return -1;
    861  1.16.2.1  bouyer 	if (doconnect(false) == -1)
    862      1.11   pooka 		return -1;
    863      1.10   pooka 
    864  1.16.2.2  bouyer 	error = handshake_req(&clispc, HANDSHAKE_FORK, rpf->fork_auth,
    865  1.16.2.2  bouyer 	    0, false);
    866      1.10   pooka 	if (error) {
    867      1.10   pooka 		pthread_mutex_destroy(&clispc.spc_mtx);
    868      1.10   pooka 		pthread_cond_destroy(&clispc.spc_cv);
    869      1.11   pooka 		errno = error;
    870      1.11   pooka 		return -1;
    871      1.10   pooka 	}
    872      1.11   pooka 
    873      1.11   pooka 	return 0;
    874       1.1   pooka }
    875  1.16.2.1  bouyer 
    876  1.16.2.1  bouyer void
    877  1.16.2.2  bouyer rumpclient_fork_cancel(struct rumpclient_fork *rpf)
    878  1.16.2.2  bouyer {
    879  1.16.2.2  bouyer 
    880  1.16.2.2  bouyer 	/* EUNIMPL */
    881  1.16.2.2  bouyer }
    882  1.16.2.2  bouyer 
    883  1.16.2.2  bouyer void
    884  1.16.2.2  bouyer rumpclient_fork_vparent(struct rumpclient_fork *rpf)
    885  1.16.2.2  bouyer {
    886  1.16.2.2  bouyer 
    887  1.16.2.2  bouyer 	clispc = rpf->fork_spc;
    888  1.16.2.2  bouyer 	kq = rpf->fork_kq;
    889  1.16.2.2  bouyer }
    890  1.16.2.2  bouyer 
    891  1.16.2.2  bouyer void
    892  1.16.2.1  bouyer rumpclient_setconnretry(time_t timeout)
    893  1.16.2.1  bouyer {
    894  1.16.2.1  bouyer 
    895  1.16.2.1  bouyer 	if (timeout < RUMPCLIENT_RETRYCONN_DIE)
    896  1.16.2.1  bouyer 		return; /* gigo */
    897  1.16.2.1  bouyer 
    898  1.16.2.1  bouyer 	retrytimo = timeout;
    899  1.16.2.1  bouyer }
    900  1.16.2.2  bouyer 
    901  1.16.2.2  bouyer int
    902  1.16.2.2  bouyer rumpclient__closenotify(int *fdp, enum rumpclient_closevariant variant)
    903  1.16.2.2  bouyer {
    904  1.16.2.2  bouyer 	int fd = *fdp;
    905  1.16.2.2  bouyer 	int untilfd, rv;
    906  1.16.2.2  bouyer 	int newfd;
    907  1.16.2.2  bouyer 
    908  1.16.2.2  bouyer 	switch (variant) {
    909  1.16.2.2  bouyer 	case RUMPCLIENT_CLOSE_FCLOSEM:
    910  1.16.2.2  bouyer 		untilfd = MAX(clispc.spc_fd, kq);
    911  1.16.2.2  bouyer 		for (; fd <= untilfd; fd++) {
    912  1.16.2.2  bouyer 			if (fd == clispc.spc_fd || fd == kq)
    913  1.16.2.2  bouyer 				continue;
    914  1.16.2.2  bouyer 			rv = host_close(fd);
    915  1.16.2.2  bouyer 			if (rv == -1)
    916  1.16.2.2  bouyer 				return -1;
    917  1.16.2.2  bouyer 		}
    918  1.16.2.2  bouyer 		*fdp = fd;
    919  1.16.2.2  bouyer 		break;
    920  1.16.2.2  bouyer 
    921  1.16.2.2  bouyer 	case RUMPCLIENT_CLOSE_CLOSE:
    922  1.16.2.2  bouyer 	case RUMPCLIENT_CLOSE_DUP2:
    923  1.16.2.2  bouyer 		if (fd == clispc.spc_fd) {
    924  1.16.2.2  bouyer 			struct kevent kev[2];
    925  1.16.2.2  bouyer 
    926  1.16.2.2  bouyer 			newfd = dupgood(clispc.spc_fd, 1);
    927  1.16.2.2  bouyer 			if (newfd == -1)
    928  1.16.2.2  bouyer 				return -1;
    929  1.16.2.2  bouyer 			/*
    930  1.16.2.2  bouyer 			 * now, we have a new socket number, so change
    931  1.16.2.2  bouyer 			 * the file descriptor that kqueue is
    932  1.16.2.2  bouyer 			 * monitoring.  remove old and add new.
    933  1.16.2.2  bouyer 			 */
    934  1.16.2.2  bouyer 			EV_SET(&kev[0], clispc.spc_fd,
    935  1.16.2.2  bouyer 			    EVFILT_READ, EV_DELETE, 0, 0, 0);
    936  1.16.2.2  bouyer 			EV_SET(&kev[1], newfd,
    937  1.16.2.2  bouyer 			    EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
    938  1.16.2.2  bouyer 			if (host_kevent(kq, kev, 2, NULL, 0, NULL) == -1) {
    939  1.16.2.2  bouyer 				int sverrno = errno;
    940  1.16.2.2  bouyer 				host_close(newfd);
    941  1.16.2.2  bouyer 				errno = sverrno;
    942  1.16.2.2  bouyer 				return -1;
    943  1.16.2.2  bouyer 			}
    944  1.16.2.2  bouyer 			clispc.spc_fd = newfd;
    945  1.16.2.2  bouyer 		}
    946  1.16.2.2  bouyer 		if (fd == kq) {
    947  1.16.2.2  bouyer 			newfd = dupgood(kq, 1);
    948  1.16.2.2  bouyer 			if (newfd == -1)
    949  1.16.2.2  bouyer 				return -1;
    950  1.16.2.2  bouyer 			kq = newfd;
    951  1.16.2.2  bouyer 		}
    952  1.16.2.2  bouyer 		break;
    953  1.16.2.2  bouyer 	}
    954  1.16.2.2  bouyer 
    955  1.16.2.2  bouyer 	return 0;
    956  1.16.2.2  bouyer }
    957  1.16.2.2  bouyer 
    958  1.16.2.2  bouyer pid_t
    959  1.16.2.2  bouyer rumpclient_fork()
    960  1.16.2.2  bouyer {
    961  1.16.2.2  bouyer 
    962  1.16.2.2  bouyer 	return rumpclient__dofork(fork);
    963  1.16.2.2  bouyer }
    964  1.16.2.2  bouyer 
    965  1.16.2.2  bouyer /*
    966  1.16.2.2  bouyer  * Process is about to exec.  Save info about our existing connection
    967  1.16.2.2  bouyer  * in the env.  rumpclient will check for this info in init().
    968  1.16.2.2  bouyer  * This is mostly for the benefit of rumphijack, but regular applications
    969  1.16.2.2  bouyer  * may use it as well.
    970  1.16.2.2  bouyer  */
    971  1.16.2.2  bouyer int
    972  1.16.2.2  bouyer rumpclient_exec(const char *path, char *const argv[], char *const envp[])
    973  1.16.2.2  bouyer {
    974  1.16.2.2  bouyer 	char buf[4096];
    975  1.16.2.2  bouyer 	char **newenv;
    976  1.16.2.2  bouyer 	char *envstr, *envstr2;
    977  1.16.2.2  bouyer 	size_t nelem;
    978  1.16.2.2  bouyer 	int rv, sverrno;
    979  1.16.2.2  bouyer 
    980  1.16.2.2  bouyer 	snprintf(buf, sizeof(buf), "RUMPCLIENT__EXECFD=%d,%d",
    981  1.16.2.2  bouyer 	    clispc.spc_fd, kq);
    982  1.16.2.2  bouyer 	envstr = malloc(strlen(buf)+1);
    983  1.16.2.2  bouyer 	if (envstr == NULL) {
    984  1.16.2.2  bouyer 		return ENOMEM;
    985  1.16.2.2  bouyer 	}
    986  1.16.2.2  bouyer 	strcpy(envstr, buf);
    987  1.16.2.2  bouyer 
    988  1.16.2.2  bouyer 	/* do we have a fully parsed url we want to forward in the env? */
    989  1.16.2.2  bouyer 	if (*parsedurl != '\0') {
    990  1.16.2.2  bouyer 		snprintf(buf, sizeof(buf),
    991  1.16.2.2  bouyer 		    "RUMP__PARSEDSERVER=%s", parsedurl);
    992  1.16.2.2  bouyer 		envstr2 = malloc(strlen(buf)+1);
    993  1.16.2.2  bouyer 		if (envstr2 == NULL) {
    994  1.16.2.2  bouyer 			free(envstr);
    995  1.16.2.2  bouyer 			return ENOMEM;
    996  1.16.2.2  bouyer 		}
    997  1.16.2.2  bouyer 		strcpy(envstr2, buf);
    998  1.16.2.2  bouyer 	} else {
    999  1.16.2.2  bouyer 		envstr2 = NULL;
   1000  1.16.2.2  bouyer 	}
   1001  1.16.2.2  bouyer 
   1002  1.16.2.2  bouyer 	for (nelem = 0; envp && envp[nelem]; nelem++)
   1003  1.16.2.2  bouyer 		continue;
   1004  1.16.2.2  bouyer 
   1005  1.16.2.2  bouyer 	newenv = malloc(sizeof(*newenv) * nelem+3);
   1006  1.16.2.2  bouyer 	if (newenv == NULL) {
   1007  1.16.2.2  bouyer 		free(envstr2);
   1008  1.16.2.2  bouyer 		free(envstr);
   1009  1.16.2.2  bouyer 		return ENOMEM;
   1010  1.16.2.2  bouyer 	}
   1011  1.16.2.2  bouyer 	memcpy(&newenv[0], envp, nelem*sizeof(*envp));
   1012  1.16.2.2  bouyer 
   1013  1.16.2.2  bouyer 	newenv[nelem] = envstr;
   1014  1.16.2.2  bouyer 	newenv[nelem+1] = envstr2;
   1015  1.16.2.2  bouyer 	newenv[nelem+2] = NULL;
   1016  1.16.2.2  bouyer 
   1017  1.16.2.2  bouyer 	rv = host_execve(path, argv, newenv);
   1018  1.16.2.2  bouyer 
   1019  1.16.2.2  bouyer 	_DIAGASSERT(rv != 0);
   1020  1.16.2.2  bouyer 	sverrno = errno;
   1021  1.16.2.2  bouyer 	free(envstr2);
   1022  1.16.2.2  bouyer 	free(envstr);
   1023  1.16.2.2  bouyer 	free(newenv);
   1024  1.16.2.2  bouyer 	errno = sverrno;
   1025  1.16.2.2  bouyer 	return rv;
   1026  1.16.2.2  bouyer }
   1027  1.16.2.2  bouyer 
   1028  1.16.2.2  bouyer int
   1029  1.16.2.2  bouyer rumpclient_daemon(int nochdir, int noclose)
   1030  1.16.2.2  bouyer {
   1031  1.16.2.2  bouyer 	struct rumpclient_fork *rf;
   1032  1.16.2.2  bouyer 	int sverrno;
   1033  1.16.2.2  bouyer 
   1034  1.16.2.2  bouyer 	if ((rf = rumpclient_prefork()) == NULL)
   1035  1.16.2.2  bouyer 		return -1;
   1036  1.16.2.2  bouyer 
   1037  1.16.2.2  bouyer 	if (daemon(nochdir, noclose) == -1) {
   1038  1.16.2.2  bouyer 		sverrno = errno;
   1039  1.16.2.2  bouyer 		rumpclient_fork_cancel(rf);
   1040  1.16.2.2  bouyer 		errno = sverrno;
   1041  1.16.2.2  bouyer 		return -1;
   1042  1.16.2.2  bouyer 	}
   1043  1.16.2.2  bouyer 
   1044  1.16.2.2  bouyer 	if (rumpclient_fork_init(rf) == -1)
   1045  1.16.2.2  bouyer 		return -1;
   1046  1.16.2.2  bouyer 
   1047  1.16.2.2  bouyer 	return 0;
   1048  1.16.2.2  bouyer }
   1049