hijack.c revision 1.18 1 /* $NetBSD: hijack.c,v 1.18 2011/01/25 12:21:36 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.18 2011/01/25 12:21:36 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 int 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 i, j, realnfds;
452 int rv, incr;
453
454 DPRINTF(("select\n"));
455
456 /*
457 * Well, first we must scan the fds to figure out how many
458 * fds there really are. This is because up to and including
459 * nb5 poll() silently refuses nfds > process_maxopen_fds.
460 * Seems to be fixed in current, thank the maker.
461 * god damn cluster...bomb.
462 */
463
464 for (i = 0, realnfds = 0; i < nfds; i++) {
465 if (readfds && FD_ISSET(i, readfds)) {
466 realnfds++;
467 continue;
468 }
469 if (writefds && FD_ISSET(i, writefds)) {
470 realnfds++;
471 continue;
472 }
473 if (exceptfds && FD_ISSET(i, exceptfds)) {
474 realnfds++;
475 continue;
476 }
477 }
478
479 if (realnfds) {
480 pfds = malloc(sizeof(*pfds) * realnfds);
481 if (!pfds)
482 return -1;
483 } else {
484 pfds = NULL;
485 }
486
487 for (i = 0, j = 0; i < nfds; i++) {
488 incr = 0;
489 pfds[j].events = pfds[j].revents = 0;
490 if (readfds && FD_ISSET(i, readfds)) {
491 pfds[j].fd = i;
492 pfds[j].events |= POLLIN;
493 incr=1;
494 }
495 if (writefds && FD_ISSET(i, writefds)) {
496 pfds[j].fd = i;
497 pfds[j].events |= POLLOUT;
498 incr=1;
499 }
500 if (exceptfds && FD_ISSET(i, exceptfds)) {
501 pfds[j].fd = i;
502 pfds[j].events |= POLLHUP|POLLERR;
503 incr=1;
504 }
505 if (incr)
506 j++;
507 }
508
509 if (timeout) {
510 TIMEVAL_TO_TIMESPEC(timeout, &ts);
511 tsp = &ts;
512 }
513 rv = pollts(pfds, realnfds, tsp, NULL);
514 if (rv <= 0)
515 goto out;
516
517 /*
518 * ok, harvest results. first zero out entries (can't use
519 * FD_ZERO for the obvious select-me-not reason). whee.
520 */
521 for (i = 0; i < nfds; i++) {
522 if (readfds)
523 FD_CLR(i, readfds);
524 if (writefds)
525 FD_CLR(i, writefds);
526 if (exceptfds)
527 FD_CLR(i, exceptfds);
528 }
529
530 /* and then plug in the results */
531 for (i = 0; i < realnfds; i++) {
532 if (readfds) {
533 if (pfds[i].revents & POLLIN) {
534 FD_SET(pfds[i].fd, readfds);
535 }
536 }
537 if (writefds) {
538 if (pfds[i].revents & POLLOUT) {
539 FD_SET(pfds[i].fd, writefds);
540 }
541 }
542 if (exceptfds) {
543 if (pfds[i].revents & (POLLHUP|POLLERR)) {
544 FD_SET(pfds[i].fd, exceptfds);
545 }
546 }
547 }
548
549 out:
550 free(pfds);
551 return rv;
552 }
553
554 static void
555 checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
556 {
557 nfds_t i;
558
559 for (i = 0; i < nfds; i++) {
560 if (fds[i].fd == -1)
561 continue;
562
563 if (fd_isrump(fds[i].fd))
564 (*rumpcall)++;
565 else
566 (*hostcall)++;
567 }
568 }
569
570 static void
571 adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
572 {
573 nfds_t i;
574
575 for (i = 0; i < nfds; i++) {
576 fds[i].fd = fdadj(fds[i].fd);
577 }
578 }
579
580 /*
581 * poll is easy as long as the call comes in the fds only in one
582 * kernel. otherwise its quite tricky...
583 */
584 struct pollarg {
585 struct pollfd *pfds;
586 nfds_t nfds;
587 const struct timespec *ts;
588 const sigset_t *sigmask;
589 int pipefd;
590 int errnum;
591 };
592
593 static void *
594 hostpoll(void *arg)
595 {
596 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
597 const sigset_t *);
598 struct pollarg *parg = arg;
599 intptr_t rv;
600
601 op_pollts = syscalls[DUALCALL_POLLTS].bs_host;
602 rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
603 if (rv == -1)
604 parg->errnum = errno;
605 rump_sys_write(parg->pipefd, &rv, sizeof(rv));
606
607 return (void *)(intptr_t)rv;
608 }
609
610 int
611 LIBCPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
612 const sigset_t *sigmask)
613 {
614 int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
615 const sigset_t *);
616 int (*host_close)(int);
617 int hostcall = 0, rumpcall = 0;
618 pthread_t pt;
619 nfds_t i;
620 int rv;
621
622 DPRINTF(("poll\n"));
623 checkpoll(fds, nfds, &hostcall, &rumpcall);
624
625 if (hostcall && rumpcall) {
626 struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
627 int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
628 struct pollarg parg;
629 uintptr_t lrv;
630 int sverrno = 0, trv;
631
632 /*
633 * ok, this is where it gets tricky. We must support
634 * this since it's a very common operation in certain
635 * types of software (telnet, netcat, etc). We allocate
636 * two vectors and run two poll commands in separate
637 * threads. Whichever returns first "wins" and the
638 * other kernel's fds won't show activity.
639 */
640 rv = -1;
641
642 /* allocate full vector for O(n) joining after call */
643 pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
644 if (!pfd_host)
645 goto out;
646 pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
647 if (!pfd_rump) {
648 goto out;
649 }
650
651 /* split vectors */
652 for (i = 0; i < nfds; i++) {
653 if (fds[i].fd == -1) {
654 pfd_host[i].fd = -1;
655 pfd_rump[i].fd = -1;
656 } else if (fd_isrump(fds[i].fd)) {
657 pfd_host[i].fd = -1;
658 pfd_rump[i].fd = fd_host2rump(fds[i].fd);
659 pfd_rump[i].events = fds[i].events;
660 } else {
661 pfd_rump[i].fd = -1;
662 pfd_host[i].fd = fds[i].fd;
663 pfd_host[i].events = fds[i].events;
664 }
665 fds[i].revents = 0;
666 }
667
668 /*
669 * then, open two pipes, one for notifications
670 * to each kernel.
671 */
672 if (rump_sys_pipe(rpipe) == -1)
673 goto out;
674 if (pipe(hpipe) == -1)
675 goto out;
676
677 pfd_host[nfds].fd = hpipe[0];
678 pfd_host[nfds].events = POLLIN;
679 pfd_rump[nfds].fd = rpipe[0];
680 pfd_rump[nfds].events = POLLIN;
681
682 /*
683 * then, create a thread to do host part and meanwhile
684 * do rump kernel part right here
685 */
686
687 parg.pfds = pfd_host;
688 parg.nfds = nfds+1;
689 parg.ts = ts;
690 parg.sigmask = sigmask;
691 parg.pipefd = rpipe[1];
692 pthread_create(&pt, NULL, hostpoll, &parg);
693
694 op_pollts = syscalls[DUALCALL_POLLTS].bs_rump;
695 lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
696 sverrno = errno;
697 write(hpipe[1], &rv, sizeof(rv));
698 pthread_join(pt, (void *)&trv);
699
700 /* check who "won" and merge results */
701 if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
702 rv = trv;
703
704 for (i = 0; i < nfds; i++) {
705 if (pfd_rump[i].fd != -1)
706 fds[i].revents = pfd_rump[i].revents;
707 }
708 sverrno = parg.errnum;
709 } else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
710 rv = trv;
711
712 for (i = 0; i < nfds; i++) {
713 if (pfd_host[i].fd != -1)
714 fds[i].revents = pfd_host[i].revents;
715 }
716 } else {
717 rv = 0;
718 }
719
720 out:
721 host_close = syscalls[DUALCALL_CLOSE].bs_host;
722 if (rpipe[0] != -1)
723 rump_sys_close(rpipe[0]);
724 if (rpipe[1] != -1)
725 rump_sys_close(rpipe[1]);
726 if (hpipe[0] != -1)
727 host_close(hpipe[0]);
728 if (hpipe[1] != -1)
729 host_close(hpipe[1]);
730 free(pfd_host);
731 free(pfd_rump);
732 errno = sverrno;
733 } else {
734 if (hostcall) {
735 op_pollts = syscalls[DUALCALL_POLLTS].bs_host;
736 } else {
737 op_pollts = syscalls[DUALCALL_POLLTS].bs_rump;
738 adjustpoll(fds, nfds, fd_host2rump);
739 }
740
741 rv = op_pollts(fds, nfds, ts, sigmask);
742 if (rumpcall)
743 adjustpoll(fds, nfds, fd_rump2host);
744 }
745
746 return rv;
747 }
748
749 int
750 LIBCPOLL(struct pollfd *fds, nfds_t nfds, int timeout)
751 {
752 struct timespec ts;
753 struct timespec *tsp = NULL;
754
755 if (timeout != INFTIM) {
756 ts.tv_sec = timeout / 1000;
757 ts.tv_nsec = (timeout % 1000) * 1000*1000;
758
759 tsp = &ts;
760 }
761
762 return pollts(fds, nfds, tsp, NULL);
763 }
764
765 int
766 kqueue(void)
767 {
768
769 fprintf(stderr, "kqueue unsupported");
770 abort();
771 /*NOTREACHED*/
772 }
773
774 /*ARGSUSED*/
775 int
776 kevent(int kq, const struct kevent *changelist, size_t nchanges,
777 struct kevent *eventlist, size_t nevents,
778 const struct timespec *timeout)
779 {
780
781 fprintf(stderr, "kqueue unsupported");
782 abort();
783 /*NOTREACHED*/
784 }
785
786 /*
787 * Rest are std type calls.
788 */
789
790 FDCALL(int, bind, DUALCALL_BIND, \
791 (int fd, const struct sockaddr *name, socklen_t namelen), \
792 (int, const struct sockaddr *, socklen_t), \
793 (fd, name, namelen))
794
795 FDCALL(int, connect, DUALCALL_CONNECT, \
796 (int fd, const struct sockaddr *name, socklen_t namelen), \
797 (int, const struct sockaddr *, socklen_t), \
798 (fd, name, namelen))
799
800 FDCALL(int, getpeername, DUALCALL_GETPEERNAME, \
801 (int fd, struct sockaddr *name, socklen_t *namelen), \
802 (int, struct sockaddr *, socklen_t *), \
803 (fd, name, namelen))
804
805 FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, \
806 (int fd, struct sockaddr *name, socklen_t *namelen), \
807 (int, struct sockaddr *, socklen_t *), \
808 (fd, name, namelen))
809
810 FDCALL(int, listen, DUALCALL_LISTEN, \
811 (int fd, int backlog), \
812 (int, int), \
813 (fd, backlog))
814
815 FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, \
816 (int fd, void *buf, size_t len, int flags, \
817 struct sockaddr *from, socklen_t *fromlen), \
818 (int, void *, size_t, int, struct sockaddr *, socklen_t *), \
819 (fd, buf, len, flags, from, fromlen))
820
821 FDCALL(ssize_t, sendto, DUALCALL_SENDTO, \
822 (int fd, const void *buf, size_t len, int flags, \
823 const struct sockaddr *to, socklen_t tolen), \
824 (int, const void *, size_t, int, \
825 const struct sockaddr *, socklen_t), \
826 (fd, buf, len, flags, to, tolen))
827
828 FDCALL(ssize_t, recvmsg, DUALCALL_RECVMSG, \
829 (int fd, struct msghdr *msg, int flags), \
830 (int, struct msghdr *, int), \
831 (fd, msg, flags))
832
833 FDCALL(ssize_t, sendmsg, DUALCALL_SENDMSG, \
834 (int fd, const struct msghdr *msg, int flags), \
835 (int, const struct msghdr *, int), \
836 (fd, msg, flags))
837
838 FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, \
839 (int fd, int level, int optn, void *optval, socklen_t *optlen), \
840 (int, int, int, void *, socklen_t *), \
841 (fd, level, optn, optval, optlen))
842
843 FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, \
844 (int fd, int level, int optn, \
845 const void *optval, socklen_t optlen), \
846 (int, int, int, const void *, socklen_t), \
847 (fd, level, optn, optval, optlen))
848
849 FDCALL(int, shutdown, DUALCALL_SHUTDOWN, \
850 (int fd, int how), \
851 (int, int), \
852 (fd, how))
853
854 FDCALL(ssize_t, read, DUALCALL_READ, \
855 (int fd, void *buf, size_t buflen), \
856 (int, void *, size_t), \
857 (fd, buf, buflen))
858
859 FDCALL(ssize_t, readv, DUALCALL_READV, \
860 (int fd, const struct iovec *iov, int iovcnt), \
861 (int, const struct iovec *, int), \
862 (fd, iov, iovcnt))
863
864 FDCALL(ssize_t, writev, DUALCALL_WRITEV, \
865 (int fd, const struct iovec *iov, int iovcnt), \
866 (int, const struct iovec *, int), \
867 (fd, iov, iovcnt))
868
869 FDCALL(int, close, DUALCALL_CLOSE, \
870 (int fd), \
871 (int), \
872 (fd))
873