Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_sp.c revision 1.3
      1  1.3  pooka /*      $NetBSD: rumpuser_sp.c,v 1.3 2010/11/01 13:49:10 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  *
     15  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  pooka  * SUCH DAMAGE.
     26  1.1  pooka  */
     27  1.1  pooka 
     28  1.1  pooka /*
     29  1.1  pooka  * Sysproxy routines.  This provides system RPC support over host sockets.
     30  1.1  pooka  * The most notable limitation is that the client and server must share
     31  1.1  pooka  * the same ABI.  This does not mean that they have to be the same
     32  1.1  pooka  * machine or that they need to run the same version of the host OS,
     33  1.1  pooka  * just that they must agree on the data structures.  This even *might*
     34  1.1  pooka  * work correctly from one hardware architecture to another.
     35  1.1  pooka  *
     36  1.1  pooka  * Not finished yet, i.e. don't use in production.  Lacks locking plus
     37  1.1  pooka  * handling of multiple clients and unexpected connection closes.
     38  1.1  pooka  */
     39  1.1  pooka 
     40  1.1  pooka #include <sys/cdefs.h>
     41  1.3  pooka __RCSID("$NetBSD: rumpuser_sp.c,v 1.3 2010/11/01 13:49:10 pooka Exp $");
     42  1.1  pooka 
     43  1.1  pooka #include <sys/types.h>
     44  1.1  pooka #include <sys/mman.h>
     45  1.1  pooka #include <sys/socket.h>
     46  1.1  pooka 
     47  1.1  pooka #include <arpa/inet.h>
     48  1.1  pooka #include <netinet/in.h>
     49  1.1  pooka #include <netinet/tcp.h>
     50  1.1  pooka 
     51  1.1  pooka #include <assert.h>
     52  1.1  pooka #include <errno.h>
     53  1.1  pooka #include <fcntl.h>
     54  1.1  pooka #include <poll.h>
     55  1.1  pooka #include <pthread.h>
     56  1.1  pooka #include <stdarg.h>
     57  1.1  pooka #include <stdio.h>
     58  1.1  pooka #include <stdlib.h>
     59  1.1  pooka #include <string.h>
     60  1.1  pooka #include <unistd.h>
     61  1.1  pooka 
     62  1.1  pooka #include <rump/rump.h>
     63  1.3  pooka #include <rump/rump_syscalls.h>
     64  1.1  pooka #include <rump/rumpuser.h>
     65  1.1  pooka 
     66  1.1  pooka //#define DEBUG
     67  1.1  pooka #ifdef DEBUG
     68  1.1  pooka #define DPRINTF(x) mydprintf x
     69  1.1  pooka static void
     70  1.1  pooka mydprintf(const char *fmt, ...)
     71  1.1  pooka {
     72  1.1  pooka 	va_list ap;
     73  1.1  pooka 
     74  1.1  pooka 	va_start(ap, fmt);
     75  1.1  pooka 	vfprintf(stderr, fmt, ap);
     76  1.1  pooka 	va_end(ap);
     77  1.1  pooka }
     78  1.1  pooka #else
     79  1.1  pooka #define DPRINTF(x)
     80  1.1  pooka #endif
     81  1.1  pooka 
     82  1.1  pooka /*
     83  1.1  pooka  * Bah, I hate writing on-off-wire conversions in C
     84  1.1  pooka  */
     85  1.1  pooka 
     86  1.1  pooka enum {
     87  1.1  pooka 	RUMPSP_SYSCALL_REQ,	RUMPSP_SYSCALL_RESP,
     88  1.1  pooka 	RUMPSP_COPYIN_REQ,	RUMPSP_COPYIN_RESP,
     89  1.1  pooka 	RUMPSP_COPYOUT_REQ,	/* no copyout resp */
     90  1.1  pooka 	RUMPSP_ANONMMAP_REQ,	RUMPSP_ANONMMAP_RESP
     91  1.1  pooka };
     92  1.1  pooka 
     93  1.1  pooka struct rsp_hdr {
     94  1.1  pooka 	uint64_t rsp_len;
     95  1.1  pooka 	uint64_t rsp_reqno;
     96  1.1  pooka 	uint32_t rsp_type;
     97  1.1  pooka 	/*
     98  1.1  pooka 	 * We want this structure 64bit-aligned for typecast fun,
     99  1.1  pooka 	 * so might as well use the following for something.
    100  1.1  pooka 	 */
    101  1.1  pooka 	uint32_t rsp_sysnum;
    102  1.1  pooka };
    103  1.1  pooka #define HDRSZ sizeof(struct rsp_hdr)
    104  1.1  pooka 
    105  1.1  pooka /*
    106  1.1  pooka  * Data follows the header.  We have two types of structured data.
    107  1.1  pooka  */
    108  1.1  pooka 
    109  1.1  pooka /* copyin/copyout */
    110  1.1  pooka struct rsp_copydata {
    111  1.1  pooka 	size_t rcp_len;
    112  1.1  pooka 	void *rcp_addr;
    113  1.1  pooka 	uint8_t rcp_data[0];
    114  1.1  pooka };
    115  1.1  pooka 
    116  1.1  pooka /* syscall response */
    117  1.1  pooka struct rsp_sysresp {
    118  1.1  pooka 	int rsys_error;
    119  1.1  pooka 	register_t rsys_retval[2];
    120  1.1  pooka };
    121  1.1  pooka 
    122  1.1  pooka 
    123  1.1  pooka struct spclient {
    124  1.1  pooka 	int spc_fd;
    125  1.2  pooka 	struct lwp *spc_lwp;
    126  1.1  pooka 
    127  1.1  pooka 	/* incoming */
    128  1.1  pooka 	struct rsp_hdr spc_hdr;
    129  1.1  pooka 	uint8_t *spc_buf;
    130  1.1  pooka 	size_t spc_off;
    131  1.1  pooka 
    132  1.1  pooka #if 0
    133  1.1  pooka 	/* outgoing */
    134  1.1  pooka 	int spc_obusy;
    135  1.1  pooka 	pthread_mutex_t spc_omtx;
    136  1.1  pooka 	pthread_cond_t spc_cv;
    137  1.1  pooka #endif
    138  1.1  pooka };
    139  1.1  pooka 
    140  1.1  pooka typedef int (*addrparse_fn)(const char *, int, struct sockaddr **);
    141  1.1  pooka typedef int (*connecthook_fn)(int);
    142  1.1  pooka 
    143  1.1  pooka #define MAXCLI 4
    144  1.1  pooka 
    145  1.1  pooka static struct pollfd pfdlist[MAXCLI];
    146  1.1  pooka static struct spclient spclist[MAXCLI];
    147  1.1  pooka static unsigned int nfds, maxidx;
    148  1.1  pooka static uint64_t nextreq;
    149  1.1  pooka static pthread_key_t spclient_tls;
    150  1.1  pooka 
    151  1.1  pooka static struct spclient clispc;
    152  1.1  pooka 
    153  1.3  pooka static struct rumpuser_sp_ops spops;
    154  1.3  pooka 
    155  1.3  pooka /*
    156  1.3  pooka  * Manual wrappers, since librump does not have access to the
    157  1.3  pooka  * user namespace wrapped interfaces.
    158  1.3  pooka  */
    159  1.3  pooka 
    160  1.3  pooka static void
    161  1.3  pooka lwproc_switch(struct lwp *l)
    162  1.3  pooka {
    163  1.3  pooka 
    164  1.3  pooka 	rump_schedule();
    165  1.3  pooka 	spops.spop_lwproc_switch(l);
    166  1.3  pooka 	rump_unschedule();
    167  1.3  pooka }
    168  1.3  pooka 
    169  1.3  pooka static void
    170  1.3  pooka lwproc_release(void)
    171  1.3  pooka {
    172  1.3  pooka 
    173  1.3  pooka 	rump_schedule();
    174  1.3  pooka 	spops.spop_lwproc_release();
    175  1.3  pooka 	rump_unschedule();
    176  1.3  pooka }
    177  1.3  pooka 
    178  1.3  pooka static int
    179  1.3  pooka lwproc_newproc(void)
    180  1.3  pooka {
    181  1.3  pooka 	int rv;
    182  1.3  pooka 
    183  1.3  pooka 	rump_schedule();
    184  1.3  pooka 	rv = spops.spop_lwproc_newproc();
    185  1.3  pooka 	rump_unschedule();
    186  1.3  pooka 
    187  1.3  pooka 	return rv;
    188  1.3  pooka }
    189  1.3  pooka 
    190  1.3  pooka static struct lwp *
    191  1.3  pooka lwproc_curlwp(void)
    192  1.3  pooka {
    193  1.3  pooka 	struct lwp *l;
    194  1.3  pooka 
    195  1.3  pooka 	rump_schedule();
    196  1.3  pooka 	l = spops.spop_lwproc_curlwp();
    197  1.3  pooka 	rump_unschedule();
    198  1.3  pooka 
    199  1.3  pooka 	return l;
    200  1.3  pooka }
    201  1.3  pooka 
    202  1.3  pooka static int
    203  1.3  pooka rumpsyscall(int sysnum, void *data, register_t *retval)
    204  1.3  pooka {
    205  1.3  pooka 	int rv;
    206  1.3  pooka 
    207  1.3  pooka 	rump_schedule();
    208  1.3  pooka 	rv = spops.spop_syscall(sysnum, data, retval);
    209  1.3  pooka 	rump_unschedule();
    210  1.3  pooka 
    211  1.3  pooka 	return rv;
    212  1.3  pooka }
    213  1.1  pooka 
    214  1.1  pooka static int
    215  1.1  pooka dosend(struct spclient *spc, const void *data, size_t dlen)
    216  1.1  pooka {
    217  1.1  pooka 	struct pollfd pfd;
    218  1.1  pooka 	const uint8_t *sdata = data;
    219  1.1  pooka 	ssize_t n;
    220  1.1  pooka 	size_t sent;
    221  1.1  pooka 	int fd = spc->spc_fd;
    222  1.1  pooka 
    223  1.1  pooka 	pfd.fd = fd;
    224  1.1  pooka 	pfd.events = POLLOUT;
    225  1.1  pooka 
    226  1.1  pooka 	for (sent = 0, n = 0; sent < dlen; ) {
    227  1.1  pooka 		if (n) {
    228  1.1  pooka 			if (poll(&pfd, 1, INFTIM) == -1) {
    229  1.1  pooka 				if (errno == EINTR)
    230  1.1  pooka 					continue;
    231  1.1  pooka 				return errno;
    232  1.1  pooka 			}
    233  1.1  pooka 		}
    234  1.1  pooka 
    235  1.1  pooka 		n = write(fd, sdata + sent, dlen - sent);
    236  1.1  pooka 		if (n == 0) {
    237  1.1  pooka 			return EFAULT;
    238  1.1  pooka 		}
    239  1.1  pooka 		if (n == -1 && errno != EAGAIN) {
    240  1.1  pooka 			return EFAULT;
    241  1.1  pooka 		}
    242  1.1  pooka 		sent += n;
    243  1.1  pooka 	}
    244  1.1  pooka 
    245  1.1  pooka 	return 0;
    246  1.1  pooka }
    247  1.1  pooka 
    248  1.1  pooka static int
    249  1.1  pooka send_syscall_req(struct spclient *spc, int sysnum,
    250  1.1  pooka 	const void *data, size_t dlen)
    251  1.1  pooka {
    252  1.1  pooka 	struct rsp_hdr rhdr;
    253  1.1  pooka 
    254  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + dlen;
    255  1.1  pooka 	rhdr.rsp_reqno = nextreq++;
    256  1.1  pooka 	rhdr.rsp_type = RUMPSP_SYSCALL_REQ;
    257  1.1  pooka 	rhdr.rsp_sysnum = sysnum;
    258  1.1  pooka 
    259  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    260  1.1  pooka 	dosend(spc, data, dlen);
    261  1.1  pooka 
    262  1.1  pooka 	return 0;
    263  1.1  pooka }
    264  1.1  pooka 
    265  1.1  pooka static int
    266  1.1  pooka send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
    267  1.1  pooka 	register_t retval[2])
    268  1.1  pooka {
    269  1.1  pooka 	struct rsp_hdr rhdr;
    270  1.1  pooka 	struct rsp_sysresp sysresp;
    271  1.1  pooka 
    272  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
    273  1.1  pooka 	rhdr.rsp_reqno = reqno;
    274  1.1  pooka 	rhdr.rsp_type = RUMPSP_SYSCALL_RESP;
    275  1.1  pooka 	rhdr.rsp_sysnum = 0;
    276  1.1  pooka 
    277  1.1  pooka 	sysresp.rsys_error = error;
    278  1.1  pooka 	memcpy(sysresp.rsys_retval, retval, sizeof(retval));
    279  1.1  pooka 
    280  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    281  1.1  pooka 	dosend(spc, &sysresp, sizeof(sysresp));
    282  1.1  pooka 
    283  1.1  pooka 	return 0;
    284  1.1  pooka }
    285  1.1  pooka 
    286  1.1  pooka static int
    287  1.1  pooka send_copyin_req(struct spclient *spc, const void *remaddr, size_t dlen)
    288  1.1  pooka {
    289  1.1  pooka 	struct rsp_hdr rhdr;
    290  1.1  pooka 	struct rsp_copydata copydata;
    291  1.1  pooka 
    292  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
    293  1.1  pooka 	rhdr.rsp_reqno = nextreq++;
    294  1.1  pooka 	rhdr.rsp_type = RUMPSP_COPYIN_REQ;
    295  1.1  pooka 	rhdr.rsp_sysnum = 0;
    296  1.1  pooka 
    297  1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    298  1.1  pooka 	copydata.rcp_len = dlen;
    299  1.1  pooka 
    300  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    301  1.1  pooka 	dosend(spc, &copydata, sizeof(copydata));
    302  1.1  pooka 
    303  1.1  pooka 	return 0;
    304  1.1  pooka }
    305  1.1  pooka 
    306  1.1  pooka static int
    307  1.1  pooka send_copyin_resp(struct spclient *spc, uint64_t reqno, void *data, size_t dlen)
    308  1.1  pooka {
    309  1.1  pooka 	struct rsp_hdr rhdr;
    310  1.1  pooka 
    311  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + dlen;
    312  1.1  pooka 	rhdr.rsp_reqno = reqno;
    313  1.1  pooka 	rhdr.rsp_type = RUMPSP_COPYIN_RESP;
    314  1.1  pooka 	rhdr.rsp_sysnum = 0;
    315  1.1  pooka 
    316  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    317  1.1  pooka 	dosend(spc, data, dlen);
    318  1.1  pooka 
    319  1.1  pooka 	return 0;
    320  1.1  pooka }
    321  1.1  pooka 
    322  1.1  pooka static int
    323  1.1  pooka send_copyout_req(struct spclient *spc, const void *remaddr,
    324  1.1  pooka 	const void *data, size_t dlen)
    325  1.1  pooka {
    326  1.1  pooka 	struct rsp_hdr rhdr;
    327  1.1  pooka 	struct rsp_copydata copydata;
    328  1.1  pooka 
    329  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
    330  1.1  pooka 	rhdr.rsp_reqno = nextreq++;
    331  1.1  pooka 	rhdr.rsp_type = RUMPSP_COPYOUT_REQ;
    332  1.1  pooka 	rhdr.rsp_sysnum = 0;
    333  1.1  pooka 
    334  1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    335  1.1  pooka 	copydata.rcp_len = dlen;
    336  1.1  pooka 
    337  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    338  1.1  pooka 	dosend(spc, &copydata, sizeof(copydata));
    339  1.1  pooka 	dosend(spc, data, dlen);
    340  1.1  pooka 
    341  1.1  pooka 	return 0;
    342  1.1  pooka }
    343  1.1  pooka 
    344  1.1  pooka static int
    345  1.1  pooka send_anonmmap_req(struct spclient *spc, size_t howmuch)
    346  1.1  pooka {
    347  1.1  pooka 	struct rsp_hdr rhdr;
    348  1.1  pooka 
    349  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
    350  1.1  pooka 	rhdr.rsp_reqno = nextreq++;
    351  1.1  pooka 	rhdr.rsp_type = RUMPSP_ANONMMAP_REQ;
    352  1.1  pooka 	rhdr.rsp_sysnum = 0;
    353  1.1  pooka 
    354  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    355  1.1  pooka 	dosend(spc, &howmuch, sizeof(howmuch));
    356  1.1  pooka 
    357  1.1  pooka 	return 0;
    358  1.1  pooka }
    359  1.1  pooka 
    360  1.1  pooka static int
    361  1.1  pooka send_anonmmap_resp(struct spclient *spc, uint64_t reqno, void *addr)
    362  1.1  pooka {
    363  1.1  pooka 	struct rsp_hdr rhdr;
    364  1.1  pooka 
    365  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(addr);
    366  1.1  pooka 	rhdr.rsp_reqno = reqno;
    367  1.1  pooka 	rhdr.rsp_type = RUMPSP_ANONMMAP_RESP;
    368  1.1  pooka 	rhdr.rsp_sysnum = 0;
    369  1.1  pooka 
    370  1.1  pooka 	dosend(spc, &rhdr, sizeof(rhdr));
    371  1.1  pooka 	dosend(spc, &addr, sizeof(addr));
    372  1.1  pooka 
    373  1.1  pooka 	return 0;
    374  1.1  pooka }
    375  1.1  pooka 
    376  1.1  pooka static void
    377  1.2  pooka serv_handledisco(unsigned int idx)
    378  1.1  pooka {
    379  1.1  pooka 	struct spclient *spc = &spclist[idx];
    380  1.1  pooka 	int fd = spc->spc_fd;
    381  1.1  pooka 
    382  1.1  pooka 	DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
    383  1.1  pooka 
    384  1.3  pooka 	lwproc_switch(spc->spc_lwp);
    385  1.3  pooka 	lwproc_release();
    386  1.2  pooka 
    387  1.1  pooka 	free(spc->spc_buf);
    388  1.1  pooka 	memset(spc, 0, sizeof(*spc));
    389  1.1  pooka 	close(fd);
    390  1.1  pooka 	pfdlist[idx].fd = -1;
    391  1.1  pooka 	nfds--;
    392  1.1  pooka 
    393  1.1  pooka 	if (idx == maxidx) {
    394  1.1  pooka 		while (idx--) {
    395  1.1  pooka 			if (pfdlist[idx].fd != -1) {
    396  1.1  pooka 				maxidx = idx;
    397  1.1  pooka 				break;
    398  1.1  pooka 			}
    399  1.1  pooka 			assert(idx != 0);
    400  1.1  pooka 		}
    401  1.1  pooka 		DPRINTF(("rump_sp: set maxidx to [%u]\n", maxidx));
    402  1.1  pooka 	}
    403  1.1  pooka }
    404  1.1  pooka 
    405  1.1  pooka static int
    406  1.1  pooka serv_handleconn(int fd, connecthook_fn connhook)
    407  1.1  pooka {
    408  1.1  pooka 	struct sockaddr_storage ss;
    409  1.1  pooka 	socklen_t sl = sizeof(ss);
    410  1.1  pooka 	int newfd, flags, error;
    411  1.1  pooka 	unsigned i;
    412  1.1  pooka 
    413  1.1  pooka 	/*LINTED: cast ok */
    414  1.1  pooka 	newfd = accept(fd, (struct sockaddr *)&ss, &sl);
    415  1.1  pooka 	if (newfd == -1)
    416  1.1  pooka 		return errno;
    417  1.1  pooka 
    418  1.1  pooka 	/* XXX: should do some sort of handshake too */
    419  1.1  pooka 
    420  1.1  pooka 	if (nfds == MAXCLI) {
    421  1.1  pooka 		close(newfd); /* EBUSY */
    422  1.1  pooka 		return EBUSY;
    423  1.1  pooka 	}
    424  1.1  pooka 
    425  1.1  pooka 	flags = fcntl(newfd, F_GETFL, 0);
    426  1.1  pooka 	if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
    427  1.1  pooka 		close(newfd);
    428  1.1  pooka 		return errno;
    429  1.1  pooka 	}
    430  1.1  pooka 	flags = 1;
    431  1.1  pooka 
    432  1.1  pooka 	if ((error = connhook(newfd)) != 0) {
    433  1.1  pooka 		close(newfd);
    434  1.1  pooka 		return error;
    435  1.1  pooka 	}
    436  1.1  pooka 
    437  1.3  pooka 	if ((error = lwproc_newproc()) != 0) {
    438  1.2  pooka 		close(newfd);
    439  1.2  pooka 		return error;
    440  1.2  pooka 	}
    441  1.2  pooka 
    442  1.1  pooka 	/* find empty slot the simple way */
    443  1.1  pooka 	for (i = 0; i < MAXCLI; i++) {
    444  1.1  pooka 		if (pfdlist[i].fd == -1)
    445  1.2  pooka 			break;
    446  1.1  pooka 	}
    447  1.1  pooka 
    448  1.1  pooka 	assert(i < MAXCLI);
    449  1.1  pooka 	nfds++;
    450  1.1  pooka 
    451  1.1  pooka 	pfdlist[i].fd = newfd;
    452  1.1  pooka 	spclist[i].spc_fd = newfd;
    453  1.3  pooka 	spclist[i].spc_lwp = lwproc_curlwp();
    454  1.1  pooka 	if (maxidx < i)
    455  1.1  pooka 		maxidx = i;
    456  1.1  pooka 
    457  1.2  pooka 	DPRINTF(("rump_sp: added new connection at idx %u, pid %d\n",
    458  1.3  pooka 	    i, rump_sys_getpid())); /* XXX: getpid not spop */
    459  1.2  pooka 
    460  1.3  pooka 	lwproc_switch(NULL);
    461  1.1  pooka 
    462  1.1  pooka 	return 0;
    463  1.1  pooka }
    464  1.1  pooka 
    465  1.1  pooka static void
    466  1.1  pooka serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
    467  1.1  pooka {
    468  1.1  pooka 	register_t retval[2];
    469  1.1  pooka 	int rv, sysnum;
    470  1.1  pooka 
    471  1.1  pooka 	sysnum = (int)rhdr->rsp_sysnum;
    472  1.1  pooka 	DPRINTF(("rump_sp: handling syscall %d from client %d\n",
    473  1.1  pooka 	    sysnum, 0));
    474  1.1  pooka 
    475  1.1  pooka 	pthread_setspecific(spclient_tls, spc);
    476  1.3  pooka 	lwproc_switch(spc->spc_lwp);
    477  1.3  pooka 	rv = rumpsyscall(sysnum, data, retval);
    478  1.3  pooka 	lwproc_switch(NULL);
    479  1.1  pooka 	pthread_setspecific(spclient_tls, NULL);
    480  1.1  pooka 
    481  1.1  pooka 	send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
    482  1.1  pooka }
    483  1.1  pooka 
    484  1.1  pooka static int
    485  1.1  pooka readframe(struct spclient *spc)
    486  1.1  pooka {
    487  1.1  pooka 	int fd = spc->spc_fd;
    488  1.1  pooka 	size_t left;
    489  1.1  pooka 	size_t framelen;
    490  1.1  pooka 	ssize_t n;
    491  1.1  pooka 
    492  1.1  pooka 	/* still reading header? */
    493  1.1  pooka 	if (spc->spc_off < HDRSZ) {
    494  1.1  pooka 		DPRINTF(("rump_sp: readframe getting header at offset %zu\n",
    495  1.1  pooka 		    spc->spc_off));
    496  1.1  pooka 
    497  1.1  pooka 		left = HDRSZ - spc->spc_off;
    498  1.1  pooka 		/*LINTED: cast ok */
    499  1.1  pooka 		n = read(fd, (uint8_t *)&spc->spc_hdr + spc->spc_off, left);
    500  1.1  pooka 		if (n == 0) {
    501  1.1  pooka 			return -1;
    502  1.1  pooka 		}
    503  1.1  pooka 		if (n == -1) {
    504  1.1  pooka 			if (errno == EAGAIN)
    505  1.1  pooka 				return 0;
    506  1.1  pooka 			return -1;
    507  1.1  pooka 		}
    508  1.1  pooka 
    509  1.1  pooka 		spc->spc_off += n;
    510  1.1  pooka 		if (spc->spc_off < HDRSZ)
    511  1.1  pooka 			return -1;
    512  1.1  pooka 
    513  1.1  pooka 		/*LINTED*/
    514  1.1  pooka 		framelen = spc->spc_hdr.rsp_len;
    515  1.1  pooka 
    516  1.1  pooka 		if (framelen < HDRSZ) {
    517  1.1  pooka 			return -1;
    518  1.1  pooka 		} else if (framelen == HDRSZ) {
    519  1.1  pooka 			return 1;
    520  1.1  pooka 		}
    521  1.1  pooka 
    522  1.1  pooka 		spc->spc_buf = malloc(framelen - HDRSZ);
    523  1.1  pooka 		if (spc->spc_buf == NULL) {
    524  1.1  pooka 			return -1;
    525  1.1  pooka 		}
    526  1.1  pooka 		memset(spc->spc_buf, 0, framelen - HDRSZ);
    527  1.1  pooka 
    528  1.1  pooka 		/* "fallthrough" */
    529  1.1  pooka 	} else {
    530  1.1  pooka 		/*LINTED*/
    531  1.1  pooka 		framelen = spc->spc_hdr.rsp_len;
    532  1.1  pooka 	}
    533  1.1  pooka 
    534  1.1  pooka 	left = framelen - spc->spc_off;
    535  1.1  pooka 
    536  1.1  pooka 	DPRINTF(("rump_sp: readframe getting body at offset %zu, left %zu\n",
    537  1.1  pooka 	    spc->spc_off, left));
    538  1.1  pooka 
    539  1.1  pooka 	if (left == 0)
    540  1.1  pooka 		return 1;
    541  1.1  pooka 	n = read(fd, spc->spc_buf + (spc->spc_off - HDRSZ), left);
    542  1.1  pooka 	if (n == 0) {
    543  1.1  pooka 		return -1;
    544  1.1  pooka 	}
    545  1.1  pooka 	if (n == -1) {
    546  1.1  pooka 		if (errno == EAGAIN)
    547  1.1  pooka 			return 0;
    548  1.1  pooka 		return -1;
    549  1.1  pooka 	}
    550  1.1  pooka 	spc->spc_off += n;
    551  1.1  pooka 	left -= n;
    552  1.1  pooka 
    553  1.1  pooka 	/* got everything? */
    554  1.1  pooka 	if (left == 0)
    555  1.1  pooka 		return 1;
    556  1.1  pooka 	else
    557  1.1  pooka 		return 0;
    558  1.1  pooka }
    559  1.1  pooka 
    560  1.1  pooka int
    561  1.1  pooka rumpuser_sp_copyin(const void *uaddr, void *kaddr, size_t len)
    562  1.1  pooka {
    563  1.1  pooka 	struct spclient *spc;
    564  1.1  pooka 	struct pollfd pfd;
    565  1.1  pooka 
    566  1.1  pooka 	spc = pthread_getspecific(spclient_tls);
    567  1.1  pooka 	if (!spc)
    568  1.1  pooka 		return EFAULT;
    569  1.1  pooka 
    570  1.1  pooka 	send_copyin_req(spc, uaddr, len);
    571  1.1  pooka 
    572  1.1  pooka 	pfd.fd = spc->spc_fd;
    573  1.1  pooka 	pfd.events = POLLIN;
    574  1.1  pooka 	do {
    575  1.1  pooka 		poll(&pfd, 1, INFTIM);
    576  1.1  pooka 	} while (readframe(spc) < 1);
    577  1.1  pooka 
    578  1.1  pooka 	if (spc->spc_hdr.rsp_type != RUMPSP_COPYIN_RESP) {
    579  1.1  pooka 		abort();
    580  1.1  pooka 	}
    581  1.1  pooka 
    582  1.1  pooka 	memcpy(kaddr, spc->spc_buf, len);
    583  1.1  pooka 	free(spc->spc_buf);
    584  1.1  pooka 	spc->spc_off = 0;
    585  1.1  pooka 
    586  1.1  pooka 	return 0;
    587  1.1  pooka }
    588  1.1  pooka 
    589  1.1  pooka int
    590  1.1  pooka rumpuser_sp_copyout(const void *kaddr, void *uaddr, size_t dlen)
    591  1.1  pooka {
    592  1.1  pooka 	struct spclient *spc;
    593  1.1  pooka 
    594  1.1  pooka 	spc = pthread_getspecific(spclient_tls);
    595  1.1  pooka 	if (!spc) {
    596  1.1  pooka 		DPRINTF(("rump_sp: copyout curlwp not found\n"));
    597  1.1  pooka 		return EFAULT;
    598  1.1  pooka 	}
    599  1.1  pooka 
    600  1.1  pooka 	send_copyout_req(spc, uaddr, kaddr, dlen);
    601  1.1  pooka 
    602  1.1  pooka 	return 0;
    603  1.1  pooka }
    604  1.1  pooka 
    605  1.1  pooka int
    606  1.1  pooka rumpuser_sp_anonmmap(size_t howmuch, void **addr)
    607  1.1  pooka {
    608  1.1  pooka 	struct spclient *spc;
    609  1.1  pooka 	struct pollfd pfd;
    610  1.1  pooka 	void *resp;
    611  1.1  pooka 
    612  1.1  pooka 	spc = pthread_getspecific(spclient_tls);
    613  1.1  pooka 	if (!spc)
    614  1.1  pooka 		return EFAULT;
    615  1.1  pooka 
    616  1.1  pooka 	send_anonmmap_req(spc, howmuch);
    617  1.1  pooka 
    618  1.1  pooka 	pfd.fd = spc->spc_fd;
    619  1.1  pooka 	pfd.events = POLLIN;
    620  1.1  pooka 	do {
    621  1.1  pooka 		poll(&pfd, 1, INFTIM);
    622  1.1  pooka 	} while (readframe(spc) < 1);
    623  1.1  pooka 
    624  1.1  pooka 	if (spc->spc_hdr.rsp_type != RUMPSP_ANONMMAP_RESP) {
    625  1.1  pooka 		abort();
    626  1.1  pooka 	}
    627  1.1  pooka 
    628  1.1  pooka 	/*LINTED*/
    629  1.1  pooka 	resp = *(void **)spc->spc_buf;
    630  1.1  pooka 	spc->spc_off = 0;
    631  1.1  pooka 
    632  1.1  pooka 	if (resp == NULL)
    633  1.1  pooka 		return ENOMEM;
    634  1.1  pooka 
    635  1.1  pooka 	*addr = resp;
    636  1.1  pooka 	return 0;
    637  1.1  pooka }
    638  1.1  pooka 
    639  1.1  pooka int
    640  1.1  pooka rumpuser_sp_syscall(int sysnum, const void *data, size_t dlen,
    641  1.1  pooka 	register_t *retval)
    642  1.1  pooka {
    643  1.1  pooka 	struct rsp_sysresp *resp;
    644  1.1  pooka 	struct rsp_copydata *copydata;
    645  1.1  pooka 	struct pollfd pfd;
    646  1.1  pooka 	size_t maplen;
    647  1.1  pooka 	void *mapaddr;
    648  1.1  pooka 	int gotresp;
    649  1.1  pooka 
    650  1.1  pooka 	DPRINTF(("rump_sp_syscall: executing syscall %d\n", sysnum));
    651  1.1  pooka 
    652  1.1  pooka 	send_syscall_req(&clispc, sysnum, data, dlen);
    653  1.1  pooka 
    654  1.1  pooka 	DPRINTF(("rump_sp_syscall: syscall %d request sent.  "
    655  1.1  pooka 	    "waiting for response\n", sysnum));
    656  1.1  pooka 
    657  1.1  pooka 	pfd.fd = clispc.spc_fd;
    658  1.1  pooka 	pfd.events = POLLIN;
    659  1.1  pooka 
    660  1.1  pooka 	gotresp = 0;
    661  1.1  pooka 	while (!gotresp) {
    662  1.1  pooka 		while (readframe(&clispc) < 1)
    663  1.1  pooka 			poll(&pfd, 1, INFTIM);
    664  1.1  pooka 
    665  1.1  pooka 		switch (clispc.spc_hdr.rsp_type) {
    666  1.1  pooka 		case RUMPSP_COPYIN_REQ:
    667  1.1  pooka 			/*LINTED*/
    668  1.1  pooka 			copydata = (struct rsp_copydata *)clispc.spc_buf;
    669  1.1  pooka 			DPRINTF(("rump_sp_syscall: copyin request: %p/%zu\n",
    670  1.1  pooka 			    copydata->rcp_addr, copydata->rcp_len));
    671  1.1  pooka 			send_copyin_resp(&clispc, clispc.spc_hdr.rsp_reqno,
    672  1.1  pooka 			    copydata->rcp_addr, copydata->rcp_len);
    673  1.1  pooka 			clispc.spc_off = 0;
    674  1.1  pooka 			break;
    675  1.1  pooka 		case RUMPSP_COPYOUT_REQ:
    676  1.1  pooka 			/*LINTED*/
    677  1.1  pooka 			copydata = (struct rsp_copydata *)clispc.spc_buf;
    678  1.1  pooka 			DPRINTF(("rump_sp_syscall: copyout request: %p/%zu\n",
    679  1.1  pooka 			    copydata->rcp_addr, copydata->rcp_len));
    680  1.1  pooka 			/*LINTED*/
    681  1.1  pooka 			memcpy(copydata->rcp_addr, copydata->rcp_data,
    682  1.1  pooka 			    copydata->rcp_len);
    683  1.1  pooka 			clispc.spc_off = 0;
    684  1.1  pooka 			break;
    685  1.1  pooka 		case RUMPSP_ANONMMAP_REQ:
    686  1.1  pooka 			/*LINTED*/
    687  1.1  pooka 			maplen = *(size_t *)clispc.spc_buf;
    688  1.1  pooka 			mapaddr = mmap(NULL, maplen, PROT_READ|PROT_WRITE,
    689  1.1  pooka 			    MAP_ANON, -1, 0);
    690  1.1  pooka 			if (mapaddr == MAP_FAILED)
    691  1.1  pooka 				mapaddr = NULL;
    692  1.1  pooka 			send_anonmmap_resp(&clispc,
    693  1.1  pooka 			    clispc.spc_hdr.rsp_reqno, mapaddr);
    694  1.1  pooka 			clispc.spc_off = 0;
    695  1.1  pooka 			break;
    696  1.1  pooka 		case RUMPSP_SYSCALL_RESP:
    697  1.1  pooka 			DPRINTF(("rump_sp_syscall: got response \n"));
    698  1.1  pooka 			gotresp = 1;
    699  1.1  pooka 			break;
    700  1.1  pooka 		}
    701  1.1  pooka 	}
    702  1.1  pooka 
    703  1.1  pooka 	/*LINTED*/
    704  1.1  pooka 	resp = (struct rsp_sysresp *)clispc.spc_buf;
    705  1.1  pooka 	memcpy(retval, &resp->rsys_retval, sizeof(resp->rsys_retval));
    706  1.1  pooka 	clispc.spc_off = 0;
    707  1.1  pooka 
    708  1.1  pooka 	return resp->rsys_error;
    709  1.1  pooka }
    710  1.1  pooka 
    711  1.1  pooka /*
    712  1.1  pooka  *
    713  1.1  pooka  * Startup routines and mainloop for server.
    714  1.1  pooka  *
    715  1.1  pooka  */
    716  1.1  pooka 
    717  1.1  pooka static int
    718  1.1  pooka tcp_parse(const char *addr, int type, struct sockaddr **sa)
    719  1.1  pooka {
    720  1.1  pooka 	struct sockaddr_in sin;
    721  1.1  pooka 	char buf[64];
    722  1.1  pooka 	const char *p;
    723  1.1  pooka 	size_t l;
    724  1.1  pooka 	int port;
    725  1.1  pooka 
    726  1.1  pooka 	memset(&sin, 0, sizeof(sin));
    727  1.1  pooka 	sin.sin_len = sizeof(sin);
    728  1.1  pooka 	sin.sin_family = AF_INET;
    729  1.1  pooka 
    730  1.1  pooka 	p = strchr(addr, ':');
    731  1.1  pooka 	if (!p) {
    732  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: missing port specifier\n");
    733  1.1  pooka 		return EINVAL;
    734  1.1  pooka 	}
    735  1.1  pooka 
    736  1.1  pooka 	l = p - addr;
    737  1.1  pooka 	if (l > sizeof(buf)-1) {
    738  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: address too long\n");
    739  1.1  pooka 		return EINVAL;
    740  1.1  pooka 	}
    741  1.1  pooka 	strncpy(buf, addr, l);
    742  1.1  pooka 	buf[l] = '\0';
    743  1.1  pooka 
    744  1.1  pooka 	/* special INADDR_ANY treatment */
    745  1.1  pooka 	if (strcmp(buf, "*") == 0 || strcmp(buf, "0") == 0) {
    746  1.1  pooka 		sin.sin_addr.s_addr = INADDR_ANY;
    747  1.1  pooka 	} else {
    748  1.1  pooka 		switch (inet_pton(AF_INET, buf, &sin.sin_addr)) {
    749  1.1  pooka 		case 1:
    750  1.1  pooka 			break;
    751  1.1  pooka 		case 0:
    752  1.1  pooka 			fprintf(stderr, "rump_sp_tcp: cannot parse %s\n", buf);
    753  1.1  pooka 			return EINVAL;
    754  1.1  pooka 		case -1:
    755  1.1  pooka 			fprintf(stderr, "rump_sp_tcp: inet_pton failed\n");
    756  1.1  pooka 			return errno;
    757  1.1  pooka 		default:
    758  1.1  pooka 			assert(/*CONSTCOND*/0);
    759  1.1  pooka 			return EINVAL;
    760  1.1  pooka 		}
    761  1.1  pooka 	}
    762  1.1  pooka 
    763  1.1  pooka 	if (type == RUMP_SP_CLIENT && sin.sin_addr.s_addr == INADDR_ANY) {
    764  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: client needs !INADDR_ANY\n");
    765  1.1  pooka 		return EINVAL;
    766  1.1  pooka 	}
    767  1.1  pooka 
    768  1.1  pooka 	/* advance to port number & parse */
    769  1.1  pooka 	p++;
    770  1.1  pooka 	l = strspn(p, "0123456789");
    771  1.1  pooka 	if (l == 0) {
    772  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: port now found: %s\n", p);
    773  1.1  pooka 		return EINVAL;
    774  1.1  pooka 	}
    775  1.1  pooka 	strncpy(buf, p, l);
    776  1.1  pooka 	buf[l] = '\0';
    777  1.1  pooka 
    778  1.1  pooka 	if (*(p+l) != '/' && *(p+l) != '\0') {
    779  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: junk at end of port: %s\n", addr);
    780  1.1  pooka 		return EINVAL;
    781  1.1  pooka 	}
    782  1.1  pooka 
    783  1.1  pooka 	port = atoi(buf);
    784  1.1  pooka 	if (port < 0 || port >= (1<<(8*sizeof(in_port_t)))) {
    785  1.1  pooka 		fprintf(stderr, "rump_sp_tcp: port %d out of range\n", port);
    786  1.1  pooka 		return ERANGE;
    787  1.1  pooka 	}
    788  1.1  pooka 	sin.sin_port = htons(port);
    789  1.1  pooka 
    790  1.1  pooka 	*sa = malloc(sizeof(sin));
    791  1.1  pooka 	if (*sa == NULL)
    792  1.1  pooka 		return errno;
    793  1.1  pooka 	memcpy(*sa, &sin, sizeof(sin));
    794  1.1  pooka 	return 0;
    795  1.1  pooka }
    796  1.1  pooka 
    797  1.1  pooka static int
    798  1.1  pooka tcp_connecthook(int s)
    799  1.1  pooka {
    800  1.1  pooka 	int x;
    801  1.1  pooka 
    802  1.1  pooka 	x = 1;
    803  1.1  pooka 	setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
    804  1.1  pooka 
    805  1.1  pooka 	return 0;
    806  1.1  pooka }
    807  1.1  pooka 
    808  1.1  pooka /*ARGSUSED*/
    809  1.1  pooka static int
    810  1.1  pooka notsupp(void)
    811  1.1  pooka {
    812  1.1  pooka 
    813  1.1  pooka 	fprintf(stderr, "rump_sp: support not yet implemented\n");
    814  1.1  pooka 	return EOPNOTSUPP;
    815  1.1  pooka }
    816  1.1  pooka 
    817  1.1  pooka static int
    818  1.1  pooka success(void)
    819  1.1  pooka {
    820  1.1  pooka 
    821  1.1  pooka 	return 0;
    822  1.1  pooka }
    823  1.1  pooka 
    824  1.1  pooka struct {
    825  1.1  pooka 	const char *id;
    826  1.1  pooka 	int domain;
    827  1.1  pooka 	addrparse_fn ap;
    828  1.1  pooka 	connecthook_fn connhook;
    829  1.1  pooka } parsetab[] = {
    830  1.1  pooka 	{ "tcp", PF_INET, tcp_parse, tcp_connecthook },
    831  1.1  pooka 	{ "unix", PF_LOCAL, (addrparse_fn)notsupp, (connecthook_fn)success },
    832  1.1  pooka 	{ "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success },
    833  1.1  pooka };
    834  1.1  pooka #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0]))
    835  1.1  pooka 
    836  1.1  pooka struct spservarg {
    837  1.1  pooka 	int sps_sock;
    838  1.1  pooka 	connecthook_fn sps_connhook;
    839  1.1  pooka };
    840  1.1  pooka 
    841  1.1  pooka static void *
    842  1.1  pooka spserver(void *arg)
    843  1.1  pooka {
    844  1.1  pooka 	struct spservarg *sarg = arg;
    845  1.1  pooka 	unsigned idx;
    846  1.1  pooka 	int seen;
    847  1.1  pooka 	int rv;
    848  1.1  pooka 
    849  1.1  pooka 	for (idx = 1; idx < MAXCLI; idx++) {
    850  1.1  pooka 		pfdlist[idx].fd = -1;
    851  1.1  pooka 		pfdlist[idx].events = POLLIN;
    852  1.1  pooka 	}
    853  1.1  pooka 	pfdlist[0].fd = sarg->sps_sock;
    854  1.1  pooka 	pfdlist[0].events = POLLIN;
    855  1.1  pooka 	nfds = 1;
    856  1.1  pooka 	maxidx = 0;
    857  1.1  pooka 
    858  1.1  pooka 	DPRINTF(("rump_sp: server mainloop\n"));
    859  1.1  pooka 
    860  1.1  pooka 	for (;;) {
    861  1.1  pooka 		DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
    862  1.1  pooka 		seen = 0;
    863  1.1  pooka 		rv = poll(pfdlist, maxidx+1, INFTIM);
    864  1.1  pooka 		assert(maxidx+1 <= MAXCLI);
    865  1.1  pooka 		assert(rv != 0);
    866  1.1  pooka 		if (rv == -1) {
    867  1.1  pooka 			if (errno == EINTR)
    868  1.1  pooka 				continue;
    869  1.1  pooka 			fprintf(stderr, "rump_spserver: poll returned %d\n",
    870  1.1  pooka 			    errno);
    871  1.1  pooka 			break;
    872  1.1  pooka 		}
    873  1.1  pooka 
    874  1.1  pooka 		for (idx = 0; seen < rv; idx++) {
    875  1.1  pooka 			assert(idx < MAXCLI);
    876  1.1  pooka 
    877  1.1  pooka 			if ((pfdlist[idx].revents & POLLIN) == 0)
    878  1.1  pooka 				continue;
    879  1.1  pooka 
    880  1.1  pooka 			seen++;
    881  1.1  pooka 			DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
    882  1.1  pooka 			    idx, seen, rv));
    883  1.1  pooka 			if (idx > 0) {
    884  1.1  pooka 				struct spclient *spc = &spclist[idx];
    885  1.1  pooka 
    886  1.1  pooka 				DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
    887  1.1  pooka 				switch (readframe(spc)) {
    888  1.1  pooka 				case 0:
    889  1.1  pooka 					break;
    890  1.1  pooka 				case -1:
    891  1.2  pooka 					serv_handledisco(idx);
    892  1.1  pooka 					break;
    893  1.1  pooka 				default:
    894  1.1  pooka 					spc->spc_off = 0;
    895  1.1  pooka 					serv_handlesyscall(spc,
    896  1.1  pooka 					    &spc->spc_hdr, spc->spc_buf);
    897  1.1  pooka 					spc->spc_buf = NULL;
    898  1.1  pooka 					break;
    899  1.1  pooka 				}
    900  1.1  pooka 			} else {
    901  1.1  pooka 				DPRINTF(("rump_sp: mainloop new connection\n"));
    902  1.1  pooka 				serv_handleconn(pfdlist[0].fd,
    903  1.1  pooka 				    sarg->sps_connhook);
    904  1.1  pooka 			}
    905  1.1  pooka 		}
    906  1.1  pooka 	}
    907  1.1  pooka 
    908  1.1  pooka 	return NULL;
    909  1.1  pooka }
    910  1.1  pooka 
    911  1.1  pooka int
    912  1.3  pooka rumpuser_sp_init(const struct rumpuser_sp_ops *spopsp, int *typep)
    913  1.1  pooka {
    914  1.1  pooka 	struct spservarg *sarg;
    915  1.1  pooka 	struct sockaddr *sap;
    916  1.1  pooka 	char id[16];
    917  1.1  pooka 	char *p, *p2;
    918  1.1  pooka 	size_t l;
    919  1.1  pooka 	unsigned i;
    920  1.1  pooka 	int error, type, s;
    921  1.1  pooka 
    922  1.1  pooka 	p = NULL;
    923  1.1  pooka 	error = 0;
    924  1.3  pooka 	spops = *spopsp;
    925  1.1  pooka 
    926  1.1  pooka 	type = RUMP_SP_NONE;
    927  1.1  pooka 	if ((p = getenv("RUMP_SP_SERVER")) != NULL) {
    928  1.1  pooka 		type = RUMP_SP_SERVER;
    929  1.1  pooka 	}
    930  1.1  pooka 	if ((p2 = getenv("RUMP_SP_CLIENT")) != NULL) {
    931  1.1  pooka 		if (type != RUMP_SP_NONE)
    932  1.1  pooka 			return EEXIST;
    933  1.1  pooka 		type = RUMP_SP_CLIENT;
    934  1.1  pooka 		p = p2;
    935  1.1  pooka 	}
    936  1.1  pooka 
    937  1.1  pooka 	/*
    938  1.1  pooka 	 * Parse the url
    939  1.1  pooka 	 */
    940  1.1  pooka 
    941  1.1  pooka 	if (type == RUMP_SP_NONE)
    942  1.1  pooka 		return RUMP_SP_NONE;
    943  1.1  pooka 
    944  1.1  pooka 	p2 = strstr(p, "://");
    945  1.1  pooka 	if (!p2) {
    946  1.1  pooka 		fprintf(stderr, "rump_sp: invalid locator ``%s''\n", p);
    947  1.1  pooka 		return EINVAL;
    948  1.1  pooka 	}
    949  1.1  pooka 	l = p2-p;
    950  1.1  pooka 	if (l > sizeof(id)-1) {
    951  1.1  pooka 		fprintf(stderr, "rump_sp: identifier too long in ``%s''\n", p);
    952  1.1  pooka 		return EINVAL;
    953  1.1  pooka 	}
    954  1.1  pooka 
    955  1.1  pooka 	strncpy(id, p, l);
    956  1.1  pooka 	id[l] = '\0';
    957  1.1  pooka 	p2 += 3; /* beginning of address */
    958  1.1  pooka 
    959  1.1  pooka 	for (i = 0; i < NPARSE; i++) {
    960  1.1  pooka 		if (strcmp(id, parsetab[i].id) == 0) {
    961  1.1  pooka 			error = parsetab[i].ap(p2, type, &sap);
    962  1.1  pooka 			if (error)
    963  1.1  pooka 				return error;
    964  1.1  pooka 			break;
    965  1.1  pooka 		}
    966  1.1  pooka 	}
    967  1.1  pooka 	if (i == NPARSE) {
    968  1.1  pooka 		fprintf(stderr, "rump_sp: invalid identifier ``%s''\n", p);
    969  1.1  pooka 		return EINVAL;
    970  1.1  pooka 	}
    971  1.1  pooka 
    972  1.1  pooka 	s = socket(parsetab[i].domain, SOCK_STREAM, 0);
    973  1.1  pooka 	if (s == -1)
    974  1.1  pooka 		return errno;
    975  1.1  pooka 
    976  1.1  pooka 	if (type == RUMP_SP_CLIENT) {
    977  1.1  pooka 		/*LINTED*/
    978  1.1  pooka 		if (connect(s, sap, sap->sa_len) == -1) {
    979  1.1  pooka 			fprintf(stderr, "rump_sp: client connect failed\n");
    980  1.1  pooka 			return errno;
    981  1.1  pooka 		}
    982  1.1  pooka 		if ((error = parsetab[i].connhook(s)) != 0) {
    983  1.1  pooka 			fprintf(stderr, "rump_sp: connect hook failed\n");
    984  1.1  pooka 			return error;
    985  1.1  pooka 		}
    986  1.1  pooka 
    987  1.1  pooka 		clispc.spc_fd = s;
    988  1.1  pooka 	} else {
    989  1.1  pooka 		pthread_t pt;
    990  1.1  pooka 
    991  1.1  pooka 		sarg = malloc(sizeof(*sarg));
    992  1.1  pooka 		if (sarg == NULL) {
    993  1.1  pooka 			close(s);
    994  1.1  pooka 			return ENOMEM;
    995  1.1  pooka 		}
    996  1.1  pooka 
    997  1.1  pooka 		sarg->sps_sock = s;
    998  1.1  pooka 		sarg->sps_connhook = parsetab[i].connhook;
    999  1.1  pooka 
   1000  1.1  pooka 		/* sloppy error recovery */
   1001  1.1  pooka 
   1002  1.1  pooka 		error = pthread_key_create(&spclient_tls, NULL);
   1003  1.1  pooka 		if (error) {
   1004  1.1  pooka 			fprintf(stderr, "rump_sp: tls create failed\n");
   1005  1.1  pooka 			return error;
   1006  1.1  pooka 		}
   1007  1.1  pooka 
   1008  1.1  pooka 		/*LINTED*/
   1009  1.1  pooka 		if (bind(s, sap, sap->sa_len) == -1) {
   1010  1.1  pooka 			fprintf(stderr, "rump_sp: server bind failed\n");
   1011  1.1  pooka 			return errno;
   1012  1.1  pooka 		}
   1013  1.1  pooka 		if (listen(s, 20) == -1) {
   1014  1.1  pooka 			fprintf(stderr, "rump_sp: server listen failed\n");
   1015  1.1  pooka 			return errno;
   1016  1.1  pooka 		}
   1017  1.1  pooka 
   1018  1.1  pooka 		if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
   1019  1.1  pooka 			fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
   1020  1.1  pooka 			return errno;
   1021  1.1  pooka 		}
   1022  1.1  pooka 		pthread_detach(pt);
   1023  1.1  pooka 	}
   1024  1.1  pooka 
   1025  1.1  pooka 	*typep = type;
   1026  1.1  pooka 	return 0;
   1027  1.1  pooka }
   1028