rumpuser_sp.c revision 1.15 1 1.15 pooka /* $NetBSD: rumpuser_sp.c,v 1.15 2010/11/25 17:59:02 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2010 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.1 pooka /*
29 1.1 pooka * Sysproxy routines. This provides system RPC support over host sockets.
30 1.1 pooka * The most notable limitation is that the client and server must share
31 1.1 pooka * the same ABI. This does not mean that they have to be the same
32 1.1 pooka * machine or that they need to run the same version of the host OS,
33 1.1 pooka * just that they must agree on the data structures. This even *might*
34 1.1 pooka * work correctly from one hardware architecture to another.
35 1.1 pooka *
36 1.1 pooka * Not finished yet, i.e. don't use in production. Lacks locking plus
37 1.1 pooka * handling of multiple clients and unexpected connection closes.
38 1.1 pooka */
39 1.1 pooka
40 1.1 pooka #include <sys/cdefs.h>
41 1.15 pooka __RCSID("$NetBSD: rumpuser_sp.c,v 1.15 2010/11/25 17:59:02 pooka Exp $");
42 1.1 pooka
43 1.1 pooka #include <sys/types.h>
44 1.11 pooka #include <sys/atomic.h>
45 1.1 pooka #include <sys/mman.h>
46 1.1 pooka #include <sys/socket.h>
47 1.1 pooka
48 1.1 pooka #include <arpa/inet.h>
49 1.1 pooka #include <netinet/in.h>
50 1.1 pooka #include <netinet/tcp.h>
51 1.1 pooka
52 1.1 pooka #include <assert.h>
53 1.1 pooka #include <errno.h>
54 1.1 pooka #include <fcntl.h>
55 1.1 pooka #include <poll.h>
56 1.1 pooka #include <pthread.h>
57 1.1 pooka #include <stdarg.h>
58 1.1 pooka #include <stdio.h>
59 1.1 pooka #include <stdlib.h>
60 1.1 pooka #include <string.h>
61 1.1 pooka #include <unistd.h>
62 1.1 pooka
63 1.1 pooka #include <rump/rumpuser.h>
64 1.13 pooka #include "rumpuser_int.h"
65 1.1 pooka
66 1.5 pooka #include "sp_common.c"
67 1.1 pooka
68 1.1 pooka #define MAXCLI 4
69 1.1 pooka
70 1.1 pooka static struct pollfd pfdlist[MAXCLI];
71 1.1 pooka static struct spclient spclist[MAXCLI];
72 1.11 pooka static unsigned int disco;
73 1.1 pooka
74 1.3 pooka static struct rumpuser_sp_ops spops;
75 1.3 pooka
76 1.3 pooka /*
77 1.3 pooka * Manual wrappers, since librump does not have access to the
78 1.3 pooka * user namespace wrapped interfaces.
79 1.3 pooka */
80 1.3 pooka
81 1.3 pooka static void
82 1.3 pooka lwproc_switch(struct lwp *l)
83 1.3 pooka {
84 1.3 pooka
85 1.4 pooka spops.spop_schedule();
86 1.3 pooka spops.spop_lwproc_switch(l);
87 1.4 pooka spops.spop_unschedule();
88 1.3 pooka }
89 1.3 pooka
90 1.3 pooka static void
91 1.3 pooka lwproc_release(void)
92 1.3 pooka {
93 1.3 pooka
94 1.4 pooka spops.spop_schedule();
95 1.3 pooka spops.spop_lwproc_release();
96 1.4 pooka spops.spop_unschedule();
97 1.3 pooka }
98 1.3 pooka
99 1.3 pooka static int
100 1.10 pooka lwproc_newproc(struct spclient *spc)
101 1.3 pooka {
102 1.3 pooka int rv;
103 1.3 pooka
104 1.4 pooka spops.spop_schedule();
105 1.10 pooka rv = spops.spop_lwproc_newproc(spc);
106 1.4 pooka spops.spop_unschedule();
107 1.3 pooka
108 1.3 pooka return rv;
109 1.3 pooka }
110 1.3 pooka
111 1.8 pooka static int
112 1.8 pooka lwproc_newlwp(pid_t pid)
113 1.8 pooka {
114 1.8 pooka int rv;
115 1.8 pooka
116 1.8 pooka spops.spop_schedule();
117 1.8 pooka rv = spops.spop_lwproc_newlwp(pid);
118 1.8 pooka spops.spop_unschedule();
119 1.8 pooka
120 1.8 pooka return rv;
121 1.8 pooka }
122 1.8 pooka
123 1.3 pooka static struct lwp *
124 1.3 pooka lwproc_curlwp(void)
125 1.3 pooka {
126 1.3 pooka struct lwp *l;
127 1.3 pooka
128 1.4 pooka spops.spop_schedule();
129 1.3 pooka l = spops.spop_lwproc_curlwp();
130 1.4 pooka spops.spop_unschedule();
131 1.3 pooka
132 1.3 pooka return l;
133 1.3 pooka }
134 1.3 pooka
135 1.8 pooka static pid_t
136 1.8 pooka lwproc_getpid(void)
137 1.8 pooka {
138 1.8 pooka pid_t p;
139 1.8 pooka
140 1.8 pooka spops.spop_schedule();
141 1.8 pooka p = spops.spop_getpid();
142 1.8 pooka spops.spop_unschedule();
143 1.8 pooka
144 1.8 pooka return p;
145 1.8 pooka }
146 1.8 pooka
147 1.3 pooka static int
148 1.3 pooka rumpsyscall(int sysnum, void *data, register_t *retval)
149 1.3 pooka {
150 1.3 pooka int rv;
151 1.3 pooka
152 1.4 pooka spops.spop_schedule();
153 1.3 pooka rv = spops.spop_syscall(sysnum, data, retval);
154 1.4 pooka spops.spop_unschedule();
155 1.3 pooka
156 1.3 pooka return rv;
157 1.3 pooka }
158 1.1 pooka
159 1.7 pooka static uint64_t
160 1.7 pooka nextreq(struct spclient *spc)
161 1.7 pooka {
162 1.7 pooka uint64_t nw;
163 1.7 pooka
164 1.7 pooka pthread_mutex_lock(&spc->spc_mtx);
165 1.7 pooka nw = spc->spc_nextreq++;
166 1.7 pooka pthread_mutex_unlock(&spc->spc_mtx);
167 1.7 pooka
168 1.7 pooka return nw;
169 1.7 pooka }
170 1.7 pooka
171 1.1 pooka static int
172 1.1 pooka send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
173 1.7 pooka register_t *retval)
174 1.1 pooka {
175 1.1 pooka struct rsp_hdr rhdr;
176 1.1 pooka struct rsp_sysresp sysresp;
177 1.7 pooka int rv;
178 1.1 pooka
179 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
180 1.1 pooka rhdr.rsp_reqno = reqno;
181 1.7 pooka rhdr.rsp_class = RUMPSP_RESP;
182 1.7 pooka rhdr.rsp_type = RUMPSP_SYSCALL;
183 1.1 pooka rhdr.rsp_sysnum = 0;
184 1.1 pooka
185 1.1 pooka sysresp.rsys_error = error;
186 1.7 pooka memcpy(sysresp.rsys_retval, retval, sizeof(sysresp.rsys_retval));
187 1.1 pooka
188 1.7 pooka sendlock(spc);
189 1.7 pooka rv = dosend(spc, &rhdr, sizeof(rhdr));
190 1.7 pooka rv = dosend(spc, &sysresp, sizeof(sysresp));
191 1.7 pooka sendunlock(spc);
192 1.1 pooka
193 1.7 pooka return rv;
194 1.1 pooka }
195 1.1 pooka
196 1.1 pooka static int
197 1.15 pooka copyin_req(struct spclient *spc, const void *remaddr, size_t *dlen,
198 1.15 pooka int wantstr, void **resp)
199 1.1 pooka {
200 1.1 pooka struct rsp_hdr rhdr;
201 1.1 pooka struct rsp_copydata copydata;
202 1.7 pooka struct respwait rw;
203 1.7 pooka int rv;
204 1.7 pooka
205 1.15 pooka DPRINTF(("copyin_req: %zu bytes from %p\n", *dlen, remaddr));
206 1.1 pooka
207 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
208 1.7 pooka rhdr.rsp_class = RUMPSP_REQ;
209 1.15 pooka if (wantstr)
210 1.15 pooka rhdr.rsp_type = RUMPSP_COPYINSTR;
211 1.15 pooka else
212 1.15 pooka rhdr.rsp_type = RUMPSP_COPYIN;
213 1.1 pooka rhdr.rsp_sysnum = 0;
214 1.1 pooka
215 1.1 pooka copydata.rcp_addr = __UNCONST(remaddr);
216 1.15 pooka copydata.rcp_len = *dlen;
217 1.1 pooka
218 1.7 pooka putwait(spc, &rw, &rhdr);
219 1.7 pooka rv = dosend(spc, &rhdr, sizeof(rhdr));
220 1.7 pooka rv = dosend(spc, ©data, sizeof(copydata));
221 1.13 pooka if (rv) {
222 1.13 pooka unputwait(spc, &rw);
223 1.13 pooka return rv;
224 1.13 pooka }
225 1.7 pooka
226 1.7 pooka rv = waitresp(spc, &rw);
227 1.7 pooka
228 1.7 pooka DPRINTF(("copyin: response %d\n", rv));
229 1.7 pooka
230 1.7 pooka *resp = rw.rw_data;
231 1.15 pooka if (wantstr)
232 1.15 pooka *dlen = rw.rw_dlen;
233 1.15 pooka
234 1.7 pooka return rv;
235 1.1 pooka
236 1.1 pooka }
237 1.1 pooka
238 1.1 pooka static int
239 1.1 pooka send_copyout_req(struct spclient *spc, const void *remaddr,
240 1.1 pooka const void *data, size_t dlen)
241 1.1 pooka {
242 1.1 pooka struct rsp_hdr rhdr;
243 1.1 pooka struct rsp_copydata copydata;
244 1.7 pooka int rv;
245 1.7 pooka
246 1.7 pooka DPRINTF(("copyout_req (async): %zu bytes to %p\n", dlen, remaddr));
247 1.1 pooka
248 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
249 1.7 pooka rhdr.rsp_reqno = nextreq(spc);
250 1.7 pooka rhdr.rsp_class = RUMPSP_REQ;
251 1.7 pooka rhdr.rsp_type = RUMPSP_COPYOUT;
252 1.1 pooka rhdr.rsp_sysnum = 0;
253 1.1 pooka
254 1.1 pooka copydata.rcp_addr = __UNCONST(remaddr);
255 1.1 pooka copydata.rcp_len = dlen;
256 1.1 pooka
257 1.7 pooka sendlock(spc);
258 1.7 pooka rv = dosend(spc, &rhdr, sizeof(rhdr));
259 1.7 pooka rv = dosend(spc, ©data, sizeof(copydata));
260 1.7 pooka rv = dosend(spc, data, dlen);
261 1.7 pooka sendunlock(spc);
262 1.1 pooka
263 1.7 pooka return rv;
264 1.1 pooka }
265 1.1 pooka
266 1.1 pooka static int
267 1.7 pooka anonmmap_req(struct spclient *spc, size_t howmuch, void **resp)
268 1.1 pooka {
269 1.1 pooka struct rsp_hdr rhdr;
270 1.7 pooka struct respwait rw;
271 1.7 pooka int rv;
272 1.7 pooka
273 1.7 pooka DPRINTF(("anonmmap_req: %zu bytes\n", howmuch));
274 1.1 pooka
275 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
276 1.7 pooka rhdr.rsp_class = RUMPSP_REQ;
277 1.7 pooka rhdr.rsp_type = RUMPSP_ANONMMAP;
278 1.1 pooka rhdr.rsp_sysnum = 0;
279 1.1 pooka
280 1.7 pooka putwait(spc, &rw, &rhdr);
281 1.7 pooka rv = dosend(spc, &rhdr, sizeof(rhdr));
282 1.7 pooka rv = dosend(spc, &howmuch, sizeof(howmuch));
283 1.13 pooka if (rv) {
284 1.13 pooka unputwait(spc, &rw);
285 1.13 pooka return rv;
286 1.13 pooka }
287 1.1 pooka
288 1.7 pooka rv = waitresp(spc, &rw);
289 1.13 pooka
290 1.7 pooka *resp = rw.rw_data;
291 1.7 pooka
292 1.7 pooka DPRINTF(("anonmmap: mapped at %p\n", **(void ***)resp));
293 1.7 pooka
294 1.7 pooka return rv;
295 1.1 pooka }
296 1.1 pooka
297 1.1 pooka static void
298 1.11 pooka spcref(struct spclient *spc)
299 1.11 pooka {
300 1.11 pooka
301 1.11 pooka pthread_mutex_lock(&spc->spc_mtx);
302 1.11 pooka spc->spc_refcnt++;
303 1.11 pooka pthread_mutex_unlock(&spc->spc_mtx);
304 1.11 pooka }
305 1.11 pooka
306 1.11 pooka static void
307 1.11 pooka spcrelease(struct spclient *spc)
308 1.11 pooka {
309 1.11 pooka int ref;
310 1.11 pooka
311 1.11 pooka pthread_mutex_lock(&spc->spc_mtx);
312 1.11 pooka ref = --spc->spc_refcnt;
313 1.11 pooka pthread_mutex_unlock(&spc->spc_mtx);
314 1.11 pooka
315 1.11 pooka if (ref > 0)
316 1.11 pooka return;
317 1.11 pooka
318 1.12 pooka DPRINTF(("spcrelease: spc %p fd %d\n", spc, spc->spc_fd));
319 1.12 pooka
320 1.13 pooka _DIAGASSERT(TAILQ_EMPTY(&spc->spc_respwait));
321 1.11 pooka _DIAGASSERT(spc->spc_buf == NULL);
322 1.11 pooka
323 1.11 pooka lwproc_switch(spc->spc_mainlwp);
324 1.11 pooka lwproc_release();
325 1.11 pooka spc->spc_mainlwp = NULL;
326 1.11 pooka
327 1.11 pooka close(spc->spc_fd);
328 1.11 pooka spc->spc_fd = -1;
329 1.12 pooka spc->spc_dying = 0;
330 1.11 pooka
331 1.11 pooka atomic_inc_uint(&disco);
332 1.11 pooka
333 1.11 pooka }
334 1.11 pooka
335 1.11 pooka static void
336 1.2 pooka serv_handledisco(unsigned int idx)
337 1.1 pooka {
338 1.1 pooka struct spclient *spc = &spclist[idx];
339 1.1 pooka
340 1.1 pooka DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
341 1.1 pooka
342 1.12 pooka pfdlist[idx].fd = -1;
343 1.12 pooka pfdlist[idx].revents = 0;
344 1.12 pooka pthread_mutex_lock(&spc->spc_mtx);
345 1.12 pooka spc->spc_dying = 1;
346 1.12 pooka kickall(spc);
347 1.12 pooka pthread_mutex_unlock(&spc->spc_mtx);
348 1.11 pooka spcrelease(spc);
349 1.1 pooka }
350 1.1 pooka
351 1.11 pooka static unsigned
352 1.11 pooka serv_handleconn(int fd, connecthook_fn connhook, int busy)
353 1.1 pooka {
354 1.1 pooka struct sockaddr_storage ss;
355 1.1 pooka socklen_t sl = sizeof(ss);
356 1.11 pooka int newfd, flags;
357 1.1 pooka unsigned i;
358 1.1 pooka
359 1.1 pooka /*LINTED: cast ok */
360 1.1 pooka newfd = accept(fd, (struct sockaddr *)&ss, &sl);
361 1.1 pooka if (newfd == -1)
362 1.11 pooka return 0;
363 1.1 pooka
364 1.11 pooka if (busy) {
365 1.1 pooka close(newfd); /* EBUSY */
366 1.11 pooka return 0;
367 1.1 pooka }
368 1.1 pooka
369 1.11 pooka /* XXX: should do some sort of handshake too */
370 1.11 pooka
371 1.1 pooka flags = fcntl(newfd, F_GETFL, 0);
372 1.1 pooka if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
373 1.1 pooka close(newfd);
374 1.11 pooka return 0;
375 1.1 pooka }
376 1.1 pooka
377 1.11 pooka if (connhook(newfd) != 0) {
378 1.1 pooka close(newfd);
379 1.11 pooka return 0;
380 1.1 pooka }
381 1.1 pooka
382 1.1 pooka /* find empty slot the simple way */
383 1.1 pooka for (i = 0; i < MAXCLI; i++) {
384 1.12 pooka if (pfdlist[i].fd == -1 && spclist[i].spc_dying == 0)
385 1.2 pooka break;
386 1.1 pooka }
387 1.1 pooka
388 1.11 pooka if (lwproc_newproc(&spclist[i]) != 0) {
389 1.10 pooka close(newfd);
390 1.11 pooka return 0;
391 1.10 pooka }
392 1.10 pooka
393 1.1 pooka assert(i < MAXCLI);
394 1.1 pooka
395 1.1 pooka pfdlist[i].fd = newfd;
396 1.1 pooka spclist[i].spc_fd = newfd;
397 1.8 pooka spclist[i].spc_mainlwp = lwproc_curlwp();
398 1.7 pooka spclist[i].spc_istatus = SPCSTATUS_BUSY; /* dedicated receiver */
399 1.8 pooka spclist[i].spc_pid = lwproc_getpid();
400 1.11 pooka spclist[i].spc_refcnt = 1;
401 1.7 pooka
402 1.7 pooka TAILQ_INIT(&spclist[i].spc_respwait);
403 1.1 pooka
404 1.13 pooka DPRINTF(("rump_sp: added new connection fd %d at idx %u, pid %d\n",
405 1.13 pooka newfd, i, lwproc_getpid()));
406 1.2 pooka
407 1.3 pooka lwproc_switch(NULL);
408 1.1 pooka
409 1.11 pooka return i;
410 1.1 pooka }
411 1.1 pooka
412 1.1 pooka static void
413 1.1 pooka serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
414 1.1 pooka {
415 1.7 pooka register_t retval[2] = {0, 0};
416 1.1 pooka int rv, sysnum;
417 1.1 pooka
418 1.1 pooka sysnum = (int)rhdr->rsp_sysnum;
419 1.1 pooka DPRINTF(("rump_sp: handling syscall %d from client %d\n",
420 1.1 pooka sysnum, 0));
421 1.1 pooka
422 1.8 pooka lwproc_newlwp(spc->spc_pid);
423 1.3 pooka rv = rumpsyscall(sysnum, data, retval);
424 1.3 pooka lwproc_switch(NULL);
425 1.6 pooka free(data);
426 1.1 pooka
427 1.7 pooka DPRINTF(("rump_sp: got return value %d & %d/%d\n",
428 1.7 pooka rv, retval[0], retval[1]));
429 1.5 pooka
430 1.1 pooka send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
431 1.1 pooka }
432 1.1 pooka
433 1.7 pooka struct sysbouncearg {
434 1.7 pooka struct spclient *sba_spc;
435 1.7 pooka struct rsp_hdr sba_hdr;
436 1.7 pooka uint8_t *sba_data;
437 1.7 pooka };
438 1.7 pooka static void *
439 1.7 pooka serv_syscallbouncer(void *arg)
440 1.7 pooka {
441 1.7 pooka struct sysbouncearg *barg = arg;
442 1.7 pooka
443 1.7 pooka serv_handlesyscall(barg->sba_spc, &barg->sba_hdr, barg->sba_data);
444 1.12 pooka spcrelease(barg->sba_spc);
445 1.7 pooka free(arg);
446 1.7 pooka return NULL;
447 1.7 pooka }
448 1.7 pooka
449 1.15 pooka static int
450 1.15 pooka sp_copyin(void *arg, const void *raddr, void *laddr, size_t *len, int wantstr)
451 1.1 pooka {
452 1.10 pooka struct spclient *spc = arg;
453 1.9 pooka void *rdata = NULL; /* XXXuninit */
454 1.13 pooka int rv, nlocks;
455 1.13 pooka
456 1.13 pooka rumpuser__kunlock(0, &nlocks, NULL);
457 1.1 pooka
458 1.15 pooka rv = copyin_req(spc, raddr, len, wantstr, &rdata);
459 1.12 pooka if (rv)
460 1.13 pooka goto out;
461 1.1 pooka
462 1.15 pooka memcpy(laddr, rdata, *len);
463 1.7 pooka free(rdata);
464 1.1 pooka
465 1.13 pooka out:
466 1.13 pooka rumpuser__klock(nlocks, NULL);
467 1.13 pooka if (rv)
468 1.13 pooka return EFAULT;
469 1.1 pooka return 0;
470 1.1 pooka }
471 1.1 pooka
472 1.1 pooka int
473 1.15 pooka rumpuser_sp_copyin(void *arg, const void *raddr, void *laddr, size_t len)
474 1.15 pooka {
475 1.15 pooka
476 1.15 pooka return sp_copyin(arg, raddr, laddr, &len, 0);
477 1.15 pooka }
478 1.15 pooka
479 1.15 pooka int
480 1.15 pooka rumpuser_sp_copyinstr(void *arg, const void *raddr, void *laddr, size_t *len)
481 1.15 pooka {
482 1.15 pooka
483 1.15 pooka return sp_copyin(arg, raddr, laddr, len, 1);
484 1.15 pooka }
485 1.15 pooka
486 1.15 pooka static int
487 1.15 pooka sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
488 1.1 pooka {
489 1.10 pooka struct spclient *spc = arg;
490 1.13 pooka int nlocks, rv;
491 1.13 pooka
492 1.13 pooka rumpuser__kunlock(0, &nlocks, NULL);
493 1.15 pooka rv = send_copyout_req(spc, raddr, laddr, dlen);
494 1.13 pooka rumpuser__klock(nlocks, NULL);
495 1.1 pooka
496 1.13 pooka if (rv)
497 1.7 pooka return EFAULT;
498 1.1 pooka return 0;
499 1.1 pooka }
500 1.1 pooka
501 1.1 pooka int
502 1.15 pooka rumpuser_sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
503 1.15 pooka {
504 1.15 pooka
505 1.15 pooka return sp_copyout(arg, laddr, raddr, dlen);
506 1.15 pooka }
507 1.15 pooka
508 1.15 pooka int
509 1.15 pooka rumpuser_sp_copyoutstr(void *arg, const void *laddr, void *raddr, size_t *dlen)
510 1.15 pooka {
511 1.15 pooka
512 1.15 pooka return sp_copyout(arg, laddr, raddr, *dlen);
513 1.15 pooka }
514 1.15 pooka
515 1.15 pooka int
516 1.10 pooka rumpuser_sp_anonmmap(void *arg, size_t howmuch, void **addr)
517 1.1 pooka {
518 1.10 pooka struct spclient *spc = arg;
519 1.7 pooka void *resp, *rdata;
520 1.13 pooka int nlocks, rv;
521 1.13 pooka
522 1.13 pooka rumpuser__kunlock(0, &nlocks, NULL);
523 1.1 pooka
524 1.7 pooka rv = anonmmap_req(spc, howmuch, &rdata);
525 1.13 pooka if (rv) {
526 1.13 pooka rv = EFAULT;
527 1.13 pooka goto out;
528 1.13 pooka }
529 1.1 pooka
530 1.7 pooka resp = *(void **)rdata;
531 1.7 pooka free(rdata);
532 1.1 pooka
533 1.7 pooka if (resp == NULL) {
534 1.13 pooka rv = ENOMEM;
535 1.1 pooka }
536 1.1 pooka
537 1.1 pooka *addr = resp;
538 1.13 pooka
539 1.13 pooka out:
540 1.13 pooka rumpuser__klock(nlocks, NULL);
541 1.13 pooka
542 1.13 pooka if (rv)
543 1.13 pooka return rv;
544 1.1 pooka return 0;
545 1.1 pooka }
546 1.1 pooka
547 1.1 pooka /*
548 1.1 pooka *
549 1.1 pooka * Startup routines and mainloop for server.
550 1.1 pooka *
551 1.1 pooka */
552 1.1 pooka
553 1.1 pooka struct spservarg {
554 1.1 pooka int sps_sock;
555 1.1 pooka connecthook_fn sps_connhook;
556 1.1 pooka };
557 1.1 pooka
558 1.14 pooka static pthread_attr_t pattr_detached;
559 1.7 pooka static void
560 1.7 pooka handlereq(struct spclient *spc)
561 1.7 pooka {
562 1.7 pooka struct sysbouncearg *sba;
563 1.7 pooka pthread_t pt;
564 1.7 pooka int rv;
565 1.7 pooka
566 1.7 pooka /* XXX: check that it's a syscall */
567 1.7 pooka
568 1.7 pooka sba = malloc(sizeof(*sba));
569 1.7 pooka if (sba == NULL) {
570 1.7 pooka /* panic */
571 1.7 pooka abort();
572 1.7 pooka }
573 1.7 pooka
574 1.7 pooka sba->sba_spc = spc;
575 1.7 pooka sba->sba_hdr = spc->spc_hdr;
576 1.7 pooka sba->sba_data = spc->spc_buf;
577 1.7 pooka
578 1.7 pooka spc->spc_buf = NULL;
579 1.7 pooka spc->spc_off = 0;
580 1.7 pooka
581 1.11 pooka spcref(spc);
582 1.14 pooka if ((rv = pthread_create(&pt, &pattr_detached,
583 1.14 pooka serv_syscallbouncer, sba)) != 0) {
584 1.7 pooka /* panic */
585 1.7 pooka abort();
586 1.7 pooka }
587 1.7 pooka }
588 1.7 pooka
589 1.1 pooka static void *
590 1.1 pooka spserver(void *arg)
591 1.1 pooka {
592 1.1 pooka struct spservarg *sarg = arg;
593 1.11 pooka struct spclient *spc;
594 1.1 pooka unsigned idx;
595 1.1 pooka int seen;
596 1.1 pooka int rv;
597 1.11 pooka unsigned int nfds, maxidx;
598 1.1 pooka
599 1.11 pooka for (idx = 0; idx < MAXCLI; idx++) {
600 1.1 pooka pfdlist[idx].fd = -1;
601 1.1 pooka pfdlist[idx].events = POLLIN;
602 1.11 pooka
603 1.11 pooka spc = &spclist[idx];
604 1.11 pooka pthread_mutex_init(&spc->spc_mtx, NULL);
605 1.11 pooka pthread_cond_init(&spc->spc_cv, NULL);
606 1.1 pooka }
607 1.1 pooka pfdlist[0].fd = sarg->sps_sock;
608 1.1 pooka pfdlist[0].events = POLLIN;
609 1.1 pooka nfds = 1;
610 1.1 pooka maxidx = 0;
611 1.1 pooka
612 1.14 pooka pthread_attr_init(&pattr_detached);
613 1.14 pooka pthread_attr_setdetachstate(&pattr_detached, PTHREAD_CREATE_DETACHED);
614 1.14 pooka
615 1.1 pooka DPRINTF(("rump_sp: server mainloop\n"));
616 1.1 pooka
617 1.1 pooka for (;;) {
618 1.11 pooka /* g/c hangarounds (eventually) */
619 1.11 pooka if (disco) {
620 1.11 pooka int discoed;
621 1.11 pooka
622 1.11 pooka discoed = atomic_swap_uint(&disco, 0);
623 1.11 pooka while (discoed--) {
624 1.11 pooka nfds--;
625 1.11 pooka idx = maxidx;
626 1.12 pooka while (idx) {
627 1.11 pooka if (pfdlist[idx].fd != -1) {
628 1.11 pooka maxidx = idx;
629 1.11 pooka break;
630 1.11 pooka }
631 1.12 pooka idx--;
632 1.11 pooka }
633 1.11 pooka DPRINTF(("rump_sp: set maxidx to [%u]\n",
634 1.11 pooka maxidx));
635 1.12 pooka assert(maxidx+1 >= nfds);
636 1.11 pooka }
637 1.11 pooka }
638 1.11 pooka
639 1.1 pooka DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
640 1.1 pooka seen = 0;
641 1.1 pooka rv = poll(pfdlist, maxidx+1, INFTIM);
642 1.1 pooka assert(maxidx+1 <= MAXCLI);
643 1.1 pooka assert(rv != 0);
644 1.1 pooka if (rv == -1) {
645 1.1 pooka if (errno == EINTR)
646 1.1 pooka continue;
647 1.1 pooka fprintf(stderr, "rump_spserver: poll returned %d\n",
648 1.1 pooka errno);
649 1.1 pooka break;
650 1.1 pooka }
651 1.1 pooka
652 1.12 pooka for (idx = 0; seen < rv && idx < MAXCLI; idx++) {
653 1.1 pooka if ((pfdlist[idx].revents & POLLIN) == 0)
654 1.1 pooka continue;
655 1.1 pooka
656 1.1 pooka seen++;
657 1.1 pooka DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
658 1.1 pooka idx, seen, rv));
659 1.1 pooka if (idx > 0) {
660 1.11 pooka spc = &spclist[idx];
661 1.1 pooka DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
662 1.1 pooka switch (readframe(spc)) {
663 1.1 pooka case 0:
664 1.1 pooka break;
665 1.1 pooka case -1:
666 1.2 pooka serv_handledisco(idx);
667 1.1 pooka break;
668 1.1 pooka default:
669 1.7 pooka switch (spc->spc_hdr.rsp_class) {
670 1.7 pooka case RUMPSP_RESP:
671 1.7 pooka kickwaiter(spc);
672 1.7 pooka break;
673 1.7 pooka case RUMPSP_REQ:
674 1.7 pooka handlereq(spc);
675 1.7 pooka break;
676 1.7 pooka default:
677 1.7 pooka printf("PANIC\n");
678 1.7 pooka abort();
679 1.7 pooka break;
680 1.7 pooka }
681 1.1 pooka break;
682 1.1 pooka }
683 1.11 pooka
684 1.1 pooka } else {
685 1.1 pooka DPRINTF(("rump_sp: mainloop new connection\n"));
686 1.11 pooka
687 1.11 pooka idx = serv_handleconn(pfdlist[0].fd,
688 1.11 pooka sarg->sps_connhook, nfds == MAXCLI);
689 1.11 pooka if (idx)
690 1.11 pooka nfds++;
691 1.11 pooka if (idx > maxidx)
692 1.11 pooka maxidx = idx;
693 1.12 pooka DPRINTF(("rump_sp: maxid now %d\n", maxidx));
694 1.1 pooka }
695 1.1 pooka }
696 1.1 pooka }
697 1.1 pooka
698 1.1 pooka return NULL;
699 1.1 pooka }
700 1.1 pooka
701 1.1 pooka int
702 1.5 pooka rumpuser_sp_init(const struct rumpuser_sp_ops *spopsp, const char *url)
703 1.1 pooka {
704 1.5 pooka pthread_t pt;
705 1.1 pooka struct spservarg *sarg;
706 1.1 pooka struct sockaddr *sap;
707 1.5 pooka char *p;
708 1.5 pooka unsigned idx;
709 1.5 pooka int error, s;
710 1.5 pooka
711 1.5 pooka p = strdup(url);
712 1.5 pooka if (p == NULL)
713 1.5 pooka return ENOMEM;
714 1.5 pooka error = parseurl(p, &sap, &idx, 1);
715 1.5 pooka free(p);
716 1.5 pooka if (error)
717 1.5 pooka return error;
718 1.5 pooka
719 1.5 pooka s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
720 1.5 pooka if (s == -1)
721 1.5 pooka return errno;
722 1.1 pooka
723 1.3 pooka spops = *spopsp;
724 1.5 pooka sarg = malloc(sizeof(*sarg));
725 1.5 pooka if (sarg == NULL) {
726 1.5 pooka close(s);
727 1.5 pooka return ENOMEM;
728 1.5 pooka }
729 1.5 pooka
730 1.5 pooka sarg->sps_sock = s;
731 1.5 pooka sarg->sps_connhook = parsetab[idx].connhook;
732 1.1 pooka
733 1.5 pooka /* sloppy error recovery */
734 1.1 pooka
735 1.5 pooka /*LINTED*/
736 1.5 pooka if (bind(s, sap, sap->sa_len) == -1) {
737 1.5 pooka fprintf(stderr, "rump_sp: server bind failed\n");
738 1.5 pooka return errno;
739 1.1 pooka }
740 1.5 pooka if (listen(s, 20) == -1) {
741 1.5 pooka fprintf(stderr, "rump_sp: server listen failed\n");
742 1.5 pooka return errno;
743 1.1 pooka }
744 1.1 pooka
745 1.5 pooka if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
746 1.5 pooka fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
747 1.1 pooka return errno;
748 1.1 pooka }
749 1.5 pooka pthread_detach(pt);
750 1.1 pooka
751 1.1 pooka return 0;
752 1.1 pooka }
753