hijack.c revision 1.6 1 1.6 pooka /* $NetBSD: hijack.c,v 1.6 2011/01/09 14:15:06 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 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 #include <sys/cdefs.h>
29 1.6 pooka __RCSID("$NetBSD: hijack.c,v 1.6 2011/01/09 14:15:06 pooka Exp $");
30 1.1 pooka
31 1.1 pooka #include <sys/param.h>
32 1.1 pooka #include <sys/types.h>
33 1.1 pooka #include <sys/ioctl.h>
34 1.1 pooka #include <sys/socket.h>
35 1.1 pooka #include <sys/poll.h>
36 1.1 pooka
37 1.1 pooka #include <rump/rump.h>
38 1.1 pooka #include <rump/rumpclient.h>
39 1.1 pooka #include <rump/rump_syscalls.h>
40 1.1 pooka
41 1.1 pooka #include <assert.h>
42 1.1 pooka #include <dlfcn.h>
43 1.1 pooka #include <err.h>
44 1.1 pooka #include <errno.h>
45 1.1 pooka #include <fcntl.h>
46 1.1 pooka #include <poll.h>
47 1.1 pooka #include <pthread.h>
48 1.3 pooka #include <signal.h>
49 1.1 pooka #include <stdarg.h>
50 1.1 pooka #include <stdio.h>
51 1.1 pooka #include <stdlib.h>
52 1.3 pooka #include <time.h>
53 1.1 pooka #include <unistd.h>
54 1.1 pooka
55 1.1 pooka enum { RUMPCALL_SOCKET, RUMPCALL_ACCEPT, RUMPCALL_BIND, RUMPCALL_CONNECT,
56 1.1 pooka RUMPCALL_GETPEERNAME, RUMPCALL_GETSOCKNAME, RUMPCALL_LISTEN,
57 1.1 pooka RUMPCALL_RECVFROM, RUMPCALL_RECVMSG,
58 1.1 pooka RUMPCALL_SENDTO, RUMPCALL_SENDMSG,
59 1.1 pooka RUMPCALL_GETSOCKOPT, RUMPCALL_SETSOCKOPT,
60 1.1 pooka RUMPCALL_SHUTDOWN,
61 1.1 pooka RUMPCALL_READ, RUMPCALL_READV,
62 1.1 pooka RUMPCALL_WRITE, RUMPCALL_WRITEV,
63 1.1 pooka RUMPCALL_IOCTL, RUMPCALL_FCNTL,
64 1.1 pooka RUMPCALL_CLOSE,
65 1.4 pooka RUMPCALL_POLLTS,
66 1.1 pooka RUMPCALL__NUM
67 1.1 pooka };
68 1.1 pooka
69 1.1 pooka const char *sysnames[] = {
70 1.1 pooka "__socket30",
71 1.1 pooka "accept",
72 1.1 pooka "bind",
73 1.1 pooka "connect",
74 1.1 pooka "getpeername",
75 1.1 pooka "getsockname",
76 1.1 pooka "listen",
77 1.1 pooka "recvfrom",
78 1.1 pooka "recvmsg",
79 1.1 pooka "sendto",
80 1.1 pooka "sendmsg",
81 1.1 pooka "getsockopt",
82 1.1 pooka "setsockopt",
83 1.1 pooka "shutdown",
84 1.1 pooka "read",
85 1.1 pooka "readv",
86 1.1 pooka "write",
87 1.1 pooka "writev",
88 1.1 pooka "ioctl",
89 1.1 pooka "fcntl",
90 1.1 pooka "close",
91 1.1 pooka "__pollts50",
92 1.1 pooka };
93 1.1 pooka
94 1.1 pooka static ssize_t (*host_read)(int, void *, size_t);
95 1.1 pooka static ssize_t (*host_readv)(int, const struct iovec *, int);
96 1.1 pooka static ssize_t (*host_write)(int, const void *, size_t);
97 1.1 pooka static ssize_t (*host_writev)(int, const struct iovec *, int);
98 1.1 pooka static int (*host_ioctl)(int, unsigned long, ...);
99 1.1 pooka static int (*host_fcntl)(int, int, ...);
100 1.1 pooka static int (*host_close)(int);
101 1.3 pooka static int (*host_pollts)(struct pollfd *, nfds_t,
102 1.3 pooka const struct timespec *, const sigset_t *);
103 1.2 pooka static pid_t (*host_fork)(void);
104 1.2 pooka static int (*host_dup2)(int, int);
105 1.1 pooka
106 1.1 pooka static void *rumpcalls[RUMPCALL__NUM];
107 1.1 pooka
108 1.1 pooka /*
109 1.1 pooka * This is called from librumpclient in case of LD_PRELOAD.
110 1.1 pooka * It ensures correct RTLD_NEXT.
111 1.1 pooka */
112 1.1 pooka static void *
113 1.1 pooka hijackdlsym(void *handle, const char *symbol)
114 1.1 pooka {
115 1.1 pooka
116 1.1 pooka return dlsym(handle, symbol);
117 1.1 pooka }
118 1.1 pooka
119 1.1 pooka static void __attribute__((constructor))
120 1.1 pooka rcinit(void)
121 1.1 pooka {
122 1.1 pooka int (*rumpcinit)(void);
123 1.1 pooka void **rumpcdlsym;
124 1.1 pooka void *hand;
125 1.1 pooka int i;
126 1.1 pooka
127 1.1 pooka hand = dlopen("librumpclient.so", RTLD_LAZY|RTLD_GLOBAL);
128 1.1 pooka if (!hand)
129 1.1 pooka err(1, "cannot open librumpclient.so");
130 1.1 pooka rumpcinit = dlsym(hand, "rumpclient_init");
131 1.1 pooka _DIAGASSERT(rumpcinit);
132 1.1 pooka
133 1.1 pooka rumpcdlsym = dlsym(hand, "rumpclient_dlsym");
134 1.1 pooka *rumpcdlsym = hijackdlsym;
135 1.1 pooka
136 1.1 pooka host_read = dlsym(RTLD_NEXT, "read");
137 1.1 pooka host_readv = dlsym(RTLD_NEXT, "readv");
138 1.1 pooka host_write = dlsym(RTLD_NEXT, "write");
139 1.1 pooka host_writev = dlsym(RTLD_NEXT, "writev");
140 1.1 pooka host_ioctl = dlsym(RTLD_NEXT, "ioctl");
141 1.1 pooka host_fcntl = dlsym(RTLD_NEXT, "fcntl");
142 1.1 pooka host_close = dlsym(RTLD_NEXT, "close");
143 1.3 pooka host_pollts = dlsym(RTLD_NEXT, "pollts");
144 1.2 pooka host_fork = dlsym(RTLD_NEXT, "fork");
145 1.2 pooka host_dup2 = dlsym(RTLD_NEXT, "dup2");
146 1.1 pooka
147 1.1 pooka for (i = 0; i < RUMPCALL__NUM; i++) {
148 1.1 pooka char sysname[128];
149 1.1 pooka
150 1.1 pooka snprintf(sysname, sizeof(sysname), "rump_sys_%s", sysnames[i]);
151 1.1 pooka rumpcalls[i] = dlsym(hand, sysname);
152 1.1 pooka if (!rumpcalls[i]) {
153 1.3 pooka fprintf(stderr, "cannot find symbol: %s\n", sysname);
154 1.1 pooka exit(1);
155 1.1 pooka }
156 1.1 pooka }
157 1.1 pooka
158 1.1 pooka if (rumpcinit() == -1)
159 1.1 pooka err(1, "rumpclient init");
160 1.1 pooka }
161 1.1 pooka
162 1.5 pooka static unsigned dup2mask;
163 1.5 pooka #define ISDUP2D(fd) (((fd+1) & dup2mask) == ((fd)+1))
164 1.5 pooka
165 1.1 pooka //#define DEBUGJACK
166 1.1 pooka #ifdef DEBUGJACK
167 1.5 pooka #define DPRINTF(x) mydprintf x
168 1.5 pooka static void
169 1.5 pooka mydprintf(const char *fmt, ...)
170 1.5 pooka {
171 1.5 pooka va_list ap;
172 1.5 pooka
173 1.5 pooka if (ISDUP2D(STDERR_FILENO))
174 1.5 pooka return;
175 1.5 pooka
176 1.5 pooka va_start(ap, fmt);
177 1.5 pooka vfprintf(stderr, fmt, ap);
178 1.5 pooka va_end(ap);
179 1.5 pooka }
180 1.5 pooka
181 1.1 pooka #else
182 1.1 pooka #define DPRINTF(x)
183 1.1 pooka #endif
184 1.1 pooka
185 1.2 pooka /* XXX: need runtime selection. low for now due to FD_SETSIZE */
186 1.2 pooka #define HIJACK_FDOFF 128
187 1.2 pooka #define HIJACK_SELECT 128 /* XXX */
188 1.2 pooka #define HIJACK_ASSERT 128 /* XXX */
189 1.2 pooka static int
190 1.2 pooka fd_rump2host(int fd)
191 1.2 pooka {
192 1.2 pooka
193 1.2 pooka if (fd == -1)
194 1.2 pooka return fd;
195 1.2 pooka
196 1.2 pooka if (!ISDUP2D(fd))
197 1.2 pooka fd += HIJACK_FDOFF;
198 1.2 pooka
199 1.2 pooka return fd;
200 1.2 pooka }
201 1.2 pooka
202 1.2 pooka static int
203 1.2 pooka fd_host2rump(int fd)
204 1.2 pooka {
205 1.2 pooka
206 1.2 pooka if (!ISDUP2D(fd))
207 1.2 pooka fd -= HIJACK_FDOFF;
208 1.2 pooka return fd;
209 1.2 pooka }
210 1.2 pooka
211 1.2 pooka static bool
212 1.2 pooka fd_isrump(int fd)
213 1.2 pooka {
214 1.2 pooka
215 1.2 pooka return ISDUP2D(fd) || fd >= HIJACK_FDOFF;
216 1.2 pooka }
217 1.2 pooka
218 1.2 pooka #define assertfd(_fd_) assert(ISDUP2D(_fd_) || (_fd_) >= HIJACK_ASSERT)
219 1.2 pooka #undef HIJACK_FDOFF
220 1.2 pooka
221 1.1 pooka /*
222 1.1 pooka * Following wrappers always call the rump kernel.
223 1.1 pooka */
224 1.1 pooka
225 1.1 pooka int __socket30(int, int, int);
226 1.1 pooka int
227 1.1 pooka __socket30(int domain, int type, int protocol)
228 1.1 pooka {
229 1.1 pooka int (*rc_socket)(int, int, int);
230 1.1 pooka int fd;
231 1.1 pooka
232 1.1 pooka rc_socket = rumpcalls[RUMPCALL_SOCKET];
233 1.1 pooka fd = rc_socket(domain, type, protocol);
234 1.2 pooka
235 1.2 pooka DPRINTF(("socket <- %d\n", fd_rump2host(fd)));
236 1.2 pooka
237 1.2 pooka return fd_rump2host(fd);
238 1.1 pooka }
239 1.1 pooka
240 1.1 pooka int
241 1.1 pooka accept(int s, struct sockaddr *addr, socklen_t *addrlen)
242 1.1 pooka {
243 1.1 pooka int (*rc_accept)(int, struct sockaddr *, socklen_t *);
244 1.1 pooka int fd;
245 1.1 pooka
246 1.2 pooka DPRINTF(("accept -> %d", s));
247 1.1 pooka assertfd(s);
248 1.1 pooka rc_accept = rumpcalls[RUMPCALL_ACCEPT];
249 1.2 pooka fd = rc_accept(fd_host2rump(s), addr, addrlen);
250 1.2 pooka DPRINTF((" <- %d\n", fd_rump2host(fd)));
251 1.2 pooka
252 1.2 pooka return fd_rump2host(fd);
253 1.1 pooka }
254 1.1 pooka
255 1.1 pooka int
256 1.1 pooka bind(int s, const struct sockaddr *name, socklen_t namelen)
257 1.1 pooka {
258 1.1 pooka int (*rc_bind)(int, const struct sockaddr *, socklen_t);
259 1.1 pooka
260 1.2 pooka DPRINTF(("bind -> %d\n", s));
261 1.1 pooka assertfd(s);
262 1.1 pooka rc_bind = rumpcalls[RUMPCALL_BIND];
263 1.2 pooka
264 1.2 pooka return rc_bind(fd_host2rump(s), name, namelen);
265 1.1 pooka }
266 1.1 pooka
267 1.1 pooka int
268 1.1 pooka connect(int s, const struct sockaddr *name, socklen_t namelen)
269 1.1 pooka {
270 1.1 pooka int (*rc_connect)(int, const struct sockaddr *, socklen_t);
271 1.1 pooka
272 1.2 pooka DPRINTF(("connect -> %d\n", s));
273 1.1 pooka assertfd(s);
274 1.1 pooka rc_connect = rumpcalls[RUMPCALL_CONNECT];
275 1.2 pooka
276 1.2 pooka return rc_connect(fd_host2rump(s), name, namelen);
277 1.1 pooka }
278 1.1 pooka
279 1.1 pooka int
280 1.1 pooka getpeername(int s, struct sockaddr *name, socklen_t *namelen)
281 1.1 pooka {
282 1.1 pooka int (*rc_getpeername)(int, struct sockaddr *, socklen_t *);
283 1.1 pooka
284 1.2 pooka DPRINTF(("getpeername -> %d\n", s));
285 1.1 pooka assertfd(s);
286 1.1 pooka rc_getpeername = rumpcalls[RUMPCALL_GETPEERNAME];
287 1.2 pooka return rc_getpeername(fd_host2rump(s), name, namelen);
288 1.1 pooka }
289 1.1 pooka
290 1.1 pooka int
291 1.1 pooka getsockname(int s, struct sockaddr *name, socklen_t *namelen)
292 1.1 pooka {
293 1.1 pooka int (*rc_getsockname)(int, struct sockaddr *, socklen_t *);
294 1.1 pooka
295 1.2 pooka DPRINTF(("getsockname -> %d\n", s));
296 1.1 pooka assertfd(s);
297 1.1 pooka rc_getsockname = rumpcalls[RUMPCALL_GETSOCKNAME];
298 1.2 pooka return rc_getsockname(fd_host2rump(s), name, namelen);
299 1.1 pooka }
300 1.1 pooka
301 1.1 pooka int
302 1.1 pooka listen(int s, int backlog)
303 1.1 pooka {
304 1.1 pooka int (*rc_listen)(int, int);
305 1.1 pooka
306 1.2 pooka DPRINTF(("listen -> %d\n", s));
307 1.1 pooka assertfd(s);
308 1.1 pooka rc_listen = rumpcalls[RUMPCALL_LISTEN];
309 1.2 pooka return rc_listen(fd_host2rump(s), backlog);
310 1.1 pooka }
311 1.1 pooka
312 1.1 pooka ssize_t
313 1.1 pooka recv(int s, void *buf, size_t len, int flags)
314 1.1 pooka {
315 1.1 pooka
316 1.1 pooka return recvfrom(s, buf, len, flags, NULL, NULL);
317 1.1 pooka }
318 1.1 pooka
319 1.1 pooka ssize_t
320 1.1 pooka recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from,
321 1.1 pooka socklen_t *fromlen)
322 1.1 pooka {
323 1.1 pooka int (*rc_recvfrom)(int, void *, size_t, int,
324 1.1 pooka struct sockaddr *, socklen_t *);
325 1.1 pooka
326 1.1 pooka DPRINTF(("recvfrom\n"));
327 1.1 pooka assertfd(s);
328 1.1 pooka rc_recvfrom = rumpcalls[RUMPCALL_RECVFROM];
329 1.2 pooka return rc_recvfrom(fd_host2rump(s), buf, len, flags, from, fromlen);
330 1.1 pooka }
331 1.1 pooka
332 1.1 pooka ssize_t
333 1.1 pooka recvmsg(int s, struct msghdr *msg, int flags)
334 1.1 pooka {
335 1.1 pooka int (*rc_recvmsg)(int, struct msghdr *, int);
336 1.1 pooka
337 1.1 pooka DPRINTF(("recvmsg\n"));
338 1.1 pooka assertfd(s);
339 1.1 pooka rc_recvmsg = rumpcalls[RUMPCALL_RECVMSG];
340 1.2 pooka return rc_recvmsg(fd_host2rump(s), msg, flags);
341 1.1 pooka }
342 1.1 pooka
343 1.1 pooka ssize_t
344 1.1 pooka send(int s, const void *buf, size_t len, int flags)
345 1.1 pooka {
346 1.1 pooka
347 1.1 pooka return sendto(s, buf, len, flags, NULL, 0);
348 1.1 pooka }
349 1.1 pooka
350 1.1 pooka ssize_t
351 1.1 pooka sendto(int s, const void *buf, size_t len, int flags,
352 1.1 pooka const struct sockaddr *to, socklen_t tolen)
353 1.1 pooka {
354 1.1 pooka int (*rc_sendto)(int, const void *, size_t, int,
355 1.1 pooka const struct sockaddr *, socklen_t);
356 1.1 pooka
357 1.1 pooka if (s == -1)
358 1.1 pooka return len;
359 1.1 pooka
360 1.1 pooka DPRINTF(("sendto\n"));
361 1.1 pooka assertfd(s);
362 1.1 pooka rc_sendto = rumpcalls[RUMPCALL_SENDTO];
363 1.2 pooka return rc_sendto(fd_host2rump(s), buf, len, flags, to, tolen);
364 1.1 pooka }
365 1.1 pooka
366 1.1 pooka ssize_t
367 1.1 pooka sendmsg(int s, const struct msghdr *msg, int flags)
368 1.1 pooka {
369 1.1 pooka int (*rc_sendmsg)(int, const struct msghdr *, int);
370 1.1 pooka
371 1.1 pooka DPRINTF(("sendmsg\n"));
372 1.1 pooka assertfd(s);
373 1.1 pooka rc_sendmsg = rumpcalls[RUMPCALL_SENDTO];
374 1.2 pooka return rc_sendmsg(fd_host2rump(s), msg, flags);
375 1.1 pooka }
376 1.1 pooka
377 1.1 pooka int
378 1.1 pooka getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
379 1.1 pooka {
380 1.1 pooka int (*rc_getsockopt)(int, int, int, void *, socklen_t *);
381 1.1 pooka
382 1.1 pooka DPRINTF(("getsockopt\n"));
383 1.1 pooka assertfd(s);
384 1.1 pooka rc_getsockopt = rumpcalls[RUMPCALL_GETSOCKOPT];
385 1.2 pooka return rc_getsockopt(fd_host2rump(s), level, optname, optval, optlen);
386 1.1 pooka }
387 1.1 pooka
388 1.1 pooka int
389 1.1 pooka setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
390 1.1 pooka {
391 1.1 pooka int (*rc_setsockopt)(int, int, int, const void *, socklen_t);
392 1.1 pooka
393 1.1 pooka DPRINTF(("setsockopt\n"));
394 1.1 pooka assertfd(s);
395 1.1 pooka rc_setsockopt = rumpcalls[RUMPCALL_SETSOCKOPT];
396 1.2 pooka return rc_setsockopt(fd_host2rump(s), level, optname, optval, optlen);
397 1.1 pooka }
398 1.1 pooka
399 1.1 pooka int
400 1.1 pooka shutdown(int s, int how)
401 1.1 pooka {
402 1.1 pooka int (*rc_shutdown)(int, int);
403 1.1 pooka
404 1.1 pooka DPRINTF(("shutdown\n"));
405 1.1 pooka assertfd(s);
406 1.1 pooka rc_shutdown = rumpcalls[RUMPCALL_SHUTDOWN];
407 1.2 pooka return rc_shutdown(fd_host2rump(s), how);
408 1.2 pooka }
409 1.2 pooka
410 1.2 pooka /*
411 1.2 pooka * dup2 is special. we allow dup2 of a rump kernel fd to 0-2 since
412 1.2 pooka * many programs do that. dup2 of a rump kernel fd to another value
413 1.2 pooka * not >= fdoff is an error.
414 1.2 pooka *
415 1.2 pooka * Note: cannot rump2host newd, because it is often hardcoded.
416 1.2 pooka *
417 1.2 pooka * XXX: should disable debug prints after stdout/stderr are dup2'd
418 1.2 pooka */
419 1.2 pooka int
420 1.2 pooka dup2(int oldd, int newd)
421 1.2 pooka {
422 1.2 pooka int rv;
423 1.2 pooka
424 1.2 pooka DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
425 1.2 pooka
426 1.2 pooka if (fd_isrump(oldd)) {
427 1.2 pooka if (!(newd >= 0 && newd <= 2))
428 1.2 pooka return EBADF;
429 1.2 pooka oldd = fd_host2rump(oldd);
430 1.2 pooka rv = rump_sys_dup2(oldd, newd);
431 1.2 pooka if (rv != -1)
432 1.2 pooka dup2mask |= newd+1;
433 1.2 pooka return rv;
434 1.2 pooka } else {
435 1.2 pooka return host_dup2(oldd, newd);
436 1.2 pooka }
437 1.2 pooka }
438 1.2 pooka
439 1.2 pooka /*
440 1.2 pooka * We just wrap fork the appropriate rump client calls to preserve
441 1.2 pooka * the file descriptors of the forked parent in the child, but
442 1.2 pooka * prevent double use of connection fd.
443 1.2 pooka */
444 1.2 pooka
445 1.2 pooka pid_t
446 1.2 pooka fork()
447 1.2 pooka {
448 1.2 pooka struct rumpclient_fork *rf;
449 1.2 pooka pid_t rv;
450 1.2 pooka
451 1.2 pooka DPRINTF(("fork\n"));
452 1.2 pooka
453 1.2 pooka if ((rf = rumpclient_prefork()) == NULL)
454 1.2 pooka return -1;
455 1.2 pooka
456 1.2 pooka switch ((rv = host_fork())) {
457 1.2 pooka case -1:
458 1.2 pooka /* XXX: cancel rf */
459 1.2 pooka break;
460 1.2 pooka case 0:
461 1.2 pooka if (rumpclient_fork_init(rf) == -1)
462 1.2 pooka rv = -1;
463 1.2 pooka break;
464 1.2 pooka default:
465 1.2 pooka break;
466 1.2 pooka }
467 1.2 pooka
468 1.2 pooka DPRINTF(("fork returns %d\n", rv));
469 1.2 pooka return rv;
470 1.1 pooka }
471 1.1 pooka
472 1.1 pooka /*
473 1.1 pooka * Hybrids
474 1.1 pooka */
475 1.1 pooka
476 1.1 pooka ssize_t
477 1.1 pooka read(int fd, void *buf, size_t len)
478 1.1 pooka {
479 1.1 pooka int (*op_read)(int, void *, size_t);
480 1.1 pooka ssize_t n;
481 1.1 pooka
482 1.1 pooka DPRINTF(("read %d\n", fd));
483 1.2 pooka if (fd_isrump(fd)) {
484 1.2 pooka fd = fd_host2rump(fd);
485 1.2 pooka op_read = rumpcalls[RUMPCALL_READ];
486 1.2 pooka } else {
487 1.1 pooka op_read = host_read;
488 1.1 pooka }
489 1.1 pooka
490 1.1 pooka n = op_read(fd, buf, len);
491 1.1 pooka return n;
492 1.1 pooka }
493 1.1 pooka
494 1.1 pooka ssize_t
495 1.1 pooka readv(int fd, const struct iovec *iov, int iovcnt)
496 1.1 pooka {
497 1.1 pooka int (*op_readv)(int, const struct iovec *, int);
498 1.1 pooka
499 1.2 pooka if (fd_isrump(fd)) {
500 1.2 pooka fd = fd_host2rump(fd);
501 1.2 pooka op_readv = rumpcalls[RUMPCALL_READV];
502 1.2 pooka } else {
503 1.1 pooka op_readv = host_readv;
504 1.1 pooka }
505 1.1 pooka
506 1.1 pooka DPRINTF(("readv\n"));
507 1.1 pooka return op_readv(fd, iov, iovcnt);
508 1.1 pooka }
509 1.1 pooka
510 1.1 pooka ssize_t
511 1.1 pooka write(int fd, const void *buf, size_t len)
512 1.1 pooka {
513 1.1 pooka int (*op_write)(int, const void *, size_t);
514 1.1 pooka
515 1.2 pooka if (fd_isrump(fd)) {
516 1.2 pooka fd = fd_host2rump(fd);
517 1.2 pooka op_write = rumpcalls[RUMPCALL_WRITE];
518 1.2 pooka } else {
519 1.1 pooka op_write = host_write;
520 1.1 pooka }
521 1.1 pooka
522 1.1 pooka return op_write(fd, buf, len);
523 1.1 pooka }
524 1.1 pooka
525 1.1 pooka ssize_t
526 1.1 pooka writev(int fd, const struct iovec *iov, int iovcnt)
527 1.1 pooka {
528 1.1 pooka int (*op_writev)(int, const struct iovec *, int);
529 1.1 pooka
530 1.2 pooka if (fd_isrump(fd)) {
531 1.2 pooka fd = fd_host2rump(fd);
532 1.2 pooka op_writev = rumpcalls[RUMPCALL_WRITEV];
533 1.2 pooka } else {
534 1.1 pooka op_writev = host_writev;
535 1.1 pooka }
536 1.1 pooka
537 1.1 pooka return op_writev(fd, iov, iovcnt);
538 1.1 pooka }
539 1.1 pooka
540 1.1 pooka int
541 1.1 pooka ioctl(int fd, unsigned long cmd, ...)
542 1.1 pooka {
543 1.1 pooka int (*op_ioctl)(int, unsigned long cmd, ...);
544 1.1 pooka va_list ap;
545 1.1 pooka int rv;
546 1.1 pooka
547 1.1 pooka DPRINTF(("ioctl\n"));
548 1.2 pooka if (fd_isrump(fd)) {
549 1.2 pooka fd = fd_host2rump(fd);
550 1.2 pooka op_ioctl = rumpcalls[RUMPCALL_IOCTL];
551 1.2 pooka } else {
552 1.1 pooka op_ioctl = host_ioctl;
553 1.1 pooka }
554 1.1 pooka
555 1.1 pooka va_start(ap, cmd);
556 1.1 pooka rv = op_ioctl(fd, cmd, va_arg(ap, void *));
557 1.1 pooka va_end(ap);
558 1.1 pooka return rv;
559 1.1 pooka }
560 1.1 pooka
561 1.1 pooka int
562 1.1 pooka fcntl(int fd, int cmd, ...)
563 1.1 pooka {
564 1.1 pooka int (*op_fcntl)(int, int, ...);
565 1.1 pooka va_list ap;
566 1.1 pooka int rv;
567 1.1 pooka
568 1.1 pooka DPRINTF(("fcntl\n"));
569 1.2 pooka if (fd_isrump(fd)) {
570 1.2 pooka fd = fd_host2rump(fd);
571 1.2 pooka op_fcntl = rumpcalls[RUMPCALL_FCNTL];
572 1.2 pooka } else {
573 1.1 pooka op_fcntl = host_fcntl;
574 1.1 pooka }
575 1.1 pooka
576 1.1 pooka va_start(ap, cmd);
577 1.1 pooka rv = op_fcntl(fd, cmd, va_arg(ap, void *));
578 1.1 pooka va_end(ap);
579 1.1 pooka return rv;
580 1.1 pooka }
581 1.1 pooka
582 1.1 pooka int
583 1.1 pooka close(int fd)
584 1.1 pooka {
585 1.1 pooka int (*op_close)(int);
586 1.1 pooka
587 1.1 pooka DPRINTF(("close %d\n", fd));
588 1.2 pooka if (fd_isrump(fd)) {
589 1.2 pooka fd = fd_host2rump(fd);
590 1.2 pooka op_close = rumpcalls[RUMPCALL_CLOSE];
591 1.2 pooka } else {
592 1.1 pooka op_close = host_close;
593 1.1 pooka }
594 1.1 pooka
595 1.1 pooka return op_close(fd);
596 1.1 pooka }
597 1.1 pooka
598 1.4 pooka int
599 1.4 pooka select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
600 1.4 pooka struct timeval *timeout)
601 1.1 pooka {
602 1.4 pooka struct pollfd *pfds;
603 1.4 pooka struct timespec ts, *tsp = NULL;
604 1.4 pooka nfds_t i, j, realnfds;
605 1.4 pooka int rv, incr;
606 1.4 pooka
607 1.4 pooka /*
608 1.4 pooka * Well, first we must scan the fds to figure out how many
609 1.4 pooka * fds there really are. This is because up to and including
610 1.4 pooka * nb5 poll() silently refuses nfds > process_open_fds.
611 1.4 pooka * Seems to be fixed in current, thank the maker.
612 1.4 pooka * god damn cluster...bomb.
613 1.4 pooka */
614 1.4 pooka
615 1.4 pooka for (i = 0, realnfds = 0; i < nfds; i++) {
616 1.4 pooka if (readfds && FD_ISSET(i, readfds)) {
617 1.4 pooka realnfds++;
618 1.4 pooka continue;
619 1.4 pooka }
620 1.4 pooka if (writefds && FD_ISSET(i, writefds)) {
621 1.4 pooka realnfds++;
622 1.4 pooka continue;
623 1.4 pooka }
624 1.4 pooka if (exceptfds && FD_ISSET(i, exceptfds)) {
625 1.4 pooka realnfds++;
626 1.4 pooka continue;
627 1.1 pooka }
628 1.1 pooka }
629 1.1 pooka
630 1.6 pooka if (realnfds) {
631 1.6 pooka pfds = malloc(sizeof(*pfds) * realnfds);
632 1.6 pooka if (!pfds)
633 1.6 pooka return -1;
634 1.6 pooka } else {
635 1.6 pooka pfds = NULL;
636 1.6 pooka }
637 1.1 pooka
638 1.4 pooka for (i = 0, j = 0; i < nfds; i++) {
639 1.4 pooka incr = 0;
640 1.4 pooka pfds[j].events = pfds[j].revents = 0;
641 1.4 pooka if (readfds && FD_ISSET(i, readfds)) {
642 1.4 pooka pfds[j].fd = i;
643 1.4 pooka pfds[j].events |= POLLIN;
644 1.4 pooka incr=1;
645 1.4 pooka }
646 1.4 pooka if (writefds && FD_ISSET(i, writefds)) {
647 1.4 pooka pfds[j].fd = i;
648 1.4 pooka pfds[j].events |= POLLOUT;
649 1.4 pooka incr=1;
650 1.4 pooka }
651 1.4 pooka if (exceptfds && FD_ISSET(i, exceptfds)) {
652 1.4 pooka pfds[j].fd = i;
653 1.4 pooka pfds[j].events |= POLLHUP|POLLERR;
654 1.4 pooka incr=1;
655 1.1 pooka }
656 1.4 pooka if (incr)
657 1.4 pooka j++;
658 1.1 pooka }
659 1.1 pooka
660 1.4 pooka if (timeout) {
661 1.4 pooka TIMEVAL_TO_TIMESPEC(timeout, &ts);
662 1.4 pooka tsp = &ts;
663 1.4 pooka }
664 1.4 pooka rv = pollts(pfds, realnfds, tsp, NULL);
665 1.4 pooka if (rv <= 0)
666 1.4 pooka goto out;
667 1.4 pooka
668 1.4 pooka /*
669 1.4 pooka * ok, harvest results. first zero out entries (can't use
670 1.4 pooka * FD_ZERO for the obvious select-me-not reason). whee.
671 1.4 pooka */
672 1.4 pooka for (i = 0; i < nfds; i++) {
673 1.4 pooka if (readfds)
674 1.4 pooka FD_CLR(i, readfds);
675 1.4 pooka if (writefds)
676 1.4 pooka FD_CLR(i, writefds);
677 1.4 pooka if (exceptfds)
678 1.4 pooka FD_CLR(i, exceptfds);
679 1.1 pooka }
680 1.1 pooka
681 1.4 pooka /* and then plug in the results */
682 1.4 pooka for (i = 0; i < realnfds; i++) {
683 1.4 pooka if (readfds) {
684 1.4 pooka if (pfds[i].revents & POLLIN) {
685 1.4 pooka FD_SET(pfds[i].fd, readfds);
686 1.4 pooka }
687 1.4 pooka }
688 1.4 pooka if (writefds) {
689 1.4 pooka if (pfds[i].revents & POLLOUT) {
690 1.4 pooka FD_SET(pfds[i].fd, writefds);
691 1.4 pooka }
692 1.4 pooka }
693 1.4 pooka if (exceptfds) {
694 1.4 pooka if (pfds[i].revents & (POLLHUP|POLLERR)) {
695 1.4 pooka FD_SET(pfds[i].fd, exceptfds);
696 1.4 pooka }
697 1.4 pooka }
698 1.1 pooka }
699 1.1 pooka
700 1.4 pooka out:
701 1.4 pooka free(pfds);
702 1.1 pooka return rv;
703 1.1 pooka }
704 1.1 pooka
705 1.1 pooka static void
706 1.1 pooka checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
707 1.1 pooka {
708 1.1 pooka nfds_t i;
709 1.1 pooka
710 1.1 pooka for (i = 0; i < nfds; i++) {
711 1.2 pooka if (fd_isrump(fds[i].fd))
712 1.2 pooka (*rumpcall)++;
713 1.2 pooka else
714 1.1 pooka (*hostcall)++;
715 1.1 pooka }
716 1.1 pooka }
717 1.1 pooka
718 1.1 pooka static void
719 1.2 pooka adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
720 1.1 pooka {
721 1.1 pooka nfds_t i;
722 1.1 pooka
723 1.1 pooka for (i = 0; i < nfds; i++) {
724 1.2 pooka fds[i].fd = fdadj(fds[i].fd);
725 1.1 pooka }
726 1.1 pooka }
727 1.1 pooka
728 1.3 pooka struct mytimespec {
729 1.3 pooka uint64_t tv_sec;
730 1.3 pooka long tv_nsec;
731 1.3 pooka };
732 1.3 pooka
733 1.1 pooka /*
734 1.1 pooka * poll is easy as long as the call comes in the fds only in one
735 1.1 pooka * kernel. otherwise its quite tricky...
736 1.1 pooka */
737 1.1 pooka struct pollarg {
738 1.1 pooka struct pollfd *pfds;
739 1.1 pooka nfds_t nfds;
740 1.3 pooka const struct timespec *ts;
741 1.3 pooka const sigset_t *sigmask;
742 1.1 pooka int pipefd;
743 1.1 pooka int errnum;
744 1.1 pooka };
745 1.1 pooka
746 1.1 pooka static void *
747 1.1 pooka hostpoll(void *arg)
748 1.1 pooka {
749 1.1 pooka struct pollarg *parg = arg;
750 1.1 pooka intptr_t rv;
751 1.1 pooka
752 1.3 pooka rv = host_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
753 1.1 pooka if (rv == -1)
754 1.1 pooka parg->errnum = errno;
755 1.1 pooka rump_sys_write(parg->pipefd, &rv, sizeof(rv));
756 1.1 pooka
757 1.1 pooka return (void *)(intptr_t)rv;
758 1.1 pooka }
759 1.1 pooka
760 1.1 pooka int
761 1.3 pooka pollts(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
762 1.3 pooka const sigset_t *sigmask)
763 1.1 pooka {
764 1.3 pooka int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
765 1.3 pooka const sigset_t *);
766 1.1 pooka int hostcall = 0, rumpcall = 0;
767 1.1 pooka pthread_t pt;
768 1.1 pooka nfds_t i;
769 1.1 pooka int rv;
770 1.1 pooka
771 1.3 pooka #if 0
772 1.3 pooka /* XXX: quick 5.0 kludge. do syscall compat in rumpclient properly */
773 1.3 pooka struct mytimespec mts;
774 1.3 pooka if (ts) {
775 1.3 pooka mts.tv_sec = ts->tv_sec;
776 1.3 pooka mts.tv_nsec = ts->tv_nsec;
777 1.3 pooka ts = (struct timespec *)&mts;
778 1.3 pooka }
779 1.3 pooka #endif
780 1.3 pooka
781 1.2 pooka DPRINTF(("poll\n"));
782 1.1 pooka checkpoll(fds, nfds, &hostcall, &rumpcall);
783 1.1 pooka
784 1.1 pooka if (hostcall && rumpcall) {
785 1.1 pooka struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
786 1.1 pooka int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
787 1.1 pooka struct pollarg parg;
788 1.1 pooka uintptr_t lrv;
789 1.1 pooka int sverrno = 0, trv;
790 1.1 pooka
791 1.1 pooka /*
792 1.1 pooka * ok, this is where it gets tricky. We must support
793 1.1 pooka * this since it's a very common operation in certain
794 1.1 pooka * types of software (telnet, netcat, etc). We allocate
795 1.1 pooka * two vectors and run two poll commands in separate
796 1.1 pooka * threads. Whichever returns first "wins" and the
797 1.1 pooka * other kernel's fds won't show activity.
798 1.1 pooka */
799 1.1 pooka rv = -1;
800 1.1 pooka
801 1.1 pooka /* allocate full vector for O(n) joining after call */
802 1.1 pooka pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
803 1.1 pooka if (!pfd_host)
804 1.1 pooka goto out;
805 1.1 pooka pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
806 1.1 pooka if (!pfd_rump) {
807 1.1 pooka goto out;
808 1.1 pooka }
809 1.1 pooka
810 1.1 pooka /* split vectors */
811 1.1 pooka for (i = 0; i < nfds; i++) {
812 1.3 pooka if (fds[i].fd == -1) {
813 1.3 pooka pfd_host[i].fd = -1;
814 1.3 pooka pfd_rump[i].fd = -1;
815 1.3 pooka } else if (fd_isrump(fds[i].fd)) {
816 1.2 pooka pfd_host[i].fd = -1;
817 1.2 pooka pfd_rump[i].fd = fd_host2rump(fds[i].fd);
818 1.2 pooka pfd_rump[i].events = fds[i].events;
819 1.2 pooka } else {
820 1.2 pooka pfd_rump[i].fd = -1;
821 1.1 pooka pfd_host[i].fd = fds[i].fd;
822 1.1 pooka pfd_host[i].events = fds[i].events;
823 1.1 pooka }
824 1.1 pooka }
825 1.1 pooka
826 1.1 pooka /*
827 1.1 pooka * then, open two pipes, one for notifications
828 1.1 pooka * to each kernel.
829 1.1 pooka */
830 1.1 pooka if (rump_sys_pipe(rpipe) == -1)
831 1.1 pooka goto out;
832 1.1 pooka if (pipe(hpipe) == -1)
833 1.1 pooka goto out;
834 1.1 pooka
835 1.1 pooka pfd_host[nfds].fd = hpipe[0];
836 1.1 pooka pfd_host[nfds].events = POLLIN;
837 1.1 pooka pfd_rump[nfds].fd = rpipe[0];
838 1.1 pooka pfd_rump[nfds].events = POLLIN;
839 1.1 pooka
840 1.1 pooka /*
841 1.1 pooka * then, create a thread to do host part and meanwhile
842 1.1 pooka * do rump kernel part right here
843 1.1 pooka */
844 1.1 pooka
845 1.1 pooka parg.pfds = pfd_host;
846 1.1 pooka parg.nfds = nfds+1;
847 1.3 pooka parg.ts = ts;
848 1.3 pooka parg.sigmask = sigmask;
849 1.1 pooka parg.pipefd = rpipe[1];
850 1.1 pooka pthread_create(&pt, NULL, hostpoll, &parg);
851 1.1 pooka
852 1.3 pooka op_pollts = rumpcalls[RUMPCALL_POLLTS];
853 1.3 pooka lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
854 1.1 pooka sverrno = errno;
855 1.1 pooka write(hpipe[1], &rv, sizeof(rv));
856 1.1 pooka pthread_join(pt, (void *)&trv);
857 1.1 pooka
858 1.1 pooka /* check who "won" and merge results */
859 1.1 pooka if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
860 1.1 pooka rv = trv;
861 1.1 pooka
862 1.1 pooka for (i = 0; i < nfds; i++) {
863 1.1 pooka if (pfd_rump[i].fd != -1)
864 1.1 pooka fds[i].revents = pfd_rump[i].revents;
865 1.1 pooka }
866 1.1 pooka sverrno = parg.errnum;
867 1.1 pooka } else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
868 1.1 pooka rv = trv;
869 1.1 pooka
870 1.1 pooka for (i = 0; i < nfds; i++) {
871 1.1 pooka if (pfd_host[i].fd != -1)
872 1.1 pooka fds[i].revents = pfd_host[i].revents;
873 1.1 pooka }
874 1.1 pooka } else {
875 1.1 pooka rv = 0;
876 1.1 pooka }
877 1.1 pooka
878 1.1 pooka out:
879 1.1 pooka if (rpipe[0] != -1)
880 1.1 pooka rump_sys_close(rpipe[0]);
881 1.1 pooka if (rpipe[1] != -1)
882 1.1 pooka rump_sys_close(rpipe[1]);
883 1.1 pooka if (hpipe[0] != -1)
884 1.1 pooka close(hpipe[0]);
885 1.1 pooka if (hpipe[1] != -1)
886 1.1 pooka close(hpipe[1]);
887 1.1 pooka free(pfd_host);
888 1.1 pooka free(pfd_rump);
889 1.1 pooka errno = sverrno;
890 1.1 pooka } else {
891 1.1 pooka if (hostcall) {
892 1.3 pooka op_pollts = host_pollts;
893 1.1 pooka } else {
894 1.3 pooka op_pollts = rumpcalls[RUMPCALL_POLLTS];
895 1.2 pooka adjustpoll(fds, nfds, fd_host2rump);
896 1.1 pooka }
897 1.1 pooka
898 1.3 pooka rv = op_pollts(fds, nfds, ts, sigmask);
899 1.1 pooka if (rumpcall)
900 1.2 pooka adjustpoll(fds, nfds, fd_rump2host);
901 1.1 pooka }
902 1.1 pooka
903 1.1 pooka return rv;
904 1.1 pooka }
905 1.1 pooka
906 1.1 pooka int
907 1.3 pooka poll(struct pollfd *fds, nfds_t nfds, int timeout)
908 1.1 pooka {
909 1.3 pooka struct timespec ts;
910 1.3 pooka struct timespec *tsp = NULL;
911 1.3 pooka
912 1.3 pooka if (timeout != INFTIM) {
913 1.3 pooka ts.tv_sec = timeout / 1000;
914 1.3 pooka ts.tv_nsec = (timeout % 1000) * 1000;
915 1.3 pooka
916 1.3 pooka tsp = &ts;
917 1.3 pooka }
918 1.1 pooka
919 1.3 pooka return pollts(fds, nfds, tsp, NULL);
920 1.1 pooka }
921