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