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