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