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