Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_sp.c revision 1.6
      1  1.6  pooka /*      $NetBSD: rumpuser_sp.c,v 1.6 2010/11/17 17:36:14 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2010 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  * Sysproxy routines.  This provides system RPC support over host sockets.
     30  1.1  pooka  * The most notable limitation is that the client and server must share
     31  1.1  pooka  * the same ABI.  This does not mean that they have to be the same
     32  1.1  pooka  * machine or that they need to run the same version of the host OS,
     33  1.1  pooka  * just that they must agree on the data structures.  This even *might*
     34  1.1  pooka  * work correctly from one hardware architecture to another.
     35  1.1  pooka  *
     36  1.1  pooka  * Not finished yet, i.e. don't use in production.  Lacks locking plus
     37  1.1  pooka  * handling of multiple clients and unexpected connection closes.
     38  1.1  pooka  */
     39  1.1  pooka 
     40  1.1  pooka #include <sys/cdefs.h>
     41  1.6  pooka __RCSID("$NetBSD: rumpuser_sp.c,v 1.6 2010/11/17 17:36:14 pooka Exp $");
     42  1.1  pooka 
     43  1.1  pooka #include <sys/types.h>
     44  1.1  pooka #include <sys/mman.h>
     45  1.1  pooka #include <sys/socket.h>
     46  1.1  pooka 
     47  1.1  pooka #include <arpa/inet.h>
     48  1.1  pooka #include <netinet/in.h>
     49  1.1  pooka #include <netinet/tcp.h>
     50  1.1  pooka 
     51  1.1  pooka #include <assert.h>
     52  1.1  pooka #include <errno.h>
     53  1.1  pooka #include <fcntl.h>
     54  1.1  pooka #include <poll.h>
     55  1.1  pooka #include <pthread.h>
     56  1.1  pooka #include <stdarg.h>
     57  1.1  pooka #include <stdio.h>
     58  1.1  pooka #include <stdlib.h>
     59  1.1  pooka #include <string.h>
     60  1.1  pooka #include <unistd.h>
     61  1.1  pooka 
     62  1.1  pooka #include <rump/rumpuser.h>
     63  1.1  pooka 
     64  1.5  pooka #include "sp_common.c"
     65  1.1  pooka 
     66  1.1  pooka #define MAXCLI 4
     67  1.1  pooka 
     68  1.1  pooka static struct pollfd pfdlist[MAXCLI];
     69  1.1  pooka static struct spclient spclist[MAXCLI];
     70  1.1  pooka static unsigned int nfds, maxidx;
     71  1.1  pooka static uint64_t nextreq;
     72  1.1  pooka static pthread_key_t spclient_tls;
     73  1.1  pooka 
     74  1.3  pooka static struct rumpuser_sp_ops spops;
     75  1.3  pooka 
     76  1.3  pooka /*
     77  1.3  pooka  * Manual wrappers, since librump does not have access to the
     78  1.3  pooka  * user namespace wrapped interfaces.
     79  1.3  pooka  */
     80  1.3  pooka 
     81  1.3  pooka static void
     82  1.3  pooka lwproc_switch(struct lwp *l)
     83  1.3  pooka {
     84  1.3  pooka 
     85  1.4  pooka 	spops.spop_schedule();
     86  1.3  pooka 	spops.spop_lwproc_switch(l);
     87  1.4  pooka 	spops.spop_unschedule();
     88  1.3  pooka }
     89  1.3  pooka 
     90  1.3  pooka static void
     91  1.3  pooka lwproc_release(void)
     92  1.3  pooka {
     93  1.3  pooka 
     94  1.4  pooka 	spops.spop_schedule();
     95  1.3  pooka 	spops.spop_lwproc_release();
     96  1.4  pooka 	spops.spop_unschedule();
     97  1.3  pooka }
     98  1.3  pooka 
     99  1.3  pooka static int
    100  1.3  pooka lwproc_newproc(void)
    101  1.3  pooka {
    102  1.3  pooka 	int rv;
    103  1.3  pooka 
    104  1.4  pooka 	spops.spop_schedule();
    105  1.3  pooka 	rv = spops.spop_lwproc_newproc();
    106  1.4  pooka 	spops.spop_unschedule();
    107  1.3  pooka 
    108  1.3  pooka 	return rv;
    109  1.3  pooka }
    110  1.3  pooka 
    111  1.3  pooka static struct lwp *
    112  1.3  pooka lwproc_curlwp(void)
    113  1.3  pooka {
    114  1.3  pooka 	struct lwp *l;
    115  1.3  pooka 
    116  1.4  pooka 	spops.spop_schedule();
    117  1.3  pooka 	l = spops.spop_lwproc_curlwp();
    118  1.4  pooka 	spops.spop_unschedule();
    119  1.3  pooka 
    120  1.3  pooka 	return l;
    121  1.3  pooka }
    122  1.3  pooka 
    123  1.3  pooka static int
    124  1.3  pooka rumpsyscall(int sysnum, void *data, register_t *retval)
    125  1.3  pooka {
    126  1.3  pooka 	int rv;
    127  1.3  pooka 
    128  1.4  pooka 	spops.spop_schedule();
    129  1.3  pooka 	rv = spops.spop_syscall(sysnum, data, retval);
    130  1.4  pooka 	spops.spop_unschedule();
    131  1.3  pooka 
    132  1.3  pooka 	return rv;
    133  1.3  pooka }
    134  1.1  pooka 
    135  1.1  pooka static int
    136  1.1  pooka send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
    137  1.1  pooka 	register_t retval[2])
    138  1.1  pooka {
    139  1.1  pooka 	struct rsp_hdr rhdr;
    140  1.1  pooka 	struct rsp_sysresp sysresp;
    141  1.1  pooka 
    142  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
    143  1.1  pooka 	rhdr.rsp_reqno = reqno;
    144  1.1  pooka 	rhdr.rsp_type = RUMPSP_SYSCALL_RESP;
    145  1.1  pooka 	rhdr.rsp_sysnum = 0;
    146  1.1  pooka 
    147  1.1  pooka 	sysresp.rsys_error = error;
    148  1.1  pooka 	memcpy(sysresp.rsys_retval, retval, sizeof(retval));
    149  1.1  pooka 
    150  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    151  1.1  pooka 	dosend(spc, &sysresp, sizeof(sysresp));
    152  1.1  pooka 
    153  1.1  pooka 	return 0;
    154  1.1  pooka }
    155  1.1  pooka 
    156  1.1  pooka static int
    157  1.1  pooka send_copyin_req(struct spclient *spc, const void *remaddr, size_t dlen)
    158  1.1  pooka {
    159  1.1  pooka 	struct rsp_hdr rhdr;
    160  1.1  pooka 	struct rsp_copydata copydata;
    161  1.1  pooka 
    162  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
    163  1.1  pooka 	rhdr.rsp_reqno = nextreq++;
    164  1.1  pooka 	rhdr.rsp_type = RUMPSP_COPYIN_REQ;
    165  1.1  pooka 	rhdr.rsp_sysnum = 0;
    166  1.1  pooka 
    167  1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    168  1.1  pooka 	copydata.rcp_len = dlen;
    169  1.1  pooka 
    170  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    171  1.1  pooka 	dosend(spc, &copydata, sizeof(copydata));
    172  1.1  pooka 
    173  1.1  pooka 	return 0;
    174  1.1  pooka }
    175  1.1  pooka 
    176  1.1  pooka static int
    177  1.1  pooka send_copyout_req(struct spclient *spc, const void *remaddr,
    178  1.1  pooka 	const void *data, size_t dlen)
    179  1.1  pooka {
    180  1.1  pooka 	struct rsp_hdr rhdr;
    181  1.1  pooka 	struct rsp_copydata copydata;
    182  1.1  pooka 
    183  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
    184  1.1  pooka 	rhdr.rsp_reqno = nextreq++;
    185  1.1  pooka 	rhdr.rsp_type = RUMPSP_COPYOUT_REQ;
    186  1.1  pooka 	rhdr.rsp_sysnum = 0;
    187  1.1  pooka 
    188  1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    189  1.1  pooka 	copydata.rcp_len = dlen;
    190  1.1  pooka 
    191  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    192  1.1  pooka 	dosend(spc, &copydata, sizeof(copydata));
    193  1.1  pooka 	dosend(spc, data, dlen);
    194  1.1  pooka 
    195  1.1  pooka 	return 0;
    196  1.1  pooka }
    197  1.1  pooka 
    198  1.1  pooka static int
    199  1.1  pooka send_anonmmap_req(struct spclient *spc, size_t howmuch)
    200  1.1  pooka {
    201  1.1  pooka 	struct rsp_hdr rhdr;
    202  1.1  pooka 
    203  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
    204  1.1  pooka 	rhdr.rsp_reqno = nextreq++;
    205  1.1  pooka 	rhdr.rsp_type = RUMPSP_ANONMMAP_REQ;
    206  1.1  pooka 	rhdr.rsp_sysnum = 0;
    207  1.1  pooka 
    208  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    209  1.1  pooka 	dosend(spc, &howmuch, sizeof(howmuch));
    210  1.1  pooka 
    211  1.1  pooka 	return 0;
    212  1.1  pooka }
    213  1.1  pooka 
    214  1.1  pooka static void
    215  1.2  pooka serv_handledisco(unsigned int idx)
    216  1.1  pooka {
    217  1.1  pooka 	struct spclient *spc = &spclist[idx];
    218  1.1  pooka 	int fd = spc->spc_fd;
    219  1.1  pooka 
    220  1.1  pooka 	DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
    221  1.1  pooka 
    222  1.3  pooka 	lwproc_switch(spc->spc_lwp);
    223  1.3  pooka 	lwproc_release();
    224  1.2  pooka 
    225  1.1  pooka 	free(spc->spc_buf);
    226  1.1  pooka 	memset(spc, 0, sizeof(*spc));
    227  1.1  pooka 	close(fd);
    228  1.1  pooka 	pfdlist[idx].fd = -1;
    229  1.1  pooka 	nfds--;
    230  1.1  pooka 
    231  1.1  pooka 	if (idx == maxidx) {
    232  1.1  pooka 		while (idx--) {
    233  1.1  pooka 			if (pfdlist[idx].fd != -1) {
    234  1.1  pooka 				maxidx = idx;
    235  1.1  pooka 				break;
    236  1.1  pooka 			}
    237  1.1  pooka 			assert(idx != 0);
    238  1.1  pooka 		}
    239  1.1  pooka 		DPRINTF(("rump_sp: set maxidx to [%u]\n", maxidx));
    240  1.1  pooka 	}
    241  1.1  pooka }
    242  1.1  pooka 
    243  1.1  pooka static int
    244  1.1  pooka serv_handleconn(int fd, connecthook_fn connhook)
    245  1.1  pooka {
    246  1.1  pooka 	struct sockaddr_storage ss;
    247  1.1  pooka 	socklen_t sl = sizeof(ss);
    248  1.1  pooka 	int newfd, flags, error;
    249  1.1  pooka 	unsigned i;
    250  1.1  pooka 
    251  1.1  pooka 	/*LINTED: cast ok */
    252  1.1  pooka 	newfd = accept(fd, (struct sockaddr *)&ss, &sl);
    253  1.1  pooka 	if (newfd == -1)
    254  1.1  pooka 		return errno;
    255  1.1  pooka 
    256  1.1  pooka 	/* XXX: should do some sort of handshake too */
    257  1.1  pooka 
    258  1.1  pooka 	if (nfds == MAXCLI) {
    259  1.1  pooka 		close(newfd); /* EBUSY */
    260  1.1  pooka 		return EBUSY;
    261  1.1  pooka 	}
    262  1.1  pooka 
    263  1.1  pooka 	flags = fcntl(newfd, F_GETFL, 0);
    264  1.1  pooka 	if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
    265  1.1  pooka 		close(newfd);
    266  1.1  pooka 		return errno;
    267  1.1  pooka 	}
    268  1.1  pooka 	flags = 1;
    269  1.1  pooka 
    270  1.1  pooka 	if ((error = connhook(newfd)) != 0) {
    271  1.1  pooka 		close(newfd);
    272  1.1  pooka 		return error;
    273  1.1  pooka 	}
    274  1.1  pooka 
    275  1.3  pooka 	if ((error = lwproc_newproc()) != 0) {
    276  1.2  pooka 		close(newfd);
    277  1.2  pooka 		return error;
    278  1.2  pooka 	}
    279  1.2  pooka 
    280  1.1  pooka 	/* find empty slot the simple way */
    281  1.1  pooka 	for (i = 0; i < MAXCLI; i++) {
    282  1.1  pooka 		if (pfdlist[i].fd == -1)
    283  1.2  pooka 			break;
    284  1.1  pooka 	}
    285  1.1  pooka 
    286  1.1  pooka 	assert(i < MAXCLI);
    287  1.1  pooka 	nfds++;
    288  1.1  pooka 
    289  1.1  pooka 	pfdlist[i].fd = newfd;
    290  1.1  pooka 	spclist[i].spc_fd = newfd;
    291  1.3  pooka 	spclist[i].spc_lwp = lwproc_curlwp();
    292  1.1  pooka 	if (maxidx < i)
    293  1.1  pooka 		maxidx = i;
    294  1.1  pooka 
    295  1.2  pooka 	DPRINTF(("rump_sp: added new connection at idx %u, pid %d\n",
    296  1.5  pooka 	    i, 9)); /* XXX: getpid not spop */
    297  1.2  pooka 
    298  1.3  pooka 	lwproc_switch(NULL);
    299  1.1  pooka 
    300  1.1  pooka 	return 0;
    301  1.1  pooka }
    302  1.1  pooka 
    303  1.1  pooka static void
    304  1.1  pooka serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
    305  1.1  pooka {
    306  1.1  pooka 	register_t retval[2];
    307  1.1  pooka 	int rv, sysnum;
    308  1.1  pooka 
    309  1.1  pooka 	sysnum = (int)rhdr->rsp_sysnum;
    310  1.1  pooka 	DPRINTF(("rump_sp: handling syscall %d from client %d\n",
    311  1.1  pooka 	    sysnum, 0));
    312  1.1  pooka 
    313  1.1  pooka 	pthread_setspecific(spclient_tls, spc);
    314  1.3  pooka 	lwproc_switch(spc->spc_lwp);
    315  1.3  pooka 	rv = rumpsyscall(sysnum, data, retval);
    316  1.3  pooka 	lwproc_switch(NULL);
    317  1.1  pooka 	pthread_setspecific(spclient_tls, NULL);
    318  1.6  pooka 	free(data);
    319  1.1  pooka 
    320  1.5  pooka 	DPRINTF(("rump_sp: got return value %d\n", rv));
    321  1.5  pooka 
    322  1.1  pooka 	send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
    323  1.1  pooka }
    324  1.1  pooka 
    325  1.1  pooka int
    326  1.1  pooka rumpuser_sp_copyin(const void *uaddr, void *kaddr, size_t len)
    327  1.1  pooka {
    328  1.1  pooka 	struct spclient *spc;
    329  1.1  pooka 	struct pollfd pfd;
    330  1.1  pooka 
    331  1.1  pooka 	spc = pthread_getspecific(spclient_tls);
    332  1.1  pooka 	if (!spc)
    333  1.1  pooka 		return EFAULT;
    334  1.1  pooka 
    335  1.1  pooka 	send_copyin_req(spc, uaddr, len);
    336  1.1  pooka 
    337  1.1  pooka 	pfd.fd = spc->spc_fd;
    338  1.1  pooka 	pfd.events = POLLIN;
    339  1.1  pooka 	do {
    340  1.1  pooka 		poll(&pfd, 1, INFTIM);
    341  1.1  pooka 	} while (readframe(spc) < 1);
    342  1.1  pooka 
    343  1.1  pooka 	if (spc->spc_hdr.rsp_type != RUMPSP_COPYIN_RESP) {
    344  1.1  pooka 		abort();
    345  1.1  pooka 	}
    346  1.1  pooka 
    347  1.1  pooka 	memcpy(kaddr, spc->spc_buf, len);
    348  1.1  pooka 	free(spc->spc_buf);
    349  1.1  pooka 	spc->spc_off = 0;
    350  1.1  pooka 
    351  1.1  pooka 	return 0;
    352  1.1  pooka }
    353  1.1  pooka 
    354  1.1  pooka int
    355  1.1  pooka rumpuser_sp_copyout(const void *kaddr, void *uaddr, size_t dlen)
    356  1.1  pooka {
    357  1.1  pooka 	struct spclient *spc;
    358  1.1  pooka 
    359  1.1  pooka 	spc = pthread_getspecific(spclient_tls);
    360  1.1  pooka 	if (!spc) {
    361  1.1  pooka 		DPRINTF(("rump_sp: copyout curlwp not found\n"));
    362  1.1  pooka 		return EFAULT;
    363  1.1  pooka 	}
    364  1.1  pooka 
    365  1.1  pooka 	send_copyout_req(spc, uaddr, kaddr, dlen);
    366  1.1  pooka 
    367  1.1  pooka 	return 0;
    368  1.1  pooka }
    369  1.1  pooka 
    370  1.1  pooka int
    371  1.1  pooka rumpuser_sp_anonmmap(size_t howmuch, void **addr)
    372  1.1  pooka {
    373  1.1  pooka 	struct spclient *spc;
    374  1.1  pooka 	struct pollfd pfd;
    375  1.1  pooka 	void *resp;
    376  1.1  pooka 
    377  1.1  pooka 	spc = pthread_getspecific(spclient_tls);
    378  1.1  pooka 	if (!spc)
    379  1.1  pooka 		return EFAULT;
    380  1.1  pooka 
    381  1.1  pooka 	send_anonmmap_req(spc, howmuch);
    382  1.1  pooka 
    383  1.1  pooka 	pfd.fd = spc->spc_fd;
    384  1.1  pooka 	pfd.events = POLLIN;
    385  1.1  pooka 	do {
    386  1.1  pooka 		poll(&pfd, 1, INFTIM);
    387  1.1  pooka 	} while (readframe(spc) < 1);
    388  1.1  pooka 
    389  1.1  pooka 	if (spc->spc_hdr.rsp_type != RUMPSP_ANONMMAP_RESP) {
    390  1.1  pooka 		abort();
    391  1.1  pooka 	}
    392  1.1  pooka 
    393  1.1  pooka 	/*LINTED*/
    394  1.1  pooka 	resp = *(void **)spc->spc_buf;
    395  1.1  pooka 	spc->spc_off = 0;
    396  1.1  pooka 
    397  1.1  pooka 	if (resp == NULL)
    398  1.1  pooka 		return ENOMEM;
    399  1.1  pooka 
    400  1.1  pooka 	*addr = resp;
    401  1.1  pooka 	return 0;
    402  1.1  pooka }
    403  1.1  pooka 
    404  1.1  pooka /*
    405  1.1  pooka  *
    406  1.1  pooka  * Startup routines and mainloop for server.
    407  1.1  pooka  *
    408  1.1  pooka  */
    409  1.1  pooka 
    410  1.1  pooka struct spservarg {
    411  1.1  pooka 	int sps_sock;
    412  1.1  pooka 	connecthook_fn sps_connhook;
    413  1.1  pooka };
    414  1.1  pooka 
    415  1.1  pooka static void *
    416  1.1  pooka spserver(void *arg)
    417  1.1  pooka {
    418  1.1  pooka 	struct spservarg *sarg = arg;
    419  1.1  pooka 	unsigned idx;
    420  1.1  pooka 	int seen;
    421  1.1  pooka 	int rv;
    422  1.1  pooka 
    423  1.1  pooka 	for (idx = 1; idx < MAXCLI; idx++) {
    424  1.1  pooka 		pfdlist[idx].fd = -1;
    425  1.1  pooka 		pfdlist[idx].events = POLLIN;
    426  1.1  pooka 	}
    427  1.1  pooka 	pfdlist[0].fd = sarg->sps_sock;
    428  1.1  pooka 	pfdlist[0].events = POLLIN;
    429  1.1  pooka 	nfds = 1;
    430  1.1  pooka 	maxidx = 0;
    431  1.1  pooka 
    432  1.1  pooka 	DPRINTF(("rump_sp: server mainloop\n"));
    433  1.1  pooka 
    434  1.1  pooka 	for (;;) {
    435  1.1  pooka 		DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
    436  1.1  pooka 		seen = 0;
    437  1.1  pooka 		rv = poll(pfdlist, maxidx+1, INFTIM);
    438  1.1  pooka 		assert(maxidx+1 <= MAXCLI);
    439  1.1  pooka 		assert(rv != 0);
    440  1.1  pooka 		if (rv == -1) {
    441  1.1  pooka 			if (errno == EINTR)
    442  1.1  pooka 				continue;
    443  1.1  pooka 			fprintf(stderr, "rump_spserver: poll returned %d\n",
    444  1.1  pooka 			    errno);
    445  1.1  pooka 			break;
    446  1.1  pooka 		}
    447  1.1  pooka 
    448  1.1  pooka 		for (idx = 0; seen < rv; idx++) {
    449  1.1  pooka 			assert(idx < MAXCLI);
    450  1.1  pooka 
    451  1.1  pooka 			if ((pfdlist[idx].revents & POLLIN) == 0)
    452  1.1  pooka 				continue;
    453  1.1  pooka 
    454  1.1  pooka 			seen++;
    455  1.1  pooka 			DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
    456  1.1  pooka 			    idx, seen, rv));
    457  1.1  pooka 			if (idx > 0) {
    458  1.1  pooka 				struct spclient *spc = &spclist[idx];
    459  1.1  pooka 
    460  1.1  pooka 				DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
    461  1.1  pooka 				switch (readframe(spc)) {
    462  1.1  pooka 				case 0:
    463  1.1  pooka 					break;
    464  1.1  pooka 				case -1:
    465  1.2  pooka 					serv_handledisco(idx);
    466  1.1  pooka 					break;
    467  1.1  pooka 				default:
    468  1.1  pooka 					spc->spc_off = 0;
    469  1.1  pooka 					serv_handlesyscall(spc,
    470  1.1  pooka 					    &spc->spc_hdr, spc->spc_buf);
    471  1.1  pooka 					spc->spc_buf = NULL;
    472  1.1  pooka 					break;
    473  1.1  pooka 				}
    474  1.1  pooka 			} else {
    475  1.1  pooka 				DPRINTF(("rump_sp: mainloop new connection\n"));
    476  1.1  pooka 				serv_handleconn(pfdlist[0].fd,
    477  1.1  pooka 				    sarg->sps_connhook);
    478  1.1  pooka 			}
    479  1.1  pooka 		}
    480  1.1  pooka 	}
    481  1.1  pooka 
    482  1.1  pooka 	return NULL;
    483  1.1  pooka }
    484  1.1  pooka 
    485  1.1  pooka int
    486  1.5  pooka rumpuser_sp_init(const struct rumpuser_sp_ops *spopsp, const char *url)
    487  1.1  pooka {
    488  1.5  pooka 	pthread_t pt;
    489  1.1  pooka 	struct spservarg *sarg;
    490  1.1  pooka 	struct sockaddr *sap;
    491  1.5  pooka 	char *p;
    492  1.5  pooka 	unsigned idx;
    493  1.5  pooka 	int error, s;
    494  1.5  pooka 
    495  1.5  pooka 	p = strdup(url);
    496  1.5  pooka 	if (p == NULL)
    497  1.5  pooka 		return ENOMEM;
    498  1.5  pooka 	error = parseurl(p, &sap, &idx, 1);
    499  1.5  pooka 	free(p);
    500  1.5  pooka 	if (error)
    501  1.5  pooka 		return error;
    502  1.5  pooka 
    503  1.5  pooka 	s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
    504  1.5  pooka 	if (s == -1)
    505  1.5  pooka 		return errno;
    506  1.1  pooka 
    507  1.3  pooka 	spops = *spopsp;
    508  1.5  pooka 	sarg = malloc(sizeof(*sarg));
    509  1.5  pooka 	if (sarg == NULL) {
    510  1.5  pooka 		close(s);
    511  1.5  pooka 		return ENOMEM;
    512  1.5  pooka 	}
    513  1.5  pooka 
    514  1.5  pooka 	sarg->sps_sock = s;
    515  1.5  pooka 	sarg->sps_connhook = parsetab[idx].connhook;
    516  1.1  pooka 
    517  1.5  pooka 	/* sloppy error recovery */
    518  1.1  pooka 
    519  1.5  pooka 	error = pthread_key_create(&spclient_tls, NULL);
    520  1.5  pooka 	if (error) {
    521  1.5  pooka 		fprintf(stderr, "rump_sp: tls create failed\n");
    522  1.5  pooka 		return error;
    523  1.1  pooka 	}
    524  1.1  pooka 
    525  1.5  pooka 	/*LINTED*/
    526  1.5  pooka 	if (bind(s, sap, sap->sa_len) == -1) {
    527  1.5  pooka 		fprintf(stderr, "rump_sp: server bind failed\n");
    528  1.5  pooka 		return errno;
    529  1.1  pooka 	}
    530  1.5  pooka 	if (listen(s, 20) == -1) {
    531  1.5  pooka 		fprintf(stderr, "rump_sp: server listen failed\n");
    532  1.5  pooka 		return errno;
    533  1.1  pooka 	}
    534  1.1  pooka 
    535  1.5  pooka 	if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
    536  1.5  pooka 		fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
    537  1.1  pooka 		return errno;
    538  1.1  pooka 	}
    539  1.5  pooka 	pthread_detach(pt);
    540  1.1  pooka 
    541  1.1  pooka 	return 0;
    542  1.1  pooka }
    543