Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_sp.c revision 1.48
      1  1.48  pooka /*      $NetBSD: rumpuser_sp.c,v 1.48 2012/09/21 14:33:03 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.47  pooka #include "rumpuser_port.h"
     38  1.47  pooka 
     39  1.47  pooka #if !defined(lint)
     40  1.48  pooka __RCSID("$NetBSD: rumpuser_sp.c,v 1.48 2012/09/21 14:33:03 pooka Exp $");
     41  1.47  pooka #endif /* !lint */
     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.28  pooka #include <rump/rump.h> /* XXX: for rfork flags */
     63   1.1  pooka #include <rump/rumpuser.h>
     64  1.47  pooka 
     65  1.13  pooka #include "rumpuser_int.h"
     66   1.1  pooka 
     67   1.5  pooka #include "sp_common.c"
     68   1.1  pooka 
     69  1.20  pooka #ifndef MAXCLI
     70  1.18  pooka #define MAXCLI 256
     71  1.20  pooka #endif
     72  1.20  pooka #ifndef MAXWORKER
     73  1.20  pooka #define MAXWORKER 128
     74  1.20  pooka #endif
     75  1.20  pooka #ifndef IDLEWORKER
     76  1.20  pooka #define IDLEWORKER 16
     77  1.20  pooka #endif
     78  1.20  pooka int rumpsp_maxworker = MAXWORKER;
     79  1.20  pooka int rumpsp_idleworker = IDLEWORKER;
     80   1.1  pooka 
     81   1.1  pooka static struct pollfd pfdlist[MAXCLI];
     82   1.1  pooka static struct spclient spclist[MAXCLI];
     83  1.11  pooka static unsigned int disco;
     84  1.24  pooka static volatile int spfini;
     85   1.1  pooka 
     86   1.3  pooka static struct rumpuser_sp_ops spops;
     87   1.3  pooka 
     88  1.26  pooka static char banner[MAXBANNER];
     89  1.26  pooka 
     90  1.26  pooka #define PROTOMAJOR 0
     91  1.48  pooka #define PROTOMINOR 4
     92  1.29  pooka 
     93  1.47  pooka 
     94  1.47  pooka /* how to use atomic ops on Linux? */
     95  1.47  pooka #ifdef __linux__
     96  1.47  pooka static pthread_mutex_t discomtx = PTHREAD_MUTEX_INITIALIZER;
     97  1.47  pooka 
     98  1.47  pooka static void
     99  1.47  pooka signaldisco(void)
    100  1.47  pooka {
    101  1.47  pooka 
    102  1.47  pooka 	pthread_mutex_lock(&discomtx);
    103  1.47  pooka 	disco++;
    104  1.47  pooka 	pthread_mutex_unlock(&discomtx);
    105  1.47  pooka }
    106  1.47  pooka 
    107  1.47  pooka static unsigned int
    108  1.47  pooka getdisco(void)
    109  1.47  pooka {
    110  1.47  pooka 	unsigned int discocnt;
    111  1.47  pooka 
    112  1.47  pooka 	pthread_mutex_lock(&discomtx);
    113  1.47  pooka 	discocnt = disco;
    114  1.47  pooka 	disco = 0;
    115  1.47  pooka 	pthread_mutex_unlock(&discomtx);
    116  1.47  pooka 
    117  1.47  pooka 	return discocnt;
    118  1.47  pooka }
    119  1.47  pooka 
    120  1.47  pooka #else /* NetBSD */
    121  1.47  pooka 
    122  1.47  pooka #include <sys/atomic.h>
    123  1.47  pooka #define signaldisco() atomic_inc_uint(&disco)
    124  1.47  pooka #define getdisco() atomic_swap_uint(&disco, 0)
    125  1.47  pooka 
    126  1.47  pooka #endif
    127  1.47  pooka 
    128  1.47  pooka 
    129  1.29  pooka struct prefork {
    130  1.29  pooka 	uint32_t pf_auth[AUTHLEN];
    131  1.29  pooka 	struct lwp *pf_lwp;
    132  1.29  pooka 
    133  1.29  pooka 	LIST_ENTRY(prefork) pf_entries;		/* global list */
    134  1.29  pooka 	LIST_ENTRY(prefork) pf_spcentries;	/* linked from forking spc */
    135  1.29  pooka };
    136  1.29  pooka static LIST_HEAD(, prefork) preforks = LIST_HEAD_INITIALIZER(preforks);
    137  1.29  pooka static pthread_mutex_t pfmtx;
    138  1.26  pooka 
    139   1.3  pooka /*
    140  1.31  pooka  * This version is for the server.  It's optimized for multiple threads
    141  1.33  pooka  * and is *NOT* reentrant wrt to signals.
    142  1.31  pooka  */
    143  1.31  pooka static int
    144  1.31  pooka waitresp(struct spclient *spc, struct respwait *rw)
    145  1.31  pooka {
    146  1.34  pooka 	int spcstate;
    147  1.31  pooka 	int rv = 0;
    148  1.31  pooka 
    149  1.34  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    150  1.31  pooka 	sendunlockl(spc);
    151  1.33  pooka 	while (!rw->rw_done && spc->spc_state != SPCSTATE_DYING) {
    152  1.33  pooka 		pthread_cond_wait(&rw->rw_cv, &spc->spc_mtx);
    153  1.31  pooka 	}
    154  1.31  pooka 	TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
    155  1.34  pooka 	spcstate = spc->spc_state;
    156  1.31  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    157  1.31  pooka 
    158  1.31  pooka 	pthread_cond_destroy(&rw->rw_cv);
    159  1.31  pooka 
    160  1.31  pooka 	if (rv)
    161  1.31  pooka 		return rv;
    162  1.34  pooka 	if (spcstate == SPCSTATE_DYING)
    163  1.31  pooka 		return ENOTCONN;
    164  1.31  pooka 	return rw->rw_error;
    165  1.31  pooka }
    166  1.31  pooka 
    167  1.31  pooka /*
    168   1.3  pooka  * Manual wrappers, since librump does not have access to the
    169   1.3  pooka  * user namespace wrapped interfaces.
    170   1.3  pooka  */
    171   1.3  pooka 
    172   1.3  pooka static void
    173   1.3  pooka lwproc_switch(struct lwp *l)
    174   1.3  pooka {
    175   1.3  pooka 
    176   1.4  pooka 	spops.spop_schedule();
    177   1.3  pooka 	spops.spop_lwproc_switch(l);
    178   1.4  pooka 	spops.spop_unschedule();
    179   1.3  pooka }
    180   1.3  pooka 
    181   1.3  pooka static void
    182   1.3  pooka lwproc_release(void)
    183   1.3  pooka {
    184   1.3  pooka 
    185   1.4  pooka 	spops.spop_schedule();
    186   1.3  pooka 	spops.spop_lwproc_release();
    187   1.4  pooka 	spops.spop_unschedule();
    188   1.3  pooka }
    189   1.3  pooka 
    190   1.3  pooka static int
    191  1.38  pooka lwproc_rfork(struct spclient *spc, int flags, const char *comm)
    192   1.3  pooka {
    193   1.3  pooka 	int rv;
    194   1.3  pooka 
    195   1.4  pooka 	spops.spop_schedule();
    196  1.38  pooka 	rv = spops.spop_lwproc_rfork(spc, flags, comm);
    197   1.4  pooka 	spops.spop_unschedule();
    198   1.3  pooka 
    199   1.3  pooka 	return rv;
    200   1.3  pooka }
    201   1.3  pooka 
    202   1.8  pooka static int
    203   1.8  pooka lwproc_newlwp(pid_t pid)
    204   1.8  pooka {
    205   1.8  pooka 	int rv;
    206   1.8  pooka 
    207   1.8  pooka 	spops.spop_schedule();
    208   1.8  pooka 	rv = spops.spop_lwproc_newlwp(pid);
    209   1.8  pooka 	spops.spop_unschedule();
    210   1.8  pooka 
    211   1.8  pooka 	return rv;
    212   1.8  pooka }
    213   1.8  pooka 
    214   1.3  pooka static struct lwp *
    215   1.3  pooka lwproc_curlwp(void)
    216   1.3  pooka {
    217   1.3  pooka 	struct lwp *l;
    218   1.3  pooka 
    219   1.4  pooka 	spops.spop_schedule();
    220   1.3  pooka 	l = spops.spop_lwproc_curlwp();
    221   1.4  pooka 	spops.spop_unschedule();
    222   1.3  pooka 
    223   1.3  pooka 	return l;
    224   1.3  pooka }
    225   1.3  pooka 
    226   1.8  pooka static pid_t
    227   1.8  pooka lwproc_getpid(void)
    228   1.8  pooka {
    229   1.8  pooka 	pid_t p;
    230   1.8  pooka 
    231   1.8  pooka 	spops.spop_schedule();
    232   1.8  pooka 	p = spops.spop_getpid();
    233   1.8  pooka 	spops.spop_unschedule();
    234   1.8  pooka 
    235   1.8  pooka 	return p;
    236   1.8  pooka }
    237  1.44  pooka 
    238  1.41  pooka static void
    239  1.41  pooka lwproc_execnotify(const char *comm)
    240  1.41  pooka {
    241  1.41  pooka 
    242  1.41  pooka 	spops.spop_schedule();
    243  1.41  pooka 	spops.spop_execnotify(comm);
    244  1.41  pooka 	spops.spop_unschedule();
    245  1.41  pooka }
    246   1.8  pooka 
    247  1.35  pooka static void
    248  1.44  pooka lwproc_lwpexit(void)
    249  1.35  pooka {
    250  1.35  pooka 
    251  1.35  pooka 	spops.spop_schedule();
    252  1.44  pooka 	spops.spop_lwpexit();
    253  1.35  pooka 	spops.spop_unschedule();
    254  1.35  pooka }
    255  1.35  pooka 
    256   1.3  pooka static int
    257   1.3  pooka rumpsyscall(int sysnum, void *data, register_t *retval)
    258   1.3  pooka {
    259   1.3  pooka 	int rv;
    260   1.3  pooka 
    261   1.4  pooka 	spops.spop_schedule();
    262   1.3  pooka 	rv = spops.spop_syscall(sysnum, data, retval);
    263   1.4  pooka 	spops.spop_unschedule();
    264   1.3  pooka 
    265   1.3  pooka 	return rv;
    266   1.3  pooka }
    267   1.1  pooka 
    268   1.7  pooka static uint64_t
    269   1.7  pooka nextreq(struct spclient *spc)
    270   1.7  pooka {
    271   1.7  pooka 	uint64_t nw;
    272   1.7  pooka 
    273   1.7  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    274   1.7  pooka 	nw = spc->spc_nextreq++;
    275   1.7  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    276   1.7  pooka 
    277   1.7  pooka 	return nw;
    278   1.7  pooka }
    279   1.7  pooka 
    280  1.45  pooka /*
    281  1.45  pooka  * XXX: we send responses with "blocking" I/O.  This is not
    282  1.45  pooka  * ok for the main thread.  XXXFIXME
    283  1.45  pooka  */
    284  1.45  pooka 
    285  1.21  pooka static void
    286  1.48  pooka send_error_resp(struct spclient *spc, uint64_t reqno, enum rumpsp_err error)
    287  1.21  pooka {
    288  1.21  pooka 	struct rsp_hdr rhdr;
    289  1.45  pooka 	struct iovec iov[1];
    290  1.21  pooka 
    291  1.21  pooka 	rhdr.rsp_len = sizeof(rhdr);
    292  1.21  pooka 	rhdr.rsp_reqno = reqno;
    293  1.21  pooka 	rhdr.rsp_class = RUMPSP_ERROR;
    294  1.21  pooka 	rhdr.rsp_type = 0;
    295  1.21  pooka 	rhdr.rsp_error = error;
    296  1.21  pooka 
    297  1.45  pooka 	IOVPUT(iov[0], rhdr);
    298  1.45  pooka 
    299  1.21  pooka 	sendlock(spc);
    300  1.45  pooka 	(void)SENDIOV(spc, iov);
    301  1.21  pooka 	sendunlock(spc);
    302  1.21  pooka }
    303  1.21  pooka 
    304   1.1  pooka static int
    305  1.27  pooka send_handshake_resp(struct spclient *spc, uint64_t reqno, int error)
    306  1.27  pooka {
    307  1.27  pooka 	struct rsp_hdr rhdr;
    308  1.45  pooka 	struct iovec iov[2];
    309  1.27  pooka 	int rv;
    310  1.27  pooka 
    311  1.27  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(error);
    312  1.27  pooka 	rhdr.rsp_reqno = reqno;
    313  1.27  pooka 	rhdr.rsp_class = RUMPSP_RESP;
    314  1.27  pooka 	rhdr.rsp_type = RUMPSP_HANDSHAKE;
    315  1.27  pooka 	rhdr.rsp_error = 0;
    316  1.27  pooka 
    317  1.45  pooka 	IOVPUT(iov[0], rhdr);
    318  1.45  pooka 	IOVPUT(iov[1], error);
    319  1.45  pooka 
    320  1.27  pooka 	sendlock(spc);
    321  1.45  pooka 	rv = SENDIOV(spc, iov);
    322  1.27  pooka 	sendunlock(spc);
    323  1.27  pooka 
    324  1.27  pooka 	return rv;
    325  1.27  pooka }
    326  1.27  pooka 
    327  1.27  pooka static int
    328   1.1  pooka send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
    329   1.7  pooka 	register_t *retval)
    330   1.1  pooka {
    331   1.1  pooka 	struct rsp_hdr rhdr;
    332   1.1  pooka 	struct rsp_sysresp sysresp;
    333  1.45  pooka 	struct iovec iov[2];
    334   1.7  pooka 	int rv;
    335   1.1  pooka 
    336   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
    337   1.1  pooka 	rhdr.rsp_reqno = reqno;
    338   1.7  pooka 	rhdr.rsp_class = RUMPSP_RESP;
    339   1.7  pooka 	rhdr.rsp_type = RUMPSP_SYSCALL;
    340   1.1  pooka 	rhdr.rsp_sysnum = 0;
    341   1.1  pooka 
    342   1.1  pooka 	sysresp.rsys_error = error;
    343   1.7  pooka 	memcpy(sysresp.rsys_retval, retval, sizeof(sysresp.rsys_retval));
    344   1.1  pooka 
    345  1.45  pooka 	IOVPUT(iov[0], rhdr);
    346  1.45  pooka 	IOVPUT(iov[1], sysresp);
    347  1.45  pooka 
    348   1.7  pooka 	sendlock(spc);
    349  1.45  pooka 	rv = SENDIOV(spc, iov);
    350   1.7  pooka 	sendunlock(spc);
    351   1.1  pooka 
    352   1.7  pooka 	return rv;
    353   1.1  pooka }
    354   1.1  pooka 
    355   1.1  pooka static int
    356  1.29  pooka send_prefork_resp(struct spclient *spc, uint64_t reqno, uint32_t *auth)
    357  1.29  pooka {
    358  1.29  pooka 	struct rsp_hdr rhdr;
    359  1.45  pooka 	struct iovec iov[2];
    360  1.29  pooka 	int rv;
    361  1.29  pooka 
    362  1.29  pooka 	rhdr.rsp_len = sizeof(rhdr) + AUTHLEN*sizeof(*auth);
    363  1.29  pooka 	rhdr.rsp_reqno = reqno;
    364  1.29  pooka 	rhdr.rsp_class = RUMPSP_RESP;
    365  1.29  pooka 	rhdr.rsp_type = RUMPSP_PREFORK;
    366  1.29  pooka 	rhdr.rsp_sysnum = 0;
    367  1.29  pooka 
    368  1.45  pooka 	IOVPUT(iov[0], rhdr);
    369  1.45  pooka 	IOVPUT_WITHSIZE(iov[1], auth, AUTHLEN*sizeof(*auth));
    370  1.45  pooka 
    371  1.29  pooka 	sendlock(spc);
    372  1.45  pooka 	rv = SENDIOV(spc, iov);
    373  1.29  pooka 	sendunlock(spc);
    374  1.29  pooka 
    375  1.29  pooka 	return rv;
    376  1.29  pooka }
    377  1.29  pooka 
    378  1.29  pooka static int
    379  1.15  pooka copyin_req(struct spclient *spc, const void *remaddr, size_t *dlen,
    380  1.15  pooka 	int wantstr, void **resp)
    381   1.1  pooka {
    382   1.1  pooka 	struct rsp_hdr rhdr;
    383   1.1  pooka 	struct rsp_copydata copydata;
    384   1.7  pooka 	struct respwait rw;
    385  1.45  pooka 	struct iovec iov[2];
    386   1.7  pooka 	int rv;
    387   1.7  pooka 
    388  1.15  pooka 	DPRINTF(("copyin_req: %zu bytes from %p\n", *dlen, remaddr));
    389   1.1  pooka 
    390   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
    391   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    392  1.15  pooka 	if (wantstr)
    393  1.15  pooka 		rhdr.rsp_type = RUMPSP_COPYINSTR;
    394  1.15  pooka 	else
    395  1.15  pooka 		rhdr.rsp_type = RUMPSP_COPYIN;
    396   1.1  pooka 	rhdr.rsp_sysnum = 0;
    397   1.1  pooka 
    398   1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    399  1.15  pooka 	copydata.rcp_len = *dlen;
    400   1.1  pooka 
    401  1.45  pooka 	IOVPUT(iov[0], rhdr);
    402  1.45  pooka 	IOVPUT(iov[1], copydata);
    403  1.45  pooka 
    404   1.7  pooka 	putwait(spc, &rw, &rhdr);
    405  1.45  pooka 	rv = SENDIOV(spc, iov);
    406  1.13  pooka 	if (rv) {
    407  1.13  pooka 		unputwait(spc, &rw);
    408  1.13  pooka 		return rv;
    409  1.13  pooka 	}
    410   1.7  pooka 
    411   1.7  pooka 	rv = waitresp(spc, &rw);
    412   1.7  pooka 
    413   1.7  pooka 	DPRINTF(("copyin: response %d\n", rv));
    414   1.7  pooka 
    415   1.7  pooka 	*resp = rw.rw_data;
    416  1.15  pooka 	if (wantstr)
    417  1.15  pooka 		*dlen = rw.rw_dlen;
    418  1.15  pooka 
    419   1.7  pooka 	return rv;
    420   1.1  pooka 
    421   1.1  pooka }
    422   1.1  pooka 
    423   1.1  pooka static int
    424   1.1  pooka send_copyout_req(struct spclient *spc, const void *remaddr,
    425   1.1  pooka 	const void *data, size_t dlen)
    426   1.1  pooka {
    427   1.1  pooka 	struct rsp_hdr rhdr;
    428   1.1  pooka 	struct rsp_copydata copydata;
    429  1.45  pooka 	struct iovec iov[3];
    430   1.7  pooka 	int rv;
    431   1.7  pooka 
    432   1.7  pooka 	DPRINTF(("copyout_req (async): %zu bytes to %p\n", dlen, remaddr));
    433   1.1  pooka 
    434   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
    435   1.7  pooka 	rhdr.rsp_reqno = nextreq(spc);
    436   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    437   1.7  pooka 	rhdr.rsp_type = RUMPSP_COPYOUT;
    438   1.1  pooka 	rhdr.rsp_sysnum = 0;
    439   1.1  pooka 
    440   1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    441   1.1  pooka 	copydata.rcp_len = dlen;
    442   1.1  pooka 
    443  1.45  pooka 	IOVPUT(iov[0], rhdr);
    444  1.45  pooka 	IOVPUT(iov[1], copydata);
    445  1.45  pooka 	IOVPUT_WITHSIZE(iov[2], __UNCONST(data), dlen);
    446  1.45  pooka 
    447   1.7  pooka 	sendlock(spc);
    448  1.45  pooka 	rv = SENDIOV(spc, iov);
    449   1.7  pooka 	sendunlock(spc);
    450   1.1  pooka 
    451   1.7  pooka 	return rv;
    452   1.1  pooka }
    453   1.1  pooka 
    454   1.1  pooka static int
    455   1.7  pooka anonmmap_req(struct spclient *spc, size_t howmuch, void **resp)
    456   1.1  pooka {
    457   1.1  pooka 	struct rsp_hdr rhdr;
    458   1.7  pooka 	struct respwait rw;
    459  1.45  pooka 	struct iovec iov[2];
    460   1.7  pooka 	int rv;
    461   1.7  pooka 
    462   1.7  pooka 	DPRINTF(("anonmmap_req: %zu bytes\n", howmuch));
    463   1.1  pooka 
    464   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
    465   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    466   1.7  pooka 	rhdr.rsp_type = RUMPSP_ANONMMAP;
    467   1.1  pooka 	rhdr.rsp_sysnum = 0;
    468   1.1  pooka 
    469  1.45  pooka 	IOVPUT(iov[0], rhdr);
    470  1.45  pooka 	IOVPUT(iov[1], howmuch);
    471  1.45  pooka 
    472   1.7  pooka 	putwait(spc, &rw, &rhdr);
    473  1.45  pooka 	rv = SENDIOV(spc, iov);
    474  1.13  pooka 	if (rv) {
    475  1.13  pooka 		unputwait(spc, &rw);
    476  1.13  pooka 		return rv;
    477  1.13  pooka 	}
    478   1.1  pooka 
    479   1.7  pooka 	rv = waitresp(spc, &rw);
    480  1.13  pooka 
    481   1.7  pooka 	*resp = rw.rw_data;
    482   1.7  pooka 
    483   1.7  pooka 	DPRINTF(("anonmmap: mapped at %p\n", **(void ***)resp));
    484   1.7  pooka 
    485   1.7  pooka 	return rv;
    486   1.1  pooka }
    487   1.1  pooka 
    488  1.36  pooka static int
    489  1.36  pooka send_raise_req(struct spclient *spc, int signo)
    490  1.36  pooka {
    491  1.36  pooka 	struct rsp_hdr rhdr;
    492  1.45  pooka 	struct iovec iov[1];
    493  1.36  pooka 	int rv;
    494  1.36  pooka 
    495  1.36  pooka 	rhdr.rsp_len = sizeof(rhdr);
    496  1.36  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    497  1.36  pooka 	rhdr.rsp_type = RUMPSP_RAISE;
    498  1.36  pooka 	rhdr.rsp_signo = signo;
    499  1.36  pooka 
    500  1.45  pooka 	IOVPUT(iov[0], rhdr);
    501  1.45  pooka 
    502  1.36  pooka 	sendlock(spc);
    503  1.45  pooka 	rv = SENDIOV(spc, iov);
    504  1.36  pooka 	sendunlock(spc);
    505  1.36  pooka 
    506  1.36  pooka 	return rv;
    507  1.36  pooka }
    508  1.36  pooka 
    509   1.1  pooka static void
    510  1.11  pooka spcref(struct spclient *spc)
    511  1.11  pooka {
    512  1.11  pooka 
    513  1.11  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    514  1.11  pooka 	spc->spc_refcnt++;
    515  1.11  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    516  1.11  pooka }
    517  1.11  pooka 
    518  1.11  pooka static void
    519  1.11  pooka spcrelease(struct spclient *spc)
    520  1.11  pooka {
    521  1.11  pooka 	int ref;
    522  1.11  pooka 
    523  1.11  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    524  1.11  pooka 	ref = --spc->spc_refcnt;
    525  1.44  pooka 	if (__predict_false(spc->spc_inexec && ref <= 2))
    526  1.44  pooka 		pthread_cond_broadcast(&spc->spc_cv);
    527  1.11  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    528  1.11  pooka 
    529  1.11  pooka 	if (ref > 0)
    530  1.11  pooka 		return;
    531  1.11  pooka 
    532  1.29  pooka 	DPRINTF(("rump_sp: spcrelease: spc %p fd %d\n", spc, spc->spc_fd));
    533  1.12  pooka 
    534  1.13  pooka 	_DIAGASSERT(TAILQ_EMPTY(&spc->spc_respwait));
    535  1.11  pooka 	_DIAGASSERT(spc->spc_buf == NULL);
    536  1.11  pooka 
    537  1.29  pooka 	if (spc->spc_mainlwp) {
    538  1.29  pooka 		lwproc_switch(spc->spc_mainlwp);
    539  1.29  pooka 		lwproc_release();
    540  1.29  pooka 	}
    541  1.11  pooka 	spc->spc_mainlwp = NULL;
    542  1.11  pooka 
    543  1.11  pooka 	close(spc->spc_fd);
    544  1.11  pooka 	spc->spc_fd = -1;
    545  1.27  pooka 	spc->spc_state = SPCSTATE_NEW;
    546  1.11  pooka 
    547  1.47  pooka 	signaldisco();
    548  1.11  pooka }
    549  1.11  pooka 
    550  1.11  pooka static void
    551   1.2  pooka serv_handledisco(unsigned int idx)
    552   1.1  pooka {
    553   1.1  pooka 	struct spclient *spc = &spclist[idx];
    554  1.44  pooka 	int dolwpexit;
    555   1.1  pooka 
    556   1.1  pooka 	DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
    557   1.1  pooka 
    558  1.12  pooka 	pfdlist[idx].fd = -1;
    559  1.12  pooka 	pfdlist[idx].revents = 0;
    560  1.12  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    561  1.27  pooka 	spc->spc_state = SPCSTATE_DYING;
    562  1.12  pooka 	kickall(spc);
    563  1.30  pooka 	sendunlockl(spc);
    564  1.44  pooka 	/* exec uses mainlwp in another thread, but also nuked all lwps */
    565  1.44  pooka 	dolwpexit = !spc->spc_inexec;
    566  1.12  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    567  1.17  pooka 
    568  1.44  pooka 	if (dolwpexit && spc->spc_mainlwp) {
    569  1.35  pooka 		lwproc_switch(spc->spc_mainlwp);
    570  1.44  pooka 		lwproc_lwpexit();
    571  1.35  pooka 		lwproc_switch(NULL);
    572  1.35  pooka 	}
    573  1.35  pooka 
    574  1.17  pooka 	/*
    575  1.17  pooka 	 * Nobody's going to attempt to send/receive anymore,
    576  1.17  pooka 	 * so reinit info relevant to that.
    577  1.17  pooka 	 */
    578  1.22  pooka 	/*LINTED:pointer casts may be ok*/
    579  1.17  pooka 	memset((char *)spc + SPC_ZEROFF, 0, sizeof(*spc) - SPC_ZEROFF);
    580  1.17  pooka 
    581  1.11  pooka 	spcrelease(spc);
    582   1.1  pooka }
    583   1.1  pooka 
    584  1.24  pooka static void
    585  1.24  pooka serv_shutdown(void)
    586  1.24  pooka {
    587  1.24  pooka 	struct spclient *spc;
    588  1.24  pooka 	unsigned int i;
    589  1.24  pooka 
    590  1.24  pooka 	for (i = 1; i < MAXCLI; i++) {
    591  1.24  pooka 		spc = &spclist[i];
    592  1.24  pooka 		if (spc->spc_fd == -1)
    593  1.24  pooka 			continue;
    594  1.24  pooka 
    595  1.24  pooka 		shutdown(spc->spc_fd, SHUT_RDWR);
    596  1.24  pooka 		serv_handledisco(i);
    597  1.24  pooka 
    598  1.24  pooka 		spcrelease(spc);
    599  1.24  pooka 	}
    600  1.24  pooka }
    601  1.24  pooka 
    602  1.11  pooka static unsigned
    603  1.11  pooka serv_handleconn(int fd, connecthook_fn connhook, int busy)
    604   1.1  pooka {
    605   1.1  pooka 	struct sockaddr_storage ss;
    606   1.1  pooka 	socklen_t sl = sizeof(ss);
    607  1.11  pooka 	int newfd, flags;
    608   1.1  pooka 	unsigned i;
    609   1.1  pooka 
    610   1.1  pooka 	/*LINTED: cast ok */
    611   1.1  pooka 	newfd = accept(fd, (struct sockaddr *)&ss, &sl);
    612   1.1  pooka 	if (newfd == -1)
    613  1.11  pooka 		return 0;
    614   1.1  pooka 
    615  1.11  pooka 	if (busy) {
    616   1.1  pooka 		close(newfd); /* EBUSY */
    617  1.11  pooka 		return 0;
    618   1.1  pooka 	}
    619   1.1  pooka 
    620   1.1  pooka 	flags = fcntl(newfd, F_GETFL, 0);
    621   1.1  pooka 	if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
    622   1.1  pooka 		close(newfd);
    623  1.11  pooka 		return 0;
    624   1.1  pooka 	}
    625   1.1  pooka 
    626  1.11  pooka 	if (connhook(newfd) != 0) {
    627   1.1  pooka 		close(newfd);
    628  1.11  pooka 		return 0;
    629   1.1  pooka 	}
    630   1.1  pooka 
    631  1.26  pooka 	/* write out a banner for the client */
    632  1.34  pooka 	if (send(newfd, banner, strlen(banner), MSG_NOSIGNAL)
    633  1.34  pooka 	    != (ssize_t)strlen(banner)) {
    634  1.26  pooka 		close(newfd);
    635  1.26  pooka 		return 0;
    636  1.26  pooka 	}
    637  1.26  pooka 
    638   1.1  pooka 	/* find empty slot the simple way */
    639   1.1  pooka 	for (i = 0; i < MAXCLI; i++) {
    640  1.27  pooka 		if (pfdlist[i].fd == -1 && spclist[i].spc_state == SPCSTATE_NEW)
    641   1.2  pooka 			break;
    642   1.1  pooka 	}
    643   1.1  pooka 
    644   1.1  pooka 	assert(i < MAXCLI);
    645   1.1  pooka 
    646   1.1  pooka 	pfdlist[i].fd = newfd;
    647   1.1  pooka 	spclist[i].spc_fd = newfd;
    648   1.7  pooka 	spclist[i].spc_istatus = SPCSTATUS_BUSY; /* dedicated receiver */
    649  1.11  pooka 	spclist[i].spc_refcnt = 1;
    650   1.7  pooka 
    651   1.7  pooka 	TAILQ_INIT(&spclist[i].spc_respwait);
    652   1.1  pooka 
    653  1.29  pooka 	DPRINTF(("rump_sp: added new connection fd %d at idx %u\n", newfd, i));
    654   1.1  pooka 
    655  1.11  pooka 	return i;
    656   1.1  pooka }
    657   1.1  pooka 
    658   1.1  pooka static void
    659   1.1  pooka serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
    660   1.1  pooka {
    661   1.7  pooka 	register_t retval[2] = {0, 0};
    662   1.1  pooka 	int rv, sysnum;
    663   1.1  pooka 
    664   1.1  pooka 	sysnum = (int)rhdr->rsp_sysnum;
    665   1.1  pooka 	DPRINTF(("rump_sp: handling syscall %d from client %d\n",
    666  1.29  pooka 	    sysnum, spc->spc_pid));
    667   1.1  pooka 
    668  1.44  pooka 	if (__predict_false((rv = lwproc_newlwp(spc->spc_pid)) != 0)) {
    669  1.44  pooka 		retval[0] = -1;
    670  1.44  pooka 		send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
    671  1.44  pooka 		return;
    672  1.44  pooka 	}
    673  1.37  pooka 	spc->spc_syscallreq = rhdr->rsp_reqno;
    674   1.3  pooka 	rv = rumpsyscall(sysnum, data, retval);
    675  1.37  pooka 	spc->spc_syscallreq = 0;
    676  1.16  pooka 	lwproc_release();
    677   1.1  pooka 
    678   1.7  pooka 	DPRINTF(("rump_sp: got return value %d & %d/%d\n",
    679   1.7  pooka 	    rv, retval[0], retval[1]));
    680   1.5  pooka 
    681   1.1  pooka 	send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
    682   1.1  pooka }
    683   1.1  pooka 
    684  1.44  pooka static void
    685  1.44  pooka serv_handleexec(struct spclient *spc, struct rsp_hdr *rhdr, char *comm)
    686  1.44  pooka {
    687  1.44  pooka 	size_t commlen = rhdr->rsp_len - HDRSZ;
    688  1.44  pooka 
    689  1.44  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    690  1.44  pooka 	/* one for the connection and one for us */
    691  1.44  pooka 	while (spc->spc_refcnt > 2)
    692  1.44  pooka 		pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
    693  1.44  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    694  1.44  pooka 
    695  1.44  pooka 	/*
    696  1.44  pooka 	 * ok, all the threads are dead (or one is still alive and
    697  1.44  pooka 	 * the connection is dead, in which case this doesn't matter
    698  1.44  pooka 	 * very much).  proceed with exec.
    699  1.44  pooka 	 */
    700  1.44  pooka 
    701  1.44  pooka 	/* ensure comm is 0-terminated */
    702  1.44  pooka 	/* TODO: make sure it contains sensible chars? */
    703  1.44  pooka 	comm[commlen] = '\0';
    704  1.44  pooka 
    705  1.44  pooka 	lwproc_switch(spc->spc_mainlwp);
    706  1.44  pooka 	lwproc_execnotify(comm);
    707  1.44  pooka 	lwproc_switch(NULL);
    708  1.44  pooka 
    709  1.44  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    710  1.44  pooka 	spc->spc_inexec = 0;
    711  1.44  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    712  1.44  pooka 	send_handshake_resp(spc, rhdr->rsp_reqno, 0);
    713  1.44  pooka }
    714  1.44  pooka 
    715  1.44  pooka enum sbatype { SBA_SYSCALL, SBA_EXEC };
    716  1.44  pooka 
    717  1.44  pooka struct servbouncearg {
    718   1.7  pooka 	struct spclient *sba_spc;
    719   1.7  pooka 	struct rsp_hdr sba_hdr;
    720  1.44  pooka 	enum sbatype sba_type;
    721   1.7  pooka 	uint8_t *sba_data;
    722  1.20  pooka 
    723  1.44  pooka 	TAILQ_ENTRY(servbouncearg) sba_entries;
    724   1.7  pooka };
    725  1.20  pooka static pthread_mutex_t sbamtx;
    726  1.20  pooka static pthread_cond_t sbacv;
    727  1.40  pooka static int nworker, idleworker, nwork;
    728  1.44  pooka static TAILQ_HEAD(, servbouncearg) wrklist = TAILQ_HEAD_INITIALIZER(wrklist);
    729  1.20  pooka 
    730  1.20  pooka /*ARGSUSED*/
    731   1.7  pooka static void *
    732  1.44  pooka serv_workbouncer(void *arg)
    733   1.7  pooka {
    734  1.44  pooka 	struct servbouncearg *sba;
    735  1.20  pooka 
    736  1.20  pooka 	for (;;) {
    737  1.20  pooka 		pthread_mutex_lock(&sbamtx);
    738  1.43  pooka 		if (__predict_false(idleworker - nwork >= rumpsp_idleworker)) {
    739  1.20  pooka 			nworker--;
    740  1.20  pooka 			pthread_mutex_unlock(&sbamtx);
    741  1.20  pooka 			break;
    742  1.20  pooka 		}
    743  1.40  pooka 		idleworker++;
    744  1.44  pooka 		while (TAILQ_EMPTY(&wrklist)) {
    745  1.40  pooka 			_DIAGASSERT(nwork == 0);
    746  1.20  pooka 			pthread_cond_wait(&sbacv, &sbamtx);
    747  1.40  pooka 		}
    748  1.40  pooka 		idleworker--;
    749  1.20  pooka 
    750  1.44  pooka 		sba = TAILQ_FIRST(&wrklist);
    751  1.44  pooka 		TAILQ_REMOVE(&wrklist, sba, sba_entries);
    752  1.40  pooka 		nwork--;
    753  1.20  pooka 		pthread_mutex_unlock(&sbamtx);
    754  1.20  pooka 
    755  1.44  pooka 		if (__predict_true(sba->sba_type == SBA_SYSCALL)) {
    756  1.44  pooka 			serv_handlesyscall(sba->sba_spc,
    757  1.44  pooka 			    &sba->sba_hdr, sba->sba_data);
    758  1.44  pooka 		} else {
    759  1.44  pooka 			_DIAGASSERT(sba->sba_type == SBA_EXEC);
    760  1.44  pooka 			serv_handleexec(sba->sba_spc, &sba->sba_hdr,
    761  1.44  pooka 			    (char *)sba->sba_data);
    762  1.44  pooka 		}
    763  1.20  pooka 		spcrelease(sba->sba_spc);
    764  1.20  pooka 		free(sba->sba_data);
    765  1.20  pooka 		free(sba);
    766  1.20  pooka 	}
    767   1.7  pooka 
    768   1.7  pooka 	return NULL;
    769   1.7  pooka }
    770   1.7  pooka 
    771  1.15  pooka static int
    772  1.15  pooka sp_copyin(void *arg, const void *raddr, void *laddr, size_t *len, int wantstr)
    773   1.1  pooka {
    774  1.10  pooka 	struct spclient *spc = arg;
    775   1.9  pooka 	void *rdata = NULL; /* XXXuninit */
    776  1.13  pooka 	int rv, nlocks;
    777  1.13  pooka 
    778  1.13  pooka 	rumpuser__kunlock(0, &nlocks, NULL);
    779   1.1  pooka 
    780  1.15  pooka 	rv = copyin_req(spc, raddr, len, wantstr, &rdata);
    781  1.12  pooka 	if (rv)
    782  1.13  pooka 		goto out;
    783   1.1  pooka 
    784  1.15  pooka 	memcpy(laddr, rdata, *len);
    785   1.7  pooka 	free(rdata);
    786   1.1  pooka 
    787  1.13  pooka  out:
    788  1.13  pooka 	rumpuser__klock(nlocks, NULL);
    789  1.13  pooka 	if (rv)
    790  1.13  pooka 		return EFAULT;
    791   1.1  pooka 	return 0;
    792   1.1  pooka }
    793   1.1  pooka 
    794   1.1  pooka int
    795  1.15  pooka rumpuser_sp_copyin(void *arg, const void *raddr, void *laddr, size_t len)
    796  1.15  pooka {
    797  1.15  pooka 
    798  1.15  pooka 	return sp_copyin(arg, raddr, laddr, &len, 0);
    799  1.15  pooka }
    800  1.15  pooka 
    801  1.15  pooka int
    802  1.15  pooka rumpuser_sp_copyinstr(void *arg, const void *raddr, void *laddr, size_t *len)
    803  1.15  pooka {
    804  1.15  pooka 
    805  1.15  pooka 	return sp_copyin(arg, raddr, laddr, len, 1);
    806  1.15  pooka }
    807  1.15  pooka 
    808  1.15  pooka static int
    809  1.15  pooka sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
    810   1.1  pooka {
    811  1.10  pooka 	struct spclient *spc = arg;
    812  1.13  pooka 	int nlocks, rv;
    813  1.13  pooka 
    814  1.13  pooka 	rumpuser__kunlock(0, &nlocks, NULL);
    815  1.15  pooka 	rv = send_copyout_req(spc, raddr, laddr, dlen);
    816  1.13  pooka 	rumpuser__klock(nlocks, NULL);
    817   1.1  pooka 
    818  1.13  pooka 	if (rv)
    819   1.7  pooka 		return EFAULT;
    820   1.1  pooka 	return 0;
    821   1.1  pooka }
    822   1.1  pooka 
    823   1.1  pooka int
    824  1.15  pooka rumpuser_sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
    825  1.15  pooka {
    826  1.15  pooka 
    827  1.15  pooka 	return sp_copyout(arg, laddr, raddr, dlen);
    828  1.15  pooka }
    829  1.15  pooka 
    830  1.15  pooka int
    831  1.15  pooka rumpuser_sp_copyoutstr(void *arg, const void *laddr, void *raddr, size_t *dlen)
    832  1.15  pooka {
    833  1.15  pooka 
    834  1.15  pooka 	return sp_copyout(arg, laddr, raddr, *dlen);
    835  1.15  pooka }
    836  1.15  pooka 
    837  1.15  pooka int
    838  1.10  pooka rumpuser_sp_anonmmap(void *arg, size_t howmuch, void **addr)
    839   1.1  pooka {
    840  1.10  pooka 	struct spclient *spc = arg;
    841   1.7  pooka 	void *resp, *rdata;
    842  1.13  pooka 	int nlocks, rv;
    843  1.13  pooka 
    844  1.13  pooka 	rumpuser__kunlock(0, &nlocks, NULL);
    845   1.1  pooka 
    846   1.7  pooka 	rv = anonmmap_req(spc, howmuch, &rdata);
    847  1.13  pooka 	if (rv) {
    848  1.13  pooka 		rv = EFAULT;
    849  1.13  pooka 		goto out;
    850  1.13  pooka 	}
    851   1.1  pooka 
    852   1.7  pooka 	resp = *(void **)rdata;
    853   1.7  pooka 	free(rdata);
    854   1.1  pooka 
    855   1.7  pooka 	if (resp == NULL) {
    856  1.13  pooka 		rv = ENOMEM;
    857   1.1  pooka 	}
    858   1.1  pooka 
    859   1.1  pooka 	*addr = resp;
    860  1.13  pooka 
    861  1.13  pooka  out:
    862  1.13  pooka 	rumpuser__klock(nlocks, NULL);
    863  1.13  pooka 
    864  1.13  pooka 	if (rv)
    865  1.13  pooka 		return rv;
    866   1.1  pooka 	return 0;
    867   1.1  pooka }
    868   1.1  pooka 
    869  1.36  pooka int
    870  1.36  pooka rumpuser_sp_raise(void *arg, int signo)
    871  1.36  pooka {
    872  1.36  pooka 	struct spclient *spc = arg;
    873  1.36  pooka 	int rv, nlocks;
    874  1.36  pooka 
    875  1.36  pooka 	rumpuser__kunlock(0, &nlocks, NULL);
    876  1.36  pooka 	rv = send_raise_req(spc, signo);
    877  1.36  pooka 	rumpuser__klock(nlocks, NULL);
    878  1.36  pooka 
    879  1.36  pooka 	return rv;
    880  1.36  pooka }
    881  1.36  pooka 
    882  1.44  pooka static pthread_attr_t pattr_detached;
    883  1.44  pooka static void
    884  1.44  pooka schedulework(struct spclient *spc, enum sbatype sba_type)
    885  1.44  pooka {
    886  1.44  pooka 	struct servbouncearg *sba;
    887  1.44  pooka 	pthread_t pt;
    888  1.44  pooka 	uint64_t reqno;
    889  1.44  pooka 	int retries = 0;
    890  1.44  pooka 
    891  1.44  pooka 	reqno = spc->spc_hdr.rsp_reqno;
    892  1.44  pooka 	while ((sba = malloc(sizeof(*sba))) == NULL) {
    893  1.44  pooka 		if (nworker == 0 || retries > 10) {
    894  1.48  pooka 			send_error_resp(spc, reqno, RUMPSP_ERR_TRYAGAIN);
    895  1.44  pooka 			spcfreebuf(spc);
    896  1.44  pooka 			return;
    897  1.44  pooka 		}
    898  1.44  pooka 		/* slim chance of more memory? */
    899  1.44  pooka 		usleep(10000);
    900  1.44  pooka 	}
    901  1.44  pooka 
    902  1.44  pooka 	sba->sba_spc = spc;
    903  1.44  pooka 	sba->sba_type = sba_type;
    904  1.44  pooka 	sba->sba_hdr = spc->spc_hdr;
    905  1.44  pooka 	sba->sba_data = spc->spc_buf;
    906  1.44  pooka 	spcresetbuf(spc);
    907  1.44  pooka 
    908  1.44  pooka 	spcref(spc);
    909  1.44  pooka 
    910  1.44  pooka 	pthread_mutex_lock(&sbamtx);
    911  1.44  pooka 	TAILQ_INSERT_TAIL(&wrklist, sba, sba_entries);
    912  1.44  pooka 	nwork++;
    913  1.44  pooka 	if (nwork <= idleworker) {
    914  1.44  pooka 		/* do we have a daemon's tool (i.e. idle threads)? */
    915  1.44  pooka 		pthread_cond_signal(&sbacv);
    916  1.44  pooka 	} else if (nworker < rumpsp_maxworker) {
    917  1.44  pooka 		/*
    918  1.44  pooka 		 * Else, need to create one
    919  1.44  pooka 		 * (if we can, otherwise just expect another
    920  1.44  pooka 		 * worker to pick up the syscall)
    921  1.44  pooka 		 */
    922  1.44  pooka 		if (pthread_create(&pt, &pattr_detached,
    923  1.44  pooka 		    serv_workbouncer, NULL) == 0) {
    924  1.44  pooka 			nworker++;
    925  1.44  pooka 		}
    926  1.44  pooka 	}
    927  1.44  pooka 	pthread_mutex_unlock(&sbamtx);
    928  1.44  pooka }
    929  1.44  pooka 
    930   1.1  pooka /*
    931   1.1  pooka  *
    932   1.1  pooka  * Startup routines and mainloop for server.
    933   1.1  pooka  *
    934   1.1  pooka  */
    935   1.1  pooka 
    936   1.1  pooka struct spservarg {
    937   1.1  pooka 	int sps_sock;
    938   1.1  pooka 	connecthook_fn sps_connhook;
    939   1.1  pooka };
    940   1.1  pooka 
    941   1.7  pooka static void
    942   1.7  pooka handlereq(struct spclient *spc)
    943   1.7  pooka {
    944  1.41  pooka 	uint64_t reqno;
    945  1.44  pooka 	int error, i;
    946  1.27  pooka 
    947  1.41  pooka 	reqno = spc->spc_hdr.rsp_reqno;
    948  1.27  pooka 	if (__predict_false(spc->spc_state == SPCSTATE_NEW)) {
    949  1.27  pooka 		if (spc->spc_hdr.rsp_type != RUMPSP_HANDSHAKE) {
    950  1.48  pooka 			send_error_resp(spc, reqno, RUMPSP_ERR_AUTH);
    951  1.29  pooka 			shutdown(spc->spc_fd, SHUT_RDWR);
    952  1.27  pooka 			spcfreebuf(spc);
    953  1.27  pooka 			return;
    954  1.27  pooka 		}
    955  1.27  pooka 
    956  1.29  pooka 		if (spc->spc_hdr.rsp_handshake == HANDSHAKE_GUEST) {
    957  1.38  pooka 			char *comm = (char *)spc->spc_buf;
    958  1.38  pooka 			size_t commlen = spc->spc_hdr.rsp_len - HDRSZ;
    959  1.38  pooka 
    960  1.38  pooka 			/* ensure it's 0-terminated */
    961  1.38  pooka 			/* XXX make sure it contains sensible chars? */
    962  1.38  pooka 			comm[commlen] = '\0';
    963  1.38  pooka 
    964  1.38  pooka 			if ((error = lwproc_rfork(spc,
    965  1.38  pooka 			    RUMP_RFCFDG, comm)) != 0) {
    966  1.29  pooka 				shutdown(spc->spc_fd, SHUT_RDWR);
    967  1.29  pooka 			}
    968  1.29  pooka 
    969  1.29  pooka 			spcfreebuf(spc);
    970  1.29  pooka 			if (error)
    971  1.29  pooka 				return;
    972  1.29  pooka 
    973  1.29  pooka 			spc->spc_mainlwp = lwproc_curlwp();
    974  1.29  pooka 
    975  1.41  pooka 			send_handshake_resp(spc, reqno, 0);
    976  1.29  pooka 		} else if (spc->spc_hdr.rsp_handshake == HANDSHAKE_FORK) {
    977  1.29  pooka 			struct lwp *tmpmain;
    978  1.29  pooka 			struct prefork *pf;
    979  1.29  pooka 			struct handshake_fork *rfp;
    980  1.29  pooka 			int cancel;
    981  1.29  pooka 
    982  1.29  pooka 			if (spc->spc_off-HDRSZ != sizeof(*rfp)) {
    983  1.48  pooka 				send_error_resp(spc, reqno,
    984  1.48  pooka 				    RUMPSP_ERR_MALFORMED_REQUEST);
    985  1.29  pooka 				shutdown(spc->spc_fd, SHUT_RDWR);
    986  1.29  pooka 				spcfreebuf(spc);
    987  1.29  pooka 				return;
    988  1.29  pooka 			}
    989  1.29  pooka 
    990  1.29  pooka 			/*LINTED*/
    991  1.29  pooka 			rfp = (void *)spc->spc_buf;
    992  1.29  pooka 			cancel = rfp->rf_cancel;
    993  1.29  pooka 
    994  1.29  pooka 			pthread_mutex_lock(&pfmtx);
    995  1.29  pooka 			LIST_FOREACH(pf, &preforks, pf_entries) {
    996  1.29  pooka 				if (memcmp(rfp->rf_auth, pf->pf_auth,
    997  1.29  pooka 				    sizeof(rfp->rf_auth)) == 0) {
    998  1.29  pooka 					LIST_REMOVE(pf, pf_entries);
    999  1.29  pooka 					LIST_REMOVE(pf, pf_spcentries);
   1000  1.29  pooka 					break;
   1001  1.29  pooka 				}
   1002  1.29  pooka 			}
   1003  1.29  pooka 			pthread_mutex_lock(&pfmtx);
   1004  1.29  pooka 			spcfreebuf(spc);
   1005  1.29  pooka 
   1006  1.29  pooka 			if (!pf) {
   1007  1.48  pooka 				send_error_resp(spc, reqno,
   1008  1.48  pooka 				    RUMPSP_ERR_INVALID_PREFORK);
   1009  1.29  pooka 				shutdown(spc->spc_fd, SHUT_RDWR);
   1010  1.29  pooka 				return;
   1011  1.29  pooka 			}
   1012  1.29  pooka 
   1013  1.29  pooka 			tmpmain = pf->pf_lwp;
   1014  1.29  pooka 			free(pf);
   1015  1.29  pooka 			lwproc_switch(tmpmain);
   1016  1.29  pooka 			if (cancel) {
   1017  1.29  pooka 				lwproc_release();
   1018  1.29  pooka 				shutdown(spc->spc_fd, SHUT_RDWR);
   1019  1.29  pooka 				return;
   1020  1.29  pooka 			}
   1021  1.29  pooka 
   1022  1.29  pooka 			/*
   1023  1.29  pooka 			 * So, we forked already during "prefork" to save
   1024  1.29  pooka 			 * the file descriptors from a parent exit
   1025  1.29  pooka 			 * race condition.  But now we need to fork
   1026  1.29  pooka 			 * a second time since the initial fork has
   1027  1.29  pooka 			 * the wrong spc pointer.  (yea, optimize
   1028  1.29  pooka 			 * interfaces some day if anyone cares)
   1029  1.29  pooka 			 */
   1030  1.38  pooka 			if ((error = lwproc_rfork(spc, 0, NULL)) != 0) {
   1031  1.48  pooka 				send_error_resp(spc, reqno,
   1032  1.48  pooka 				    RUMPSP_ERR_RFORK_FAILED);
   1033  1.29  pooka 				shutdown(spc->spc_fd, SHUT_RDWR);
   1034  1.29  pooka 				lwproc_release();
   1035  1.29  pooka 				return;
   1036  1.29  pooka 			}
   1037  1.29  pooka 			spc->spc_mainlwp = lwproc_curlwp();
   1038  1.29  pooka 			lwproc_switch(tmpmain);
   1039  1.29  pooka 			lwproc_release();
   1040  1.29  pooka 			lwproc_switch(spc->spc_mainlwp);
   1041  1.29  pooka 
   1042  1.29  pooka 			send_handshake_resp(spc, reqno, 0);
   1043  1.44  pooka 		} else {
   1044  1.48  pooka 			send_error_resp(spc, reqno, RUMPSP_ERR_AUTH);
   1045  1.44  pooka 			shutdown(spc->spc_fd, SHUT_RDWR);
   1046  1.44  pooka 			spcfreebuf(spc);
   1047  1.44  pooka 			return;
   1048  1.29  pooka 		}
   1049  1.29  pooka 
   1050  1.29  pooka 		spc->spc_pid = lwproc_getpid();
   1051  1.29  pooka 
   1052  1.29  pooka 		DPRINTF(("rump_sp: handshake for client %p complete, pid %d\n",
   1053  1.29  pooka 		    spc, spc->spc_pid));
   1054  1.29  pooka 
   1055  1.29  pooka 		lwproc_switch(NULL);
   1056  1.29  pooka 		spc->spc_state = SPCSTATE_RUNNING;
   1057  1.29  pooka 		return;
   1058  1.29  pooka 	}
   1059  1.29  pooka 
   1060  1.29  pooka 	if (__predict_false(spc->spc_hdr.rsp_type == RUMPSP_PREFORK)) {
   1061  1.29  pooka 		struct prefork *pf;
   1062  1.29  pooka 		uint32_t auth[AUTHLEN];
   1063  1.44  pooka 		int inexec;
   1064  1.29  pooka 
   1065  1.29  pooka 		DPRINTF(("rump_sp: prefork handler executing for %p\n", spc));
   1066  1.27  pooka 		spcfreebuf(spc);
   1067  1.29  pooka 
   1068  1.44  pooka 		pthread_mutex_lock(&spc->spc_mtx);
   1069  1.44  pooka 		inexec = spc->spc_inexec;
   1070  1.44  pooka 		pthread_mutex_unlock(&spc->spc_mtx);
   1071  1.44  pooka 		if (inexec) {
   1072  1.48  pooka 			send_error_resp(spc, reqno, RUMPSP_ERR_INEXEC);
   1073  1.44  pooka 			shutdown(spc->spc_fd, SHUT_RDWR);
   1074  1.44  pooka 			return;
   1075  1.44  pooka 		}
   1076  1.44  pooka 
   1077  1.29  pooka 		pf = malloc(sizeof(*pf));
   1078  1.29  pooka 		if (pf == NULL) {
   1079  1.48  pooka 			send_error_resp(spc, reqno, RUMPSP_ERR_NOMEM);
   1080  1.29  pooka 			return;
   1081  1.29  pooka 		}
   1082  1.29  pooka 
   1083  1.29  pooka 		/*
   1084  1.29  pooka 		 * Use client main lwp to fork.  this is never used by
   1085  1.44  pooka 		 * worker threads (except in exec, but we checked for that
   1086  1.44  pooka 		 * above) so we can safely use it here.
   1087  1.29  pooka 		 */
   1088  1.29  pooka 		lwproc_switch(spc->spc_mainlwp);
   1089  1.38  pooka 		if ((error = lwproc_rfork(spc, RUMP_RFFDG, NULL)) != 0) {
   1090  1.29  pooka 			DPRINTF(("rump_sp: fork failed: %d (%p)\n",error, spc));
   1091  1.48  pooka 			send_error_resp(spc, reqno, RUMPSP_ERR_RFORK_FAILED);
   1092  1.29  pooka 			lwproc_switch(NULL);
   1093  1.29  pooka 			free(pf);
   1094  1.27  pooka 			return;
   1095  1.27  pooka 		}
   1096  1.29  pooka 
   1097  1.29  pooka 		/* Ok, we have a new process context and a new curlwp */
   1098  1.29  pooka 		for (i = 0; i < AUTHLEN; i++) {
   1099  1.29  pooka 			pf->pf_auth[i] = auth[i] = arc4random();
   1100  1.29  pooka 		}
   1101  1.29  pooka 		pf->pf_lwp = lwproc_curlwp();
   1102  1.29  pooka 		lwproc_switch(NULL);
   1103  1.29  pooka 
   1104  1.29  pooka 		pthread_mutex_lock(&pfmtx);
   1105  1.29  pooka 		LIST_INSERT_HEAD(&preforks, pf, pf_entries);
   1106  1.29  pooka 		LIST_INSERT_HEAD(&spc->spc_pflist, pf, pf_spcentries);
   1107  1.29  pooka 		pthread_mutex_unlock(&pfmtx);
   1108  1.29  pooka 
   1109  1.29  pooka 		DPRINTF(("rump_sp: prefork handler success %p\n", spc));
   1110  1.29  pooka 
   1111  1.29  pooka 		send_prefork_resp(spc, reqno, auth);
   1112  1.27  pooka 		return;
   1113  1.27  pooka 	}
   1114   1.7  pooka 
   1115  1.41  pooka 	if (__predict_false(spc->spc_hdr.rsp_type == RUMPSP_HANDSHAKE)) {
   1116  1.44  pooka 		int inexec;
   1117  1.41  pooka 
   1118  1.41  pooka 		if (spc->spc_hdr.rsp_handshake != HANDSHAKE_EXEC) {
   1119  1.48  pooka 			send_error_resp(spc, reqno,
   1120  1.48  pooka 			    RUMPSP_ERR_MALFORMED_REQUEST);
   1121  1.44  pooka 			shutdown(spc->spc_fd, SHUT_RDWR);
   1122  1.41  pooka 			spcfreebuf(spc);
   1123  1.41  pooka 			return;
   1124  1.41  pooka 		}
   1125  1.41  pooka 
   1126  1.44  pooka 		pthread_mutex_lock(&spc->spc_mtx);
   1127  1.44  pooka 		inexec = spc->spc_inexec;
   1128  1.44  pooka 		pthread_mutex_unlock(&spc->spc_mtx);
   1129  1.44  pooka 		if (inexec) {
   1130  1.48  pooka 			send_error_resp(spc, reqno, RUMPSP_ERR_INEXEC);
   1131  1.44  pooka 			shutdown(spc->spc_fd, SHUT_RDWR);
   1132  1.44  pooka 			spcfreebuf(spc);
   1133  1.44  pooka 			return;
   1134  1.44  pooka 		}
   1135  1.41  pooka 
   1136  1.44  pooka 		pthread_mutex_lock(&spc->spc_mtx);
   1137  1.44  pooka 		spc->spc_inexec = 1;
   1138  1.44  pooka 		pthread_mutex_unlock(&spc->spc_mtx);
   1139  1.44  pooka 
   1140  1.44  pooka 		/*
   1141  1.44  pooka 		 * start to drain lwps.  we will wait for it to finish
   1142  1.44  pooka 		 * in another thread
   1143  1.44  pooka 		 */
   1144  1.41  pooka 		lwproc_switch(spc->spc_mainlwp);
   1145  1.44  pooka 		lwproc_lwpexit();
   1146  1.41  pooka 		lwproc_switch(NULL);
   1147  1.41  pooka 
   1148  1.44  pooka 		/*
   1149  1.44  pooka 		 * exec has to wait for lwps to drain, so finish it off
   1150  1.44  pooka 		 * in another thread
   1151  1.44  pooka 		 */
   1152  1.44  pooka 		schedulework(spc, SBA_EXEC);
   1153  1.41  pooka 		return;
   1154  1.41  pooka 	}
   1155  1.41  pooka 
   1156  1.21  pooka 	if (__predict_false(spc->spc_hdr.rsp_type != RUMPSP_SYSCALL)) {
   1157  1.48  pooka 		send_error_resp(spc, reqno, RUMPSP_ERR_MALFORMED_REQUEST);
   1158  1.21  pooka 		spcfreebuf(spc);
   1159  1.21  pooka 		return;
   1160  1.21  pooka 	}
   1161   1.7  pooka 
   1162  1.44  pooka 	schedulework(spc, SBA_SYSCALL);
   1163   1.7  pooka }
   1164   1.7  pooka 
   1165   1.1  pooka static void *
   1166   1.1  pooka spserver(void *arg)
   1167   1.1  pooka {
   1168   1.1  pooka 	struct spservarg *sarg = arg;
   1169  1.11  pooka 	struct spclient *spc;
   1170   1.1  pooka 	unsigned idx;
   1171   1.1  pooka 	int seen;
   1172   1.1  pooka 	int rv;
   1173  1.11  pooka 	unsigned int nfds, maxidx;
   1174   1.1  pooka 
   1175  1.11  pooka 	for (idx = 0; idx < MAXCLI; idx++) {
   1176   1.1  pooka 		pfdlist[idx].fd = -1;
   1177   1.1  pooka 		pfdlist[idx].events = POLLIN;
   1178  1.11  pooka 
   1179  1.11  pooka 		spc = &spclist[idx];
   1180  1.11  pooka 		pthread_mutex_init(&spc->spc_mtx, NULL);
   1181  1.11  pooka 		pthread_cond_init(&spc->spc_cv, NULL);
   1182  1.24  pooka 		spc->spc_fd = -1;
   1183   1.1  pooka 	}
   1184  1.24  pooka 	pfdlist[0].fd = spclist[0].spc_fd = sarg->sps_sock;
   1185   1.1  pooka 	pfdlist[0].events = POLLIN;
   1186   1.1  pooka 	nfds = 1;
   1187   1.1  pooka 	maxidx = 0;
   1188   1.1  pooka 
   1189  1.14  pooka 	pthread_attr_init(&pattr_detached);
   1190  1.14  pooka 	pthread_attr_setdetachstate(&pattr_detached, PTHREAD_CREATE_DETACHED);
   1191  1.46  joerg #if NOTYET
   1192  1.19  pooka 	pthread_attr_setstacksize(&pattr_detached, 32*1024);
   1193  1.46  joerg #endif
   1194  1.14  pooka 
   1195  1.20  pooka 	pthread_mutex_init(&sbamtx, NULL);
   1196  1.20  pooka 	pthread_cond_init(&sbacv, NULL);
   1197  1.20  pooka 
   1198   1.1  pooka 	DPRINTF(("rump_sp: server mainloop\n"));
   1199   1.1  pooka 
   1200   1.1  pooka 	for (;;) {
   1201  1.18  pooka 		int discoed;
   1202  1.18  pooka 
   1203  1.11  pooka 		/* g/c hangarounds (eventually) */
   1204  1.47  pooka 		discoed = getdisco();
   1205  1.18  pooka 		while (discoed--) {
   1206  1.18  pooka 			nfds--;
   1207  1.18  pooka 			idx = maxidx;
   1208  1.18  pooka 			while (idx) {
   1209  1.18  pooka 				if (pfdlist[idx].fd != -1) {
   1210  1.18  pooka 					maxidx = idx;
   1211  1.18  pooka 					break;
   1212  1.11  pooka 				}
   1213  1.18  pooka 				idx--;
   1214  1.11  pooka 			}
   1215  1.18  pooka 			DPRINTF(("rump_sp: set maxidx to [%u]\n",
   1216  1.18  pooka 			    maxidx));
   1217  1.11  pooka 		}
   1218  1.11  pooka 
   1219   1.1  pooka 		DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
   1220   1.1  pooka 		seen = 0;
   1221   1.1  pooka 		rv = poll(pfdlist, maxidx+1, INFTIM);
   1222   1.1  pooka 		assert(maxidx+1 <= MAXCLI);
   1223   1.1  pooka 		assert(rv != 0);
   1224   1.1  pooka 		if (rv == -1) {
   1225   1.1  pooka 			if (errno == EINTR)
   1226   1.1  pooka 				continue;
   1227   1.1  pooka 			fprintf(stderr, "rump_spserver: poll returned %d\n",
   1228   1.1  pooka 			    errno);
   1229   1.1  pooka 			break;
   1230   1.1  pooka 		}
   1231   1.1  pooka 
   1232  1.12  pooka 		for (idx = 0; seen < rv && idx < MAXCLI; idx++) {
   1233   1.1  pooka 			if ((pfdlist[idx].revents & POLLIN) == 0)
   1234   1.1  pooka 				continue;
   1235   1.1  pooka 
   1236   1.1  pooka 			seen++;
   1237   1.1  pooka 			DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
   1238   1.1  pooka 			    idx, seen, rv));
   1239   1.1  pooka 			if (idx > 0) {
   1240  1.11  pooka 				spc = &spclist[idx];
   1241   1.1  pooka 				DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
   1242   1.1  pooka 				switch (readframe(spc)) {
   1243   1.1  pooka 				case 0:
   1244   1.1  pooka 					break;
   1245   1.1  pooka 				case -1:
   1246   1.2  pooka 					serv_handledisco(idx);
   1247   1.1  pooka 					break;
   1248   1.1  pooka 				default:
   1249   1.7  pooka 					switch (spc->spc_hdr.rsp_class) {
   1250   1.7  pooka 					case RUMPSP_RESP:
   1251   1.7  pooka 						kickwaiter(spc);
   1252   1.7  pooka 						break;
   1253   1.7  pooka 					case RUMPSP_REQ:
   1254   1.7  pooka 						handlereq(spc);
   1255   1.7  pooka 						break;
   1256   1.7  pooka 					default:
   1257  1.21  pooka 						send_error_resp(spc,
   1258  1.48  pooka 						  spc->spc_hdr.rsp_reqno,
   1259  1.48  pooka 						  RUMPSP_ERR_MALFORMED_REQUEST);
   1260  1.21  pooka 						spcfreebuf(spc);
   1261   1.7  pooka 						break;
   1262   1.7  pooka 					}
   1263   1.1  pooka 					break;
   1264   1.1  pooka 				}
   1265  1.11  pooka 
   1266   1.1  pooka 			} else {
   1267   1.1  pooka 				DPRINTF(("rump_sp: mainloop new connection\n"));
   1268  1.11  pooka 
   1269  1.24  pooka 				if (__predict_false(spfini)) {
   1270  1.24  pooka 					close(spclist[0].spc_fd);
   1271  1.24  pooka 					serv_shutdown();
   1272  1.24  pooka 					goto out;
   1273  1.24  pooka 				}
   1274  1.24  pooka 
   1275  1.11  pooka 				idx = serv_handleconn(pfdlist[0].fd,
   1276  1.11  pooka 				    sarg->sps_connhook, nfds == MAXCLI);
   1277  1.11  pooka 				if (idx)
   1278  1.11  pooka 					nfds++;
   1279  1.11  pooka 				if (idx > maxidx)
   1280  1.11  pooka 					maxidx = idx;
   1281  1.12  pooka 				DPRINTF(("rump_sp: maxid now %d\n", maxidx));
   1282   1.1  pooka 			}
   1283   1.1  pooka 		}
   1284   1.1  pooka 	}
   1285   1.1  pooka 
   1286  1.24  pooka  out:
   1287   1.1  pooka 	return NULL;
   1288   1.1  pooka }
   1289   1.1  pooka 
   1290  1.25  pooka static unsigned cleanupidx;
   1291  1.25  pooka static struct sockaddr *cleanupsa;
   1292   1.1  pooka int
   1293  1.26  pooka rumpuser_sp_init(const char *url, const struct rumpuser_sp_ops *spopsp,
   1294  1.26  pooka 	const char *ostype, const char *osrelease, const char *machine)
   1295   1.1  pooka {
   1296   1.5  pooka 	pthread_t pt;
   1297   1.1  pooka 	struct spservarg *sarg;
   1298   1.1  pooka 	struct sockaddr *sap;
   1299   1.5  pooka 	char *p;
   1300   1.5  pooka 	unsigned idx;
   1301   1.5  pooka 	int error, s;
   1302   1.5  pooka 
   1303   1.5  pooka 	p = strdup(url);
   1304   1.5  pooka 	if (p == NULL)
   1305   1.5  pooka 		return ENOMEM;
   1306   1.5  pooka 	error = parseurl(p, &sap, &idx, 1);
   1307   1.5  pooka 	free(p);
   1308   1.5  pooka 	if (error)
   1309   1.5  pooka 		return error;
   1310   1.5  pooka 
   1311  1.26  pooka 	snprintf(banner, sizeof(banner), "RUMPSP-%d.%d-%s-%s/%s\n",
   1312  1.26  pooka 	    PROTOMAJOR, PROTOMINOR, ostype, osrelease, machine);
   1313  1.26  pooka 
   1314   1.5  pooka 	s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
   1315   1.5  pooka 	if (s == -1)
   1316   1.5  pooka 		return errno;
   1317   1.1  pooka 
   1318   1.3  pooka 	spops = *spopsp;
   1319   1.5  pooka 	sarg = malloc(sizeof(*sarg));
   1320   1.5  pooka 	if (sarg == NULL) {
   1321   1.5  pooka 		close(s);
   1322   1.5  pooka 		return ENOMEM;
   1323   1.5  pooka 	}
   1324   1.5  pooka 
   1325   1.5  pooka 	sarg->sps_sock = s;
   1326   1.5  pooka 	sarg->sps_connhook = parsetab[idx].connhook;
   1327   1.1  pooka 
   1328  1.25  pooka 	cleanupidx = idx;
   1329  1.25  pooka 	cleanupsa = sap;
   1330  1.25  pooka 
   1331   1.5  pooka 	/* sloppy error recovery */
   1332   1.1  pooka 
   1333   1.5  pooka 	/*LINTED*/
   1334  1.47  pooka 	if (bind(s, sap, parsetab[idx].slen) == -1) {
   1335   1.5  pooka 		fprintf(stderr, "rump_sp: server bind failed\n");
   1336   1.5  pooka 		return errno;
   1337   1.1  pooka 	}
   1338  1.25  pooka 
   1339  1.18  pooka 	if (listen(s, MAXCLI) == -1) {
   1340   1.5  pooka 		fprintf(stderr, "rump_sp: server listen failed\n");
   1341   1.5  pooka 		return errno;
   1342   1.1  pooka 	}
   1343   1.1  pooka 
   1344   1.5  pooka 	if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
   1345   1.5  pooka 		fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
   1346   1.1  pooka 		return errno;
   1347   1.1  pooka 	}
   1348   1.5  pooka 	pthread_detach(pt);
   1349   1.1  pooka 
   1350   1.1  pooka 	return 0;
   1351   1.1  pooka }
   1352  1.24  pooka 
   1353  1.24  pooka void
   1354  1.37  pooka rumpuser_sp_fini(void *arg)
   1355  1.24  pooka {
   1356  1.37  pooka 	struct spclient *spc = arg;
   1357  1.37  pooka 	register_t retval[2] = {0, 0};
   1358  1.37  pooka 
   1359  1.42  pooka 	if (spclist[0].spc_fd) {
   1360  1.42  pooka 		parsetab[cleanupidx].cleanup(cleanupsa);
   1361  1.42  pooka 	}
   1362  1.42  pooka 
   1363  1.37  pooka 	/*
   1364  1.37  pooka 	 * stuff response into the socket, since this process is just
   1365  1.37  pooka 	 * about to exit
   1366  1.37  pooka 	 */
   1367  1.37  pooka 	if (spc && spc->spc_syscallreq)
   1368  1.37  pooka 		send_syscall_resp(spc, spc->spc_syscallreq, 0, retval);
   1369  1.24  pooka 
   1370  1.24  pooka 	if (spclist[0].spc_fd) {
   1371  1.24  pooka 		shutdown(spclist[0].spc_fd, SHUT_RDWR);
   1372  1.24  pooka 		spfini = 1;
   1373  1.24  pooka 	}
   1374  1.24  pooka }
   1375