Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_sp.c revision 1.10
      1  1.10  pooka /*      $NetBSD: rumpuser_sp.c,v 1.10 2010/11/22 20:42:19 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.10  pooka __RCSID("$NetBSD: rumpuser_sp.c,v 1.10 2010/11/22 20:42:19 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 
     72   1.3  pooka static struct rumpuser_sp_ops spops;
     73   1.3  pooka 
     74   1.3  pooka /*
     75   1.3  pooka  * Manual wrappers, since librump does not have access to the
     76   1.3  pooka  * user namespace wrapped interfaces.
     77   1.3  pooka  */
     78   1.3  pooka 
     79   1.3  pooka static void
     80   1.3  pooka lwproc_switch(struct lwp *l)
     81   1.3  pooka {
     82   1.3  pooka 
     83   1.4  pooka 	spops.spop_schedule();
     84   1.3  pooka 	spops.spop_lwproc_switch(l);
     85   1.4  pooka 	spops.spop_unschedule();
     86   1.3  pooka }
     87   1.3  pooka 
     88   1.3  pooka static void
     89   1.3  pooka lwproc_release(void)
     90   1.3  pooka {
     91   1.3  pooka 
     92   1.4  pooka 	spops.spop_schedule();
     93   1.3  pooka 	spops.spop_lwproc_release();
     94   1.4  pooka 	spops.spop_unschedule();
     95   1.3  pooka }
     96   1.3  pooka 
     97   1.3  pooka static int
     98  1.10  pooka lwproc_newproc(struct spclient *spc)
     99   1.3  pooka {
    100   1.3  pooka 	int rv;
    101   1.3  pooka 
    102   1.4  pooka 	spops.spop_schedule();
    103  1.10  pooka 	rv = spops.spop_lwproc_newproc(spc);
    104   1.4  pooka 	spops.spop_unschedule();
    105   1.3  pooka 
    106   1.3  pooka 	return rv;
    107   1.3  pooka }
    108   1.3  pooka 
    109   1.8  pooka static int
    110   1.8  pooka lwproc_newlwp(pid_t pid)
    111   1.8  pooka {
    112   1.8  pooka 	int rv;
    113   1.8  pooka 
    114   1.8  pooka 	spops.spop_schedule();
    115   1.8  pooka 	rv = spops.spop_lwproc_newlwp(pid);
    116   1.8  pooka 	spops.spop_unschedule();
    117   1.8  pooka 
    118   1.8  pooka 	return rv;
    119   1.8  pooka }
    120   1.8  pooka 
    121   1.3  pooka static struct lwp *
    122   1.3  pooka lwproc_curlwp(void)
    123   1.3  pooka {
    124   1.3  pooka 	struct lwp *l;
    125   1.3  pooka 
    126   1.4  pooka 	spops.spop_schedule();
    127   1.3  pooka 	l = spops.spop_lwproc_curlwp();
    128   1.4  pooka 	spops.spop_unschedule();
    129   1.3  pooka 
    130   1.3  pooka 	return l;
    131   1.3  pooka }
    132   1.3  pooka 
    133   1.8  pooka static pid_t
    134   1.8  pooka lwproc_getpid(void)
    135   1.8  pooka {
    136   1.8  pooka 	pid_t p;
    137   1.8  pooka 
    138   1.8  pooka 	spops.spop_schedule();
    139   1.8  pooka 	p = spops.spop_getpid();
    140   1.8  pooka 	spops.spop_unschedule();
    141   1.8  pooka 
    142   1.8  pooka 	return p;
    143   1.8  pooka }
    144   1.8  pooka 
    145   1.3  pooka static int
    146   1.3  pooka rumpsyscall(int sysnum, void *data, register_t *retval)
    147   1.3  pooka {
    148   1.3  pooka 	int rv;
    149   1.3  pooka 
    150   1.4  pooka 	spops.spop_schedule();
    151   1.3  pooka 	rv = spops.spop_syscall(sysnum, data, retval);
    152   1.4  pooka 	spops.spop_unschedule();
    153   1.3  pooka 
    154   1.3  pooka 	return rv;
    155   1.3  pooka }
    156   1.1  pooka 
    157   1.7  pooka static uint64_t
    158   1.7  pooka nextreq(struct spclient *spc)
    159   1.7  pooka {
    160   1.7  pooka 	uint64_t nw;
    161   1.7  pooka 
    162   1.7  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    163   1.7  pooka 	nw = spc->spc_nextreq++;
    164   1.7  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    165   1.7  pooka 
    166   1.7  pooka 	return nw;
    167   1.7  pooka }
    168   1.7  pooka 
    169   1.1  pooka static int
    170   1.1  pooka send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
    171   1.7  pooka 	register_t *retval)
    172   1.1  pooka {
    173   1.1  pooka 	struct rsp_hdr rhdr;
    174   1.1  pooka 	struct rsp_sysresp sysresp;
    175   1.7  pooka 	int rv;
    176   1.1  pooka 
    177   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
    178   1.1  pooka 	rhdr.rsp_reqno = reqno;
    179   1.7  pooka 	rhdr.rsp_class = RUMPSP_RESP;
    180   1.7  pooka 	rhdr.rsp_type = RUMPSP_SYSCALL;
    181   1.1  pooka 	rhdr.rsp_sysnum = 0;
    182   1.1  pooka 
    183   1.1  pooka 	sysresp.rsys_error = error;
    184   1.7  pooka 	memcpy(sysresp.rsys_retval, retval, sizeof(sysresp.rsys_retval));
    185   1.1  pooka 
    186   1.7  pooka 	sendlock(spc);
    187   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    188   1.7  pooka 	rv = dosend(spc, &sysresp, sizeof(sysresp));
    189   1.7  pooka 	sendunlock(spc);
    190   1.1  pooka 
    191   1.7  pooka 	return rv;
    192   1.1  pooka }
    193   1.1  pooka 
    194   1.1  pooka static int
    195   1.7  pooka copyin_req(struct spclient *spc, const void *remaddr, size_t dlen, void **resp)
    196   1.1  pooka {
    197   1.1  pooka 	struct rsp_hdr rhdr;
    198   1.1  pooka 	struct rsp_copydata copydata;
    199   1.7  pooka 	struct respwait rw;
    200   1.7  pooka 	int rv;
    201   1.7  pooka 
    202   1.7  pooka 	DPRINTF(("copyin_req: %zu bytes from %p\n", dlen, remaddr));
    203   1.1  pooka 
    204   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
    205   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    206   1.7  pooka 	rhdr.rsp_type = RUMPSP_COPYIN;
    207   1.1  pooka 	rhdr.rsp_sysnum = 0;
    208   1.1  pooka 
    209   1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    210   1.1  pooka 	copydata.rcp_len = dlen;
    211   1.1  pooka 
    212   1.7  pooka 	putwait(spc, &rw, &rhdr);
    213   1.7  pooka 
    214   1.7  pooka 	sendlock(spc);
    215   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    216   1.7  pooka 	rv = dosend(spc, &copydata, sizeof(copydata));
    217   1.7  pooka 	sendunlock(spc);
    218   1.7  pooka 	if (rv)
    219   1.7  pooka 		return rv; /* XXX: unputwait */
    220   1.7  pooka 
    221   1.7  pooka 	rv = waitresp(spc, &rw);
    222   1.7  pooka 
    223   1.7  pooka 	DPRINTF(("copyin: response %d\n", rv));
    224   1.7  pooka 
    225   1.7  pooka 	*resp = rw.rw_data;
    226   1.7  pooka 	return rv;
    227   1.1  pooka 
    228   1.1  pooka }
    229   1.1  pooka 
    230   1.1  pooka static int
    231   1.1  pooka send_copyout_req(struct spclient *spc, const void *remaddr,
    232   1.1  pooka 	const void *data, size_t dlen)
    233   1.1  pooka {
    234   1.1  pooka 	struct rsp_hdr rhdr;
    235   1.1  pooka 	struct rsp_copydata copydata;
    236   1.7  pooka 	int rv;
    237   1.7  pooka 
    238   1.7  pooka 	DPRINTF(("copyout_req (async): %zu bytes to %p\n", dlen, remaddr));
    239   1.1  pooka 
    240   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
    241   1.7  pooka 	rhdr.rsp_reqno = nextreq(spc);
    242   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    243   1.7  pooka 	rhdr.rsp_type = RUMPSP_COPYOUT;
    244   1.1  pooka 	rhdr.rsp_sysnum = 0;
    245   1.1  pooka 
    246   1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    247   1.1  pooka 	copydata.rcp_len = dlen;
    248   1.1  pooka 
    249   1.7  pooka 	sendlock(spc);
    250   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    251   1.7  pooka 	rv = dosend(spc, &copydata, sizeof(copydata));
    252   1.7  pooka 	rv = dosend(spc, data, dlen);
    253   1.7  pooka 	sendunlock(spc);
    254   1.1  pooka 
    255   1.7  pooka 	return rv;
    256   1.1  pooka }
    257   1.1  pooka 
    258   1.1  pooka static int
    259   1.7  pooka anonmmap_req(struct spclient *spc, size_t howmuch, void **resp)
    260   1.1  pooka {
    261   1.1  pooka 	struct rsp_hdr rhdr;
    262   1.7  pooka 	struct respwait rw;
    263   1.7  pooka 	int rv;
    264   1.7  pooka 
    265   1.7  pooka 	DPRINTF(("anonmmap_req: %zu bytes\n", howmuch));
    266   1.1  pooka 
    267   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
    268   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    269   1.7  pooka 	rhdr.rsp_type = RUMPSP_ANONMMAP;
    270   1.1  pooka 	rhdr.rsp_sysnum = 0;
    271   1.1  pooka 
    272   1.7  pooka 	putwait(spc, &rw, &rhdr);
    273   1.7  pooka 
    274   1.7  pooka 	sendlock(spc);
    275   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    276   1.7  pooka 	rv = dosend(spc, &howmuch, sizeof(howmuch));
    277   1.7  pooka 	sendunlock(spc);
    278   1.7  pooka 	if (rv)
    279   1.7  pooka 		return rv; /* XXX: unputwait */
    280   1.1  pooka 
    281   1.7  pooka 	rv = waitresp(spc, &rw);
    282   1.7  pooka 	*resp = rw.rw_data;
    283   1.7  pooka 
    284   1.7  pooka 	DPRINTF(("anonmmap: mapped at %p\n", **(void ***)resp));
    285   1.7  pooka 
    286   1.7  pooka 	return rv;
    287   1.1  pooka }
    288   1.1  pooka 
    289   1.1  pooka static void
    290   1.2  pooka serv_handledisco(unsigned int idx)
    291   1.1  pooka {
    292   1.1  pooka 	struct spclient *spc = &spclist[idx];
    293   1.1  pooka 	int fd = spc->spc_fd;
    294   1.1  pooka 
    295   1.1  pooka 	DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
    296   1.1  pooka 
    297   1.8  pooka 	lwproc_switch(spc->spc_mainlwp);
    298   1.3  pooka 	lwproc_release();
    299   1.2  pooka 
    300   1.7  pooka 	pthread_mutex_destroy(&spc->spc_mtx);
    301   1.7  pooka 	pthread_cond_destroy(&spc->spc_cv);
    302   1.1  pooka 	free(spc->spc_buf);
    303   1.1  pooka 	memset(spc, 0, sizeof(*spc));
    304   1.1  pooka 	close(fd);
    305   1.1  pooka 	pfdlist[idx].fd = -1;
    306   1.1  pooka 	nfds--;
    307   1.1  pooka 
    308   1.1  pooka 	if (idx == maxidx) {
    309   1.1  pooka 		while (idx--) {
    310   1.1  pooka 			if (pfdlist[idx].fd != -1) {
    311   1.1  pooka 				maxidx = idx;
    312   1.1  pooka 				break;
    313   1.1  pooka 			}
    314   1.1  pooka 			assert(idx != 0);
    315   1.1  pooka 		}
    316   1.1  pooka 		DPRINTF(("rump_sp: set maxidx to [%u]\n", maxidx));
    317   1.1  pooka 	}
    318   1.1  pooka }
    319   1.1  pooka 
    320   1.1  pooka static int
    321   1.1  pooka serv_handleconn(int fd, connecthook_fn connhook)
    322   1.1  pooka {
    323   1.1  pooka 	struct sockaddr_storage ss;
    324   1.1  pooka 	socklen_t sl = sizeof(ss);
    325   1.1  pooka 	int newfd, flags, error;
    326   1.1  pooka 	unsigned i;
    327   1.1  pooka 
    328   1.1  pooka 	/*LINTED: cast ok */
    329   1.1  pooka 	newfd = accept(fd, (struct sockaddr *)&ss, &sl);
    330   1.1  pooka 	if (newfd == -1)
    331   1.1  pooka 		return errno;
    332   1.1  pooka 
    333   1.1  pooka 	/* XXX: should do some sort of handshake too */
    334   1.1  pooka 
    335   1.1  pooka 	if (nfds == MAXCLI) {
    336   1.1  pooka 		close(newfd); /* EBUSY */
    337   1.1  pooka 		return EBUSY;
    338   1.1  pooka 	}
    339   1.1  pooka 
    340   1.1  pooka 	flags = fcntl(newfd, F_GETFL, 0);
    341   1.1  pooka 	if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
    342   1.1  pooka 		close(newfd);
    343   1.1  pooka 		return errno;
    344   1.1  pooka 	}
    345   1.1  pooka 	flags = 1;
    346   1.1  pooka 
    347   1.1  pooka 	if ((error = connhook(newfd)) != 0) {
    348   1.1  pooka 		close(newfd);
    349   1.1  pooka 		return error;
    350   1.1  pooka 	}
    351   1.1  pooka 
    352   1.1  pooka 	/* find empty slot the simple way */
    353   1.1  pooka 	for (i = 0; i < MAXCLI; i++) {
    354   1.1  pooka 		if (pfdlist[i].fd == -1)
    355   1.2  pooka 			break;
    356   1.1  pooka 	}
    357   1.1  pooka 
    358  1.10  pooka 	if ((error = lwproc_newproc(&spclist[i])) != 0) {
    359  1.10  pooka 		close(newfd);
    360  1.10  pooka 		return error;
    361  1.10  pooka 	}
    362  1.10  pooka 
    363   1.1  pooka 	assert(i < MAXCLI);
    364   1.1  pooka 	nfds++;
    365   1.1  pooka 
    366   1.1  pooka 	pfdlist[i].fd = newfd;
    367   1.1  pooka 	spclist[i].spc_fd = newfd;
    368   1.8  pooka 	spclist[i].spc_mainlwp = lwproc_curlwp();
    369   1.7  pooka 	spclist[i].spc_istatus = SPCSTATUS_BUSY; /* dedicated receiver */
    370   1.8  pooka 	spclist[i].spc_pid = lwproc_getpid();
    371   1.7  pooka 
    372   1.7  pooka 	TAILQ_INIT(&spclist[i].spc_respwait);
    373   1.7  pooka 	pthread_mutex_init(&spclist[i].spc_mtx, NULL);
    374   1.7  pooka 	pthread_cond_init(&spclist[i].spc_cv, NULL);
    375   1.7  pooka 
    376   1.1  pooka 	if (maxidx < i)
    377   1.1  pooka 		maxidx = i;
    378   1.1  pooka 
    379   1.2  pooka 	DPRINTF(("rump_sp: added new connection at idx %u, pid %d\n",
    380   1.8  pooka 	    i, lwproc_getpid()));
    381   1.2  pooka 
    382   1.3  pooka 	lwproc_switch(NULL);
    383   1.1  pooka 
    384   1.1  pooka 	return 0;
    385   1.1  pooka }
    386   1.1  pooka 
    387   1.1  pooka static void
    388   1.1  pooka serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
    389   1.1  pooka {
    390   1.7  pooka 	register_t retval[2] = {0, 0};
    391   1.1  pooka 	int rv, sysnum;
    392   1.1  pooka 
    393   1.1  pooka 	sysnum = (int)rhdr->rsp_sysnum;
    394   1.1  pooka 	DPRINTF(("rump_sp: handling syscall %d from client %d\n",
    395   1.1  pooka 	    sysnum, 0));
    396   1.1  pooka 
    397   1.8  pooka 	lwproc_newlwp(spc->spc_pid);
    398   1.3  pooka 	rv = rumpsyscall(sysnum, data, retval);
    399   1.3  pooka 	lwproc_switch(NULL);
    400   1.6  pooka 	free(data);
    401   1.1  pooka 
    402   1.7  pooka 	DPRINTF(("rump_sp: got return value %d & %d/%d\n",
    403   1.7  pooka 	    rv, retval[0], retval[1]));
    404   1.5  pooka 
    405   1.1  pooka 	send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
    406   1.1  pooka }
    407   1.1  pooka 
    408   1.7  pooka struct sysbouncearg {
    409   1.7  pooka 	struct spclient *sba_spc;
    410   1.7  pooka 	struct rsp_hdr sba_hdr;
    411   1.7  pooka 	uint8_t *sba_data;
    412   1.7  pooka };
    413   1.7  pooka static void *
    414   1.7  pooka serv_syscallbouncer(void *arg)
    415   1.7  pooka {
    416   1.7  pooka 	struct sysbouncearg *barg = arg;
    417   1.7  pooka 
    418   1.7  pooka 	serv_handlesyscall(barg->sba_spc, &barg->sba_hdr, barg->sba_data);
    419   1.7  pooka 	free(arg);
    420   1.7  pooka 	return NULL;
    421   1.7  pooka }
    422   1.7  pooka 
    423   1.1  pooka int
    424  1.10  pooka rumpuser_sp_copyin(void *arg, const void *uaddr, void *kaddr, size_t len)
    425   1.1  pooka {
    426  1.10  pooka 	struct spclient *spc = arg;
    427   1.9  pooka 	void *rdata = NULL; /* XXXuninit */
    428   1.1  pooka 
    429   1.7  pooka 	copyin_req(spc, uaddr, len, &rdata);
    430   1.1  pooka 
    431   1.7  pooka 	memcpy(kaddr, rdata, len);
    432   1.7  pooka 	free(rdata);
    433   1.1  pooka 
    434   1.1  pooka 	return 0;
    435   1.1  pooka }
    436   1.1  pooka 
    437   1.1  pooka int
    438  1.10  pooka rumpuser_sp_copyout(void *arg, const void *kaddr, void *uaddr, size_t dlen)
    439   1.1  pooka {
    440  1.10  pooka 	struct spclient *spc = arg;
    441   1.1  pooka 
    442   1.7  pooka 	if (send_copyout_req(spc, uaddr, kaddr, dlen) != 0)
    443   1.7  pooka 		return EFAULT;
    444   1.1  pooka 	return 0;
    445   1.1  pooka }
    446   1.1  pooka 
    447   1.1  pooka int
    448  1.10  pooka rumpuser_sp_anonmmap(void *arg, size_t howmuch, void **addr)
    449   1.1  pooka {
    450  1.10  pooka 	struct spclient *spc = arg;
    451   1.7  pooka 	void *resp, *rdata;
    452   1.7  pooka 	int rv;
    453   1.1  pooka 
    454   1.7  pooka 	rv = anonmmap_req(spc, howmuch, &rdata);
    455   1.7  pooka 	if (rv)
    456   1.7  pooka 		return rv;
    457   1.1  pooka 
    458   1.7  pooka 	resp = *(void **)rdata;
    459   1.7  pooka 	free(rdata);
    460   1.1  pooka 
    461   1.7  pooka 	if (resp == NULL) {
    462   1.7  pooka 		return ENOMEM;
    463   1.1  pooka 	}
    464   1.1  pooka 
    465   1.1  pooka 	*addr = resp;
    466   1.1  pooka 	return 0;
    467   1.1  pooka }
    468   1.1  pooka 
    469   1.1  pooka /*
    470   1.1  pooka  *
    471   1.1  pooka  * Startup routines and mainloop for server.
    472   1.1  pooka  *
    473   1.1  pooka  */
    474   1.1  pooka 
    475   1.1  pooka struct spservarg {
    476   1.1  pooka 	int sps_sock;
    477   1.1  pooka 	connecthook_fn sps_connhook;
    478   1.1  pooka };
    479   1.1  pooka 
    480   1.7  pooka static void
    481   1.7  pooka handlereq(struct spclient *spc)
    482   1.7  pooka {
    483   1.7  pooka 	struct sysbouncearg *sba;
    484   1.7  pooka 	pthread_attr_t pattr;
    485   1.7  pooka 	pthread_t pt;
    486   1.7  pooka 	int rv;
    487   1.7  pooka 
    488   1.7  pooka 	/* XXX: check that it's a syscall */
    489   1.7  pooka 
    490   1.7  pooka 	sba = malloc(sizeof(*sba));
    491   1.7  pooka 	if (sba == NULL) {
    492   1.7  pooka 		/* panic */
    493   1.7  pooka 		abort();
    494   1.7  pooka 	}
    495   1.7  pooka 
    496   1.7  pooka 	sba->sba_spc = spc;
    497   1.7  pooka 	sba->sba_hdr = spc->spc_hdr;
    498   1.7  pooka 	sba->sba_data = spc->spc_buf;
    499   1.7  pooka 
    500   1.7  pooka 	spc->spc_buf = NULL;
    501   1.7  pooka 	spc->spc_off = 0;
    502   1.7  pooka 
    503   1.7  pooka 	pthread_attr_init(&pattr);
    504   1.7  pooka 	pthread_attr_setdetachstate(&pattr, 1);
    505   1.7  pooka 
    506   1.7  pooka 	if ((rv = pthread_create(&pt, &pattr, serv_syscallbouncer, sba)) != 0) {
    507   1.7  pooka 		/* panic */
    508   1.7  pooka 		abort();
    509   1.7  pooka 	}
    510   1.7  pooka }
    511   1.7  pooka 
    512   1.1  pooka static void *
    513   1.1  pooka spserver(void *arg)
    514   1.1  pooka {
    515   1.1  pooka 	struct spservarg *sarg = arg;
    516   1.1  pooka 	unsigned idx;
    517   1.1  pooka 	int seen;
    518   1.1  pooka 	int rv;
    519   1.1  pooka 
    520   1.1  pooka 	for (idx = 1; idx < MAXCLI; idx++) {
    521   1.1  pooka 		pfdlist[idx].fd = -1;
    522   1.1  pooka 		pfdlist[idx].events = POLLIN;
    523   1.1  pooka 	}
    524   1.1  pooka 	pfdlist[0].fd = sarg->sps_sock;
    525   1.1  pooka 	pfdlist[0].events = POLLIN;
    526   1.1  pooka 	nfds = 1;
    527   1.1  pooka 	maxidx = 0;
    528   1.1  pooka 
    529   1.1  pooka 	DPRINTF(("rump_sp: server mainloop\n"));
    530   1.1  pooka 
    531   1.1  pooka 	for (;;) {
    532   1.1  pooka 		DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
    533   1.1  pooka 		seen = 0;
    534   1.1  pooka 		rv = poll(pfdlist, maxidx+1, INFTIM);
    535   1.1  pooka 		assert(maxidx+1 <= MAXCLI);
    536   1.1  pooka 		assert(rv != 0);
    537   1.1  pooka 		if (rv == -1) {
    538   1.1  pooka 			if (errno == EINTR)
    539   1.1  pooka 				continue;
    540   1.1  pooka 			fprintf(stderr, "rump_spserver: poll returned %d\n",
    541   1.1  pooka 			    errno);
    542   1.1  pooka 			break;
    543   1.1  pooka 		}
    544   1.1  pooka 
    545   1.1  pooka 		for (idx = 0; seen < rv; idx++) {
    546   1.1  pooka 			assert(idx < MAXCLI);
    547   1.1  pooka 
    548   1.1  pooka 			if ((pfdlist[idx].revents & POLLIN) == 0)
    549   1.1  pooka 				continue;
    550   1.1  pooka 
    551   1.1  pooka 			seen++;
    552   1.1  pooka 			DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
    553   1.1  pooka 			    idx, seen, rv));
    554   1.1  pooka 			if (idx > 0) {
    555   1.1  pooka 				struct spclient *spc = &spclist[idx];
    556   1.1  pooka 
    557   1.1  pooka 				DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
    558   1.1  pooka 				switch (readframe(spc)) {
    559   1.1  pooka 				case 0:
    560   1.1  pooka 					break;
    561   1.1  pooka 				case -1:
    562   1.2  pooka 					serv_handledisco(idx);
    563   1.1  pooka 					break;
    564   1.1  pooka 				default:
    565   1.7  pooka 					switch (spc->spc_hdr.rsp_class) {
    566   1.7  pooka 					case RUMPSP_RESP:
    567   1.7  pooka 						kickwaiter(spc);
    568   1.7  pooka 						break;
    569   1.7  pooka 					case RUMPSP_REQ:
    570   1.7  pooka 						handlereq(spc);
    571   1.7  pooka 						break;
    572   1.7  pooka 					default:
    573   1.7  pooka 						printf("PANIC\n");
    574   1.7  pooka 						abort();
    575   1.7  pooka 						break;
    576   1.7  pooka 					}
    577   1.1  pooka 					break;
    578   1.1  pooka 				}
    579   1.1  pooka 			} else {
    580   1.1  pooka 				DPRINTF(("rump_sp: mainloop new connection\n"));
    581   1.1  pooka 				serv_handleconn(pfdlist[0].fd,
    582   1.1  pooka 				    sarg->sps_connhook);
    583   1.1  pooka 			}
    584   1.1  pooka 		}
    585   1.1  pooka 	}
    586   1.1  pooka 
    587   1.1  pooka 	return NULL;
    588   1.1  pooka }
    589   1.1  pooka 
    590   1.1  pooka int
    591   1.5  pooka rumpuser_sp_init(const struct rumpuser_sp_ops *spopsp, const char *url)
    592   1.1  pooka {
    593   1.5  pooka 	pthread_t pt;
    594   1.1  pooka 	struct spservarg *sarg;
    595   1.1  pooka 	struct sockaddr *sap;
    596   1.5  pooka 	char *p;
    597   1.5  pooka 	unsigned idx;
    598   1.5  pooka 	int error, s;
    599   1.5  pooka 
    600   1.5  pooka 	p = strdup(url);
    601   1.5  pooka 	if (p == NULL)
    602   1.5  pooka 		return ENOMEM;
    603   1.5  pooka 	error = parseurl(p, &sap, &idx, 1);
    604   1.5  pooka 	free(p);
    605   1.5  pooka 	if (error)
    606   1.5  pooka 		return error;
    607   1.5  pooka 
    608   1.5  pooka 	s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
    609   1.5  pooka 	if (s == -1)
    610   1.5  pooka 		return errno;
    611   1.1  pooka 
    612   1.3  pooka 	spops = *spopsp;
    613   1.5  pooka 	sarg = malloc(sizeof(*sarg));
    614   1.5  pooka 	if (sarg == NULL) {
    615   1.5  pooka 		close(s);
    616   1.5  pooka 		return ENOMEM;
    617   1.5  pooka 	}
    618   1.5  pooka 
    619   1.5  pooka 	sarg->sps_sock = s;
    620   1.5  pooka 	sarg->sps_connhook = parsetab[idx].connhook;
    621   1.1  pooka 
    622   1.5  pooka 	/* sloppy error recovery */
    623   1.1  pooka 
    624   1.5  pooka 	/*LINTED*/
    625   1.5  pooka 	if (bind(s, sap, sap->sa_len) == -1) {
    626   1.5  pooka 		fprintf(stderr, "rump_sp: server bind failed\n");
    627   1.5  pooka 		return errno;
    628   1.1  pooka 	}
    629   1.5  pooka 	if (listen(s, 20) == -1) {
    630   1.5  pooka 		fprintf(stderr, "rump_sp: server listen failed\n");
    631   1.5  pooka 		return errno;
    632   1.1  pooka 	}
    633   1.1  pooka 
    634   1.5  pooka 	if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
    635   1.5  pooka 		fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
    636   1.1  pooka 		return errno;
    637   1.1  pooka 	}
    638   1.5  pooka 	pthread_detach(pt);
    639   1.1  pooka 
    640   1.1  pooka 	return 0;
    641   1.1  pooka }
    642