Home | History | Annotate | Line # | Download | only in librumpuser
sp_common.c revision 1.31.4.3
      1  1.31.4.3   yamt /*      $NetBSD: sp_common.c,v 1.31.4.3 2013/01/23 00:05:27 yamt Exp $	*/
      2       1.1  pooka 
      3       1.1  pooka /*
      4      1.18  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  * Common client/server sysproxy routines.  #included.
     30       1.1  pooka  */
     31       1.1  pooka 
     32  1.31.4.1   yamt #include "rumpuser_port.h"
     33       1.1  pooka 
     34       1.1  pooka #include <sys/types.h>
     35       1.1  pooka #include <sys/mman.h>
     36       1.4  pooka #include <sys/queue.h>
     37       1.1  pooka #include <sys/socket.h>
     38       1.2  pooka #include <sys/un.h>
     39       1.1  pooka 
     40       1.1  pooka #include <arpa/inet.h>
     41       1.1  pooka #include <netinet/in.h>
     42       1.1  pooka #include <netinet/tcp.h>
     43       1.1  pooka 
     44       1.1  pooka #include <assert.h>
     45       1.1  pooka #include <errno.h>
     46       1.1  pooka #include <fcntl.h>
     47      1.13  pooka #include <inttypes.h>
     48  1.31.4.1   yamt #include <limits.h>
     49       1.1  pooka #include <poll.h>
     50       1.4  pooka #include <pthread.h>
     51       1.1  pooka #include <stdarg.h>
     52      1.11  pooka #include <stddef.h>
     53       1.1  pooka #include <stdio.h>
     54       1.1  pooka #include <stdlib.h>
     55       1.1  pooka #include <string.h>
     56       1.1  pooka #include <unistd.h>
     57       1.1  pooka 
     58  1.31.4.1   yamt /*
     59  1.31.4.1   yamt  * XXX: NetBSD's __unused collides with Linux headers, so we cannot
     60  1.31.4.1   yamt  * define it before we've included everything.
     61  1.31.4.1   yamt  */
     62  1.31.4.1   yamt #if !defined(__unused) && defined(__GNUC__)
     63  1.31.4.1   yamt #define __unused __attribute__((__unused__))
     64  1.31.4.1   yamt #endif
     65  1.31.4.1   yamt 
     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.20  pooka #ifndef HOSTOPS
     83      1.20  pooka #define host_poll poll
     84      1.20  pooka #define host_read read
     85      1.31  pooka #define host_sendmsg sendmsg
     86      1.20  pooka #define host_setsockopt setsockopt
     87      1.20  pooka #endif
     88      1.20  pooka 
     89  1.31.4.2   yamt #define IOVPUT(_io_, _b_) _io_.iov_base = 			\
     90  1.31.4.2   yamt     (void *)&_b_; _io_.iov_len = sizeof(_b_);
     91  1.31.4.2   yamt #define IOVPUT_WITHSIZE(_io_, _b_, _l_) _io_.iov_base =		\
     92  1.31.4.2   yamt     (void *)(_b_); _io_.iov_len = _l_;
     93      1.31  pooka #define SENDIOV(_spc_, _iov_) dosend(_spc_, _iov_, __arraycount(_iov_))
     94      1.31  pooka 
     95       1.1  pooka /*
     96       1.1  pooka  * Bah, I hate writing on-off-wire conversions in C
     97       1.1  pooka  */
     98       1.1  pooka 
     99      1.13  pooka enum { RUMPSP_REQ, RUMPSP_RESP, RUMPSP_ERROR };
    100      1.17  pooka enum {	RUMPSP_HANDSHAKE,
    101      1.17  pooka 	RUMPSP_SYSCALL,
    102      1.10  pooka 	RUMPSP_COPYIN, RUMPSP_COPYINSTR,
    103      1.10  pooka 	RUMPSP_COPYOUT, RUMPSP_COPYOUTSTR,
    104      1.18  pooka 	RUMPSP_ANONMMAP,
    105      1.24  pooka 	RUMPSP_PREFORK,
    106      1.24  pooka 	RUMPSP_RAISE };
    107       1.1  pooka 
    108      1.28  pooka enum { HANDSHAKE_GUEST, HANDSHAKE_AUTH, HANDSHAKE_FORK, HANDSHAKE_EXEC };
    109      1.18  pooka 
    110  1.31.4.1   yamt /*
    111  1.31.4.1   yamt  * error types used for RUMPSP_ERROR
    112  1.31.4.1   yamt  */
    113  1.31.4.1   yamt enum rumpsp_err { RUMPSP_ERR_NONE = 0, RUMPSP_ERR_TRYAGAIN, RUMPSP_ERR_AUTH,
    114  1.31.4.1   yamt 	RUMPSP_ERR_INVALID_PREFORK, RUMPSP_ERR_RFORK_FAILED,
    115  1.31.4.1   yamt 	RUMPSP_ERR_INEXEC, RUMPSP_ERR_NOMEM, RUMPSP_ERR_MALFORMED_REQUEST };
    116  1.31.4.1   yamt 
    117  1.31.4.1   yamt /*
    118  1.31.4.1   yamt  * The mapping of the above types to errno.  They are almost never exposed
    119  1.31.4.1   yamt  * to the client after handshake (except for a server resource shortage
    120  1.31.4.1   yamt  * and the client trying to be funny).  This is a function instead of
    121  1.31.4.1   yamt  * an array to catch missing values.  Theoretically, the compiled code
    122  1.31.4.1   yamt  * should be the same.
    123  1.31.4.1   yamt  */
    124  1.31.4.1   yamt static int
    125  1.31.4.1   yamt errmap(enum rumpsp_err error)
    126  1.31.4.1   yamt {
    127  1.31.4.1   yamt 
    128  1.31.4.1   yamt 	switch (error) {
    129  1.31.4.1   yamt 	/* XXX: no EAUTH on Linux */
    130  1.31.4.1   yamt 	case RUMPSP_ERR_NONE:			return 0;
    131  1.31.4.1   yamt 	case RUMPSP_ERR_AUTH:			return EPERM;
    132  1.31.4.1   yamt 	case RUMPSP_ERR_TRYAGAIN:		return EAGAIN;
    133  1.31.4.1   yamt 	case RUMPSP_ERR_INVALID_PREFORK:	return ESRCH;
    134  1.31.4.1   yamt 	case RUMPSP_ERR_RFORK_FAILED:		return EIO; /* got a light? */
    135  1.31.4.1   yamt 	case RUMPSP_ERR_INEXEC:			return EBUSY;
    136  1.31.4.1   yamt 	case RUMPSP_ERR_NOMEM:			return ENOMEM;
    137  1.31.4.1   yamt 	case RUMPSP_ERR_MALFORMED_REQUEST:	return EINVAL;
    138  1.31.4.1   yamt 	}
    139  1.31.4.1   yamt 
    140  1.31.4.1   yamt 	return -1;
    141  1.31.4.1   yamt }
    142  1.31.4.1   yamt 
    143      1.18  pooka #define AUTHLEN 4 /* 128bit fork auth */
    144      1.17  pooka 
    145       1.1  pooka struct rsp_hdr {
    146       1.1  pooka 	uint64_t rsp_len;
    147       1.1  pooka 	uint64_t rsp_reqno;
    148       1.4  pooka 	uint16_t rsp_class;
    149       1.4  pooka 	uint16_t rsp_type;
    150       1.1  pooka 	/*
    151       1.1  pooka 	 * We want this structure 64bit-aligned for typecast fun,
    152       1.1  pooka 	 * so might as well use the following for something.
    153       1.1  pooka 	 */
    154      1.13  pooka 	union {
    155      1.13  pooka 		uint32_t sysnum;
    156      1.13  pooka 		uint32_t error;
    157      1.17  pooka 		uint32_t handshake;
    158      1.24  pooka 		uint32_t signo;
    159      1.13  pooka 	} u;
    160       1.1  pooka };
    161       1.1  pooka #define HDRSZ sizeof(struct rsp_hdr)
    162      1.13  pooka #define rsp_sysnum u.sysnum
    163      1.13  pooka #define rsp_error u.error
    164      1.17  pooka #define rsp_handshake u.handshake
    165      1.24  pooka #define rsp_signo u.signo
    166       1.1  pooka 
    167      1.16  pooka #define MAXBANNER 96
    168      1.16  pooka 
    169       1.1  pooka /*
    170       1.1  pooka  * Data follows the header.  We have two types of structured data.
    171       1.1  pooka  */
    172       1.1  pooka 
    173       1.1  pooka /* copyin/copyout */
    174       1.1  pooka struct rsp_copydata {
    175       1.1  pooka 	size_t rcp_len;
    176       1.1  pooka 	void *rcp_addr;
    177       1.1  pooka 	uint8_t rcp_data[0];
    178       1.1  pooka };
    179       1.1  pooka 
    180       1.1  pooka /* syscall response */
    181       1.1  pooka struct rsp_sysresp {
    182       1.1  pooka 	int rsys_error;
    183       1.1  pooka 	register_t rsys_retval[2];
    184       1.1  pooka };
    185       1.1  pooka 
    186      1.18  pooka struct handshake_fork {
    187      1.18  pooka 	uint32_t rf_auth[4];
    188      1.18  pooka 	int rf_cancel;
    189      1.18  pooka };
    190      1.18  pooka 
    191       1.4  pooka struct respwait {
    192       1.4  pooka 	uint64_t rw_reqno;
    193       1.4  pooka 	void *rw_data;
    194       1.4  pooka 	size_t rw_dlen;
    195      1.21  pooka 	int rw_done;
    196      1.13  pooka 	int rw_error;
    197       1.4  pooka 
    198       1.4  pooka 	pthread_cond_t rw_cv;
    199       1.4  pooka 
    200       1.4  pooka 	TAILQ_ENTRY(respwait) rw_entries;
    201       1.4  pooka };
    202       1.1  pooka 
    203      1.18  pooka struct prefork;
    204       1.1  pooka struct spclient {
    205       1.1  pooka 	int spc_fd;
    206       1.7  pooka 	int spc_refcnt;
    207      1.17  pooka 	int spc_state;
    208       1.6  pooka 
    209      1.11  pooka 	pthread_mutex_t spc_mtx;
    210      1.11  pooka 	pthread_cond_t spc_cv;
    211      1.11  pooka 
    212       1.6  pooka 	struct lwp *spc_mainlwp;
    213       1.6  pooka 	pid_t spc_pid;
    214       1.1  pooka 
    215      1.11  pooka 	TAILQ_HEAD(, respwait) spc_respwait;
    216      1.11  pooka 
    217      1.11  pooka 	/* rest of the fields are zeroed upon disconnect */
    218      1.12  pooka #define SPC_ZEROFF offsetof(struct spclient, spc_pfd)
    219       1.7  pooka 	struct pollfd *spc_pfd;
    220       1.7  pooka 
    221       1.1  pooka 	struct rsp_hdr spc_hdr;
    222       1.1  pooka 	uint8_t *spc_buf;
    223       1.1  pooka 	size_t spc_off;
    224       1.1  pooka 
    225       1.4  pooka 	uint64_t spc_nextreq;
    226      1.25  pooka 	uint64_t spc_syscallreq;
    227      1.26  pooka 	uint64_t spc_generation;
    228       1.4  pooka 	int spc_ostatus, spc_istatus;
    229      1.26  pooka 	int spc_reconnecting;
    230      1.30  pooka 	int spc_inexec;
    231      1.18  pooka 
    232      1.18  pooka 	LIST_HEAD(, prefork) spc_pflist;
    233       1.1  pooka };
    234       1.4  pooka #define SPCSTATUS_FREE 0
    235       1.4  pooka #define SPCSTATUS_BUSY 1
    236       1.4  pooka #define SPCSTATUS_WANTED 2
    237       1.1  pooka 
    238      1.17  pooka #define SPCSTATE_NEW     0
    239      1.17  pooka #define SPCSTATE_RUNNING 1
    240      1.17  pooka #define SPCSTATE_DYING   2
    241      1.17  pooka 
    242       1.1  pooka typedef int (*addrparse_fn)(const char *, struct sockaddr **, int);
    243       1.1  pooka typedef int (*connecthook_fn)(int);
    244      1.15  pooka typedef void (*cleanup_fn)(struct sockaddr *);
    245       1.1  pooka 
    246       1.4  pooka static int readframe(struct spclient *);
    247       1.4  pooka static void handlereq(struct spclient *);
    248       1.4  pooka 
    249      1.13  pooka static __inline void
    250      1.13  pooka spcresetbuf(struct spclient *spc)
    251      1.13  pooka {
    252      1.13  pooka 
    253      1.13  pooka 	spc->spc_buf = NULL;
    254      1.13  pooka 	spc->spc_off = 0;
    255      1.13  pooka }
    256      1.13  pooka 
    257      1.13  pooka static __inline void
    258      1.13  pooka spcfreebuf(struct spclient *spc)
    259      1.13  pooka {
    260      1.13  pooka 
    261      1.13  pooka 	free(spc->spc_buf);
    262      1.13  pooka 	spcresetbuf(spc);
    263      1.13  pooka }
    264      1.13  pooka 
    265       1.4  pooka static void
    266      1.12  pooka sendlockl(struct spclient *spc)
    267       1.4  pooka {
    268       1.4  pooka 
    269       1.4  pooka 	while (spc->spc_ostatus != SPCSTATUS_FREE) {
    270       1.4  pooka 		spc->spc_ostatus = SPCSTATUS_WANTED;
    271       1.4  pooka 		pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
    272       1.4  pooka 	}
    273       1.4  pooka 	spc->spc_ostatus = SPCSTATUS_BUSY;
    274      1.12  pooka }
    275      1.12  pooka 
    276      1.26  pooka static void __unused
    277      1.12  pooka sendlock(struct spclient *spc)
    278      1.12  pooka {
    279      1.12  pooka 
    280      1.12  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    281      1.12  pooka 	sendlockl(spc);
    282       1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    283       1.4  pooka }
    284       1.4  pooka 
    285       1.4  pooka static void
    286      1.12  pooka sendunlockl(struct spclient *spc)
    287       1.4  pooka {
    288       1.4  pooka 
    289       1.4  pooka 	if (spc->spc_ostatus == SPCSTATUS_WANTED)
    290       1.4  pooka 		pthread_cond_broadcast(&spc->spc_cv);
    291       1.4  pooka 	spc->spc_ostatus = SPCSTATUS_FREE;
    292      1.12  pooka }
    293      1.12  pooka 
    294      1.12  pooka static void
    295      1.12  pooka sendunlock(struct spclient *spc)
    296      1.12  pooka {
    297      1.12  pooka 
    298      1.12  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    299      1.12  pooka 	sendunlockl(spc);
    300       1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    301       1.4  pooka }
    302       1.1  pooka 
    303       1.1  pooka static int
    304      1.31  pooka dosend(struct spclient *spc, struct iovec *iov, size_t iovlen)
    305       1.1  pooka {
    306      1.31  pooka 	struct msghdr msg;
    307       1.1  pooka 	struct pollfd pfd;
    308      1.31  pooka 	ssize_t n = 0;
    309       1.1  pooka 	int fd = spc->spc_fd;
    310       1.1  pooka 
    311       1.1  pooka 	pfd.fd = fd;
    312       1.1  pooka 	pfd.events = POLLOUT;
    313       1.1  pooka 
    314      1.31  pooka 	memset(&msg, 0, sizeof(msg));
    315      1.31  pooka 
    316      1.31  pooka 	for (;;) {
    317      1.31  pooka 		/* not first round?  poll */
    318       1.1  pooka 		if (n) {
    319      1.20  pooka 			if (host_poll(&pfd, 1, INFTIM) == -1) {
    320       1.1  pooka 				if (errno == EINTR)
    321       1.1  pooka 					continue;
    322       1.1  pooka 				return errno;
    323       1.1  pooka 			}
    324       1.1  pooka 		}
    325       1.1  pooka 
    326      1.31  pooka 		msg.msg_iov = iov;
    327      1.31  pooka 		msg.msg_iovlen = iovlen;
    328      1.31  pooka 		n = host_sendmsg(fd, &msg, MSG_NOSIGNAL);
    329      1.10  pooka 		if (n == -1)  {
    330      1.26  pooka 			if (errno == EPIPE)
    331      1.26  pooka 				return ENOTCONN;
    332      1.10  pooka 			if (errno != EAGAIN)
    333      1.18  pooka 				return errno;
    334      1.10  pooka 			continue;
    335       1.1  pooka 		}
    336      1.26  pooka 		if (n == 0) {
    337      1.26  pooka 			return ENOTCONN;
    338      1.26  pooka 		}
    339      1.31  pooka 
    340      1.31  pooka 		/* ok, need to adjust iovec for potential next round */
    341      1.31  pooka 		while (n >= (ssize_t)iov[0].iov_len && iovlen) {
    342      1.31  pooka 			n -= iov[0].iov_len;
    343      1.31  pooka 			iov++;
    344      1.31  pooka 			iovlen--;
    345      1.31  pooka 		}
    346      1.31  pooka 
    347      1.31  pooka 		if (iovlen == 0) {
    348      1.31  pooka 			_DIAGASSERT(n == 0);
    349      1.31  pooka 			break;
    350      1.31  pooka 		} else {
    351  1.31.4.2   yamt 			iov[0].iov_base =
    352  1.31.4.2   yamt 			    (void *)((uint8_t *)iov[0].iov_base + n);
    353      1.31  pooka 			iov[0].iov_len -= n;
    354      1.31  pooka 		}
    355       1.1  pooka 	}
    356       1.1  pooka 
    357       1.1  pooka 	return 0;
    358       1.1  pooka }
    359       1.1  pooka 
    360       1.4  pooka static void
    361      1.26  pooka doputwait(struct spclient *spc, struct respwait *rw, struct rsp_hdr *rhdr)
    362       1.4  pooka {
    363       1.4  pooka 
    364       1.4  pooka 	rw->rw_data = NULL;
    365      1.22  pooka 	rw->rw_dlen = rw->rw_done = rw->rw_error = 0;
    366       1.4  pooka 	pthread_cond_init(&rw->rw_cv, NULL);
    367       1.4  pooka 
    368       1.4  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    369       1.4  pooka 	rw->rw_reqno = rhdr->rsp_reqno = spc->spc_nextreq++;
    370       1.4  pooka 	TAILQ_INSERT_TAIL(&spc->spc_respwait, rw, rw_entries);
    371      1.26  pooka }
    372      1.26  pooka 
    373      1.26  pooka static void __unused
    374      1.26  pooka putwait_locked(struct spclient *spc, struct respwait *rw, struct rsp_hdr *rhdr)
    375      1.26  pooka {
    376      1.26  pooka 
    377      1.26  pooka 	doputwait(spc, rw, rhdr);
    378      1.26  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    379      1.26  pooka }
    380      1.26  pooka 
    381      1.26  pooka static void
    382      1.26  pooka putwait(struct spclient *spc, struct respwait *rw, struct rsp_hdr *rhdr)
    383      1.26  pooka {
    384      1.12  pooka 
    385      1.26  pooka 	doputwait(spc, rw, rhdr);
    386      1.12  pooka 	sendlockl(spc);
    387      1.23  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    388       1.8  pooka }
    389       1.8  pooka 
    390       1.8  pooka static void
    391      1.26  pooka dounputwait(struct spclient *spc, struct respwait *rw)
    392      1.26  pooka {
    393      1.26  pooka 
    394      1.26  pooka 	TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
    395      1.26  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    396      1.26  pooka 	pthread_cond_destroy(&rw->rw_cv);
    397      1.26  pooka 
    398      1.26  pooka }
    399      1.26  pooka 
    400      1.26  pooka static void __unused
    401      1.26  pooka unputwait_locked(struct spclient *spc, struct respwait *rw)
    402      1.26  pooka {
    403      1.26  pooka 
    404      1.26  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    405      1.26  pooka 	dounputwait(spc, rw);
    406      1.26  pooka }
    407      1.26  pooka 
    408      1.26  pooka static void
    409       1.8  pooka unputwait(struct spclient *spc, struct respwait *rw)
    410       1.8  pooka {
    411       1.8  pooka 
    412      1.23  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    413      1.12  pooka 	sendunlockl(spc);
    414      1.12  pooka 
    415      1.26  pooka 	dounputwait(spc, rw);
    416       1.4  pooka }
    417       1.4  pooka 
    418       1.4  pooka static void
    419       1.4  pooka kickwaiter(struct spclient *spc)
    420       1.4  pooka {
    421       1.4  pooka 	struct respwait *rw;
    422      1.22  pooka 	int error = 0;
    423       1.4  pooka 
    424       1.4  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    425       1.4  pooka 	TAILQ_FOREACH(rw, &spc->spc_respwait, rw_entries) {
    426       1.4  pooka 		if (rw->rw_reqno == spc->spc_hdr.rsp_reqno)
    427       1.4  pooka 			break;
    428       1.4  pooka 	}
    429       1.4  pooka 	if (rw == NULL) {
    430      1.13  pooka 		DPRINTF(("no waiter found, invalid reqno %" PRIu64 "?\n",
    431      1.13  pooka 		    spc->spc_hdr.rsp_reqno));
    432      1.23  pooka 		pthread_mutex_unlock(&spc->spc_mtx);
    433      1.18  pooka 		spcfreebuf(spc);
    434       1.4  pooka 		return;
    435       1.4  pooka 	}
    436      1.10  pooka 	DPRINTF(("rump_sp: client %p woke up waiter at %p\n", spc, rw));
    437       1.4  pooka 	rw->rw_data = spc->spc_buf;
    438      1.21  pooka 	rw->rw_done = 1;
    439      1.11  pooka 	rw->rw_dlen = (size_t)(spc->spc_off - HDRSZ);
    440      1.13  pooka 	if (spc->spc_hdr.rsp_class == RUMPSP_ERROR) {
    441  1.31.4.1   yamt 		error = rw->rw_error = errmap(spc->spc_hdr.rsp_error);
    442      1.13  pooka 	}
    443       1.4  pooka 	pthread_cond_signal(&rw->rw_cv);
    444       1.4  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    445       1.4  pooka 
    446      1.14  pooka 	if (error)
    447      1.13  pooka 		spcfreebuf(spc);
    448      1.13  pooka 	else
    449      1.13  pooka 		spcresetbuf(spc);
    450       1.4  pooka }
    451       1.4  pooka 
    452       1.4  pooka static void
    453       1.4  pooka kickall(struct spclient *spc)
    454       1.4  pooka {
    455       1.4  pooka 	struct respwait *rw;
    456       1.4  pooka 
    457       1.4  pooka 	/* DIAGASSERT(mutex_owned(spc_lock)) */
    458       1.4  pooka 	TAILQ_FOREACH(rw, &spc->spc_respwait, rw_entries)
    459      1.12  pooka 		pthread_cond_broadcast(&rw->rw_cv);
    460       1.4  pooka }
    461       1.4  pooka 
    462       1.4  pooka static int
    463       1.1  pooka readframe(struct spclient *spc)
    464       1.1  pooka {
    465       1.1  pooka 	int fd = spc->spc_fd;
    466       1.1  pooka 	size_t left;
    467       1.1  pooka 	size_t framelen;
    468       1.1  pooka 	ssize_t n;
    469       1.1  pooka 
    470       1.1  pooka 	/* still reading header? */
    471       1.1  pooka 	if (spc->spc_off < HDRSZ) {
    472       1.1  pooka 		DPRINTF(("rump_sp: readframe getting header at offset %zu\n",
    473       1.1  pooka 		    spc->spc_off));
    474       1.1  pooka 
    475       1.1  pooka 		left = HDRSZ - spc->spc_off;
    476       1.1  pooka 		/*LINTED: cast ok */
    477      1.20  pooka 		n = host_read(fd, (uint8_t*)&spc->spc_hdr + spc->spc_off, left);
    478       1.1  pooka 		if (n == 0) {
    479       1.1  pooka 			return -1;
    480       1.1  pooka 		}
    481       1.1  pooka 		if (n == -1) {
    482       1.1  pooka 			if (errno == EAGAIN)
    483       1.1  pooka 				return 0;
    484       1.1  pooka 			return -1;
    485       1.1  pooka 		}
    486       1.1  pooka 
    487       1.1  pooka 		spc->spc_off += n;
    488      1.29  pooka 		if (spc->spc_off < HDRSZ) {
    489      1.29  pooka 			return 0;
    490      1.29  pooka 		}
    491       1.1  pooka 
    492       1.1  pooka 		/*LINTED*/
    493       1.1  pooka 		framelen = spc->spc_hdr.rsp_len;
    494       1.1  pooka 
    495       1.1  pooka 		if (framelen < HDRSZ) {
    496       1.1  pooka 			return -1;
    497       1.1  pooka 		} else if (framelen == HDRSZ) {
    498       1.1  pooka 			return 1;
    499       1.1  pooka 		}
    500       1.1  pooka 
    501       1.1  pooka 		spc->spc_buf = malloc(framelen - HDRSZ);
    502       1.1  pooka 		if (spc->spc_buf == NULL) {
    503       1.1  pooka 			return -1;
    504       1.1  pooka 		}
    505       1.1  pooka 		memset(spc->spc_buf, 0, framelen - HDRSZ);
    506       1.1  pooka 
    507       1.1  pooka 		/* "fallthrough" */
    508       1.1  pooka 	} else {
    509       1.1  pooka 		/*LINTED*/
    510       1.1  pooka 		framelen = spc->spc_hdr.rsp_len;
    511       1.1  pooka 	}
    512       1.1  pooka 
    513       1.1  pooka 	left = framelen - spc->spc_off;
    514       1.1  pooka 
    515       1.1  pooka 	DPRINTF(("rump_sp: readframe getting body at offset %zu, left %zu\n",
    516       1.1  pooka 	    spc->spc_off, left));
    517       1.1  pooka 
    518       1.1  pooka 	if (left == 0)
    519       1.1  pooka 		return 1;
    520      1.20  pooka 	n = host_read(fd, spc->spc_buf + (spc->spc_off - HDRSZ), left);
    521       1.1  pooka 	if (n == 0) {
    522       1.1  pooka 		return -1;
    523       1.1  pooka 	}
    524       1.1  pooka 	if (n == -1) {
    525       1.1  pooka 		if (errno == EAGAIN)
    526       1.1  pooka 			return 0;
    527       1.1  pooka 		return -1;
    528       1.1  pooka 	}
    529       1.1  pooka 	spc->spc_off += n;
    530       1.1  pooka 	left -= n;
    531       1.1  pooka 
    532       1.1  pooka 	/* got everything? */
    533       1.1  pooka 	if (left == 0)
    534       1.1  pooka 		return 1;
    535       1.1  pooka 	else
    536       1.1  pooka 		return 0;
    537       1.1  pooka }
    538       1.1  pooka 
    539       1.1  pooka static int
    540       1.1  pooka tcp_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
    541       1.1  pooka {
    542       1.1  pooka 	struct sockaddr_in sin;
    543       1.1  pooka 	char buf[64];
    544       1.1  pooka 	const char *p;
    545       1.1  pooka 	size_t l;
    546       1.1  pooka 	int port;
    547       1.1  pooka 
    548       1.1  pooka 	memset(&sin, 0, sizeof(sin));
    549  1.31.4.2   yamt 	SIN_SETLEN(sin, sizeof(sin));
    550       1.1  pooka 	sin.sin_family = AF_INET;
    551       1.1  pooka 
    552       1.1  pooka 	p = strchr(addr, ':');
    553       1.1  pooka 	if (!p) {
    554       1.1  pooka 		fprintf(stderr, "rump_sp_tcp: missing port specifier\n");
    555       1.1  pooka 		return EINVAL;
    556       1.1  pooka 	}
    557       1.1  pooka 
    558       1.1  pooka 	l = p - addr;
    559       1.1  pooka 	if (l > sizeof(buf)-1) {
    560       1.1  pooka 		fprintf(stderr, "rump_sp_tcp: address too long\n");
    561       1.1  pooka 		return EINVAL;
    562       1.1  pooka 	}
    563       1.1  pooka 	strncpy(buf, addr, l);
    564       1.1  pooka 	buf[l] = '\0';
    565       1.1  pooka 
    566       1.1  pooka 	/* special INADDR_ANY treatment */
    567       1.1  pooka 	if (strcmp(buf, "*") == 0 || strcmp(buf, "0") == 0) {
    568       1.1  pooka 		sin.sin_addr.s_addr = INADDR_ANY;
    569       1.1  pooka 	} else {
    570       1.1  pooka 		switch (inet_pton(AF_INET, buf, &sin.sin_addr)) {
    571       1.1  pooka 		case 1:
    572       1.1  pooka 			break;
    573       1.1  pooka 		case 0:
    574       1.1  pooka 			fprintf(stderr, "rump_sp_tcp: cannot parse %s\n", buf);
    575       1.1  pooka 			return EINVAL;
    576       1.1  pooka 		case -1:
    577       1.1  pooka 			fprintf(stderr, "rump_sp_tcp: inet_pton failed\n");
    578       1.1  pooka 			return errno;
    579       1.1  pooka 		default:
    580       1.1  pooka 			assert(/*CONSTCOND*/0);
    581       1.1  pooka 			return EINVAL;
    582       1.1  pooka 		}
    583       1.1  pooka 	}
    584       1.1  pooka 
    585       1.1  pooka 	if (!allow_wildcard && sin.sin_addr.s_addr == INADDR_ANY) {
    586       1.1  pooka 		fprintf(stderr, "rump_sp_tcp: client needs !INADDR_ANY\n");
    587       1.1  pooka 		return EINVAL;
    588       1.1  pooka 	}
    589       1.1  pooka 
    590       1.1  pooka 	/* advance to port number & parse */
    591       1.1  pooka 	p++;
    592       1.1  pooka 	l = strspn(p, "0123456789");
    593       1.1  pooka 	if (l == 0) {
    594       1.1  pooka 		fprintf(stderr, "rump_sp_tcp: port now found: %s\n", p);
    595       1.1  pooka 		return EINVAL;
    596       1.1  pooka 	}
    597       1.1  pooka 	strncpy(buf, p, l);
    598       1.1  pooka 	buf[l] = '\0';
    599       1.1  pooka 
    600       1.1  pooka 	if (*(p+l) != '/' && *(p+l) != '\0') {
    601       1.1  pooka 		fprintf(stderr, "rump_sp_tcp: junk at end of port: %s\n", addr);
    602       1.1  pooka 		return EINVAL;
    603       1.1  pooka 	}
    604       1.1  pooka 
    605       1.1  pooka 	port = atoi(buf);
    606       1.1  pooka 	if (port < 0 || port >= (1<<(8*sizeof(in_port_t)))) {
    607       1.1  pooka 		fprintf(stderr, "rump_sp_tcp: port %d out of range\n", port);
    608       1.1  pooka 		return ERANGE;
    609       1.1  pooka 	}
    610       1.1  pooka 	sin.sin_port = htons(port);
    611       1.1  pooka 
    612       1.1  pooka 	*sa = malloc(sizeof(sin));
    613       1.1  pooka 	if (*sa == NULL)
    614       1.1  pooka 		return errno;
    615       1.1  pooka 	memcpy(*sa, &sin, sizeof(sin));
    616       1.1  pooka 	return 0;
    617       1.1  pooka }
    618       1.1  pooka 
    619       1.1  pooka static int
    620       1.1  pooka tcp_connecthook(int s)
    621       1.1  pooka {
    622       1.1  pooka 	int x;
    623       1.1  pooka 
    624       1.1  pooka 	x = 1;
    625      1.20  pooka 	host_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
    626       1.1  pooka 
    627       1.1  pooka 	return 0;
    628       1.1  pooka }
    629       1.1  pooka 
    630      1.27  pooka static char parsedurl[256];
    631      1.27  pooka 
    632       1.5  pooka /*ARGSUSED*/
    633       1.2  pooka static int
    634       1.2  pooka unix_parse(const char *addr, struct sockaddr **sa, int allow_wildcard)
    635       1.2  pooka {
    636  1.31.4.2   yamt 	struct sockaddr_un s_un;
    637       1.5  pooka 	size_t slen;
    638      1.27  pooka 	int savepath = 0;
    639       1.2  pooka 
    640  1.31.4.2   yamt 	if (strlen(addr) >= sizeof(s_un.sun_path))
    641       1.2  pooka 		return ENAMETOOLONG;
    642       1.2  pooka 
    643       1.2  pooka 	/*
    644       1.2  pooka 	 * The pathname can be all kinds of spaghetti elementals,
    645      1.15  pooka 	 * so meek and obidient we accept everything.  However, use
    646      1.15  pooka 	 * full path for easy cleanup in case someone gives a relative
    647      1.15  pooka 	 * one and the server does a chdir() between now than the
    648      1.15  pooka 	 * cleanup.
    649       1.2  pooka 	 */
    650  1.31.4.2   yamt 	memset(&s_un, 0, sizeof(s_un));
    651  1.31.4.2   yamt 	s_un.sun_family = AF_LOCAL;
    652      1.15  pooka 	if (*addr != '/') {
    653      1.15  pooka 		char mywd[PATH_MAX];
    654      1.15  pooka 
    655      1.15  pooka 		if (getcwd(mywd, sizeof(mywd)) == NULL) {
    656      1.15  pooka 			fprintf(stderr, "warning: cannot determine cwd, "
    657      1.15  pooka 			    "omitting socket cleanup\n");
    658      1.15  pooka 		} else {
    659  1.31.4.2   yamt 			if (strlen(addr)+strlen(mywd)+1
    660  1.31.4.2   yamt 			    >= sizeof(s_un.sun_path))
    661      1.15  pooka 				return ENAMETOOLONG;
    662  1.31.4.2   yamt 			strcpy(s_un.sun_path, mywd);
    663  1.31.4.2   yamt 			strcat(s_un.sun_path, "/");
    664      1.27  pooka 			savepath = 1;
    665      1.15  pooka 		}
    666      1.15  pooka 	}
    667  1.31.4.2   yamt 	strcat(s_un.sun_path, addr);
    668  1.31.4.3   yamt #if defined(__linux__) || defined(__sun__) || defined(__CYGWIN__)
    669  1.31.4.2   yamt 	slen = sizeof(s_un);
    670  1.31.4.1   yamt #else
    671  1.31.4.2   yamt 	s_un.sun_len = SUN_LEN(&s_un);
    672  1.31.4.2   yamt 	slen = s_un.sun_len+1; /* get the 0 too */
    673  1.31.4.1   yamt #endif
    674       1.2  pooka 
    675      1.27  pooka 	if (savepath && *parsedurl == '\0') {
    676      1.27  pooka 		snprintf(parsedurl, sizeof(parsedurl),
    677  1.31.4.2   yamt 		    "unix://%s", s_un.sun_path);
    678      1.27  pooka 	}
    679      1.27  pooka 
    680       1.5  pooka 	*sa = malloc(slen);
    681       1.2  pooka 	if (*sa == NULL)
    682       1.2  pooka 		return errno;
    683  1.31.4.2   yamt 	memcpy(*sa, &s_un, slen);
    684       1.2  pooka 
    685       1.2  pooka 	return 0;
    686       1.2  pooka }
    687       1.2  pooka 
    688      1.15  pooka static void
    689      1.15  pooka unix_cleanup(struct sockaddr *sa)
    690      1.15  pooka {
    691  1.31.4.2   yamt 	struct sockaddr_un *s_sun = (void *)sa;
    692      1.15  pooka 
    693      1.15  pooka 	/*
    694      1.15  pooka 	 * cleanup only absolute paths.  see unix_parse() above
    695      1.15  pooka 	 */
    696  1.31.4.2   yamt 	if (*s_sun->sun_path == '/') {
    697  1.31.4.2   yamt 		unlink(s_sun->sun_path);
    698      1.15  pooka 	}
    699      1.15  pooka }
    700      1.15  pooka 
    701       1.1  pooka /*ARGSUSED*/
    702       1.1  pooka static int
    703       1.1  pooka notsupp(void)
    704       1.1  pooka {
    705       1.1  pooka 
    706       1.1  pooka 	fprintf(stderr, "rump_sp: support not yet implemented\n");
    707       1.1  pooka 	return EOPNOTSUPP;
    708       1.1  pooka }
    709       1.1  pooka 
    710       1.1  pooka static int
    711       1.1  pooka success(void)
    712       1.1  pooka {
    713       1.1  pooka 
    714       1.1  pooka 	return 0;
    715       1.1  pooka }
    716       1.1  pooka 
    717       1.1  pooka struct {
    718       1.1  pooka 	const char *id;
    719       1.1  pooka 	int domain;
    720  1.31.4.1   yamt 	socklen_t slen;
    721       1.1  pooka 	addrparse_fn ap;
    722       1.1  pooka 	connecthook_fn connhook;
    723      1.15  pooka 	cleanup_fn cleanup;
    724       1.1  pooka } parsetab[] = {
    725  1.31.4.1   yamt 	{ "tcp", PF_INET, sizeof(struct sockaddr_in),
    726  1.31.4.1   yamt 	    tcp_parse, tcp_connecthook, (cleanup_fn)success },
    727  1.31.4.1   yamt 	{ "unix", PF_LOCAL, sizeof(struct sockaddr_un),
    728  1.31.4.1   yamt 	    unix_parse, (connecthook_fn)success, unix_cleanup },
    729  1.31.4.1   yamt 	{ "tcp6", PF_INET6, sizeof(struct sockaddr_in6),
    730  1.31.4.1   yamt 	    (addrparse_fn)notsupp, (connecthook_fn)success,
    731  1.31.4.1   yamt 	    (cleanup_fn)success },
    732       1.1  pooka };
    733       1.1  pooka #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0]))
    734       1.1  pooka 
    735       1.1  pooka static int
    736       1.1  pooka parseurl(const char *url, struct sockaddr **sap, unsigned *idxp,
    737       1.1  pooka 	int allow_wildcard)
    738       1.1  pooka {
    739       1.1  pooka 	char id[16];
    740       1.1  pooka 	const char *p, *p2;
    741       1.1  pooka 	size_t l;
    742       1.1  pooka 	unsigned i;
    743       1.1  pooka 	int error;
    744       1.1  pooka 
    745       1.1  pooka 	/*
    746       1.1  pooka 	 * Parse the url
    747       1.1  pooka 	 */
    748       1.1  pooka 
    749       1.1  pooka 	p = url;
    750       1.1  pooka 	p2 = strstr(p, "://");
    751       1.1  pooka 	if (!p2) {
    752       1.1  pooka 		fprintf(stderr, "rump_sp: invalid locator ``%s''\n", p);
    753       1.1  pooka 		return EINVAL;
    754       1.1  pooka 	}
    755       1.1  pooka 	l = p2-p;
    756       1.1  pooka 	if (l > sizeof(id)-1) {
    757       1.1  pooka 		fprintf(stderr, "rump_sp: identifier too long in ``%s''\n", p);
    758       1.1  pooka 		return EINVAL;
    759       1.1  pooka 	}
    760       1.1  pooka 
    761       1.1  pooka 	strncpy(id, p, l);
    762       1.1  pooka 	id[l] = '\0';
    763       1.1  pooka 	p2 += 3; /* beginning of address */
    764       1.1  pooka 
    765       1.1  pooka 	for (i = 0; i < NPARSE; i++) {
    766       1.1  pooka 		if (strcmp(id, parsetab[i].id) == 0) {
    767       1.1  pooka 			error = parsetab[i].ap(p2, sap, allow_wildcard);
    768       1.1  pooka 			if (error)
    769       1.1  pooka 				return error;
    770       1.1  pooka 			break;
    771       1.1  pooka 		}
    772       1.1  pooka 	}
    773       1.1  pooka 	if (i == NPARSE) {
    774       1.1  pooka 		fprintf(stderr, "rump_sp: invalid identifier ``%s''\n", p);
    775       1.1  pooka 		return EINVAL;
    776       1.1  pooka 	}
    777       1.1  pooka 
    778       1.1  pooka 	*idxp = i;
    779       1.1  pooka 	return 0;
    780       1.1  pooka }
    781