rumpuser_sp.c revision 1.22 1 /* $NetBSD: rumpuser_sp.c,v 1.22 2010/11/30 22:32:01 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2010 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 * Not finished yet, i.e. don't use in production. Lacks locking plus
37 * handling of multiple clients and unexpected connection closes.
38 */
39
40 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: rumpuser_sp.c,v 1.22 2010/11/30 22:32:01 pooka Exp $");
42
43 #include <sys/types.h>
44 #include <sys/atomic.h>
45 #include <sys/mman.h>
46 #include <sys/socket.h>
47
48 #include <arpa/inet.h>
49 #include <netinet/in.h>
50 #include <netinet/tcp.h>
51
52 #include <assert.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <poll.h>
56 #include <pthread.h>
57 #include <stdarg.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include <rump/rumpuser.h>
64 #include "rumpuser_int.h"
65
66 #include "sp_common.c"
67
68 #ifndef MAXCLI
69 #define MAXCLI 256
70 #endif
71 #ifndef MAXWORKER
72 #define MAXWORKER 128
73 #endif
74 #ifndef IDLEWORKER
75 #define IDLEWORKER 16
76 #endif
77 int rumpsp_maxworker = MAXWORKER;
78 int rumpsp_idleworker = IDLEWORKER;
79
80 static struct pollfd pfdlist[MAXCLI];
81 static struct spclient spclist[MAXCLI];
82 static unsigned int disco;
83
84 static struct rumpuser_sp_ops spops;
85
86 /*
87 * Manual wrappers, since librump does not have access to the
88 * user namespace wrapped interfaces.
89 */
90
91 static void
92 lwproc_switch(struct lwp *l)
93 {
94
95 spops.spop_schedule();
96 spops.spop_lwproc_switch(l);
97 spops.spop_unschedule();
98 }
99
100 static void
101 lwproc_release(void)
102 {
103
104 spops.spop_schedule();
105 spops.spop_lwproc_release();
106 spops.spop_unschedule();
107 }
108
109 static int
110 lwproc_newproc(struct spclient *spc)
111 {
112 int rv;
113
114 spops.spop_schedule();
115 rv = spops.spop_lwproc_newproc(spc);
116 spops.spop_unschedule();
117
118 return rv;
119 }
120
121 static int
122 lwproc_newlwp(pid_t pid)
123 {
124 int rv;
125
126 spops.spop_schedule();
127 rv = spops.spop_lwproc_newlwp(pid);
128 spops.spop_unschedule();
129
130 return rv;
131 }
132
133 static struct lwp *
134 lwproc_curlwp(void)
135 {
136 struct lwp *l;
137
138 spops.spop_schedule();
139 l = spops.spop_lwproc_curlwp();
140 spops.spop_unschedule();
141
142 return l;
143 }
144
145 static pid_t
146 lwproc_getpid(void)
147 {
148 pid_t p;
149
150 spops.spop_schedule();
151 p = spops.spop_getpid();
152 spops.spop_unschedule();
153
154 return p;
155 }
156
157 static int
158 rumpsyscall(int sysnum, void *data, register_t *retval)
159 {
160 int rv;
161
162 spops.spop_schedule();
163 rv = spops.spop_syscall(sysnum, data, retval);
164 spops.spop_unschedule();
165
166 return rv;
167 }
168
169 static uint64_t
170 nextreq(struct spclient *spc)
171 {
172 uint64_t nw;
173
174 pthread_mutex_lock(&spc->spc_mtx);
175 nw = spc->spc_nextreq++;
176 pthread_mutex_unlock(&spc->spc_mtx);
177
178 return nw;
179 }
180
181 static void
182 send_error_resp(struct spclient *spc, uint64_t reqno, int error)
183 {
184 struct rsp_hdr rhdr;
185
186 rhdr.rsp_len = sizeof(rhdr);
187 rhdr.rsp_reqno = reqno;
188 rhdr.rsp_class = RUMPSP_ERROR;
189 rhdr.rsp_type = 0;
190 rhdr.rsp_error = error;
191
192 sendlock(spc);
193 (void)dosend(spc, &rhdr, sizeof(rhdr));
194 sendunlock(spc);
195 }
196
197 static int
198 send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
199 register_t *retval)
200 {
201 struct rsp_hdr rhdr;
202 struct rsp_sysresp sysresp;
203 int rv;
204
205 rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
206 rhdr.rsp_reqno = reqno;
207 rhdr.rsp_class = RUMPSP_RESP;
208 rhdr.rsp_type = RUMPSP_SYSCALL;
209 rhdr.rsp_sysnum = 0;
210
211 sysresp.rsys_error = error;
212 memcpy(sysresp.rsys_retval, retval, sizeof(sysresp.rsys_retval));
213
214 sendlock(spc);
215 rv = dosend(spc, &rhdr, sizeof(rhdr));
216 rv = dosend(spc, &sysresp, sizeof(sysresp));
217 sendunlock(spc);
218
219 return rv;
220 }
221
222 static int
223 copyin_req(struct spclient *spc, const void *remaddr, size_t *dlen,
224 int wantstr, void **resp)
225 {
226 struct rsp_hdr rhdr;
227 struct rsp_copydata copydata;
228 struct respwait rw;
229 int rv;
230
231 DPRINTF(("copyin_req: %zu bytes from %p\n", *dlen, remaddr));
232
233 rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
234 rhdr.rsp_class = RUMPSP_REQ;
235 if (wantstr)
236 rhdr.rsp_type = RUMPSP_COPYINSTR;
237 else
238 rhdr.rsp_type = RUMPSP_COPYIN;
239 rhdr.rsp_sysnum = 0;
240
241 copydata.rcp_addr = __UNCONST(remaddr);
242 copydata.rcp_len = *dlen;
243
244 putwait(spc, &rw, &rhdr);
245 rv = dosend(spc, &rhdr, sizeof(rhdr));
246 rv = dosend(spc, ©data, sizeof(copydata));
247 if (rv) {
248 unputwait(spc, &rw);
249 return rv;
250 }
251
252 rv = waitresp(spc, &rw);
253
254 DPRINTF(("copyin: response %d\n", rv));
255
256 *resp = rw.rw_data;
257 if (wantstr)
258 *dlen = rw.rw_dlen;
259
260 return rv;
261
262 }
263
264 static int
265 send_copyout_req(struct spclient *spc, const void *remaddr,
266 const void *data, size_t dlen)
267 {
268 struct rsp_hdr rhdr;
269 struct rsp_copydata copydata;
270 int rv;
271
272 DPRINTF(("copyout_req (async): %zu bytes to %p\n", dlen, remaddr));
273
274 rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
275 rhdr.rsp_reqno = nextreq(spc);
276 rhdr.rsp_class = RUMPSP_REQ;
277 rhdr.rsp_type = RUMPSP_COPYOUT;
278 rhdr.rsp_sysnum = 0;
279
280 copydata.rcp_addr = __UNCONST(remaddr);
281 copydata.rcp_len = dlen;
282
283 sendlock(spc);
284 rv = dosend(spc, &rhdr, sizeof(rhdr));
285 rv = dosend(spc, ©data, sizeof(copydata));
286 rv = dosend(spc, data, dlen);
287 sendunlock(spc);
288
289 return rv;
290 }
291
292 static int
293 anonmmap_req(struct spclient *spc, size_t howmuch, void **resp)
294 {
295 struct rsp_hdr rhdr;
296 struct respwait rw;
297 int rv;
298
299 DPRINTF(("anonmmap_req: %zu bytes\n", howmuch));
300
301 rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
302 rhdr.rsp_class = RUMPSP_REQ;
303 rhdr.rsp_type = RUMPSP_ANONMMAP;
304 rhdr.rsp_sysnum = 0;
305
306 putwait(spc, &rw, &rhdr);
307 rv = dosend(spc, &rhdr, sizeof(rhdr));
308 rv = dosend(spc, &howmuch, sizeof(howmuch));
309 if (rv) {
310 unputwait(spc, &rw);
311 return rv;
312 }
313
314 rv = waitresp(spc, &rw);
315
316 *resp = rw.rw_data;
317
318 DPRINTF(("anonmmap: mapped at %p\n", **(void ***)resp));
319
320 return rv;
321 }
322
323 static void
324 spcref(struct spclient *spc)
325 {
326
327 pthread_mutex_lock(&spc->spc_mtx);
328 spc->spc_refcnt++;
329 pthread_mutex_unlock(&spc->spc_mtx);
330 }
331
332 static void
333 spcrelease(struct spclient *spc)
334 {
335 int ref;
336
337 pthread_mutex_lock(&spc->spc_mtx);
338 ref = --spc->spc_refcnt;
339 pthread_mutex_unlock(&spc->spc_mtx);
340
341 if (ref > 0)
342 return;
343
344 DPRINTF(("spcrelease: spc %p fd %d\n", spc, spc->spc_fd));
345
346 _DIAGASSERT(TAILQ_EMPTY(&spc->spc_respwait));
347 _DIAGASSERT(spc->spc_buf == NULL);
348
349 lwproc_switch(spc->spc_mainlwp);
350 lwproc_release();
351 spc->spc_mainlwp = NULL;
352
353 close(spc->spc_fd);
354 spc->spc_fd = -1;
355 spc->spc_dying = 0;
356
357 atomic_inc_uint(&disco);
358 }
359
360 static void
361 serv_handledisco(unsigned int idx)
362 {
363 struct spclient *spc = &spclist[idx];
364
365 DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
366
367 pfdlist[idx].fd = -1;
368 pfdlist[idx].revents = 0;
369 pthread_mutex_lock(&spc->spc_mtx);
370 spc->spc_dying = 1;
371 kickall(spc);
372 pthread_mutex_unlock(&spc->spc_mtx);
373
374 /*
375 * Nobody's going to attempt to send/receive anymore,
376 * so reinit info relevant to that.
377 */
378 /*LINTED:pointer casts may be ok*/
379 memset((char *)spc + SPC_ZEROFF, 0, sizeof(*spc) - SPC_ZEROFF);
380
381 spcrelease(spc);
382 }
383
384 static unsigned
385 serv_handleconn(int fd, connecthook_fn connhook, int busy)
386 {
387 struct sockaddr_storage ss;
388 socklen_t sl = sizeof(ss);
389 int newfd, flags;
390 unsigned i;
391
392 /*LINTED: cast ok */
393 newfd = accept(fd, (struct sockaddr *)&ss, &sl);
394 if (newfd == -1)
395 return 0;
396
397 if (busy) {
398 close(newfd); /* EBUSY */
399 return 0;
400 }
401
402 /* XXX: should do some sort of handshake too */
403
404 flags = fcntl(newfd, F_GETFL, 0);
405 if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
406 close(newfd);
407 return 0;
408 }
409
410 if (connhook(newfd) != 0) {
411 close(newfd);
412 return 0;
413 }
414
415 /* find empty slot the simple way */
416 for (i = 0; i < MAXCLI; i++) {
417 if (pfdlist[i].fd == -1 && spclist[i].spc_dying == 0)
418 break;
419 }
420
421 if (lwproc_newproc(&spclist[i]) != 0) {
422 close(newfd);
423 return 0;
424 }
425
426 assert(i < MAXCLI);
427
428 pfdlist[i].fd = newfd;
429 spclist[i].spc_fd = newfd;
430 spclist[i].spc_mainlwp = lwproc_curlwp();
431 spclist[i].spc_istatus = SPCSTATUS_BUSY; /* dedicated receiver */
432 spclist[i].spc_pid = lwproc_getpid();
433 spclist[i].spc_refcnt = 1;
434
435 TAILQ_INIT(&spclist[i].spc_respwait);
436
437 DPRINTF(("rump_sp: added new connection fd %d at idx %u, pid %d\n",
438 newfd, i, lwproc_getpid()));
439
440 lwproc_switch(NULL);
441
442 return i;
443 }
444
445 static void
446 serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
447 {
448 register_t retval[2] = {0, 0};
449 int rv, sysnum;
450
451 sysnum = (int)rhdr->rsp_sysnum;
452 DPRINTF(("rump_sp: handling syscall %d from client %d\n",
453 sysnum, 0));
454
455 lwproc_newlwp(spc->spc_pid);
456 rv = rumpsyscall(sysnum, data, retval);
457 lwproc_release();
458
459 DPRINTF(("rump_sp: got return value %d & %d/%d\n",
460 rv, retval[0], retval[1]));
461
462 send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
463 }
464
465 struct sysbouncearg {
466 struct spclient *sba_spc;
467 struct rsp_hdr sba_hdr;
468 uint8_t *sba_data;
469
470 TAILQ_ENTRY(sysbouncearg) sba_entries;
471 };
472 static pthread_mutex_t sbamtx;
473 static pthread_cond_t sbacv;
474 static int nworker, idleworker;
475 static TAILQ_HEAD(, sysbouncearg) syslist = TAILQ_HEAD_INITIALIZER(syslist);
476
477 /*ARGSUSED*/
478 static void *
479 serv_syscallbouncer(void *arg)
480 {
481 struct sysbouncearg *sba;
482
483 for (;;) {
484 pthread_mutex_lock(&sbamtx);
485 if (idleworker >= rumpsp_idleworker) {
486 nworker--;
487 pthread_mutex_unlock(&sbamtx);
488 break;
489 }
490 idleworker++;
491 while (TAILQ_EMPTY(&syslist)) {
492 pthread_cond_wait(&sbacv, &sbamtx);
493 }
494
495 sba = TAILQ_FIRST(&syslist);
496 TAILQ_REMOVE(&syslist, sba, sba_entries);
497 idleworker--;
498 pthread_mutex_unlock(&sbamtx);
499
500 serv_handlesyscall(sba->sba_spc,
501 &sba->sba_hdr, sba->sba_data);
502 spcrelease(sba->sba_spc);
503 free(sba->sba_data);
504 free(sba);
505 }
506
507 return NULL;
508 }
509
510 static int
511 sp_copyin(void *arg, const void *raddr, void *laddr, size_t *len, int wantstr)
512 {
513 struct spclient *spc = arg;
514 void *rdata = NULL; /* XXXuninit */
515 int rv, nlocks;
516
517 rumpuser__kunlock(0, &nlocks, NULL);
518
519 rv = copyin_req(spc, raddr, len, wantstr, &rdata);
520 if (rv)
521 goto out;
522
523 memcpy(laddr, rdata, *len);
524 free(rdata);
525
526 out:
527 rumpuser__klock(nlocks, NULL);
528 if (rv)
529 return EFAULT;
530 return 0;
531 }
532
533 int
534 rumpuser_sp_copyin(void *arg, const void *raddr, void *laddr, size_t len)
535 {
536
537 return sp_copyin(arg, raddr, laddr, &len, 0);
538 }
539
540 int
541 rumpuser_sp_copyinstr(void *arg, const void *raddr, void *laddr, size_t *len)
542 {
543
544 return sp_copyin(arg, raddr, laddr, len, 1);
545 }
546
547 static int
548 sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
549 {
550 struct spclient *spc = arg;
551 int nlocks, rv;
552
553 rumpuser__kunlock(0, &nlocks, NULL);
554 rv = send_copyout_req(spc, raddr, laddr, dlen);
555 rumpuser__klock(nlocks, NULL);
556
557 if (rv)
558 return EFAULT;
559 return 0;
560 }
561
562 int
563 rumpuser_sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
564 {
565
566 return sp_copyout(arg, laddr, raddr, dlen);
567 }
568
569 int
570 rumpuser_sp_copyoutstr(void *arg, const void *laddr, void *raddr, size_t *dlen)
571 {
572
573 return sp_copyout(arg, laddr, raddr, *dlen);
574 }
575
576 int
577 rumpuser_sp_anonmmap(void *arg, size_t howmuch, void **addr)
578 {
579 struct spclient *spc = arg;
580 void *resp, *rdata;
581 int nlocks, rv;
582
583 rumpuser__kunlock(0, &nlocks, NULL);
584
585 rv = anonmmap_req(spc, howmuch, &rdata);
586 if (rv) {
587 rv = EFAULT;
588 goto out;
589 }
590
591 resp = *(void **)rdata;
592 free(rdata);
593
594 if (resp == NULL) {
595 rv = ENOMEM;
596 }
597
598 *addr = resp;
599
600 out:
601 rumpuser__klock(nlocks, NULL);
602
603 if (rv)
604 return rv;
605 return 0;
606 }
607
608 /*
609 *
610 * Startup routines and mainloop for server.
611 *
612 */
613
614 struct spservarg {
615 int sps_sock;
616 connecthook_fn sps_connhook;
617 };
618
619 static pthread_attr_t pattr_detached;
620 static void
621 handlereq(struct spclient *spc)
622 {
623 struct sysbouncearg *sba;
624 pthread_t pt;
625 int retries;
626
627 if (__predict_false(spc->spc_hdr.rsp_type != RUMPSP_SYSCALL)) {
628 send_error_resp(spc, spc->spc_hdr.rsp_reqno, EINVAL);
629 spcfreebuf(spc);
630 return;
631 }
632
633 retries = 0;
634 while ((sba = malloc(sizeof(*sba))) == NULL) {
635 if (nworker == 0 || retries > 10) {
636 send_error_resp(spc, spc->spc_hdr.rsp_reqno, EAGAIN);
637 spcfreebuf(spc);
638 return;
639 }
640 /* slim chance of more memory? */
641 usleep(10000);
642 }
643
644 sba->sba_spc = spc;
645 sba->sba_hdr = spc->spc_hdr;
646 sba->sba_data = spc->spc_buf;
647 spcresetbuf(spc);
648
649 spcref(spc);
650
651 pthread_mutex_lock(&sbamtx);
652 TAILQ_INSERT_TAIL(&syslist, sba, sba_entries);
653 if (idleworker > 0) {
654 /* do we have a daemon's tool (i.e. idle threads)? */
655 pthread_cond_signal(&sbacv);
656 } else if (nworker < rumpsp_maxworker) {
657 /*
658 * Else, need to create one
659 * (if we can, otherwise just expect another
660 * worker to pick up the syscall)
661 */
662 if (pthread_create(&pt, &pattr_detached,
663 serv_syscallbouncer, NULL) == 0)
664 nworker++;
665 }
666 pthread_mutex_unlock(&sbamtx);
667 }
668
669 static void *
670 spserver(void *arg)
671 {
672 struct spservarg *sarg = arg;
673 struct spclient *spc;
674 unsigned idx;
675 int seen;
676 int rv;
677 unsigned int nfds, maxidx;
678
679 for (idx = 0; idx < MAXCLI; idx++) {
680 pfdlist[idx].fd = -1;
681 pfdlist[idx].events = POLLIN;
682
683 spc = &spclist[idx];
684 pthread_mutex_init(&spc->spc_mtx, NULL);
685 pthread_cond_init(&spc->spc_cv, NULL);
686 }
687 pfdlist[0].fd = sarg->sps_sock;
688 pfdlist[0].events = POLLIN;
689 nfds = 1;
690 maxidx = 0;
691
692 pthread_attr_init(&pattr_detached);
693 pthread_attr_setdetachstate(&pattr_detached, PTHREAD_CREATE_DETACHED);
694 /* XXX: doesn't stacksize currently work on NetBSD */
695 pthread_attr_setstacksize(&pattr_detached, 32*1024);
696
697 pthread_mutex_init(&sbamtx, NULL);
698 pthread_cond_init(&sbacv, NULL);
699
700 DPRINTF(("rump_sp: server mainloop\n"));
701
702 for (;;) {
703 int discoed;
704
705 /* g/c hangarounds (eventually) */
706 discoed = atomic_swap_uint(&disco, 0);
707 while (discoed--) {
708 nfds--;
709 idx = maxidx;
710 while (idx) {
711 if (pfdlist[idx].fd != -1) {
712 maxidx = idx;
713 break;
714 }
715 idx--;
716 }
717 DPRINTF(("rump_sp: set maxidx to [%u]\n",
718 maxidx));
719 }
720
721 DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
722 seen = 0;
723 rv = poll(pfdlist, maxidx+1, INFTIM);
724 assert(maxidx+1 <= MAXCLI);
725 assert(rv != 0);
726 if (rv == -1) {
727 if (errno == EINTR)
728 continue;
729 fprintf(stderr, "rump_spserver: poll returned %d\n",
730 errno);
731 break;
732 }
733
734 for (idx = 0; seen < rv && idx < MAXCLI; idx++) {
735 if ((pfdlist[idx].revents & POLLIN) == 0)
736 continue;
737
738 seen++;
739 DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
740 idx, seen, rv));
741 if (idx > 0) {
742 spc = &spclist[idx];
743 DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
744 switch (readframe(spc)) {
745 case 0:
746 break;
747 case -1:
748 serv_handledisco(idx);
749 break;
750 default:
751 switch (spc->spc_hdr.rsp_class) {
752 case RUMPSP_RESP:
753 kickwaiter(spc);
754 break;
755 case RUMPSP_REQ:
756 handlereq(spc);
757 break;
758 default:
759 send_error_resp(spc,
760 spc->spc_hdr.rsp_reqno,
761 ENOENT);
762 spcfreebuf(spc);
763 break;
764 }
765 break;
766 }
767
768 } else {
769 DPRINTF(("rump_sp: mainloop new connection\n"));
770
771 idx = serv_handleconn(pfdlist[0].fd,
772 sarg->sps_connhook, nfds == MAXCLI);
773 if (idx)
774 nfds++;
775 if (idx > maxidx)
776 maxidx = idx;
777 DPRINTF(("rump_sp: maxid now %d\n", maxidx));
778 }
779 }
780 }
781
782 return NULL;
783 }
784
785 int
786 rumpuser_sp_init(const struct rumpuser_sp_ops *spopsp, const char *url)
787 {
788 pthread_t pt;
789 struct spservarg *sarg;
790 struct sockaddr *sap;
791 char *p;
792 unsigned idx;
793 int error, s;
794
795 p = strdup(url);
796 if (p == NULL)
797 return ENOMEM;
798 error = parseurl(p, &sap, &idx, 1);
799 free(p);
800 if (error)
801 return error;
802
803 s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
804 if (s == -1)
805 return errno;
806
807 spops = *spopsp;
808 sarg = malloc(sizeof(*sarg));
809 if (sarg == NULL) {
810 close(s);
811 return ENOMEM;
812 }
813
814 sarg->sps_sock = s;
815 sarg->sps_connhook = parsetab[idx].connhook;
816
817 /* sloppy error recovery */
818
819 /*LINTED*/
820 if (bind(s, sap, sap->sa_len) == -1) {
821 fprintf(stderr, "rump_sp: server bind failed\n");
822 return errno;
823 }
824 if (listen(s, MAXCLI) == -1) {
825 fprintf(stderr, "rump_sp: server listen failed\n");
826 return errno;
827 }
828
829 if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
830 fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
831 return errno;
832 }
833 pthread_detach(pt);
834
835 return 0;
836 }
837