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