rumpclient.c revision 1.18 1 1.18 pooka /* $NetBSD: rumpclient.c,v 1.18 2011/01/24 17:47:51 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.11 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 * Client side routines for rump syscall proxy.
30 1.1 pooka */
31 1.1 pooka
32 1.1 pooka #include <sys/cdefs.h>
33 1.1 pooka __RCSID("$NetBSD");
34 1.1 pooka
35 1.5 pooka #include <sys/param.h>
36 1.15 pooka #include <sys/event.h>
37 1.1 pooka #include <sys/mman.h>
38 1.1 pooka #include <sys/socket.h>
39 1.1 pooka
40 1.1 pooka #include <arpa/inet.h>
41 1.1 pooka #include <netinet/in.h>
42 1.1 pooka #include <netinet/tcp.h>
43 1.1 pooka
44 1.1 pooka #include <assert.h>
45 1.13 pooka #include <dlfcn.h>
46 1.1 pooka #include <errno.h>
47 1.1 pooka #include <fcntl.h>
48 1.13 pooka #include <link.h>
49 1.1 pooka #include <poll.h>
50 1.1 pooka #include <pthread.h>
51 1.11 pooka #include <signal.h>
52 1.1 pooka #include <stdarg.h>
53 1.18 pooka #include <stdbool.h>
54 1.1 pooka #include <stdio.h>
55 1.1 pooka #include <stdlib.h>
56 1.1 pooka #include <string.h>
57 1.1 pooka #include <unistd.h>
58 1.1 pooka
59 1.1 pooka #include <rump/rumpclient.h>
60 1.1 pooka
61 1.13 pooka #define HOSTOPS
62 1.13 pooka int (*host_socket)(int, int, int);
63 1.13 pooka int (*host_close)(int);
64 1.13 pooka int (*host_connect)(int, const struct sockaddr *, socklen_t);
65 1.15 pooka int (*host_fcntl)(int, int, ...);
66 1.13 pooka int (*host_poll)(struct pollfd *, nfds_t, int);
67 1.13 pooka ssize_t (*host_read)(int, void *, size_t);
68 1.13 pooka ssize_t (*host_sendto)(int, const void *, size_t, int,
69 1.13 pooka const struct sockaddr *, socklen_t);
70 1.13 pooka int (*host_setsockopt)(int, int, int, const void *, socklen_t);
71 1.13 pooka
72 1.17 pooka int (*host_kqueue)(void);
73 1.17 pooka int (*host_kevent)(int, const struct kevent *, size_t,
74 1.17 pooka struct kevent *, size_t, const struct timespec *);
75 1.17 pooka
76 1.1 pooka #include "sp_common.c"
77 1.1 pooka
78 1.11 pooka static struct spclient clispc = {
79 1.11 pooka .spc_fd = -1,
80 1.11 pooka };
81 1.1 pooka
82 1.18 pooka static int kq = -1;
83 1.15 pooka static sigset_t fullset;
84 1.12 pooka
85 1.18 pooka static int doconnect(int);
86 1.18 pooka static int handshake_req(struct spclient *, uint32_t *, int, bool);
87 1.18 pooka
88 1.18 pooka int didrecon;
89 1.18 pooka
90 1.18 pooka static int
91 1.18 pooka send_with_recon(struct spclient *spc, const void *data, size_t dlen)
92 1.18 pooka {
93 1.18 pooka int rv;
94 1.18 pooka
95 1.18 pooka do {
96 1.18 pooka rv = dosend(spc, data, dlen);
97 1.18 pooka if (__predict_false(rv == ENOTCONN || rv == EBADF)) {
98 1.18 pooka if ((rv = doconnect(1)) != 0)
99 1.18 pooka continue;
100 1.18 pooka if ((rv = handshake_req(&clispc, NULL, 0, true)) != 0)
101 1.18 pooka continue;
102 1.18 pooka rv = ENOTCONN;
103 1.18 pooka break;
104 1.18 pooka }
105 1.18 pooka } while (__predict_false(rv != 0));
106 1.18 pooka
107 1.18 pooka return rv;
108 1.18 pooka }
109 1.18 pooka
110 1.12 pooka static int
111 1.18 pooka cliwaitresp(struct spclient *spc, struct respwait *rw, sigset_t *mask,
112 1.18 pooka bool keeplock)
113 1.12 pooka {
114 1.18 pooka uint64_t mygen;
115 1.18 pooka bool imalive = true;
116 1.12 pooka
117 1.15 pooka pthread_mutex_lock(&spc->spc_mtx);
118 1.18 pooka if (!keeplock)
119 1.18 pooka sendunlockl(spc);
120 1.18 pooka mygen = spc->spc_generation;
121 1.12 pooka
122 1.12 pooka rw->rw_error = 0;
123 1.18 pooka while (!rw->rw_done && rw->rw_error == 0) {
124 1.18 pooka if (__predict_false(spc->spc_generation != mygen || !imalive))
125 1.18 pooka break;
126 1.18 pooka
127 1.12 pooka /* are we free to receive? */
128 1.12 pooka if (spc->spc_istatus == SPCSTATUS_FREE) {
129 1.15 pooka struct kevent kev[8];
130 1.15 pooka int gotresp, dosig, rv, i;
131 1.15 pooka
132 1.12 pooka spc->spc_istatus = SPCSTATUS_BUSY;
133 1.12 pooka pthread_mutex_unlock(&spc->spc_mtx);
134 1.12 pooka
135 1.15 pooka dosig = 0;
136 1.15 pooka for (gotresp = 0; !gotresp; ) {
137 1.15 pooka switch (readframe(spc)) {
138 1.15 pooka case 0:
139 1.17 pooka rv = host_kevent(kq, NULL, 0,
140 1.15 pooka kev, __arraycount(kev), NULL);
141 1.18 pooka
142 1.18 pooka /*
143 1.18 pooka * XXX: don't know how this can
144 1.18 pooka * happen (timeout cannot expire
145 1.18 pooka * since there isn't one), but
146 1.18 pooka * it does happen
147 1.18 pooka */
148 1.18 pooka if (__predict_false(rv == 0))
149 1.18 pooka continue;
150 1.18 pooka
151 1.15 pooka for (i = 0; i < rv; i++) {
152 1.15 pooka if (kev[i].filter
153 1.15 pooka == EVFILT_SIGNAL)
154 1.15 pooka dosig++;
155 1.15 pooka }
156 1.15 pooka if (dosig)
157 1.15 pooka goto cleanup;
158 1.15 pooka
159 1.15 pooka continue;
160 1.15 pooka case -1:
161 1.18 pooka imalive = false;
162 1.15 pooka goto cleanup;
163 1.15 pooka default:
164 1.15 pooka break;
165 1.15 pooka }
166 1.12 pooka
167 1.15 pooka switch (spc->spc_hdr.rsp_class) {
168 1.12 pooka case RUMPSP_RESP:
169 1.12 pooka case RUMPSP_ERROR:
170 1.12 pooka kickwaiter(spc);
171 1.15 pooka gotresp = spc->spc_hdr.rsp_reqno ==
172 1.15 pooka rw->rw_reqno;
173 1.12 pooka break;
174 1.12 pooka case RUMPSP_REQ:
175 1.12 pooka handlereq(spc);
176 1.12 pooka break;
177 1.12 pooka default:
178 1.12 pooka /* panic */
179 1.12 pooka break;
180 1.15 pooka }
181 1.12 pooka }
182 1.12 pooka
183 1.15 pooka cleanup:
184 1.15 pooka pthread_mutex_lock(&spc->spc_mtx);
185 1.15 pooka if (spc->spc_istatus == SPCSTATUS_WANTED)
186 1.15 pooka kickall(spc);
187 1.15 pooka spc->spc_istatus = SPCSTATUS_FREE;
188 1.15 pooka
189 1.15 pooka /* take one for the team */
190 1.15 pooka if (dosig) {
191 1.15 pooka pthread_mutex_unlock(&spc->spc_mtx);
192 1.15 pooka pthread_sigmask(SIG_SETMASK, mask, NULL);
193 1.15 pooka pthread_sigmask(SIG_SETMASK, &fullset, NULL);
194 1.15 pooka pthread_mutex_lock(&spc->spc_mtx);
195 1.15 pooka }
196 1.12 pooka } else {
197 1.12 pooka spc->spc_istatus = SPCSTATUS_WANTED;
198 1.12 pooka pthread_cond_wait(&rw->rw_cv, &spc->spc_mtx);
199 1.12 pooka }
200 1.12 pooka }
201 1.12 pooka TAILQ_REMOVE(&spc->spc_respwait, rw, rw_entries);
202 1.12 pooka pthread_mutex_unlock(&spc->spc_mtx);
203 1.12 pooka pthread_cond_destroy(&rw->rw_cv);
204 1.12 pooka
205 1.18 pooka if (spc->spc_generation != mygen || !imalive) {
206 1.12 pooka return ENOTCONN;
207 1.18 pooka }
208 1.12 pooka return rw->rw_error;
209 1.12 pooka }
210 1.12 pooka
211 1.1 pooka static int
212 1.3 pooka syscall_req(struct spclient *spc, int sysnum,
213 1.3 pooka const void *data, size_t dlen, void **resp)
214 1.1 pooka {
215 1.1 pooka struct rsp_hdr rhdr;
216 1.3 pooka struct respwait rw;
217 1.12 pooka sigset_t omask;
218 1.3 pooka int rv;
219 1.1 pooka
220 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + dlen;
221 1.3 pooka rhdr.rsp_class = RUMPSP_REQ;
222 1.3 pooka rhdr.rsp_type = RUMPSP_SYSCALL;
223 1.1 pooka rhdr.rsp_sysnum = sysnum;
224 1.1 pooka
225 1.12 pooka pthread_sigmask(SIG_SETMASK, &fullset, &omask);
226 1.6 pooka do {
227 1.6 pooka putwait(spc, &rw, &rhdr);
228 1.18 pooka if ((rv = send_with_recon(spc, &rhdr, sizeof(rhdr))) != 0) {
229 1.18 pooka unputwait(spc, &rw);
230 1.18 pooka continue;
231 1.18 pooka }
232 1.18 pooka if ((rv = send_with_recon(spc, data, dlen)) != 0) {
233 1.6 pooka unputwait(spc, &rw);
234 1.18 pooka continue;
235 1.6 pooka }
236 1.6 pooka
237 1.18 pooka rv = cliwaitresp(spc, &rw, &omask, false);
238 1.18 pooka } while (rv == ENOTCONN || rv == EAGAIN);
239 1.12 pooka pthread_sigmask(SIG_SETMASK, &omask, NULL);
240 1.3 pooka
241 1.3 pooka *resp = rw.rw_data;
242 1.3 pooka return rv;
243 1.1 pooka }
244 1.1 pooka
245 1.1 pooka static int
246 1.18 pooka handshake_req(struct spclient *spc, uint32_t *auth, int cancel, bool haslock)
247 1.10 pooka {
248 1.11 pooka struct handshake_fork rf;
249 1.10 pooka struct rsp_hdr rhdr;
250 1.10 pooka struct respwait rw;
251 1.12 pooka sigset_t omask;
252 1.10 pooka int rv;
253 1.10 pooka
254 1.10 pooka /* performs server handshake */
255 1.11 pooka rhdr.rsp_len = sizeof(rhdr) + (auth ? sizeof(rf) : 0);
256 1.10 pooka rhdr.rsp_class = RUMPSP_REQ;
257 1.10 pooka rhdr.rsp_type = RUMPSP_HANDSHAKE;
258 1.11 pooka if (auth)
259 1.11 pooka rhdr.rsp_handshake = HANDSHAKE_FORK;
260 1.11 pooka else
261 1.11 pooka rhdr.rsp_handshake = HANDSHAKE_GUEST;
262 1.10 pooka
263 1.12 pooka pthread_sigmask(SIG_SETMASK, &fullset, &omask);
264 1.18 pooka if (haslock)
265 1.18 pooka putwait_locked(spc, &rw, &rhdr);
266 1.18 pooka else
267 1.18 pooka putwait(spc, &rw, &rhdr);
268 1.10 pooka rv = dosend(spc, &rhdr, sizeof(rhdr));
269 1.11 pooka if (auth) {
270 1.11 pooka memcpy(rf.rf_auth, auth, AUTHLEN*sizeof(*auth));
271 1.11 pooka rf.rf_cancel = cancel;
272 1.18 pooka rv = send_with_recon(spc, &rf, sizeof(rf));
273 1.11 pooka }
274 1.18 pooka if (rv || cancel) {
275 1.18 pooka if (haslock)
276 1.18 pooka unputwait_locked(spc, &rw);
277 1.18 pooka else
278 1.18 pooka unputwait(spc, &rw);
279 1.18 pooka if (cancel) {
280 1.18 pooka pthread_sigmask(SIG_SETMASK, &omask, NULL);
281 1.18 pooka return rv;
282 1.18 pooka }
283 1.18 pooka } else {
284 1.18 pooka rv = cliwaitresp(spc, &rw, &omask, haslock);
285 1.10 pooka }
286 1.12 pooka pthread_sigmask(SIG_SETMASK, &omask, NULL);
287 1.10 pooka if (rv)
288 1.10 pooka return rv;
289 1.10 pooka
290 1.10 pooka rv = *(int *)rw.rw_data;
291 1.10 pooka free(rw.rw_data);
292 1.10 pooka
293 1.10 pooka return rv;
294 1.10 pooka }
295 1.10 pooka
296 1.10 pooka static int
297 1.11 pooka prefork_req(struct spclient *spc, void **resp)
298 1.11 pooka {
299 1.11 pooka struct rsp_hdr rhdr;
300 1.11 pooka struct respwait rw;
301 1.12 pooka sigset_t omask;
302 1.11 pooka int rv;
303 1.11 pooka
304 1.11 pooka rhdr.rsp_len = sizeof(rhdr);
305 1.11 pooka rhdr.rsp_class = RUMPSP_REQ;
306 1.11 pooka rhdr.rsp_type = RUMPSP_PREFORK;
307 1.11 pooka rhdr.rsp_error = 0;
308 1.11 pooka
309 1.12 pooka pthread_sigmask(SIG_SETMASK, &fullset, &omask);
310 1.18 pooka do {
311 1.18 pooka putwait(spc, &rw, &rhdr);
312 1.18 pooka rv = send_with_recon(spc, &rhdr, sizeof(rhdr));
313 1.18 pooka if (rv != 0) {
314 1.18 pooka unputwait(spc, &rw);
315 1.18 pooka continue;
316 1.18 pooka }
317 1.11 pooka
318 1.18 pooka rv = cliwaitresp(spc, &rw, &omask, false);
319 1.18 pooka } while (rv == ENOTCONN || rv == EAGAIN);
320 1.12 pooka pthread_sigmask(SIG_SETMASK, &omask, NULL);
321 1.18 pooka
322 1.11 pooka *resp = rw.rw_data;
323 1.11 pooka return rv;
324 1.11 pooka }
325 1.11 pooka
326 1.18 pooka /*
327 1.18 pooka * prevent response code from deadlocking with reconnect code
328 1.18 pooka */
329 1.11 pooka static int
330 1.18 pooka resp_sendlock(struct spclient *spc)
331 1.18 pooka {
332 1.18 pooka int rv = 0;
333 1.18 pooka
334 1.18 pooka pthread_mutex_lock(&spc->spc_mtx);
335 1.18 pooka while (spc->spc_ostatus != SPCSTATUS_FREE) {
336 1.18 pooka if (__predict_false(spc->spc_reconnecting)) {
337 1.18 pooka rv = EBUSY;
338 1.18 pooka goto out;
339 1.18 pooka }
340 1.18 pooka spc->spc_ostatus = SPCSTATUS_WANTED;
341 1.18 pooka pthread_cond_wait(&spc->spc_cv, &spc->spc_mtx);
342 1.18 pooka }
343 1.18 pooka spc->spc_ostatus = SPCSTATUS_BUSY;
344 1.18 pooka
345 1.18 pooka out:
346 1.18 pooka pthread_mutex_unlock(&spc->spc_mtx);
347 1.18 pooka return rv;
348 1.18 pooka }
349 1.18 pooka
350 1.18 pooka static void
351 1.5 pooka send_copyin_resp(struct spclient *spc, uint64_t reqno, void *data, size_t dlen,
352 1.5 pooka int wantstr)
353 1.1 pooka {
354 1.1 pooka struct rsp_hdr rhdr;
355 1.1 pooka
356 1.5 pooka if (wantstr)
357 1.5 pooka dlen = MIN(dlen, strlen(data)+1);
358 1.5 pooka
359 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + dlen;
360 1.1 pooka rhdr.rsp_reqno = reqno;
361 1.3 pooka rhdr.rsp_class = RUMPSP_RESP;
362 1.3 pooka rhdr.rsp_type = RUMPSP_COPYIN;
363 1.1 pooka rhdr.rsp_sysnum = 0;
364 1.1 pooka
365 1.18 pooka if (resp_sendlock(spc) != 0)
366 1.18 pooka return;
367 1.18 pooka (void)dosend(spc, &rhdr, sizeof(rhdr));
368 1.18 pooka (void)dosend(spc, data, dlen);
369 1.3 pooka sendunlock(spc);
370 1.1 pooka }
371 1.1 pooka
372 1.18 pooka static void
373 1.1 pooka send_anonmmap_resp(struct spclient *spc, uint64_t reqno, void *addr)
374 1.1 pooka {
375 1.1 pooka struct rsp_hdr rhdr;
376 1.1 pooka
377 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(addr);
378 1.1 pooka rhdr.rsp_reqno = reqno;
379 1.3 pooka rhdr.rsp_class = RUMPSP_RESP;
380 1.3 pooka rhdr.rsp_type = RUMPSP_ANONMMAP;
381 1.1 pooka rhdr.rsp_sysnum = 0;
382 1.1 pooka
383 1.18 pooka if (resp_sendlock(spc) != 0)
384 1.18 pooka return;
385 1.18 pooka (void)dosend(spc, &rhdr, sizeof(rhdr));
386 1.18 pooka (void)dosend(spc, &addr, sizeof(addr));
387 1.3 pooka sendunlock(spc);
388 1.1 pooka }
389 1.1 pooka
390 1.1 pooka int
391 1.1 pooka rumpclient_syscall(int sysnum, const void *data, size_t dlen,
392 1.1 pooka register_t *retval)
393 1.1 pooka {
394 1.1 pooka struct rsp_sysresp *resp;
395 1.3 pooka void *rdata;
396 1.3 pooka int rv;
397 1.3 pooka
398 1.3 pooka DPRINTF(("rumpsp syscall_req: syscall %d with %p/%zu\n",
399 1.3 pooka sysnum, data, dlen));
400 1.3 pooka
401 1.3 pooka rv = syscall_req(&clispc, sysnum, data, dlen, &rdata);
402 1.3 pooka if (rv)
403 1.3 pooka return rv;
404 1.3 pooka
405 1.3 pooka resp = rdata;
406 1.3 pooka DPRINTF(("rumpsp syscall_resp: syscall %d error %d, rv: %d/%d\n",
407 1.3 pooka sysnum, rv, resp->rsys_retval[0], resp->rsys_retval[1]));
408 1.1 pooka
409 1.3 pooka memcpy(retval, &resp->rsys_retval, sizeof(resp->rsys_retval));
410 1.3 pooka rv = resp->rsys_error;
411 1.3 pooka free(rdata);
412 1.1 pooka
413 1.3 pooka return rv;
414 1.3 pooka }
415 1.1 pooka
416 1.3 pooka static void
417 1.3 pooka handlereq(struct spclient *spc)
418 1.3 pooka {
419 1.3 pooka struct rsp_copydata *copydata;
420 1.16 pooka struct rsp_hdr *rhdr = &spc->spc_hdr;
421 1.3 pooka void *mapaddr;
422 1.3 pooka size_t maplen;
423 1.5 pooka int reqtype = spc->spc_hdr.rsp_type;
424 1.1 pooka
425 1.5 pooka switch (reqtype) {
426 1.3 pooka case RUMPSP_COPYIN:
427 1.5 pooka case RUMPSP_COPYINSTR:
428 1.3 pooka /*LINTED*/
429 1.3 pooka copydata = (struct rsp_copydata *)spc->spc_buf;
430 1.3 pooka DPRINTF(("rump_sp handlereq: copyin request: %p/%zu\n",
431 1.3 pooka copydata->rcp_addr, copydata->rcp_len));
432 1.3 pooka send_copyin_resp(spc, spc->spc_hdr.rsp_reqno,
433 1.5 pooka copydata->rcp_addr, copydata->rcp_len,
434 1.5 pooka reqtype == RUMPSP_COPYINSTR);
435 1.3 pooka break;
436 1.3 pooka case RUMPSP_COPYOUT:
437 1.5 pooka case RUMPSP_COPYOUTSTR:
438 1.3 pooka /*LINTED*/
439 1.3 pooka copydata = (struct rsp_copydata *)spc->spc_buf;
440 1.3 pooka DPRINTF(("rump_sp handlereq: copyout request: %p/%zu\n",
441 1.3 pooka copydata->rcp_addr, copydata->rcp_len));
442 1.3 pooka /*LINTED*/
443 1.3 pooka memcpy(copydata->rcp_addr, copydata->rcp_data,
444 1.3 pooka copydata->rcp_len);
445 1.3 pooka break;
446 1.3 pooka case RUMPSP_ANONMMAP:
447 1.3 pooka /*LINTED*/
448 1.3 pooka maplen = *(size_t *)spc->spc_buf;
449 1.3 pooka mapaddr = mmap(NULL, maplen, PROT_READ|PROT_WRITE,
450 1.3 pooka MAP_ANON, -1, 0);
451 1.3 pooka if (mapaddr == MAP_FAILED)
452 1.3 pooka mapaddr = NULL;
453 1.3 pooka DPRINTF(("rump_sp handlereq: anonmmap: %p\n", mapaddr));
454 1.3 pooka send_anonmmap_resp(spc, spc->spc_hdr.rsp_reqno, mapaddr);
455 1.3 pooka break;
456 1.16 pooka case RUMPSP_RAISE:
457 1.16 pooka DPRINTF(("rump_sp handlereq: raise sig %d\n", rhdr->rsp_signo));
458 1.18 pooka raise((int)rhdr->rsp_signo);
459 1.16 pooka /*
460 1.16 pooka * We most likely have signals blocked, but the signal
461 1.16 pooka * will be handled soon enough when we return.
462 1.16 pooka */
463 1.16 pooka break;
464 1.3 pooka default:
465 1.12 pooka printf("PANIC: INVALID TYPE %d\n", reqtype);
466 1.3 pooka abort();
467 1.3 pooka break;
468 1.1 pooka }
469 1.1 pooka
470 1.6 pooka spcfreebuf(spc);
471 1.1 pooka }
472 1.1 pooka
473 1.11 pooka static unsigned ptab_idx;
474 1.11 pooka static struct sockaddr *serv_sa;
475 1.11 pooka
476 1.11 pooka static int
477 1.18 pooka doconnect(int retry)
478 1.1 pooka {
479 1.18 pooka time_t prevreconmsg;
480 1.18 pooka unsigned reconretries;
481 1.18 pooka struct respwait rw;
482 1.18 pooka struct rsp_hdr rhdr;
483 1.15 pooka struct kevent kev[NSIG+1];
484 1.9 pooka char banner[MAXBANNER];
485 1.18 pooka struct pollfd pfd;
486 1.15 pooka int s, error, flags, i;
487 1.9 pooka ssize_t n;
488 1.1 pooka
489 1.18 pooka if (kq != -1)
490 1.18 pooka host_close(kq);
491 1.18 pooka kq = -1;
492 1.18 pooka
493 1.18 pooka prevreconmsg = 0;
494 1.18 pooka reconretries = 0;
495 1.18 pooka
496 1.18 pooka again:
497 1.18 pooka if (clispc.spc_fd != -1)
498 1.18 pooka host_close(clispc.spc_fd);
499 1.18 pooka clispc.spc_fd = -1;
500 1.18 pooka
501 1.18 pooka /*
502 1.18 pooka * for reconnect, gate everyone out of the receiver code
503 1.18 pooka */
504 1.18 pooka putwait_locked(&clispc, &rw, &rhdr);
505 1.18 pooka
506 1.18 pooka pthread_mutex_lock(&clispc.spc_mtx);
507 1.18 pooka clispc.spc_reconnecting = 1;
508 1.18 pooka pthread_cond_broadcast(&clispc.spc_cv);
509 1.18 pooka clispc.spc_generation++;
510 1.18 pooka while (clispc.spc_istatus != SPCSTATUS_FREE) {
511 1.18 pooka clispc.spc_istatus = SPCSTATUS_WANTED;
512 1.18 pooka pthread_cond_wait(&rw.rw_cv, &clispc.spc_mtx);
513 1.18 pooka }
514 1.18 pooka kickall(&clispc);
515 1.18 pooka
516 1.18 pooka /*
517 1.18 pooka * we can release it already since we hold the
518 1.18 pooka * send lock during reconnect
519 1.18 pooka * XXX: assert it
520 1.18 pooka */
521 1.18 pooka clispc.spc_istatus = SPCSTATUS_FREE;
522 1.18 pooka pthread_mutex_unlock(&clispc.spc_mtx);
523 1.18 pooka unputwait_locked(&clispc, &rw);
524 1.18 pooka
525 1.18 pooka free(clispc.spc_buf);
526 1.18 pooka clispc.spc_off = 0;
527 1.18 pooka
528 1.13 pooka s = host_socket(parsetab[ptab_idx].domain, SOCK_STREAM, 0);
529 1.11 pooka if (s == -1)
530 1.2 pooka return -1;
531 1.1 pooka
532 1.18 pooka pfd.fd = s;
533 1.18 pooka pfd.events = POLLIN;
534 1.18 pooka while (host_connect(s, serv_sa, (socklen_t)serv_sa->sa_len) == -1) {
535 1.18 pooka if (errno == EINTR)
536 1.18 pooka continue;
537 1.18 pooka if (!retry) {
538 1.18 pooka error = errno;
539 1.18 pooka fprintf(stderr, "rump_sp: client connect failed: %s\n",
540 1.18 pooka strerror(errno));
541 1.18 pooka errno = error;
542 1.18 pooka return -1;
543 1.18 pooka }
544 1.18 pooka
545 1.18 pooka if (prevreconmsg == 0) {
546 1.18 pooka fprintf(stderr, "rump_sp: connection to kernel lost, "
547 1.18 pooka "trying to reconnect ...\n");
548 1.18 pooka prevreconmsg = time(NULL);
549 1.18 pooka }
550 1.18 pooka if (time(NULL) - prevreconmsg > 120) {
551 1.18 pooka fprintf(stderr, "rump_sp: still trying to "
552 1.18 pooka "reconnect ...\n");
553 1.18 pooka prevreconmsg = time(NULL);
554 1.18 pooka }
555 1.18 pooka
556 1.18 pooka /* adhoc backoff timer */
557 1.18 pooka if (reconretries++ < 10) {
558 1.18 pooka usleep(100000 * reconretries);
559 1.18 pooka } else {
560 1.18 pooka sleep(MIN(10, reconretries-9));
561 1.18 pooka }
562 1.18 pooka goto again;
563 1.2 pooka }
564 1.1 pooka
565 1.11 pooka if ((error = parsetab[ptab_idx].connhook(s)) != 0) {
566 1.2 pooka error = errno;
567 1.11 pooka fprintf(stderr, "rump_sp: connect hook failed\n");
568 1.2 pooka errno = error;
569 1.2 pooka return -1;
570 1.1 pooka }
571 1.4 pooka
572 1.13 pooka if ((n = host_read(s, banner, sizeof(banner)-1)) < 0) {
573 1.2 pooka error = errno;
574 1.10 pooka fprintf(stderr, "rump_sp: failed to read banner\n");
575 1.2 pooka errno = error;
576 1.2 pooka return -1;
577 1.1 pooka }
578 1.9 pooka
579 1.9 pooka if (banner[n-1] != '\n') {
580 1.9 pooka fprintf(stderr, "rump_sp: invalid banner\n");
581 1.9 pooka errno = EINVAL;
582 1.9 pooka return -1;
583 1.9 pooka }
584 1.9 pooka banner[n] = '\0';
585 1.18 pooka /* parse the banner some day */
586 1.9 pooka
587 1.15 pooka flags = host_fcntl(s, F_GETFL, 0);
588 1.15 pooka if (host_fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1) {
589 1.18 pooka fprintf(stderr, "rump_sp: socket fd NONBLOCK: %s\n",
590 1.18 pooka strerror(errno));
591 1.15 pooka errno = EINVAL;
592 1.15 pooka return -1;
593 1.15 pooka }
594 1.18 pooka clispc.spc_fd = s;
595 1.18 pooka clispc.spc_state = SPCSTATE_RUNNING;
596 1.18 pooka clispc.spc_reconnecting = 0;
597 1.15 pooka
598 1.18 pooka if (prevreconmsg) {
599 1.18 pooka fprintf(stderr, "rump_sp: reconnected!\n");
600 1.18 pooka }
601 1.9 pooka
602 1.15 pooka /* setup kqueue, we want all signals and the fd */
603 1.17 pooka if ((kq = host_kqueue()) == -1) {
604 1.15 pooka error = errno;
605 1.15 pooka fprintf(stderr, "rump_sp: cannot setup kqueue");
606 1.15 pooka errno = error;
607 1.15 pooka return -1;
608 1.15 pooka }
609 1.15 pooka
610 1.15 pooka for (i = 0; i < NSIG; i++) {
611 1.15 pooka EV_SET(&kev[i], i+1, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0, 0, 0);
612 1.15 pooka }
613 1.18 pooka EV_SET(&kev[NSIG], clispc.spc_fd,
614 1.18 pooka EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
615 1.17 pooka if (host_kevent(kq, kev, NSIG+1, NULL, 0, NULL) == -1) {
616 1.15 pooka error = errno;
617 1.15 pooka fprintf(stderr, "rump_sp: kevent() failed");
618 1.15 pooka errno = error;
619 1.15 pooka return -1;
620 1.15 pooka }
621 1.15 pooka
622 1.18 pooka return 0;
623 1.18 pooka }
624 1.18 pooka
625 1.18 pooka static int
626 1.18 pooka doinit(void)
627 1.18 pooka {
628 1.18 pooka
629 1.11 pooka TAILQ_INIT(&clispc.spc_respwait);
630 1.11 pooka pthread_mutex_init(&clispc.spc_mtx, NULL);
631 1.11 pooka pthread_cond_init(&clispc.spc_cv, NULL);
632 1.11 pooka
633 1.11 pooka return 0;
634 1.11 pooka }
635 1.11 pooka
636 1.13 pooka void *(*rumpclient_dlsym)(void *, const char *);
637 1.13 pooka
638 1.11 pooka int
639 1.11 pooka rumpclient_init()
640 1.11 pooka {
641 1.11 pooka char *p;
642 1.11 pooka int error;
643 1.11 pooka
644 1.13 pooka /* dlsym overrided by rumphijack? */
645 1.13 pooka if (!rumpclient_dlsym)
646 1.13 pooka rumpclient_dlsym = dlsym;
647 1.13 pooka
648 1.13 pooka /*
649 1.13 pooka * sag mir, wo die symbol sind. zogen fort, der krieg beginnt.
650 1.13 pooka * wann wird man je verstehen? wann wird man je verstehen?
651 1.13 pooka */
652 1.13 pooka #define FINDSYM2(_name_,_syscall_) \
653 1.13 pooka if ((host_##_name_ = rumpclient_dlsym(RTLD_NEXT, \
654 1.13 pooka #_syscall_)) == NULL) \
655 1.13 pooka /* host_##_name_ = _syscall_ */;
656 1.13 pooka #define FINDSYM(_name_) FINDSYM2(_name_,_name_)
657 1.13 pooka FINDSYM2(socket,__socket30);
658 1.13 pooka FINDSYM(close);
659 1.13 pooka FINDSYM(connect);
660 1.15 pooka FINDSYM(fcntl);
661 1.13 pooka FINDSYM(poll);
662 1.13 pooka FINDSYM(read);
663 1.13 pooka FINDSYM(sendto);
664 1.13 pooka FINDSYM(setsockopt);
665 1.17 pooka FINDSYM(kqueue);
666 1.17 pooka FINDSYM(kevent);
667 1.13 pooka #undef FINDSYM
668 1.13 pooka #undef FINDSY2
669 1.13 pooka
670 1.11 pooka if ((p = getenv("RUMP_SERVER")) == NULL) {
671 1.11 pooka errno = ENOENT;
672 1.11 pooka return -1;
673 1.11 pooka }
674 1.11 pooka
675 1.11 pooka if ((error = parseurl(p, &serv_sa, &ptab_idx, 0)) != 0) {
676 1.11 pooka errno = error;
677 1.11 pooka return -1;
678 1.11 pooka }
679 1.11 pooka
680 1.18 pooka if (doinit() == -1)
681 1.18 pooka return -1;
682 1.18 pooka if (doconnect(0) == -1)
683 1.11 pooka return -1;
684 1.11 pooka
685 1.18 pooka error = handshake_req(&clispc, NULL, 0, false);
686 1.11 pooka if (error) {
687 1.11 pooka pthread_mutex_destroy(&clispc.spc_mtx);
688 1.11 pooka pthread_cond_destroy(&clispc.spc_cv);
689 1.18 pooka if (clispc.spc_fd != -1)
690 1.18 pooka host_close(clispc.spc_fd);
691 1.10 pooka errno = error;
692 1.10 pooka return -1;
693 1.10 pooka }
694 1.10 pooka
695 1.12 pooka sigfillset(&fullset);
696 1.11 pooka return 0;
697 1.11 pooka }
698 1.11 pooka
699 1.11 pooka struct rumpclient_fork {
700 1.11 pooka uint32_t fork_auth[AUTHLEN];
701 1.11 pooka };
702 1.11 pooka
703 1.11 pooka struct rumpclient_fork *
704 1.11 pooka rumpclient_prefork(void)
705 1.11 pooka {
706 1.11 pooka struct rumpclient_fork *rpf;
707 1.11 pooka void *resp;
708 1.11 pooka int rv;
709 1.11 pooka
710 1.11 pooka rpf = malloc(sizeof(*rpf));
711 1.11 pooka if (rpf == NULL)
712 1.11 pooka return NULL;
713 1.11 pooka
714 1.11 pooka if ((rv = prefork_req(&clispc, &resp)) != 0) {
715 1.11 pooka free(rpf);
716 1.11 pooka errno = rv;
717 1.11 pooka return NULL;
718 1.11 pooka }
719 1.11 pooka
720 1.11 pooka memcpy(rpf->fork_auth, resp, sizeof(rpf->fork_auth));
721 1.11 pooka free(resp);
722 1.11 pooka
723 1.11 pooka return rpf;
724 1.11 pooka }
725 1.11 pooka
726 1.11 pooka int
727 1.11 pooka rumpclient_fork_init(struct rumpclient_fork *rpf)
728 1.11 pooka {
729 1.11 pooka int error;
730 1.11 pooka
731 1.11 pooka memset(&clispc, 0, sizeof(clispc));
732 1.11 pooka clispc.spc_fd = -1;
733 1.18 pooka kq = -1;
734 1.11 pooka
735 1.18 pooka if (doinit() == -1)
736 1.18 pooka return -1;
737 1.18 pooka if (doconnect(1) == -1)
738 1.11 pooka return -1;
739 1.10 pooka
740 1.18 pooka error = handshake_req(&clispc, rpf->fork_auth, 0, false);
741 1.10 pooka if (error) {
742 1.10 pooka pthread_mutex_destroy(&clispc.spc_mtx);
743 1.10 pooka pthread_cond_destroy(&clispc.spc_cv);
744 1.11 pooka errno = error;
745 1.11 pooka return -1;
746 1.10 pooka }
747 1.11 pooka
748 1.11 pooka return 0;
749 1.1 pooka }
750