hijack.c revision 1.19 1 /* $NetBSD: hijack.c,v 1.19 2011/01/25 12:53:45 pooka Exp $ */
2
3 /*-
4 * Copyright (c) 2011 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: hijack.c,v 1.19 2011/01/25 12:53:45 pooka Exp $");
30
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/event.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/poll.h>
37
38 #include <rump/rumpclient.h>
39 #include <rump/rump_syscalls.h>
40
41 #include <assert.h>
42 #include <dlfcn.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <poll.h>
47 #include <pthread.h>
48 #include <signal.h>
49 #include <stdarg.h>
50 #include <stdbool.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <time.h>
54 #include <unistd.h>
55
56 enum dualcall {
57 DUALCALL_WRITE, DUALCALL_WRITEV,
58 DUALCALL_IOCTL, DUALCALL_FCNTL,
59 DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_BIND, DUALCALL_CONNECT,
60 DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN,
61 DUALCALL_RECVFROM, DUALCALL_RECVMSG,
62 DUALCALL_SENDTO, DUALCALL_SENDMSG,
63 DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT,
64 DUALCALL_SHUTDOWN,
65 DUALCALL_READ, DUALCALL_READV,
66 DUALCALL_DUP2, DUALCALL_CLOSE,
67 DUALCALL_POLLTS,
68 DUALCALL__NUM
69 };
70
71 #define RSYS_STRING(a) __STRING(a)
72 #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a))
73
74 /*
75 * Would be nice to get this automatically in sync with libc.
76 * Also, this does not work for compat-using binaries!
77 */
78 #if !__NetBSD_Prereq__(5,99,7)
79 #define LIBCSELECT select
80 #define LIBCPOLLTS pollts
81 #define LIBCPOLL poll
82 #else
83 #define LIBCSELECT __select50
84 #define LIBCPOLLTS __pollts50
85 #define LIBCPOLL __poll50
86 #endif
87
88 int SELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *);
89 int POLLTS(struct pollfd *, nfds_t, const struct timespec *, const sigset_t *);
90 int POLL(struct pollfd *, nfds_t, int);
91
92 #define S(a) __STRING(a)
93 struct sysnames {
94 enum dualcall scm_callnum;
95 const char *scm_hostname;
96 const char *scm_rumpname;
97 } syscnames[] = {
98 { DUALCALL_SOCKET, "__socket30", RSYS_NAME(SOCKET) },
99 { DUALCALL_ACCEPT, "accept", RSYS_NAME(ACCEPT) },
100 { DUALCALL_BIND, "bind", RSYS_NAME(BIND) },
101 { DUALCALL_CONNECT, "connect", RSYS_NAME(CONNECT) },
102 { DUALCALL_GETPEERNAME, "getpeername", RSYS_NAME(GETPEERNAME) },
103 { DUALCALL_GETSOCKNAME, "getsockname", RSYS_NAME(GETSOCKNAME) },
104 { DUALCALL_LISTEN, "listen", RSYS_NAME(LISTEN) },
105 { DUALCALL_RECVFROM, "recvfrom", RSYS_NAME(RECVFROM) },
106 { DUALCALL_RECVMSG, "recvmsg", RSYS_NAME(RECVMSG) },
107 { DUALCALL_SENDTO, "sendto", RSYS_NAME(SENDTO) },
108 { DUALCALL_SENDMSG, "sendmsg", RSYS_NAME(SENDMSG) },
109 { DUALCALL_GETSOCKOPT, "getsockopt", RSYS_NAME(GETSOCKOPT) },
110 { DUALCALL_SETSOCKOPT, "setsockopt", RSYS_NAME(SETSOCKOPT) },
111 { DUALCALL_SHUTDOWN, "shutdown", RSYS_NAME(SHUTDOWN) },
112 { DUALCALL_READ, "read", RSYS_NAME(READ) },
113 { DUALCALL_READV, "readv", RSYS_NAME(READV) },
114 { DUALCALL_WRITE, "write", RSYS_NAME(WRITE) },
115 { DUALCALL_WRITEV, "writev", RSYS_NAME(WRITEV) },
116 { DUALCALL_IOCTL, "ioctl", RSYS_NAME(IOCTL) },
117 { DUALCALL_FCNTL, "fcntl", RSYS_NAME(FCNTL) },
118 { DUALCALL_DUP2, "dup2", RSYS_NAME(DUP2) },
119 { DUALCALL_CLOSE, "close", RSYS_NAME(CLOSE) },
120 { DUALCALL_POLLTS, S(LIBCPOLLTS), RSYS_NAME(POLLTS) },
121 };
122 #undef S
123
124 struct bothsys {
125 void *bs_host;
126 void *bs_rump;
127 } syscalls[DUALCALL__NUM];
128 #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which
129
130 pid_t (*host_fork)(void);
131
132 static unsigned dup2mask;
133 #define ISDUP2D(fd) (1<<(fd) & dup2mask)
134
135 //#define DEBUGJACK
136 #ifdef DEBUGJACK
137 #define DPRINTF(x) mydprintf x
138 static void
139 mydprintf(const char *fmt, ...)
140 {
141 va_list ap;
142
143 if (ISDUP2D(STDERR_FILENO))
144 return;
145
146 va_start(ap, fmt);
147 vfprintf(stderr, fmt, ap);
148 va_end(ap);
149 }
150
151 #else
152 #define DPRINTF(x)
153 #endif
154
155 #define FDCALL(type, name, rcname, args, proto, vars) \
156 type name args \
157 { \
158 type (*fun) proto; \
159 \
160 if (fd_isrump(fd)) { \
161 fun = syscalls[rcname].bs_rump; \
162 fd = fd_host2rump(fd); \
163 } else { \
164 fun = syscalls[rcname].bs_host; \
165 } \
166 \
167 return fun vars; \
168 }
169
170 /*
171 * This is called from librumpclient in case of LD_PRELOAD.
172 * It ensures correct RTLD_NEXT.
173 */
174 static void *
175 hijackdlsym(void *handle, const char *symbol)
176 {
177
178 return dlsym(handle, symbol);
179 }
180
181 /* low calorie sockets? */
182 static bool hostlocalsockets = true;
183
184 static void __attribute__((constructor))
185 rcinit(void)
186 {
187 int (*rumpcinit)(void);
188 void **rumpcdlsym;
189 void *hand;
190 unsigned i, j;
191
192 hand = dlopen("librumpclient.so", RTLD_LAZY|RTLD_GLOBAL);
193 if (!hand)
194 err(1, "cannot open librumpclient.so");
195 rumpcinit = dlsym(hand, "rumpclient_init");
196 _DIAGASSERT(rumpcinit);
197
198 rumpcdlsym = dlsym(hand, "rumpclient_dlsym");
199 *rumpcdlsym = hijackdlsym;
200 host_fork = dlsym(RTLD_NEXT, "fork");
201
202 /*
203 * In theory cannot print anything during lookups because
204 * we might not have the call vector set up. so, the errx()
205 * is a bit of a strech, but it might work.
206 */
207
208 for (i = 0; i < DUALCALL__NUM; i++) {
209 /* build runtime O(1) access */
210 for (j = 0; j < __arraycount(syscnames); j++) {
211 if (syscnames[j].scm_callnum == i)
212 break;
213 }
214
215 if (j == __arraycount(syscnames))
216 errx(1, "rumphijack error: syscall pos %d missing", i);
217
218 syscalls[i].bs_host = dlsym(hand,syscnames[j].scm_hostname);
219 if (syscalls[i].bs_host == NULL)
220 errx(1, "hostcall %s not found missing",
221 syscnames[j].scm_hostname);
222
223 syscalls[i].bs_rump = dlsym(hand,syscnames[j].scm_rumpname);
224 if (syscalls[i].bs_rump == NULL)
225 errx(1, "rumpcall %s not found missing",
226 syscnames[j].scm_rumpname);
227 }
228
229 if (rumpcinit() == -1)
230 err(1, "rumpclient init");
231 }
232
233 /* XXX: need runtime selection. low for now due to FD_SETSIZE */
234 #define HIJACK_FDOFF 128
235 #define HIJACK_SELECT 128 /* XXX */
236 #define HIJACK_ASSERT 128 /* XXX */
237 static int
238 fd_rump2host(int fd)
239 {
240
241 if (fd == -1)
242 return fd;
243
244 if (!ISDUP2D(fd))
245 fd += HIJACK_FDOFF;
246
247 return fd;
248 }
249
250 static int
251 fd_host2rump(int fd)
252 {
253
254 if (!ISDUP2D(fd))
255 fd -= HIJACK_FDOFF;
256 return fd;
257 }
258
259 static bool
260 fd_isrump(int fd)
261 {
262
263 return ISDUP2D(fd) || fd >= HIJACK_FDOFF;
264 }
265
266 #define assertfd(_fd_) assert(ISDUP2D(_fd_) || (_fd_) >= HIJACK_ASSERT)
267 #undef HIJACK_FDOFF
268
269 int __socket30(int, int, int);
270 int
271 __socket30(int domain, int type, int protocol)
272 {
273 int (*op_socket)(int, int, int);
274 int fd;
275 bool dohost;
276
277 dohost = hostlocalsockets && (domain == AF_LOCAL);
278
279 if (dohost)
280 op_socket = GETSYSCALL(host, SOCKET);
281 else
282 op_socket = GETSYSCALL(rump, SOCKET);
283 fd = op_socket(domain, type, protocol);
284
285 if (!dohost)
286 fd = fd_rump2host(fd);
287 DPRINTF(("socket <- %d\n", fd));
288
289 return fd;
290 }
291
292 int
293 accept(int s, struct sockaddr *addr, socklen_t *addrlen)
294 {
295 int (*op_accept)(int, struct sockaddr *, socklen_t *);
296 int fd;
297 bool isrump;
298
299 isrump = fd_isrump(s);
300
301 DPRINTF(("accept -> %d", s));
302 if (isrump) {
303 op_accept = GETSYSCALL(rump, ACCEPT);
304 s = fd_host2rump(s);
305 } else {
306 op_accept = GETSYSCALL(host, ACCEPT);
307 }
308 fd = op_accept(s, addr, addrlen);
309 if (fd != -1 && isrump)
310 fd = fd_rump2host(fd);
311
312 DPRINTF((" <- %d\n", fd));
313
314 return fd;
315 }
316
317 /*
318 * ioctl and fcntl are varargs calls and need special treatment
319 */
320 int
321 ioctl(int fd, unsigned long cmd, ...)
322 {
323 int (*op_ioctl)(int, unsigned long cmd, ...);
324 va_list ap;
325 int rv;
326
327 DPRINTF(("ioctl -> %d\n", fd));
328 if (fd_isrump(fd)) {
329 fd = fd_host2rump(fd);
330 op_ioctl = GETSYSCALL(rump, IOCTL);
331 } else {
332 op_ioctl = GETSYSCALL(host, IOCTL);
333 }
334
335 va_start(ap, cmd);
336 rv = op_ioctl(fd, cmd, va_arg(ap, void *));
337 va_end(ap);
338 return rv;
339 }
340
341 int
342 fcntl(int fd, int cmd, ...)
343 {
344 int (*op_fcntl)(int, int, ...);
345 va_list ap;
346 int rv;
347
348 DPRINTF(("fcntl -> %d\n", fd));
349 if (fd_isrump(fd)) {
350 fd = fd_host2rump(fd);
351 op_fcntl = GETSYSCALL(rump, FCNTL);
352 } else {
353 op_fcntl = GETSYSCALL(host, FCNTL);
354 }
355
356 va_start(ap, cmd);
357 rv = op_fcntl(fd, cmd, va_arg(ap, void *));
358 va_end(ap);
359 return rv;
360 }
361
362 /*
363 * write cannot issue a standard debug printf due to recursion
364 */
365 ssize_t
366 write(int fd, const void *buf, size_t blen)
367 {
368 ssize_t (*op_write)(int, const void *, size_t);
369
370 if (fd_isrump(fd)) {
371 fd = fd_host2rump(fd);
372 op_write = GETSYSCALL(rump, WRITE);
373 } else {
374 op_write = GETSYSCALL(host, WRITE);
375 }
376
377 return op_write(fd, buf, blen);
378 }
379
380 /*
381 * dup2 is special. we allow dup2 of a rump kernel fd to 0-2 since
382 * many programs do that. dup2 of a rump kernel fd to another value
383 * not >= fdoff is an error.
384 *
385 * Note: cannot rump2host newd, because it is often hardcoded.
386 */
387 int
388 dup2(int oldd, int newd)
389 {
390 int (*host_dup2)(int, int);
391 int rv;
392
393 DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
394
395 if (fd_isrump(oldd)) {
396 if (!(newd >= 0 && newd <= 2))
397 return EBADF;
398 oldd = fd_host2rump(oldd);
399 rv = rump_sys_dup2(oldd, newd);
400 if (rv != -1)
401 dup2mask |= 1<<newd;
402 } else {
403 host_dup2 = syscalls[DUALCALL_DUP2].bs_host;
404 rv = host_dup2(oldd, newd);
405 }
406
407 return rv;
408 }
409
410 /*
411 * We just wrap fork the appropriate rump client calls to preserve
412 * the file descriptors of the forked parent in the child, but
413 * prevent double use of connection fd.
414 */
415 pid_t
416 fork()
417 {
418 struct rumpclient_fork *rf;
419 pid_t rv;
420
421 DPRINTF(("fork\n"));
422
423 if ((rf = rumpclient_prefork()) == NULL)
424 return -1;
425
426 switch ((rv = host_fork())) {
427 case -1:
428 /* XXX: cancel rf */
429 break;
430 case 0:
431 if (rumpclient_fork_init(rf) == -1)
432 rv = -1;
433 break;
434 default:
435 break;
436 }
437
438 DPRINTF(("fork returns %d\n", rv));
439 return rv;
440 }
441
442 /*
443 * select is done by calling poll.
444 */
445 int
446 LIBCSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
447 struct timeval *timeout)
448 {
449 struct pollfd *pfds;
450 struct timespec ts, *tsp = NULL;
451 nfds_t realnfds;
452 int i, j;
453 int rv, incr;
454
455 DPRINTF(("select\n"));
456
457 /*
458 * Well, first we must scan the fds to figure out how many
459 * fds there really are. This is because up to and including
460 * nb5 poll() silently refuses nfds > process_maxopen_fds.
461 * Seems to be fixed in current, thank the maker.
462 * god damn cluster...bomb.
463 */
464
465 for (i = 0, realnfds = 0; i < nfds; i++) {
466 if (readfds && FD_ISSET(i, readfds)) {
467 realnfds++;
468 continue;
469 }
470 if (writefds && FD_ISSET(i, writefds)) {
471 realnfds++;
472 continue;
473 }
474 if (exceptfds && FD_ISSET(i, exceptfds)) {
475 realnfds++;
476 continue;
477 }
478 }
479
480 if (realnfds) {
481 pfds = malloc(sizeof(*pfds) * realnfds);
482 if (!pfds)
483 return -1;
484 } else {
485 pfds = NULL;
486 }
487
488 for (i = 0, j = 0; i < nfds; i++) {
489 incr = 0;
490 pfds[j].events = pfds[j].revents = 0;
491 if (readfds && FD_ISSET(i, readfds)) {
492 pfds[j].fd = i;
493 pfds[j].events |= POLLIN;
494 incr=1;
495 }
496 if (writefds && FD_ISSET(i, writefds)) {
497 pfds[j].fd = i;
498 pfds[j].events |= POLLOUT;
499 incr=1;
500 }
501 if (exceptfds && FD_ISSET(i, exceptfds)) {
502 pfds[j].fd = i;
503 pfds[j].events |= POLLHUP|POLLERR;
504 incr=1;
505 }
506 if (incr)
507 j++;
508 }
509
510 if (timeout) {
511 TIMEVAL_TO_TIMESPEC(timeout, &ts);
512 tsp = &ts;
513 }
514 rv = pollts(pfds, realnfds, tsp, NULL);
515 if (rv <= 0)
516 goto out;
517
518 /*
519 * ok, harvest results. first zero out entries (can't use
520 * FD_ZERO for the obvious select-me-not reason). whee.
521 */
522 for (i = 0; i < nfds; i++) {
523 if (readfds)
524 FD_CLR(i, readfds);
525 if (writefds)
526 FD_CLR(i, writefds);
527 if (exceptfds)
528 FD_CLR(i, exceptfds);
529 }
530
531 /* and then plug in the results */
532 for (i = 0; i < (int)realnfds; i++) {
533 if (readfds) {
534 if (pfds[i].revents & POLLIN) {
535 FD_SET(pfds[i].fd, readfds);
536 }
537 }
538 if (writefds) {
539 if (pfds[i].revents & POLLOUT) {
540 FD_SET(pfds[i].fd, writefds);
541 }
542 }
543 if (exceptfds) {
544 if (pfds[i].revents & (POLLHUP|POLLERR)) {
545 FD_SET(pfds[i].fd, exceptfds);
546 }
547 }
548 }
549
550 out:
551 free(pfds);
552 return rv;
553 }
554
555 static void
556 checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
557 {
558 nfds_t i;
559
560 for (i = 0; i < nfds; i++) {
561 if (fds[i].fd == -1)
562 continue;
563
564 if (fd_isrump(fds[i].fd))
565 (*rumpcall)++;
566 else
567 (*hostcall)++;
568 }
569 }
570
571 static void
572 adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
573 {
574 nfds_t i;
575
576 for (i = 0; i < nfds; i++) {
577 fds[i].fd = fdadj(fds[i].fd);
578 }
579 }
580
581 /*
582 * poll is easy as long as the call comes in the fds only in one
583 * kernel. otherwise its quite tricky...
584 */
585 struct pollarg {
586 struct pollfd *pfds;
587 nfds_t nfds;
588 const struct timespec *ts;
589 const sigset_t *sigmask;
590 int pipefd;
591 int errnum;
592 };
593
594 static void *
595 hostpoll(void *arg)
596 {
597 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
598 const sigset_t *);
599 struct pollarg *parg = arg;
600 intptr_t rv;
601
602 op_pollts = syscalls[DUALCALL_POLLTS].bs_host;
603 rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
604 if (rv == -1)
605 parg->errnum = errno;
606 rump_sys_write(parg->pipefd, &rv, sizeof(rv));
607
608 return (void *)(intptr_t)rv;
609 }
610
611 int
612 LIBCPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
613 const sigset_t *sigmask)
614 {
615 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
616 const sigset_t *);
617 int (*host_close)(int);
618 int hostcall = 0, rumpcall = 0;
619 pthread_t pt;
620 nfds_t i;
621 int rv;
622
623 DPRINTF(("poll\n"));
624 checkpoll(fds, nfds, &hostcall, &rumpcall);
625
626 if (hostcall && rumpcall) {
627 struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
628 int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
629 struct pollarg parg;
630 uintptr_t lrv;
631 int sverrno = 0, trv;
632
633 /*
634 * ok, this is where it gets tricky. We must support
635 * this since it's a very common operation in certain
636 * types of software (telnet, netcat, etc). We allocate
637 * two vectors and run two poll commands in separate
638 * threads. Whichever returns first "wins" and the
639 * other kernel's fds won't show activity.
640 */
641 rv = -1;
642
643 /* allocate full vector for O(n) joining after call */
644 pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
645 if (!pfd_host)
646 goto out;
647 pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
648 if (!pfd_rump) {
649 goto out;
650 }
651
652 /* split vectors */
653 for (i = 0; i < nfds; i++) {
654 if (fds[i].fd == -1) {
655 pfd_host[i].fd = -1;
656 pfd_rump[i].fd = -1;
657 } else if (fd_isrump(fds[i].fd)) {
658 pfd_host[i].fd = -1;
659 pfd_rump[i].fd = fd_host2rump(fds[i].fd);
660 pfd_rump[i].events = fds[i].events;
661 } else {
662 pfd_rump[i].fd = -1;
663 pfd_host[i].fd = fds[i].fd;
664 pfd_host[i].events = fds[i].events;
665 }
666 fds[i].revents = 0;
667 }
668
669 /*
670 * then, open two pipes, one for notifications
671 * to each kernel.
672 */
673 if (rump_sys_pipe(rpipe) == -1)
674 goto out;
675 if (pipe(hpipe) == -1)
676 goto out;
677
678 pfd_host[nfds].fd = hpipe[0];
679 pfd_host[nfds].events = POLLIN;
680 pfd_rump[nfds].fd = rpipe[0];
681 pfd_rump[nfds].events = POLLIN;
682
683 /*
684 * then, create a thread to do host part and meanwhile
685 * do rump kernel part right here
686 */
687
688 parg.pfds = pfd_host;
689 parg.nfds = nfds+1;
690 parg.ts = ts;
691 parg.sigmask = sigmask;
692 parg.pipefd = rpipe[1];
693 pthread_create(&pt, NULL, hostpoll, &parg);
694
695 op_pollts = syscalls[DUALCALL_POLLTS].bs_rump;
696 lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
697 sverrno = errno;
698 write(hpipe[1], &rv, sizeof(rv));
699 pthread_join(pt, (void *)&trv);
700
701 /* check who "won" and merge results */
702 if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
703 rv = trv;
704
705 for (i = 0; i < nfds; i++) {
706 if (pfd_rump[i].fd != -1)
707 fds[i].revents = pfd_rump[i].revents;
708 }
709 sverrno = parg.errnum;
710 } else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
711 rv = trv;
712
713 for (i = 0; i < nfds; i++) {
714 if (pfd_host[i].fd != -1)
715 fds[i].revents = pfd_host[i].revents;
716 }
717 } else {
718 rv = 0;
719 }
720
721 out:
722 host_close = syscalls[DUALCALL_CLOSE].bs_host;
723 if (rpipe[0] != -1)
724 rump_sys_close(rpipe[0]);
725 if (rpipe[1] != -1)
726 rump_sys_close(rpipe[1]);
727 if (hpipe[0] != -1)
728 host_close(hpipe[0]);
729 if (hpipe[1] != -1)
730 host_close(hpipe[1]);
731 free(pfd_host);
732 free(pfd_rump);
733 errno = sverrno;
734 } else {
735 if (hostcall) {
736 op_pollts = syscalls[DUALCALL_POLLTS].bs_host;
737 } else {
738 op_pollts = syscalls[DUALCALL_POLLTS].bs_rump;
739 adjustpoll(fds, nfds, fd_host2rump);
740 }
741
742 rv = op_pollts(fds, nfds, ts, sigmask);
743 if (rumpcall)
744 adjustpoll(fds, nfds, fd_rump2host);
745 }
746
747 return rv;
748 }
749
750 int
751 LIBCPOLL(struct pollfd *fds, nfds_t nfds, int timeout)
752 {
753 struct timespec ts;
754 struct timespec *tsp = NULL;
755
756 if (timeout != INFTIM) {
757 ts.tv_sec = timeout / 1000;
758 ts.tv_nsec = (timeout % 1000) * 1000*1000;
759
760 tsp = &ts;
761 }
762
763 return pollts(fds, nfds, tsp, NULL);
764 }
765
766 int
767 kqueue(void)
768 {
769
770 fprintf(stderr, "kqueue unsupported");
771 abort();
772 /*NOTREACHED*/
773 }
774
775 /*ARGSUSED*/
776 int
777 kevent(int kq, const struct kevent *changelist, size_t nchanges,
778 struct kevent *eventlist, size_t nevents,
779 const struct timespec *timeout)
780 {
781
782 fprintf(stderr, "kqueue unsupported");
783 abort();
784 /*NOTREACHED*/
785 }
786
787 /*
788 * Rest are std type calls.
789 */
790
791 FDCALL(int, bind, DUALCALL_BIND, \
792 (int fd, const struct sockaddr *name, socklen_t namelen), \
793 (int, const struct sockaddr *, socklen_t), \
794 (fd, name, namelen))
795
796 FDCALL(int, connect, DUALCALL_CONNECT, \
797 (int fd, const struct sockaddr *name, socklen_t namelen), \
798 (int, const struct sockaddr *, socklen_t), \
799 (fd, name, namelen))
800
801 FDCALL(int, getpeername, DUALCALL_GETPEERNAME, \
802 (int fd, struct sockaddr *name, socklen_t *namelen), \
803 (int, struct sockaddr *, socklen_t *), \
804 (fd, name, namelen))
805
806 FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, \
807 (int fd, struct sockaddr *name, socklen_t *namelen), \
808 (int, struct sockaddr *, socklen_t *), \
809 (fd, name, namelen))
810
811 FDCALL(int, listen, DUALCALL_LISTEN, \
812 (int fd, int backlog), \
813 (int, int), \
814 (fd, backlog))
815
816 FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, \
817 (int fd, void *buf, size_t len, int flags, \
818 struct sockaddr *from, socklen_t *fromlen), \
819 (int, void *, size_t, int, struct sockaddr *, socklen_t *), \
820 (fd, buf, len, flags, from, fromlen))
821
822 FDCALL(ssize_t, sendto, DUALCALL_SENDTO, \
823 (int fd, const void *buf, size_t len, int flags, \
824 const struct sockaddr *to, socklen_t tolen), \
825 (int, const void *, size_t, int, \
826 const struct sockaddr *, socklen_t), \
827 (fd, buf, len, flags, to, tolen))
828
829 FDCALL(ssize_t, recvmsg, DUALCALL_RECVMSG, \
830 (int fd, struct msghdr *msg, int flags), \
831 (int, struct msghdr *, int), \
832 (fd, msg, flags))
833
834 FDCALL(ssize_t, sendmsg, DUALCALL_SENDMSG, \
835 (int fd, const struct msghdr *msg, int flags), \
836 (int, const struct msghdr *, int), \
837 (fd, msg, flags))
838
839 FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, \
840 (int fd, int level, int optn, void *optval, socklen_t *optlen), \
841 (int, int, int, void *, socklen_t *), \
842 (fd, level, optn, optval, optlen))
843
844 FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, \
845 (int fd, int level, int optn, \
846 const void *optval, socklen_t optlen), \
847 (int, int, int, const void *, socklen_t), \
848 (fd, level, optn, optval, optlen))
849
850 FDCALL(int, shutdown, DUALCALL_SHUTDOWN, \
851 (int fd, int how), \
852 (int, int), \
853 (fd, how))
854
855 FDCALL(ssize_t, read, DUALCALL_READ, \
856 (int fd, void *buf, size_t buflen), \
857 (int, void *, size_t), \
858 (fd, buf, buflen))
859
860 FDCALL(ssize_t, readv, DUALCALL_READV, \
861 (int fd, const struct iovec *iov, int iovcnt), \
862 (int, const struct iovec *, int), \
863 (fd, iov, iovcnt))
864
865 FDCALL(ssize_t, writev, DUALCALL_WRITEV, \
866 (int fd, const struct iovec *iov, int iovcnt), \
867 (int, const struct iovec *, int), \
868 (fd, iov, iovcnt))
869
870 FDCALL(int, close, DUALCALL_CLOSE, \
871 (int fd), \
872 (int), \
873 (fd))
874