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