Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_sp.c revision 1.21
      1 /*      $NetBSD: rumpuser_sp.c,v 1.21 2010/11/29 16:08:03 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010 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  * Sysproxy routines.  This provides system RPC support over host sockets.
     30  * The most notable limitation is that the client and server must share
     31  * the same ABI.  This does not mean that they have to be the same
     32  * machine or that they need to run the same version of the host OS,
     33  * just that they must agree on the data structures.  This even *might*
     34  * work correctly from one hardware architecture to another.
     35  *
     36  * Not finished yet, i.e. don't use in production.  Lacks locking plus
     37  * handling of multiple clients and unexpected connection closes.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __RCSID("$NetBSD: rumpuser_sp.c,v 1.21 2010/11/29 16:08:03 pooka Exp $");
     42 
     43 #include <sys/types.h>
     44 #include <sys/atomic.h>
     45 #include <sys/mman.h>
     46 #include <sys/socket.h>
     47 
     48 #include <arpa/inet.h>
     49 #include <netinet/in.h>
     50 #include <netinet/tcp.h>
     51 
     52 #include <assert.h>
     53 #include <errno.h>
     54 #include <fcntl.h>
     55 #include <poll.h>
     56 #include <pthread.h>
     57 #include <stdarg.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <unistd.h>
     62 
     63 #include <rump/rumpuser.h>
     64 #include "rumpuser_int.h"
     65 
     66 #include "sp_common.c"
     67 
     68 #ifndef MAXCLI
     69 #define MAXCLI 256
     70 #endif
     71 #ifndef MAXWORKER
     72 #define MAXWORKER 128
     73 #endif
     74 #ifndef IDLEWORKER
     75 #define IDLEWORKER 16
     76 #endif
     77 int rumpsp_maxworker = MAXWORKER;
     78 int rumpsp_idleworker = IDLEWORKER;
     79 
     80 static struct pollfd pfdlist[MAXCLI];
     81 static struct spclient spclist[MAXCLI];
     82 static unsigned int disco;
     83 
     84 static struct rumpuser_sp_ops spops;
     85 
     86 /*
     87  * Manual wrappers, since librump does not have access to the
     88  * user namespace wrapped interfaces.
     89  */
     90 
     91 static void
     92 lwproc_switch(struct lwp *l)
     93 {
     94 
     95 	spops.spop_schedule();
     96 	spops.spop_lwproc_switch(l);
     97 	spops.spop_unschedule();
     98 }
     99 
    100 static void
    101 lwproc_release(void)
    102 {
    103 
    104 	spops.spop_schedule();
    105 	spops.spop_lwproc_release();
    106 	spops.spop_unschedule();
    107 }
    108 
    109 static int
    110 lwproc_newproc(struct spclient *spc)
    111 {
    112 	int rv;
    113 
    114 	spops.spop_schedule();
    115 	rv = spops.spop_lwproc_newproc(spc);
    116 	spops.spop_unschedule();
    117 
    118 	return rv;
    119 }
    120 
    121 static int
    122 lwproc_newlwp(pid_t pid)
    123 {
    124 	int rv;
    125 
    126 	spops.spop_schedule();
    127 	rv = spops.spop_lwproc_newlwp(pid);
    128 	spops.spop_unschedule();
    129 
    130 	return rv;
    131 }
    132 
    133 static struct lwp *
    134 lwproc_curlwp(void)
    135 {
    136 	struct lwp *l;
    137 
    138 	spops.spop_schedule();
    139 	l = spops.spop_lwproc_curlwp();
    140 	spops.spop_unschedule();
    141 
    142 	return l;
    143 }
    144 
    145 static pid_t
    146 lwproc_getpid(void)
    147 {
    148 	pid_t p;
    149 
    150 	spops.spop_schedule();
    151 	p = spops.spop_getpid();
    152 	spops.spop_unschedule();
    153 
    154 	return p;
    155 }
    156 
    157 static int
    158 rumpsyscall(int sysnum, void *data, register_t *retval)
    159 {
    160 	int rv;
    161 
    162 	spops.spop_schedule();
    163 	rv = spops.spop_syscall(sysnum, data, retval);
    164 	spops.spop_unschedule();
    165 
    166 	return rv;
    167 }
    168 
    169 static uint64_t
    170 nextreq(struct spclient *spc)
    171 {
    172 	uint64_t nw;
    173 
    174 	pthread_mutex_lock(&spc->spc_mtx);
    175 	nw = spc->spc_nextreq++;
    176 	pthread_mutex_unlock(&spc->spc_mtx);
    177 
    178 	return nw;
    179 }
    180 
    181 static void
    182 send_error_resp(struct spclient *spc, uint64_t reqno, int error)
    183 {
    184 	struct rsp_hdr rhdr;
    185 
    186 	rhdr.rsp_len = sizeof(rhdr);
    187 	rhdr.rsp_reqno = reqno;
    188 	rhdr.rsp_class = RUMPSP_ERROR;
    189 	rhdr.rsp_type = 0;
    190 	rhdr.rsp_error = error;
    191 
    192 	sendlock(spc);
    193 	(void)dosend(spc, &rhdr, sizeof(rhdr));
    194 	sendunlock(spc);
    195 }
    196 
    197 static int
    198 send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
    199 	register_t *retval)
    200 {
    201 	struct rsp_hdr rhdr;
    202 	struct rsp_sysresp sysresp;
    203 	int rv;
    204 
    205 	rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
    206 	rhdr.rsp_reqno = reqno;
    207 	rhdr.rsp_class = RUMPSP_RESP;
    208 	rhdr.rsp_type = RUMPSP_SYSCALL;
    209 	rhdr.rsp_sysnum = 0;
    210 
    211 	sysresp.rsys_error = error;
    212 	memcpy(sysresp.rsys_retval, retval, sizeof(sysresp.rsys_retval));
    213 
    214 	sendlock(spc);
    215 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    216 	rv = dosend(spc, &sysresp, sizeof(sysresp));
    217 	sendunlock(spc);
    218 
    219 	return rv;
    220 }
    221 
    222 static int
    223 copyin_req(struct spclient *spc, const void *remaddr, size_t *dlen,
    224 	int wantstr, void **resp)
    225 {
    226 	struct rsp_hdr rhdr;
    227 	struct rsp_copydata copydata;
    228 	struct respwait rw;
    229 	int rv;
    230 
    231 	DPRINTF(("copyin_req: %zu bytes from %p\n", *dlen, remaddr));
    232 
    233 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
    234 	rhdr.rsp_class = RUMPSP_REQ;
    235 	if (wantstr)
    236 		rhdr.rsp_type = RUMPSP_COPYINSTR;
    237 	else
    238 		rhdr.rsp_type = RUMPSP_COPYIN;
    239 	rhdr.rsp_sysnum = 0;
    240 
    241 	copydata.rcp_addr = __UNCONST(remaddr);
    242 	copydata.rcp_len = *dlen;
    243 
    244 	putwait(spc, &rw, &rhdr);
    245 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    246 	rv = dosend(spc, &copydata, sizeof(copydata));
    247 	if (rv) {
    248 		unputwait(spc, &rw);
    249 		return rv;
    250 	}
    251 
    252 	rv = waitresp(spc, &rw);
    253 
    254 	DPRINTF(("copyin: response %d\n", rv));
    255 
    256 	*resp = rw.rw_data;
    257 	if (wantstr)
    258 		*dlen = rw.rw_dlen;
    259 
    260 	return rv;
    261 
    262 }
    263 
    264 static int
    265 send_copyout_req(struct spclient *spc, const void *remaddr,
    266 	const void *data, size_t dlen)
    267 {
    268 	struct rsp_hdr rhdr;
    269 	struct rsp_copydata copydata;
    270 	int rv;
    271 
    272 	DPRINTF(("copyout_req (async): %zu bytes to %p\n", dlen, remaddr));
    273 
    274 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
    275 	rhdr.rsp_reqno = nextreq(spc);
    276 	rhdr.rsp_class = RUMPSP_REQ;
    277 	rhdr.rsp_type = RUMPSP_COPYOUT;
    278 	rhdr.rsp_sysnum = 0;
    279 
    280 	copydata.rcp_addr = __UNCONST(remaddr);
    281 	copydata.rcp_len = dlen;
    282 
    283 	sendlock(spc);
    284 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    285 	rv = dosend(spc, &copydata, sizeof(copydata));
    286 	rv = dosend(spc, data, dlen);
    287 	sendunlock(spc);
    288 
    289 	return rv;
    290 }
    291 
    292 static int
    293 anonmmap_req(struct spclient *spc, size_t howmuch, void **resp)
    294 {
    295 	struct rsp_hdr rhdr;
    296 	struct respwait rw;
    297 	int rv;
    298 
    299 	DPRINTF(("anonmmap_req: %zu bytes\n", howmuch));
    300 
    301 	rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
    302 	rhdr.rsp_class = RUMPSP_REQ;
    303 	rhdr.rsp_type = RUMPSP_ANONMMAP;
    304 	rhdr.rsp_sysnum = 0;
    305 
    306 	putwait(spc, &rw, &rhdr);
    307 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    308 	rv = dosend(spc, &howmuch, sizeof(howmuch));
    309 	if (rv) {
    310 		unputwait(spc, &rw);
    311 		return rv;
    312 	}
    313 
    314 	rv = waitresp(spc, &rw);
    315 
    316 	*resp = rw.rw_data;
    317 
    318 	DPRINTF(("anonmmap: mapped at %p\n", **(void ***)resp));
    319 
    320 	return rv;
    321 }
    322 
    323 static void
    324 spcref(struct spclient *spc)
    325 {
    326 
    327 	pthread_mutex_lock(&spc->spc_mtx);
    328 	spc->spc_refcnt++;
    329 	pthread_mutex_unlock(&spc->spc_mtx);
    330 }
    331 
    332 static void
    333 spcrelease(struct spclient *spc)
    334 {
    335 	int ref;
    336 
    337 	pthread_mutex_lock(&spc->spc_mtx);
    338 	ref = --spc->spc_refcnt;
    339 	pthread_mutex_unlock(&spc->spc_mtx);
    340 
    341 	if (ref > 0)
    342 		return;
    343 
    344 	DPRINTF(("spcrelease: spc %p fd %d\n", spc, spc->spc_fd));
    345 
    346 	_DIAGASSERT(TAILQ_EMPTY(&spc->spc_respwait));
    347 	_DIAGASSERT(spc->spc_buf == NULL);
    348 
    349 	lwproc_switch(spc->spc_mainlwp);
    350 	lwproc_release();
    351 	spc->spc_mainlwp = NULL;
    352 
    353 	close(spc->spc_fd);
    354 	spc->spc_fd = -1;
    355 	spc->spc_dying = 0;
    356 
    357 	atomic_inc_uint(&disco);
    358 }
    359 
    360 static void
    361 serv_handledisco(unsigned int idx)
    362 {
    363 	struct spclient *spc = &spclist[idx];
    364 
    365 	DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
    366 
    367 	pfdlist[idx].fd = -1;
    368 	pfdlist[idx].revents = 0;
    369 	pthread_mutex_lock(&spc->spc_mtx);
    370 	spc->spc_dying = 1;
    371 	kickall(spc);
    372 	pthread_mutex_unlock(&spc->spc_mtx);
    373 
    374 	/*
    375 	 * Nobody's going to attempt to send/receive anymore,
    376 	 * so reinit info relevant to that.
    377 	 */
    378 	memset((char *)spc + SPC_ZEROFF, 0, sizeof(*spc) - SPC_ZEROFF);
    379 
    380 	spcrelease(spc);
    381 }
    382 
    383 static unsigned
    384 serv_handleconn(int fd, connecthook_fn connhook, int busy)
    385 {
    386 	struct sockaddr_storage ss;
    387 	socklen_t sl = sizeof(ss);
    388 	int newfd, flags;
    389 	unsigned i;
    390 
    391 	/*LINTED: cast ok */
    392 	newfd = accept(fd, (struct sockaddr *)&ss, &sl);
    393 	if (newfd == -1)
    394 		return 0;
    395 
    396 	if (busy) {
    397 		close(newfd); /* EBUSY */
    398 		return 0;
    399 	}
    400 
    401 	/* XXX: should do some sort of handshake too */
    402 
    403 	flags = fcntl(newfd, F_GETFL, 0);
    404 	if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
    405 		close(newfd);
    406 		return 0;
    407 	}
    408 
    409 	if (connhook(newfd) != 0) {
    410 		close(newfd);
    411 		return 0;
    412 	}
    413 
    414 	/* find empty slot the simple way */
    415 	for (i = 0; i < MAXCLI; i++) {
    416 		if (pfdlist[i].fd == -1 && spclist[i].spc_dying == 0)
    417 			break;
    418 	}
    419 
    420 	if (lwproc_newproc(&spclist[i]) != 0) {
    421 		close(newfd);
    422 		return 0;
    423 	}
    424 
    425 	assert(i < MAXCLI);
    426 
    427 	pfdlist[i].fd = newfd;
    428 	spclist[i].spc_fd = newfd;
    429 	spclist[i].spc_mainlwp = lwproc_curlwp();
    430 	spclist[i].spc_istatus = SPCSTATUS_BUSY; /* dedicated receiver */
    431 	spclist[i].spc_pid = lwproc_getpid();
    432 	spclist[i].spc_refcnt = 1;
    433 
    434 	TAILQ_INIT(&spclist[i].spc_respwait);
    435 
    436 	DPRINTF(("rump_sp: added new connection fd %d at idx %u, pid %d\n",
    437 	    newfd, i, lwproc_getpid()));
    438 
    439 	lwproc_switch(NULL);
    440 
    441 	return i;
    442 }
    443 
    444 static void
    445 serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
    446 {
    447 	register_t retval[2] = {0, 0};
    448 	int rv, sysnum;
    449 
    450 	sysnum = (int)rhdr->rsp_sysnum;
    451 	DPRINTF(("rump_sp: handling syscall %d from client %d\n",
    452 	    sysnum, 0));
    453 
    454 	lwproc_newlwp(spc->spc_pid);
    455 	rv = rumpsyscall(sysnum, data, retval);
    456 	lwproc_release();
    457 
    458 	DPRINTF(("rump_sp: got return value %d & %d/%d\n",
    459 	    rv, retval[0], retval[1]));
    460 
    461 	send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
    462 }
    463 
    464 struct sysbouncearg {
    465 	struct spclient *sba_spc;
    466 	struct rsp_hdr sba_hdr;
    467 	uint8_t *sba_data;
    468 
    469 	TAILQ_ENTRY(sysbouncearg) sba_entries;
    470 };
    471 static pthread_mutex_t sbamtx;
    472 static pthread_cond_t sbacv;
    473 static int nworker, idleworker;
    474 static TAILQ_HEAD(, sysbouncearg) syslist = TAILQ_HEAD_INITIALIZER(syslist);
    475 
    476 /*ARGSUSED*/
    477 static void *
    478 serv_syscallbouncer(void *arg)
    479 {
    480 	struct sysbouncearg *sba;
    481 
    482 	for (;;) {
    483 		pthread_mutex_lock(&sbamtx);
    484 		if (idleworker >= rumpsp_idleworker) {
    485 			nworker--;
    486 			pthread_mutex_unlock(&sbamtx);
    487 			break;
    488 		}
    489 		idleworker++;
    490 		while (TAILQ_EMPTY(&syslist)) {
    491 			pthread_cond_wait(&sbacv, &sbamtx);
    492 		}
    493 
    494 		sba = TAILQ_FIRST(&syslist);
    495 		TAILQ_REMOVE(&syslist, sba, sba_entries);
    496 		idleworker--;
    497 		pthread_mutex_unlock(&sbamtx);
    498 
    499 		serv_handlesyscall(sba->sba_spc,
    500 		    &sba->sba_hdr, sba->sba_data);
    501 		spcrelease(sba->sba_spc);
    502 		free(sba->sba_data);
    503 		free(sba);
    504 	}
    505 
    506 	return NULL;
    507 }
    508 
    509 static int
    510 sp_copyin(void *arg, const void *raddr, void *laddr, size_t *len, int wantstr)
    511 {
    512 	struct spclient *spc = arg;
    513 	void *rdata = NULL; /* XXXuninit */
    514 	int rv, nlocks;
    515 
    516 	rumpuser__kunlock(0, &nlocks, NULL);
    517 
    518 	rv = copyin_req(spc, raddr, len, wantstr, &rdata);
    519 	if (rv)
    520 		goto out;
    521 
    522 	memcpy(laddr, rdata, *len);
    523 	free(rdata);
    524 
    525  out:
    526 	rumpuser__klock(nlocks, NULL);
    527 	if (rv)
    528 		return EFAULT;
    529 	return 0;
    530 }
    531 
    532 int
    533 rumpuser_sp_copyin(void *arg, const void *raddr, void *laddr, size_t len)
    534 {
    535 
    536 	return sp_copyin(arg, raddr, laddr, &len, 0);
    537 }
    538 
    539 int
    540 rumpuser_sp_copyinstr(void *arg, const void *raddr, void *laddr, size_t *len)
    541 {
    542 
    543 	return sp_copyin(arg, raddr, laddr, len, 1);
    544 }
    545 
    546 static int
    547 sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
    548 {
    549 	struct spclient *spc = arg;
    550 	int nlocks, rv;
    551 
    552 	rumpuser__kunlock(0, &nlocks, NULL);
    553 	rv = send_copyout_req(spc, raddr, laddr, dlen);
    554 	rumpuser__klock(nlocks, NULL);
    555 
    556 	if (rv)
    557 		return EFAULT;
    558 	return 0;
    559 }
    560 
    561 int
    562 rumpuser_sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
    563 {
    564 
    565 	return sp_copyout(arg, laddr, raddr, dlen);
    566 }
    567 
    568 int
    569 rumpuser_sp_copyoutstr(void *arg, const void *laddr, void *raddr, size_t *dlen)
    570 {
    571 
    572 	return sp_copyout(arg, laddr, raddr, *dlen);
    573 }
    574 
    575 int
    576 rumpuser_sp_anonmmap(void *arg, size_t howmuch, void **addr)
    577 {
    578 	struct spclient *spc = arg;
    579 	void *resp, *rdata;
    580 	int nlocks, rv;
    581 
    582 	rumpuser__kunlock(0, &nlocks, NULL);
    583 
    584 	rv = anonmmap_req(spc, howmuch, &rdata);
    585 	if (rv) {
    586 		rv = EFAULT;
    587 		goto out;
    588 	}
    589 
    590 	resp = *(void **)rdata;
    591 	free(rdata);
    592 
    593 	if (resp == NULL) {
    594 		rv = ENOMEM;
    595 	}
    596 
    597 	*addr = resp;
    598 
    599  out:
    600 	rumpuser__klock(nlocks, NULL);
    601 
    602 	if (rv)
    603 		return rv;
    604 	return 0;
    605 }
    606 
    607 /*
    608  *
    609  * Startup routines and mainloop for server.
    610  *
    611  */
    612 
    613 struct spservarg {
    614 	int sps_sock;
    615 	connecthook_fn sps_connhook;
    616 };
    617 
    618 static pthread_attr_t pattr_detached;
    619 static void
    620 handlereq(struct spclient *spc)
    621 {
    622 	struct sysbouncearg *sba;
    623 	pthread_t pt;
    624 	int retries;
    625 
    626 	if (__predict_false(spc->spc_hdr.rsp_type != RUMPSP_SYSCALL)) {
    627 		send_error_resp(spc, spc->spc_hdr.rsp_reqno, EINVAL);
    628 		spcfreebuf(spc);
    629 		return;
    630 	}
    631 
    632 	retries = 0;
    633 	while ((sba = malloc(sizeof(*sba))) == NULL) {
    634 		if (nworker == 0 || retries > 10) {
    635 			send_error_resp(spc, spc->spc_hdr.rsp_reqno, EAGAIN);
    636 			spcfreebuf(spc);
    637 			return;
    638 		}
    639 		/* slim chance of more memory? */
    640 		usleep(10000);
    641 	}
    642 
    643 	sba->sba_spc = spc;
    644 	sba->sba_hdr = spc->spc_hdr;
    645 	sba->sba_data = spc->spc_buf;
    646 	spcresetbuf(spc);
    647 
    648 	spcref(spc);
    649 
    650 	pthread_mutex_lock(&sbamtx);
    651 	TAILQ_INSERT_TAIL(&syslist, sba, sba_entries);
    652 	if (idleworker > 0) {
    653 		/* do we have a daemon's tool (i.e. idle threads)? */
    654 		pthread_cond_signal(&sbacv);
    655 	} else if (nworker < rumpsp_maxworker) {
    656 		/*
    657 		 * Else, need to create one
    658 		 * (if we can, otherwise just expect another
    659 		 * worker to pick up the syscall)
    660 		 */
    661 		if (pthread_create(&pt, &pattr_detached,
    662 		    serv_syscallbouncer, NULL) == 0)
    663 			nworker++;
    664 	}
    665 	pthread_mutex_unlock(&sbamtx);
    666 }
    667 
    668 static void *
    669 spserver(void *arg)
    670 {
    671 	struct spservarg *sarg = arg;
    672 	struct spclient *spc;
    673 	unsigned idx;
    674 	int seen;
    675 	int rv;
    676 	unsigned int nfds, maxidx;
    677 
    678 	for (idx = 0; idx < MAXCLI; idx++) {
    679 		pfdlist[idx].fd = -1;
    680 		pfdlist[idx].events = POLLIN;
    681 
    682 		spc = &spclist[idx];
    683 		pthread_mutex_init(&spc->spc_mtx, NULL);
    684 		pthread_cond_init(&spc->spc_cv, NULL);
    685 	}
    686 	pfdlist[0].fd = sarg->sps_sock;
    687 	pfdlist[0].events = POLLIN;
    688 	nfds = 1;
    689 	maxidx = 0;
    690 
    691 	pthread_attr_init(&pattr_detached);
    692 	pthread_attr_setdetachstate(&pattr_detached, PTHREAD_CREATE_DETACHED);
    693 	/* XXX: doesn't stacksize currently work on NetBSD */
    694 	pthread_attr_setstacksize(&pattr_detached, 32*1024);
    695 
    696 	pthread_mutex_init(&sbamtx, NULL);
    697 	pthread_cond_init(&sbacv, NULL);
    698 
    699 	DPRINTF(("rump_sp: server mainloop\n"));
    700 
    701 	for (;;) {
    702 		int discoed;
    703 
    704 		/* g/c hangarounds (eventually) */
    705 		discoed = atomic_swap_uint(&disco, 0);
    706 		while (discoed--) {
    707 			nfds--;
    708 			idx = maxidx;
    709 			while (idx) {
    710 				if (pfdlist[idx].fd != -1) {
    711 					maxidx = idx;
    712 					break;
    713 				}
    714 				idx--;
    715 			}
    716 			DPRINTF(("rump_sp: set maxidx to [%u]\n",
    717 			    maxidx));
    718 		}
    719 
    720 		DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
    721 		seen = 0;
    722 		rv = poll(pfdlist, maxidx+1, INFTIM);
    723 		assert(maxidx+1 <= MAXCLI);
    724 		assert(rv != 0);
    725 		if (rv == -1) {
    726 			if (errno == EINTR)
    727 				continue;
    728 			fprintf(stderr, "rump_spserver: poll returned %d\n",
    729 			    errno);
    730 			break;
    731 		}
    732 
    733 		for (idx = 0; seen < rv && idx < MAXCLI; idx++) {
    734 			if ((pfdlist[idx].revents & POLLIN) == 0)
    735 				continue;
    736 
    737 			seen++;
    738 			DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
    739 			    idx, seen, rv));
    740 			if (idx > 0) {
    741 				spc = &spclist[idx];
    742 				DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
    743 				switch (readframe(spc)) {
    744 				case 0:
    745 					break;
    746 				case -1:
    747 					serv_handledisco(idx);
    748 					break;
    749 				default:
    750 					switch (spc->spc_hdr.rsp_class) {
    751 					case RUMPSP_RESP:
    752 						kickwaiter(spc);
    753 						break;
    754 					case RUMPSP_REQ:
    755 						handlereq(spc);
    756 						break;
    757 					default:
    758 						send_error_resp(spc,
    759 						    spc->spc_hdr.rsp_reqno,
    760 						    ENOENT);
    761 						spcfreebuf(spc);
    762 						break;
    763 					}
    764 					break;
    765 				}
    766 
    767 			} else {
    768 				DPRINTF(("rump_sp: mainloop new connection\n"));
    769 
    770 				idx = serv_handleconn(pfdlist[0].fd,
    771 				    sarg->sps_connhook, nfds == MAXCLI);
    772 				if (idx)
    773 					nfds++;
    774 				if (idx > maxidx)
    775 					maxidx = idx;
    776 				DPRINTF(("rump_sp: maxid now %d\n", maxidx));
    777 			}
    778 		}
    779 	}
    780 
    781 	return NULL;
    782 }
    783 
    784 int
    785 rumpuser_sp_init(const struct rumpuser_sp_ops *spopsp, const char *url)
    786 {
    787 	pthread_t pt;
    788 	struct spservarg *sarg;
    789 	struct sockaddr *sap;
    790 	char *p;
    791 	unsigned idx;
    792 	int error, s;
    793 
    794 	p = strdup(url);
    795 	if (p == NULL)
    796 		return ENOMEM;
    797 	error = parseurl(p, &sap, &idx, 1);
    798 	free(p);
    799 	if (error)
    800 		return error;
    801 
    802 	s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
    803 	if (s == -1)
    804 		return errno;
    805 
    806 	spops = *spopsp;
    807 	sarg = malloc(sizeof(*sarg));
    808 	if (sarg == NULL) {
    809 		close(s);
    810 		return ENOMEM;
    811 	}
    812 
    813 	sarg->sps_sock = s;
    814 	sarg->sps_connhook = parsetab[idx].connhook;
    815 
    816 	/* sloppy error recovery */
    817 
    818 	/*LINTED*/
    819 	if (bind(s, sap, sap->sa_len) == -1) {
    820 		fprintf(stderr, "rump_sp: server bind failed\n");
    821 		return errno;
    822 	}
    823 	if (listen(s, MAXCLI) == -1) {
    824 		fprintf(stderr, "rump_sp: server listen failed\n");
    825 		return errno;
    826 	}
    827 
    828 	if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
    829 		fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
    830 		return errno;
    831 	}
    832 	pthread_detach(pt);
    833 
    834 	return 0;
    835 }
    836