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