Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_sp.c revision 1.30
      1  1.30  pooka /*      $NetBSD: rumpuser_sp.c,v 1.30 2011/01/05 22:57:01 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4  1.29  pooka  * Copyright (c) 2010, 2011 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 
     37   1.1  pooka #include <sys/cdefs.h>
     38  1.30  pooka __RCSID("$NetBSD: rumpuser_sp.c,v 1.30 2011/01/05 22:57:01 pooka Exp $");
     39   1.1  pooka 
     40   1.1  pooka #include <sys/types.h>
     41  1.11  pooka #include <sys/atomic.h>
     42   1.1  pooka #include <sys/mman.h>
     43   1.1  pooka #include <sys/socket.h>
     44   1.1  pooka 
     45   1.1  pooka #include <arpa/inet.h>
     46   1.1  pooka #include <netinet/in.h>
     47   1.1  pooka #include <netinet/tcp.h>
     48   1.1  pooka 
     49   1.1  pooka #include <assert.h>
     50   1.1  pooka #include <errno.h>
     51   1.1  pooka #include <fcntl.h>
     52   1.1  pooka #include <poll.h>
     53   1.1  pooka #include <pthread.h>
     54   1.1  pooka #include <stdarg.h>
     55   1.1  pooka #include <stdio.h>
     56   1.1  pooka #include <stdlib.h>
     57   1.1  pooka #include <string.h>
     58   1.1  pooka #include <unistd.h>
     59   1.1  pooka 
     60  1.28  pooka #include <rump/rump.h> /* XXX: for rfork flags */
     61   1.1  pooka #include <rump/rumpuser.h>
     62  1.13  pooka #include "rumpuser_int.h"
     63   1.1  pooka 
     64   1.5  pooka #include "sp_common.c"
     65   1.1  pooka 
     66  1.20  pooka #ifndef MAXCLI
     67  1.18  pooka #define MAXCLI 256
     68  1.20  pooka #endif
     69  1.20  pooka #ifndef MAXWORKER
     70  1.20  pooka #define MAXWORKER 128
     71  1.20  pooka #endif
     72  1.20  pooka #ifndef IDLEWORKER
     73  1.20  pooka #define IDLEWORKER 16
     74  1.20  pooka #endif
     75  1.20  pooka int rumpsp_maxworker = MAXWORKER;
     76  1.20  pooka int rumpsp_idleworker = IDLEWORKER;
     77   1.1  pooka 
     78   1.1  pooka static struct pollfd pfdlist[MAXCLI];
     79   1.1  pooka static struct spclient spclist[MAXCLI];
     80  1.11  pooka static unsigned int disco;
     81  1.24  pooka static volatile int spfini;
     82   1.1  pooka 
     83   1.3  pooka static struct rumpuser_sp_ops spops;
     84   1.3  pooka 
     85  1.26  pooka static char banner[MAXBANNER];
     86  1.26  pooka 
     87  1.26  pooka #define PROTOMAJOR 0
     88  1.29  pooka #define PROTOMINOR 1
     89  1.29  pooka 
     90  1.29  pooka struct prefork {
     91  1.29  pooka 	uint32_t pf_auth[AUTHLEN];
     92  1.29  pooka 	struct lwp *pf_lwp;
     93  1.29  pooka 
     94  1.29  pooka 	LIST_ENTRY(prefork) pf_entries;		/* global list */
     95  1.29  pooka 	LIST_ENTRY(prefork) pf_spcentries;	/* linked from forking spc */
     96  1.29  pooka };
     97  1.29  pooka static LIST_HEAD(, prefork) preforks = LIST_HEAD_INITIALIZER(preforks);
     98  1.29  pooka static pthread_mutex_t pfmtx;
     99  1.26  pooka 
    100   1.3  pooka /*
    101   1.3  pooka  * Manual wrappers, since librump does not have access to the
    102   1.3  pooka  * user namespace wrapped interfaces.
    103   1.3  pooka  */
    104   1.3  pooka 
    105   1.3  pooka static void
    106   1.3  pooka lwproc_switch(struct lwp *l)
    107   1.3  pooka {
    108   1.3  pooka 
    109   1.4  pooka 	spops.spop_schedule();
    110   1.3  pooka 	spops.spop_lwproc_switch(l);
    111   1.4  pooka 	spops.spop_unschedule();
    112   1.3  pooka }
    113   1.3  pooka 
    114   1.3  pooka static void
    115   1.3  pooka lwproc_release(void)
    116   1.3  pooka {
    117   1.3  pooka 
    118   1.4  pooka 	spops.spop_schedule();
    119   1.3  pooka 	spops.spop_lwproc_release();
    120   1.4  pooka 	spops.spop_unschedule();
    121   1.3  pooka }
    122   1.3  pooka 
    123   1.3  pooka static int
    124  1.28  pooka lwproc_rfork(struct spclient *spc, int flags)
    125   1.3  pooka {
    126   1.3  pooka 	int rv;
    127   1.3  pooka 
    128   1.4  pooka 	spops.spop_schedule();
    129  1.28  pooka 	rv = spops.spop_lwproc_rfork(spc, flags);
    130   1.4  pooka 	spops.spop_unschedule();
    131   1.3  pooka 
    132   1.3  pooka 	return rv;
    133   1.3  pooka }
    134   1.3  pooka 
    135   1.8  pooka static int
    136   1.8  pooka lwproc_newlwp(pid_t pid)
    137   1.8  pooka {
    138   1.8  pooka 	int rv;
    139   1.8  pooka 
    140   1.8  pooka 	spops.spop_schedule();
    141   1.8  pooka 	rv = spops.spop_lwproc_newlwp(pid);
    142   1.8  pooka 	spops.spop_unschedule();
    143   1.8  pooka 
    144   1.8  pooka 	return rv;
    145   1.8  pooka }
    146   1.8  pooka 
    147   1.3  pooka static struct lwp *
    148   1.3  pooka lwproc_curlwp(void)
    149   1.3  pooka {
    150   1.3  pooka 	struct lwp *l;
    151   1.3  pooka 
    152   1.4  pooka 	spops.spop_schedule();
    153   1.3  pooka 	l = spops.spop_lwproc_curlwp();
    154   1.4  pooka 	spops.spop_unschedule();
    155   1.3  pooka 
    156   1.3  pooka 	return l;
    157   1.3  pooka }
    158   1.3  pooka 
    159   1.8  pooka static pid_t
    160   1.8  pooka lwproc_getpid(void)
    161   1.8  pooka {
    162   1.8  pooka 	pid_t p;
    163   1.8  pooka 
    164   1.8  pooka 	spops.spop_schedule();
    165   1.8  pooka 	p = spops.spop_getpid();
    166   1.8  pooka 	spops.spop_unschedule();
    167   1.8  pooka 
    168   1.8  pooka 	return p;
    169   1.8  pooka }
    170   1.8  pooka 
    171   1.3  pooka static int
    172   1.3  pooka rumpsyscall(int sysnum, void *data, register_t *retval)
    173   1.3  pooka {
    174   1.3  pooka 	int rv;
    175   1.3  pooka 
    176   1.4  pooka 	spops.spop_schedule();
    177   1.3  pooka 	rv = spops.spop_syscall(sysnum, data, retval);
    178   1.4  pooka 	spops.spop_unschedule();
    179   1.3  pooka 
    180   1.3  pooka 	return rv;
    181   1.3  pooka }
    182   1.1  pooka 
    183   1.7  pooka static uint64_t
    184   1.7  pooka nextreq(struct spclient *spc)
    185   1.7  pooka {
    186   1.7  pooka 	uint64_t nw;
    187   1.7  pooka 
    188   1.7  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    189   1.7  pooka 	nw = spc->spc_nextreq++;
    190   1.7  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    191   1.7  pooka 
    192   1.7  pooka 	return nw;
    193   1.7  pooka }
    194   1.7  pooka 
    195  1.21  pooka static void
    196  1.21  pooka send_error_resp(struct spclient *spc, uint64_t reqno, int error)
    197  1.21  pooka {
    198  1.21  pooka 	struct rsp_hdr rhdr;
    199  1.21  pooka 
    200  1.21  pooka 	rhdr.rsp_len = sizeof(rhdr);
    201  1.21  pooka 	rhdr.rsp_reqno = reqno;
    202  1.21  pooka 	rhdr.rsp_class = RUMPSP_ERROR;
    203  1.21  pooka 	rhdr.rsp_type = 0;
    204  1.21  pooka 	rhdr.rsp_error = error;
    205  1.21  pooka 
    206  1.21  pooka 	sendlock(spc);
    207  1.21  pooka 	(void)dosend(spc, &rhdr, sizeof(rhdr));
    208  1.21  pooka 	sendunlock(spc);
    209  1.21  pooka }
    210  1.21  pooka 
    211   1.1  pooka static int
    212  1.27  pooka send_handshake_resp(struct spclient *spc, uint64_t reqno, int error)
    213  1.27  pooka {
    214  1.27  pooka 	struct rsp_hdr rhdr;
    215  1.27  pooka 	int rv;
    216  1.27  pooka 
    217  1.27  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(error);
    218  1.27  pooka 	rhdr.rsp_reqno = reqno;
    219  1.27  pooka 	rhdr.rsp_class = RUMPSP_RESP;
    220  1.27  pooka 	rhdr.rsp_type = RUMPSP_HANDSHAKE;
    221  1.27  pooka 	rhdr.rsp_error = 0;
    222  1.27  pooka 
    223  1.27  pooka 	sendlock(spc);
    224  1.27  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    225  1.27  pooka 	rv = dosend(spc, &error, sizeof(error));
    226  1.27  pooka 	sendunlock(spc);
    227  1.27  pooka 
    228  1.27  pooka 	return rv;
    229  1.27  pooka }
    230  1.27  pooka 
    231  1.27  pooka static int
    232   1.1  pooka send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
    233   1.7  pooka 	register_t *retval)
    234   1.1  pooka {
    235   1.1  pooka 	struct rsp_hdr rhdr;
    236   1.1  pooka 	struct rsp_sysresp sysresp;
    237   1.7  pooka 	int rv;
    238   1.1  pooka 
    239   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
    240   1.1  pooka 	rhdr.rsp_reqno = reqno;
    241   1.7  pooka 	rhdr.rsp_class = RUMPSP_RESP;
    242   1.7  pooka 	rhdr.rsp_type = RUMPSP_SYSCALL;
    243   1.1  pooka 	rhdr.rsp_sysnum = 0;
    244   1.1  pooka 
    245   1.1  pooka 	sysresp.rsys_error = error;
    246   1.7  pooka 	memcpy(sysresp.rsys_retval, retval, sizeof(sysresp.rsys_retval));
    247   1.1  pooka 
    248   1.7  pooka 	sendlock(spc);
    249   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    250   1.7  pooka 	rv = dosend(spc, &sysresp, sizeof(sysresp));
    251   1.7  pooka 	sendunlock(spc);
    252   1.1  pooka 
    253   1.7  pooka 	return rv;
    254   1.1  pooka }
    255   1.1  pooka 
    256   1.1  pooka static int
    257  1.29  pooka send_prefork_resp(struct spclient *spc, uint64_t reqno, uint32_t *auth)
    258  1.29  pooka {
    259  1.29  pooka 	struct rsp_hdr rhdr;
    260  1.29  pooka 	int rv;
    261  1.29  pooka 
    262  1.29  pooka 	rhdr.rsp_len = sizeof(rhdr) + AUTHLEN*sizeof(*auth);
    263  1.29  pooka 	rhdr.rsp_reqno = reqno;
    264  1.29  pooka 	rhdr.rsp_class = RUMPSP_RESP;
    265  1.29  pooka 	rhdr.rsp_type = RUMPSP_PREFORK;
    266  1.29  pooka 	rhdr.rsp_sysnum = 0;
    267  1.29  pooka 
    268  1.29  pooka 	sendlock(spc);
    269  1.29  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    270  1.29  pooka 	rv = dosend(spc, auth, AUTHLEN*sizeof(*auth));
    271  1.29  pooka 	sendunlock(spc);
    272  1.29  pooka 
    273  1.29  pooka 	return rv;
    274  1.29  pooka }
    275  1.29  pooka 
    276  1.29  pooka static int
    277  1.15  pooka copyin_req(struct spclient *spc, const void *remaddr, size_t *dlen,
    278  1.15  pooka 	int wantstr, void **resp)
    279   1.1  pooka {
    280   1.1  pooka 	struct rsp_hdr rhdr;
    281   1.1  pooka 	struct rsp_copydata copydata;
    282   1.7  pooka 	struct respwait rw;
    283   1.7  pooka 	int rv;
    284   1.7  pooka 
    285  1.15  pooka 	DPRINTF(("copyin_req: %zu bytes from %p\n", *dlen, remaddr));
    286   1.1  pooka 
    287   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
    288   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    289  1.15  pooka 	if (wantstr)
    290  1.15  pooka 		rhdr.rsp_type = RUMPSP_COPYINSTR;
    291  1.15  pooka 	else
    292  1.15  pooka 		rhdr.rsp_type = RUMPSP_COPYIN;
    293   1.1  pooka 	rhdr.rsp_sysnum = 0;
    294   1.1  pooka 
    295   1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    296  1.15  pooka 	copydata.rcp_len = *dlen;
    297   1.1  pooka 
    298   1.7  pooka 	putwait(spc, &rw, &rhdr);
    299   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    300   1.7  pooka 	rv = dosend(spc, &copydata, sizeof(copydata));
    301  1.13  pooka 	if (rv) {
    302  1.13  pooka 		unputwait(spc, &rw);
    303  1.13  pooka 		return rv;
    304  1.13  pooka 	}
    305   1.7  pooka 
    306   1.7  pooka 	rv = waitresp(spc, &rw);
    307   1.7  pooka 
    308   1.7  pooka 	DPRINTF(("copyin: response %d\n", rv));
    309   1.7  pooka 
    310   1.7  pooka 	*resp = rw.rw_data;
    311  1.15  pooka 	if (wantstr)
    312  1.15  pooka 		*dlen = rw.rw_dlen;
    313  1.15  pooka 
    314   1.7  pooka 	return rv;
    315   1.1  pooka 
    316   1.1  pooka }
    317   1.1  pooka 
    318   1.1  pooka static int
    319   1.1  pooka send_copyout_req(struct spclient *spc, const void *remaddr,
    320   1.1  pooka 	const void *data, size_t dlen)
    321   1.1  pooka {
    322   1.1  pooka 	struct rsp_hdr rhdr;
    323   1.1  pooka 	struct rsp_copydata copydata;
    324   1.7  pooka 	int rv;
    325   1.7  pooka 
    326   1.7  pooka 	DPRINTF(("copyout_req (async): %zu bytes to %p\n", dlen, remaddr));
    327   1.1  pooka 
    328   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
    329   1.7  pooka 	rhdr.rsp_reqno = nextreq(spc);
    330   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    331   1.7  pooka 	rhdr.rsp_type = RUMPSP_COPYOUT;
    332   1.1  pooka 	rhdr.rsp_sysnum = 0;
    333   1.1  pooka 
    334   1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    335   1.1  pooka 	copydata.rcp_len = dlen;
    336   1.1  pooka 
    337   1.7  pooka 	sendlock(spc);
    338   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    339   1.7  pooka 	rv = dosend(spc, &copydata, sizeof(copydata));
    340   1.7  pooka 	rv = dosend(spc, data, dlen);
    341   1.7  pooka 	sendunlock(spc);
    342   1.1  pooka 
    343   1.7  pooka 	return rv;
    344   1.1  pooka }
    345   1.1  pooka 
    346   1.1  pooka static int
    347   1.7  pooka anonmmap_req(struct spclient *spc, size_t howmuch, void **resp)
    348   1.1  pooka {
    349   1.1  pooka 	struct rsp_hdr rhdr;
    350   1.7  pooka 	struct respwait rw;
    351   1.7  pooka 	int rv;
    352   1.7  pooka 
    353   1.7  pooka 	DPRINTF(("anonmmap_req: %zu bytes\n", howmuch));
    354   1.1  pooka 
    355   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
    356   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    357   1.7  pooka 	rhdr.rsp_type = RUMPSP_ANONMMAP;
    358   1.1  pooka 	rhdr.rsp_sysnum = 0;
    359   1.1  pooka 
    360   1.7  pooka 	putwait(spc, &rw, &rhdr);
    361   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    362   1.7  pooka 	rv = dosend(spc, &howmuch, sizeof(howmuch));
    363  1.13  pooka 	if (rv) {
    364  1.13  pooka 		unputwait(spc, &rw);
    365  1.13  pooka 		return rv;
    366  1.13  pooka 	}
    367   1.1  pooka 
    368   1.7  pooka 	rv = waitresp(spc, &rw);
    369  1.13  pooka 
    370   1.7  pooka 	*resp = rw.rw_data;
    371   1.7  pooka 
    372   1.7  pooka 	DPRINTF(("anonmmap: mapped at %p\n", **(void ***)resp));
    373   1.7  pooka 
    374   1.7  pooka 	return rv;
    375   1.1  pooka }
    376   1.1  pooka 
    377   1.1  pooka static void
    378  1.11  pooka spcref(struct spclient *spc)
    379  1.11  pooka {
    380  1.11  pooka 
    381  1.11  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    382  1.11  pooka 	spc->spc_refcnt++;
    383  1.11  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    384  1.11  pooka }
    385  1.11  pooka 
    386  1.11  pooka static void
    387  1.11  pooka spcrelease(struct spclient *spc)
    388  1.11  pooka {
    389  1.11  pooka 	int ref;
    390  1.11  pooka 
    391  1.11  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    392  1.11  pooka 	ref = --spc->spc_refcnt;
    393  1.11  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    394  1.11  pooka 
    395  1.11  pooka 	if (ref > 0)
    396  1.11  pooka 		return;
    397  1.11  pooka 
    398  1.29  pooka 	DPRINTF(("rump_sp: spcrelease: spc %p fd %d\n", spc, spc->spc_fd));
    399  1.12  pooka 
    400  1.13  pooka 	_DIAGASSERT(TAILQ_EMPTY(&spc->spc_respwait));
    401  1.11  pooka 	_DIAGASSERT(spc->spc_buf == NULL);
    402  1.11  pooka 
    403  1.29  pooka 	if (spc->spc_mainlwp) {
    404  1.29  pooka 		lwproc_switch(spc->spc_mainlwp);
    405  1.29  pooka 		lwproc_release();
    406  1.29  pooka 	}
    407  1.11  pooka 	spc->spc_mainlwp = NULL;
    408  1.11  pooka 
    409  1.11  pooka 	close(spc->spc_fd);
    410  1.11  pooka 	spc->spc_fd = -1;
    411  1.27  pooka 	spc->spc_state = SPCSTATE_NEW;
    412  1.11  pooka 
    413  1.11  pooka 	atomic_inc_uint(&disco);
    414  1.11  pooka }
    415  1.11  pooka 
    416  1.11  pooka static void
    417   1.2  pooka serv_handledisco(unsigned int idx)
    418   1.1  pooka {
    419   1.1  pooka 	struct spclient *spc = &spclist[idx];
    420   1.1  pooka 
    421   1.1  pooka 	DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
    422   1.1  pooka 
    423  1.12  pooka 	pfdlist[idx].fd = -1;
    424  1.12  pooka 	pfdlist[idx].revents = 0;
    425  1.12  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    426  1.27  pooka 	spc->spc_state = SPCSTATE_DYING;
    427  1.12  pooka 	kickall(spc);
    428  1.30  pooka 	sendunlockl(spc);
    429  1.12  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    430  1.17  pooka 
    431  1.17  pooka 	/*
    432  1.17  pooka 	 * Nobody's going to attempt to send/receive anymore,
    433  1.17  pooka 	 * so reinit info relevant to that.
    434  1.17  pooka 	 */
    435  1.22  pooka 	/*LINTED:pointer casts may be ok*/
    436  1.17  pooka 	memset((char *)spc + SPC_ZEROFF, 0, sizeof(*spc) - SPC_ZEROFF);
    437  1.17  pooka 
    438  1.11  pooka 	spcrelease(spc);
    439   1.1  pooka }
    440   1.1  pooka 
    441  1.24  pooka static void
    442  1.24  pooka serv_shutdown(void)
    443  1.24  pooka {
    444  1.24  pooka 	struct spclient *spc;
    445  1.24  pooka 	unsigned int i;
    446  1.24  pooka 
    447  1.24  pooka 	for (i = 1; i < MAXCLI; i++) {
    448  1.24  pooka 		spc = &spclist[i];
    449  1.24  pooka 		if (spc->spc_fd == -1)
    450  1.24  pooka 			continue;
    451  1.24  pooka 
    452  1.24  pooka 		shutdown(spc->spc_fd, SHUT_RDWR);
    453  1.24  pooka 		serv_handledisco(i);
    454  1.24  pooka 
    455  1.24  pooka 		spcrelease(spc);
    456  1.24  pooka 	}
    457  1.24  pooka }
    458  1.24  pooka 
    459  1.11  pooka static unsigned
    460  1.11  pooka serv_handleconn(int fd, connecthook_fn connhook, int busy)
    461   1.1  pooka {
    462   1.1  pooka 	struct sockaddr_storage ss;
    463   1.1  pooka 	socklen_t sl = sizeof(ss);
    464  1.11  pooka 	int newfd, flags;
    465   1.1  pooka 	unsigned i;
    466   1.1  pooka 
    467   1.1  pooka 	/*LINTED: cast ok */
    468   1.1  pooka 	newfd = accept(fd, (struct sockaddr *)&ss, &sl);
    469   1.1  pooka 	if (newfd == -1)
    470  1.11  pooka 		return 0;
    471   1.1  pooka 
    472  1.11  pooka 	if (busy) {
    473   1.1  pooka 		close(newfd); /* EBUSY */
    474  1.11  pooka 		return 0;
    475   1.1  pooka 	}
    476   1.1  pooka 
    477   1.1  pooka 	flags = fcntl(newfd, F_GETFL, 0);
    478   1.1  pooka 	if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
    479   1.1  pooka 		close(newfd);
    480  1.11  pooka 		return 0;
    481   1.1  pooka 	}
    482   1.1  pooka 
    483  1.11  pooka 	if (connhook(newfd) != 0) {
    484   1.1  pooka 		close(newfd);
    485  1.11  pooka 		return 0;
    486   1.1  pooka 	}
    487   1.1  pooka 
    488  1.26  pooka 	/* write out a banner for the client */
    489  1.26  pooka 	if (write(newfd, banner, strlen(banner)) != (ssize_t)strlen(banner)) {
    490  1.26  pooka 		close(newfd);
    491  1.26  pooka 		return 0;
    492  1.26  pooka 	}
    493  1.26  pooka 
    494   1.1  pooka 	/* find empty slot the simple way */
    495   1.1  pooka 	for (i = 0; i < MAXCLI; i++) {
    496  1.27  pooka 		if (pfdlist[i].fd == -1 && spclist[i].spc_state == SPCSTATE_NEW)
    497   1.2  pooka 			break;
    498   1.1  pooka 	}
    499   1.1  pooka 
    500   1.1  pooka 	assert(i < MAXCLI);
    501   1.1  pooka 
    502   1.1  pooka 	pfdlist[i].fd = newfd;
    503   1.1  pooka 	spclist[i].spc_fd = newfd;
    504   1.7  pooka 	spclist[i].spc_istatus = SPCSTATUS_BUSY; /* dedicated receiver */
    505  1.11  pooka 	spclist[i].spc_refcnt = 1;
    506   1.7  pooka 
    507   1.7  pooka 	TAILQ_INIT(&spclist[i].spc_respwait);
    508   1.1  pooka 
    509  1.29  pooka 	DPRINTF(("rump_sp: added new connection fd %d at idx %u\n", newfd, i));
    510   1.1  pooka 
    511  1.11  pooka 	return i;
    512   1.1  pooka }
    513   1.1  pooka 
    514   1.1  pooka static void
    515   1.1  pooka serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
    516   1.1  pooka {
    517   1.7  pooka 	register_t retval[2] = {0, 0};
    518   1.1  pooka 	int rv, sysnum;
    519   1.1  pooka 
    520   1.1  pooka 	sysnum = (int)rhdr->rsp_sysnum;
    521   1.1  pooka 	DPRINTF(("rump_sp: handling syscall %d from client %d\n",
    522  1.29  pooka 	    sysnum, spc->spc_pid));
    523   1.1  pooka 
    524   1.8  pooka 	lwproc_newlwp(spc->spc_pid);
    525   1.3  pooka 	rv = rumpsyscall(sysnum, data, retval);
    526  1.16  pooka 	lwproc_release();
    527   1.1  pooka 
    528   1.7  pooka 	DPRINTF(("rump_sp: got return value %d & %d/%d\n",
    529   1.7  pooka 	    rv, retval[0], retval[1]));
    530   1.5  pooka 
    531   1.1  pooka 	send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
    532   1.1  pooka }
    533   1.1  pooka 
    534   1.7  pooka struct sysbouncearg {
    535   1.7  pooka 	struct spclient *sba_spc;
    536   1.7  pooka 	struct rsp_hdr sba_hdr;
    537   1.7  pooka 	uint8_t *sba_data;
    538  1.20  pooka 
    539  1.20  pooka 	TAILQ_ENTRY(sysbouncearg) sba_entries;
    540   1.7  pooka };
    541  1.20  pooka static pthread_mutex_t sbamtx;
    542  1.20  pooka static pthread_cond_t sbacv;
    543  1.20  pooka static int nworker, idleworker;
    544  1.20  pooka static TAILQ_HEAD(, sysbouncearg) syslist = TAILQ_HEAD_INITIALIZER(syslist);
    545  1.20  pooka 
    546  1.20  pooka /*ARGSUSED*/
    547   1.7  pooka static void *
    548   1.7  pooka serv_syscallbouncer(void *arg)
    549   1.7  pooka {
    550  1.20  pooka 	struct sysbouncearg *sba;
    551  1.20  pooka 
    552  1.20  pooka 	for (;;) {
    553  1.20  pooka 		pthread_mutex_lock(&sbamtx);
    554  1.20  pooka 		if (idleworker >= rumpsp_idleworker) {
    555  1.20  pooka 			nworker--;
    556  1.20  pooka 			pthread_mutex_unlock(&sbamtx);
    557  1.20  pooka 			break;
    558  1.20  pooka 		}
    559  1.20  pooka 		idleworker++;
    560  1.20  pooka 		while (TAILQ_EMPTY(&syslist)) {
    561  1.20  pooka 			pthread_cond_wait(&sbacv, &sbamtx);
    562  1.20  pooka 		}
    563  1.20  pooka 
    564  1.20  pooka 		sba = TAILQ_FIRST(&syslist);
    565  1.20  pooka 		TAILQ_REMOVE(&syslist, sba, sba_entries);
    566  1.20  pooka 		idleworker--;
    567  1.20  pooka 		pthread_mutex_unlock(&sbamtx);
    568  1.20  pooka 
    569  1.20  pooka 		serv_handlesyscall(sba->sba_spc,
    570  1.20  pooka 		    &sba->sba_hdr, sba->sba_data);
    571  1.20  pooka 		spcrelease(sba->sba_spc);
    572  1.20  pooka 		free(sba->sba_data);
    573  1.20  pooka 		free(sba);
    574  1.20  pooka 	}
    575   1.7  pooka 
    576   1.7  pooka 	return NULL;
    577   1.7  pooka }
    578   1.7  pooka 
    579  1.15  pooka static int
    580  1.15  pooka sp_copyin(void *arg, const void *raddr, void *laddr, size_t *len, int wantstr)
    581   1.1  pooka {
    582  1.10  pooka 	struct spclient *spc = arg;
    583   1.9  pooka 	void *rdata = NULL; /* XXXuninit */
    584  1.13  pooka 	int rv, nlocks;
    585  1.13  pooka 
    586  1.13  pooka 	rumpuser__kunlock(0, &nlocks, NULL);
    587   1.1  pooka 
    588  1.15  pooka 	rv = copyin_req(spc, raddr, len, wantstr, &rdata);
    589  1.12  pooka 	if (rv)
    590  1.13  pooka 		goto out;
    591   1.1  pooka 
    592  1.15  pooka 	memcpy(laddr, rdata, *len);
    593   1.7  pooka 	free(rdata);
    594   1.1  pooka 
    595  1.13  pooka  out:
    596  1.13  pooka 	rumpuser__klock(nlocks, NULL);
    597  1.13  pooka 	if (rv)
    598  1.13  pooka 		return EFAULT;
    599   1.1  pooka 	return 0;
    600   1.1  pooka }
    601   1.1  pooka 
    602   1.1  pooka int
    603  1.15  pooka rumpuser_sp_copyin(void *arg, const void *raddr, void *laddr, size_t len)
    604  1.15  pooka {
    605  1.15  pooka 
    606  1.15  pooka 	return sp_copyin(arg, raddr, laddr, &len, 0);
    607  1.15  pooka }
    608  1.15  pooka 
    609  1.15  pooka int
    610  1.15  pooka rumpuser_sp_copyinstr(void *arg, const void *raddr, void *laddr, size_t *len)
    611  1.15  pooka {
    612  1.15  pooka 
    613  1.15  pooka 	return sp_copyin(arg, raddr, laddr, len, 1);
    614  1.15  pooka }
    615  1.15  pooka 
    616  1.15  pooka static int
    617  1.15  pooka sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
    618   1.1  pooka {
    619  1.10  pooka 	struct spclient *spc = arg;
    620  1.13  pooka 	int nlocks, rv;
    621  1.13  pooka 
    622  1.13  pooka 	rumpuser__kunlock(0, &nlocks, NULL);
    623  1.15  pooka 	rv = send_copyout_req(spc, raddr, laddr, dlen);
    624  1.13  pooka 	rumpuser__klock(nlocks, NULL);
    625   1.1  pooka 
    626  1.13  pooka 	if (rv)
    627   1.7  pooka 		return EFAULT;
    628   1.1  pooka 	return 0;
    629   1.1  pooka }
    630   1.1  pooka 
    631   1.1  pooka int
    632  1.15  pooka rumpuser_sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
    633  1.15  pooka {
    634  1.15  pooka 
    635  1.15  pooka 	return sp_copyout(arg, laddr, raddr, dlen);
    636  1.15  pooka }
    637  1.15  pooka 
    638  1.15  pooka int
    639  1.15  pooka rumpuser_sp_copyoutstr(void *arg, const void *laddr, void *raddr, size_t *dlen)
    640  1.15  pooka {
    641  1.15  pooka 
    642  1.15  pooka 	return sp_copyout(arg, laddr, raddr, *dlen);
    643  1.15  pooka }
    644  1.15  pooka 
    645  1.15  pooka int
    646  1.10  pooka rumpuser_sp_anonmmap(void *arg, size_t howmuch, void **addr)
    647   1.1  pooka {
    648  1.10  pooka 	struct spclient *spc = arg;
    649   1.7  pooka 	void *resp, *rdata;
    650  1.13  pooka 	int nlocks, rv;
    651  1.13  pooka 
    652  1.13  pooka 	rumpuser__kunlock(0, &nlocks, NULL);
    653   1.1  pooka 
    654   1.7  pooka 	rv = anonmmap_req(spc, howmuch, &rdata);
    655  1.13  pooka 	if (rv) {
    656  1.13  pooka 		rv = EFAULT;
    657  1.13  pooka 		goto out;
    658  1.13  pooka 	}
    659   1.1  pooka 
    660   1.7  pooka 	resp = *(void **)rdata;
    661   1.7  pooka 	free(rdata);
    662   1.1  pooka 
    663   1.7  pooka 	if (resp == NULL) {
    664  1.13  pooka 		rv = ENOMEM;
    665   1.1  pooka 	}
    666   1.1  pooka 
    667   1.1  pooka 	*addr = resp;
    668  1.13  pooka 
    669  1.13  pooka  out:
    670  1.13  pooka 	rumpuser__klock(nlocks, NULL);
    671  1.13  pooka 
    672  1.13  pooka 	if (rv)
    673  1.13  pooka 		return rv;
    674   1.1  pooka 	return 0;
    675   1.1  pooka }
    676   1.1  pooka 
    677   1.1  pooka /*
    678   1.1  pooka  *
    679   1.1  pooka  * Startup routines and mainloop for server.
    680   1.1  pooka  *
    681   1.1  pooka  */
    682   1.1  pooka 
    683   1.1  pooka struct spservarg {
    684   1.1  pooka 	int sps_sock;
    685   1.1  pooka 	connecthook_fn sps_connhook;
    686   1.1  pooka };
    687   1.1  pooka 
    688  1.14  pooka static pthread_attr_t pattr_detached;
    689   1.7  pooka static void
    690   1.7  pooka handlereq(struct spclient *spc)
    691   1.7  pooka {
    692   1.7  pooka 	struct sysbouncearg *sba;
    693   1.7  pooka 	pthread_t pt;
    694  1.29  pooka 	int retries, error, i;
    695  1.27  pooka 
    696  1.27  pooka 	if (__predict_false(spc->spc_state == SPCSTATE_NEW)) {
    697  1.27  pooka 		if (spc->spc_hdr.rsp_type != RUMPSP_HANDSHAKE) {
    698  1.27  pooka 			send_error_resp(spc, spc->spc_hdr.rsp_reqno, EAUTH);
    699  1.29  pooka 			shutdown(spc->spc_fd, SHUT_RDWR);
    700  1.27  pooka 			spcfreebuf(spc);
    701  1.27  pooka 			return;
    702  1.27  pooka 		}
    703  1.27  pooka 
    704  1.29  pooka 		if (spc->spc_hdr.rsp_handshake == HANDSHAKE_GUEST) {
    705  1.29  pooka 			if ((error = lwproc_rfork(spc, RUMP_RFCFDG)) != 0) {
    706  1.29  pooka 				shutdown(spc->spc_fd, SHUT_RDWR);
    707  1.29  pooka 			}
    708  1.29  pooka 
    709  1.29  pooka 			spcfreebuf(spc);
    710  1.29  pooka 			if (error)
    711  1.29  pooka 				return;
    712  1.29  pooka 
    713  1.29  pooka 			spc->spc_mainlwp = lwproc_curlwp();
    714  1.29  pooka 
    715  1.29  pooka 			send_handshake_resp(spc, spc->spc_hdr.rsp_reqno, 0);
    716  1.29  pooka 		} else if (spc->spc_hdr.rsp_handshake == HANDSHAKE_FORK) {
    717  1.29  pooka 			struct lwp *tmpmain;
    718  1.29  pooka 			struct prefork *pf;
    719  1.29  pooka 			struct handshake_fork *rfp;
    720  1.29  pooka 			uint64_t reqno;
    721  1.29  pooka 			int cancel;
    722  1.29  pooka 
    723  1.29  pooka 			reqno = spc->spc_hdr.rsp_reqno;
    724  1.29  pooka 			if (spc->spc_off-HDRSZ != sizeof(*rfp)) {
    725  1.29  pooka 				send_error_resp(spc, reqno, EINVAL);
    726  1.29  pooka 				shutdown(spc->spc_fd, SHUT_RDWR);
    727  1.29  pooka 				spcfreebuf(spc);
    728  1.29  pooka 				return;
    729  1.29  pooka 			}
    730  1.29  pooka 
    731  1.29  pooka 			/*LINTED*/
    732  1.29  pooka 			rfp = (void *)spc->spc_buf;
    733  1.29  pooka 			cancel = rfp->rf_cancel;
    734  1.29  pooka 
    735  1.29  pooka 			pthread_mutex_lock(&pfmtx);
    736  1.29  pooka 			LIST_FOREACH(pf, &preforks, pf_entries) {
    737  1.29  pooka 				if (memcmp(rfp->rf_auth, pf->pf_auth,
    738  1.29  pooka 				    sizeof(rfp->rf_auth)) == 0) {
    739  1.29  pooka 					LIST_REMOVE(pf, pf_entries);
    740  1.29  pooka 					LIST_REMOVE(pf, pf_spcentries);
    741  1.29  pooka 					break;
    742  1.29  pooka 				}
    743  1.29  pooka 			}
    744  1.29  pooka 			pthread_mutex_lock(&pfmtx);
    745  1.29  pooka 			spcfreebuf(spc);
    746  1.29  pooka 
    747  1.29  pooka 			if (!pf) {
    748  1.29  pooka 				send_error_resp(spc, reqno, ESRCH);
    749  1.29  pooka 				shutdown(spc->spc_fd, SHUT_RDWR);
    750  1.29  pooka 				return;
    751  1.29  pooka 			}
    752  1.29  pooka 
    753  1.29  pooka 			tmpmain = pf->pf_lwp;
    754  1.29  pooka 			free(pf);
    755  1.29  pooka 			lwproc_switch(tmpmain);
    756  1.29  pooka 			if (cancel) {
    757  1.29  pooka 				lwproc_release();
    758  1.29  pooka 				shutdown(spc->spc_fd, SHUT_RDWR);
    759  1.29  pooka 				return;
    760  1.29  pooka 			}
    761  1.29  pooka 
    762  1.29  pooka 			/*
    763  1.29  pooka 			 * So, we forked already during "prefork" to save
    764  1.29  pooka 			 * the file descriptors from a parent exit
    765  1.29  pooka 			 * race condition.  But now we need to fork
    766  1.29  pooka 			 * a second time since the initial fork has
    767  1.29  pooka 			 * the wrong spc pointer.  (yea, optimize
    768  1.29  pooka 			 * interfaces some day if anyone cares)
    769  1.29  pooka 			 */
    770  1.29  pooka 			if ((error = lwproc_rfork(spc, 0)) != 0) {
    771  1.29  pooka 				send_error_resp(spc, reqno, error);
    772  1.29  pooka 				shutdown(spc->spc_fd, SHUT_RDWR);
    773  1.29  pooka 				lwproc_release();
    774  1.29  pooka 				return;
    775  1.29  pooka 			}
    776  1.29  pooka 			spc->spc_mainlwp = lwproc_curlwp();
    777  1.29  pooka 			lwproc_switch(tmpmain);
    778  1.29  pooka 			lwproc_release();
    779  1.29  pooka 			lwproc_switch(spc->spc_mainlwp);
    780  1.29  pooka 
    781  1.29  pooka 			send_handshake_resp(spc, reqno, 0);
    782  1.29  pooka 		}
    783  1.29  pooka 
    784  1.29  pooka 		spc->spc_pid = lwproc_getpid();
    785  1.29  pooka 
    786  1.29  pooka 		DPRINTF(("rump_sp: handshake for client %p complete, pid %d\n",
    787  1.29  pooka 		    spc, spc->spc_pid));
    788  1.29  pooka 
    789  1.29  pooka 		lwproc_switch(NULL);
    790  1.29  pooka 		spc->spc_state = SPCSTATE_RUNNING;
    791  1.29  pooka 		return;
    792  1.29  pooka 	}
    793  1.29  pooka 
    794  1.29  pooka 	if (__predict_false(spc->spc_hdr.rsp_type == RUMPSP_PREFORK)) {
    795  1.29  pooka 		struct prefork *pf;
    796  1.29  pooka 		uint64_t reqno;
    797  1.29  pooka 		uint32_t auth[AUTHLEN];
    798  1.29  pooka 
    799  1.29  pooka 		DPRINTF(("rump_sp: prefork handler executing for %p\n", spc));
    800  1.29  pooka 		reqno = spc->spc_hdr.rsp_reqno;
    801  1.27  pooka 		spcfreebuf(spc);
    802  1.29  pooka 
    803  1.29  pooka 		pf = malloc(sizeof(*pf));
    804  1.29  pooka 		if (pf == NULL) {
    805  1.29  pooka 			send_error_resp(spc, reqno, ENOMEM);
    806  1.29  pooka 			return;
    807  1.29  pooka 		}
    808  1.29  pooka 
    809  1.29  pooka 		/*
    810  1.29  pooka 		 * Use client main lwp to fork.  this is never used by
    811  1.29  pooka 		 * worker threads (except if spc refcount goes to 0),
    812  1.29  pooka 		 * so we can safely use it here.
    813  1.29  pooka 		 */
    814  1.29  pooka 		lwproc_switch(spc->spc_mainlwp);
    815  1.29  pooka 		if ((error = lwproc_rfork(spc, RUMP_RFFDG)) != 0) {
    816  1.29  pooka 			DPRINTF(("rump_sp: fork failed: %d (%p)\n",error, spc));
    817  1.29  pooka 			send_error_resp(spc, reqno, error);
    818  1.29  pooka 			lwproc_switch(NULL);
    819  1.29  pooka 			free(pf);
    820  1.27  pooka 			return;
    821  1.27  pooka 		}
    822  1.29  pooka 
    823  1.29  pooka 		/* Ok, we have a new process context and a new curlwp */
    824  1.29  pooka 		for (i = 0; i < AUTHLEN; i++) {
    825  1.29  pooka 			pf->pf_auth[i] = auth[i] = arc4random();
    826  1.29  pooka 		}
    827  1.29  pooka 		pf->pf_lwp = lwproc_curlwp();
    828  1.29  pooka 		lwproc_switch(NULL);
    829  1.29  pooka 
    830  1.29  pooka 		pthread_mutex_lock(&pfmtx);
    831  1.29  pooka 		LIST_INSERT_HEAD(&preforks, pf, pf_entries);
    832  1.29  pooka 		LIST_INSERT_HEAD(&spc->spc_pflist, pf, pf_spcentries);
    833  1.29  pooka 		pthread_mutex_unlock(&pfmtx);
    834  1.29  pooka 
    835  1.29  pooka 		DPRINTF(("rump_sp: prefork handler success %p\n", spc));
    836  1.29  pooka 
    837  1.29  pooka 		send_prefork_resp(spc, reqno, auth);
    838  1.27  pooka 		return;
    839  1.27  pooka 	}
    840   1.7  pooka 
    841  1.21  pooka 	if (__predict_false(spc->spc_hdr.rsp_type != RUMPSP_SYSCALL)) {
    842  1.21  pooka 		send_error_resp(spc, spc->spc_hdr.rsp_reqno, EINVAL);
    843  1.21  pooka 		spcfreebuf(spc);
    844  1.21  pooka 		return;
    845  1.21  pooka 	}
    846   1.7  pooka 
    847  1.21  pooka 	retries = 0;
    848  1.21  pooka 	while ((sba = malloc(sizeof(*sba))) == NULL) {
    849  1.21  pooka 		if (nworker == 0 || retries > 10) {
    850  1.21  pooka 			send_error_resp(spc, spc->spc_hdr.rsp_reqno, EAGAIN);
    851  1.21  pooka 			spcfreebuf(spc);
    852  1.21  pooka 			return;
    853  1.21  pooka 		}
    854  1.21  pooka 		/* slim chance of more memory? */
    855  1.21  pooka 		usleep(10000);
    856   1.7  pooka 	}
    857   1.7  pooka 
    858   1.7  pooka 	sba->sba_spc = spc;
    859   1.7  pooka 	sba->sba_hdr = spc->spc_hdr;
    860   1.7  pooka 	sba->sba_data = spc->spc_buf;
    861  1.21  pooka 	spcresetbuf(spc);
    862   1.7  pooka 
    863  1.11  pooka 	spcref(spc);
    864  1.20  pooka 
    865  1.20  pooka 	pthread_mutex_lock(&sbamtx);
    866  1.20  pooka 	TAILQ_INSERT_TAIL(&syslist, sba, sba_entries);
    867  1.20  pooka 	if (idleworker > 0) {
    868  1.20  pooka 		/* do we have a daemon's tool (i.e. idle threads)? */
    869  1.20  pooka 		pthread_cond_signal(&sbacv);
    870  1.20  pooka 	} else if (nworker < rumpsp_maxworker) {
    871  1.20  pooka 		/*
    872  1.20  pooka 		 * Else, need to create one
    873  1.20  pooka 		 * (if we can, otherwise just expect another
    874  1.20  pooka 		 * worker to pick up the syscall)
    875  1.20  pooka 		 */
    876  1.20  pooka 		if (pthread_create(&pt, &pattr_detached,
    877  1.20  pooka 		    serv_syscallbouncer, NULL) == 0)
    878  1.20  pooka 			nworker++;
    879   1.7  pooka 	}
    880  1.20  pooka 	pthread_mutex_unlock(&sbamtx);
    881   1.7  pooka }
    882   1.7  pooka 
    883   1.1  pooka static void *
    884   1.1  pooka spserver(void *arg)
    885   1.1  pooka {
    886   1.1  pooka 	struct spservarg *sarg = arg;
    887  1.11  pooka 	struct spclient *spc;
    888   1.1  pooka 	unsigned idx;
    889   1.1  pooka 	int seen;
    890   1.1  pooka 	int rv;
    891  1.11  pooka 	unsigned int nfds, maxidx;
    892   1.1  pooka 
    893  1.11  pooka 	for (idx = 0; idx < MAXCLI; idx++) {
    894   1.1  pooka 		pfdlist[idx].fd = -1;
    895   1.1  pooka 		pfdlist[idx].events = POLLIN;
    896  1.11  pooka 
    897  1.11  pooka 		spc = &spclist[idx];
    898  1.11  pooka 		pthread_mutex_init(&spc->spc_mtx, NULL);
    899  1.11  pooka 		pthread_cond_init(&spc->spc_cv, NULL);
    900  1.24  pooka 		spc->spc_fd = -1;
    901   1.1  pooka 	}
    902  1.24  pooka 	pfdlist[0].fd = spclist[0].spc_fd = sarg->sps_sock;
    903   1.1  pooka 	pfdlist[0].events = POLLIN;
    904   1.1  pooka 	nfds = 1;
    905   1.1  pooka 	maxidx = 0;
    906   1.1  pooka 
    907  1.14  pooka 	pthread_attr_init(&pattr_detached);
    908  1.14  pooka 	pthread_attr_setdetachstate(&pattr_detached, PTHREAD_CREATE_DETACHED);
    909  1.19  pooka 	/* XXX: doesn't stacksize currently work on NetBSD */
    910  1.19  pooka 	pthread_attr_setstacksize(&pattr_detached, 32*1024);
    911  1.14  pooka 
    912  1.20  pooka 	pthread_mutex_init(&sbamtx, NULL);
    913  1.20  pooka 	pthread_cond_init(&sbacv, NULL);
    914  1.20  pooka 
    915   1.1  pooka 	DPRINTF(("rump_sp: server mainloop\n"));
    916   1.1  pooka 
    917   1.1  pooka 	for (;;) {
    918  1.18  pooka 		int discoed;
    919  1.18  pooka 
    920  1.11  pooka 		/* g/c hangarounds (eventually) */
    921  1.18  pooka 		discoed = atomic_swap_uint(&disco, 0);
    922  1.18  pooka 		while (discoed--) {
    923  1.18  pooka 			nfds--;
    924  1.18  pooka 			idx = maxidx;
    925  1.18  pooka 			while (idx) {
    926  1.18  pooka 				if (pfdlist[idx].fd != -1) {
    927  1.18  pooka 					maxidx = idx;
    928  1.18  pooka 					break;
    929  1.11  pooka 				}
    930  1.18  pooka 				idx--;
    931  1.11  pooka 			}
    932  1.18  pooka 			DPRINTF(("rump_sp: set maxidx to [%u]\n",
    933  1.18  pooka 			    maxidx));
    934  1.11  pooka 		}
    935  1.11  pooka 
    936   1.1  pooka 		DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
    937   1.1  pooka 		seen = 0;
    938   1.1  pooka 		rv = poll(pfdlist, maxidx+1, INFTIM);
    939   1.1  pooka 		assert(maxidx+1 <= MAXCLI);
    940   1.1  pooka 		assert(rv != 0);
    941   1.1  pooka 		if (rv == -1) {
    942   1.1  pooka 			if (errno == EINTR)
    943   1.1  pooka 				continue;
    944   1.1  pooka 			fprintf(stderr, "rump_spserver: poll returned %d\n",
    945   1.1  pooka 			    errno);
    946   1.1  pooka 			break;
    947   1.1  pooka 		}
    948   1.1  pooka 
    949  1.12  pooka 		for (idx = 0; seen < rv && idx < MAXCLI; idx++) {
    950   1.1  pooka 			if ((pfdlist[idx].revents & POLLIN) == 0)
    951   1.1  pooka 				continue;
    952   1.1  pooka 
    953   1.1  pooka 			seen++;
    954   1.1  pooka 			DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
    955   1.1  pooka 			    idx, seen, rv));
    956   1.1  pooka 			if (idx > 0) {
    957  1.11  pooka 				spc = &spclist[idx];
    958   1.1  pooka 				DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
    959   1.1  pooka 				switch (readframe(spc)) {
    960   1.1  pooka 				case 0:
    961   1.1  pooka 					break;
    962   1.1  pooka 				case -1:
    963   1.2  pooka 					serv_handledisco(idx);
    964   1.1  pooka 					break;
    965   1.1  pooka 				default:
    966   1.7  pooka 					switch (spc->spc_hdr.rsp_class) {
    967   1.7  pooka 					case RUMPSP_RESP:
    968   1.7  pooka 						kickwaiter(spc);
    969   1.7  pooka 						break;
    970   1.7  pooka 					case RUMPSP_REQ:
    971   1.7  pooka 						handlereq(spc);
    972   1.7  pooka 						break;
    973   1.7  pooka 					default:
    974  1.21  pooka 						send_error_resp(spc,
    975  1.21  pooka 						    spc->spc_hdr.rsp_reqno,
    976  1.21  pooka 						    ENOENT);
    977  1.21  pooka 						spcfreebuf(spc);
    978   1.7  pooka 						break;
    979   1.7  pooka 					}
    980   1.1  pooka 					break;
    981   1.1  pooka 				}
    982  1.11  pooka 
    983   1.1  pooka 			} else {
    984   1.1  pooka 				DPRINTF(("rump_sp: mainloop new connection\n"));
    985  1.11  pooka 
    986  1.24  pooka 				if (__predict_false(spfini)) {
    987  1.24  pooka 					close(spclist[0].spc_fd);
    988  1.24  pooka 					serv_shutdown();
    989  1.24  pooka 					goto out;
    990  1.24  pooka 				}
    991  1.24  pooka 
    992  1.11  pooka 				idx = serv_handleconn(pfdlist[0].fd,
    993  1.11  pooka 				    sarg->sps_connhook, nfds == MAXCLI);
    994  1.11  pooka 				if (idx)
    995  1.11  pooka 					nfds++;
    996  1.11  pooka 				if (idx > maxidx)
    997  1.11  pooka 					maxidx = idx;
    998  1.12  pooka 				DPRINTF(("rump_sp: maxid now %d\n", maxidx));
    999   1.1  pooka 			}
   1000   1.1  pooka 		}
   1001   1.1  pooka 	}
   1002   1.1  pooka 
   1003  1.24  pooka  out:
   1004   1.1  pooka 	return NULL;
   1005   1.1  pooka }
   1006   1.1  pooka 
   1007  1.25  pooka static unsigned cleanupidx;
   1008  1.25  pooka static struct sockaddr *cleanupsa;
   1009   1.1  pooka int
   1010  1.26  pooka rumpuser_sp_init(const char *url, const struct rumpuser_sp_ops *spopsp,
   1011  1.26  pooka 	const char *ostype, const char *osrelease, const char *machine)
   1012   1.1  pooka {
   1013   1.5  pooka 	pthread_t pt;
   1014   1.1  pooka 	struct spservarg *sarg;
   1015   1.1  pooka 	struct sockaddr *sap;
   1016   1.5  pooka 	char *p;
   1017   1.5  pooka 	unsigned idx;
   1018   1.5  pooka 	int error, s;
   1019   1.5  pooka 
   1020   1.5  pooka 	p = strdup(url);
   1021   1.5  pooka 	if (p == NULL)
   1022   1.5  pooka 		return ENOMEM;
   1023   1.5  pooka 	error = parseurl(p, &sap, &idx, 1);
   1024   1.5  pooka 	free(p);
   1025   1.5  pooka 	if (error)
   1026   1.5  pooka 		return error;
   1027   1.5  pooka 
   1028  1.26  pooka 	snprintf(banner, sizeof(banner), "RUMPSP-%d.%d-%s-%s/%s\n",
   1029  1.26  pooka 	    PROTOMAJOR, PROTOMINOR, ostype, osrelease, machine);
   1030  1.26  pooka 
   1031   1.5  pooka 	s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
   1032   1.5  pooka 	if (s == -1)
   1033   1.5  pooka 		return errno;
   1034   1.1  pooka 
   1035   1.3  pooka 	spops = *spopsp;
   1036   1.5  pooka 	sarg = malloc(sizeof(*sarg));
   1037   1.5  pooka 	if (sarg == NULL) {
   1038   1.5  pooka 		close(s);
   1039   1.5  pooka 		return ENOMEM;
   1040   1.5  pooka 	}
   1041   1.5  pooka 
   1042   1.5  pooka 	sarg->sps_sock = s;
   1043   1.5  pooka 	sarg->sps_connhook = parsetab[idx].connhook;
   1044   1.1  pooka 
   1045  1.25  pooka 	cleanupidx = idx;
   1046  1.25  pooka 	cleanupsa = sap;
   1047  1.25  pooka 
   1048   1.5  pooka 	/* sloppy error recovery */
   1049   1.1  pooka 
   1050   1.5  pooka 	/*LINTED*/
   1051   1.5  pooka 	if (bind(s, sap, sap->sa_len) == -1) {
   1052   1.5  pooka 		fprintf(stderr, "rump_sp: server bind failed\n");
   1053   1.5  pooka 		return errno;
   1054   1.1  pooka 	}
   1055  1.25  pooka 
   1056  1.18  pooka 	if (listen(s, MAXCLI) == -1) {
   1057   1.5  pooka 		fprintf(stderr, "rump_sp: server listen failed\n");
   1058   1.5  pooka 		return errno;
   1059   1.1  pooka 	}
   1060   1.1  pooka 
   1061   1.5  pooka 	if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
   1062   1.5  pooka 		fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
   1063   1.1  pooka 		return errno;
   1064   1.1  pooka 	}
   1065   1.5  pooka 	pthread_detach(pt);
   1066   1.1  pooka 
   1067   1.1  pooka 	return 0;
   1068   1.1  pooka }
   1069  1.24  pooka 
   1070  1.24  pooka void
   1071  1.24  pooka rumpuser_sp_fini()
   1072  1.24  pooka {
   1073  1.24  pooka 
   1074  1.24  pooka 	if (spclist[0].spc_fd) {
   1075  1.25  pooka 		parsetab[cleanupidx].cleanup(cleanupsa);
   1076  1.24  pooka 		shutdown(spclist[0].spc_fd, SHUT_RDWR);
   1077  1.24  pooka 		spfini = 1;
   1078  1.24  pooka 	}
   1079  1.24  pooka }
   1080