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