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