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