hijack.c revision 1.22 1 /* $NetBSD: hijack.c,v 1.22 2011/01/27 18:05:16 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.22 2011/01/27 18:05:16 pooka Exp $");
30
31 #define __ssp_weak_name(fun) _hijack_ ## fun
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/event.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/poll.h>
39
40 #include <rump/rumpclient.h>
41 #include <rump/rump_syscalls.h>
42
43 #include <assert.h>
44 #include <dlfcn.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <poll.h>
49 #include <pthread.h>
50 #include <signal.h>
51 #include <stdarg.h>
52 #include <stdbool.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <time.h>
56 #include <unistd.h>
57
58 enum dualcall {
59 DUALCALL_WRITE, DUALCALL_WRITEV,
60 DUALCALL_IOCTL, DUALCALL_FCNTL,
61 DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_BIND, DUALCALL_CONNECT,
62 DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN,
63 DUALCALL_RECVFROM, DUALCALL_RECVMSG,
64 DUALCALL_SENDTO, DUALCALL_SENDMSG,
65 DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT,
66 DUALCALL_SHUTDOWN,
67 DUALCALL_READ, DUALCALL_READV,
68 DUALCALL_DUP2, DUALCALL_CLOSE,
69 DUALCALL_POLLTS,
70 DUALCALL__NUM
71 };
72
73 #define RSYS_STRING(a) __STRING(a)
74 #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a))
75
76 /*
77 * Would be nice to get this automatically in sync with libc.
78 * Also, this does not work for compat-using binaries!
79 */
80 #if !__NetBSD_Prereq__(5,99,7)
81 #define LIBCSELECT select
82 #define LIBCPOLLTS pollts
83 #define LIBCPOLL poll
84 #else
85 #define LIBCSELECT __select50
86 #define LIBCPOLLTS __pollts50
87 #define LIBCPOLL __poll50
88 #endif
89
90 int LIBCSELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *);
91 int LIBCPOLLTS(struct pollfd *, nfds_t,
92 const struct timespec *, const sigset_t *);
93 int LIBCPOLL(struct pollfd *, nfds_t, int);
94
95 #define S(a) __STRING(a)
96 struct sysnames {
97 enum dualcall scm_callnum;
98 const char *scm_hostname;
99 const char *scm_rumpname;
100 } syscnames[] = {
101 { DUALCALL_SOCKET, "__socket30", RSYS_NAME(SOCKET) },
102 { DUALCALL_ACCEPT, "accept", RSYS_NAME(ACCEPT) },
103 { DUALCALL_BIND, "bind", RSYS_NAME(BIND) },
104 { DUALCALL_CONNECT, "connect", RSYS_NAME(CONNECT) },
105 { DUALCALL_GETPEERNAME, "getpeername", RSYS_NAME(GETPEERNAME) },
106 { DUALCALL_GETSOCKNAME, "getsockname", RSYS_NAME(GETSOCKNAME) },
107 { DUALCALL_LISTEN, "listen", RSYS_NAME(LISTEN) },
108 { DUALCALL_RECVFROM, "recvfrom", RSYS_NAME(RECVFROM) },
109 { DUALCALL_RECVMSG, "recvmsg", RSYS_NAME(RECVMSG) },
110 { DUALCALL_SENDTO, "sendto", RSYS_NAME(SENDTO) },
111 { DUALCALL_SENDMSG, "sendmsg", RSYS_NAME(SENDMSG) },
112 { DUALCALL_GETSOCKOPT, "getsockopt", RSYS_NAME(GETSOCKOPT) },
113 { DUALCALL_SETSOCKOPT, "setsockopt", RSYS_NAME(SETSOCKOPT) },
114 { DUALCALL_SHUTDOWN, "shutdown", RSYS_NAME(SHUTDOWN) },
115 { DUALCALL_READ, "read", RSYS_NAME(READ) },
116 { DUALCALL_READV, "readv", RSYS_NAME(READV) },
117 { DUALCALL_WRITE, "write", RSYS_NAME(WRITE) },
118 { DUALCALL_WRITEV, "writev", RSYS_NAME(WRITEV) },
119 { DUALCALL_IOCTL, "ioctl", RSYS_NAME(IOCTL) },
120 { DUALCALL_FCNTL, "fcntl", RSYS_NAME(FCNTL) },
121 { DUALCALL_DUP2, "dup2", RSYS_NAME(DUP2) },
122 { DUALCALL_CLOSE, "close", RSYS_NAME(CLOSE) },
123 { DUALCALL_POLLTS, S(LIBCPOLLTS), RSYS_NAME(POLLTS) },
124 };
125 #undef S
126
127 struct bothsys {
128 void *bs_host;
129 void *bs_rump;
130 } syscalls[DUALCALL__NUM];
131 #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which
132
133 pid_t (*host_fork)(void);
134
135 static unsigned dup2mask;
136 #define ISDUP2D(fd) (1<<(fd) & dup2mask)
137
138 //#define DEBUGJACK
139 #ifdef DEBUGJACK
140 #define DPRINTF(x) mydprintf x
141 static void
142 mydprintf(const char *fmt, ...)
143 {
144 va_list ap;
145
146 if (ISDUP2D(STDERR_FILENO))
147 return;
148
149 va_start(ap, fmt);
150 vfprintf(stderr, fmt, ap);
151 va_end(ap);
152 }
153
154 #else
155 #define DPRINTF(x)
156 #endif
157
158 #define FDCALL(type, name, rcname, args, proto, vars) \
159 type name args \
160 { \
161 type (*fun) proto; \
162 \
163 if (fd_isrump(fd)) { \
164 fun = syscalls[rcname].bs_rump; \
165 fd = fd_host2rump(fd); \
166 } else { \
167 fun = syscalls[rcname].bs_host; \
168 } \
169 \
170 return fun vars; \
171 }
172
173 /*
174 * This is called from librumpclient in case of LD_PRELOAD.
175 * It ensures correct RTLD_NEXT.
176 */
177 static void *
178 hijackdlsym(void *handle, const char *symbol)
179 {
180
181 return dlsym(handle, symbol);
182 }
183
184 /* low calorie sockets? */
185 static bool hostlocalsockets = true;
186
187 static void __attribute__((constructor))
188 rcinit(void)
189 {
190 void **rumpcdlsym;
191 void *hand;
192 unsigned i, j;
193
194 hand = dlopen("librumpclient.so", RTLD_LAZY|RTLD_GLOBAL);
195 if (!hand)
196 err(1, "cannot open librumpclient.so");
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 (rumpclient_init() == -1)
230 err(1, "rumpclient init");
231 rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME);
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 #if _FORTIFY_SOURCE > 0
857 #define STUB(fun) __ssp_weak_name(fun)
858 ssize_t _sys_readlink(const char * __restrict, char * __restrict, size_t);
859 ssize_t
860 STUB(readlink)(const char * __restrict path, char * __restrict buf,
861 size_t bufsiz)
862 {
863 return _sys_readlink(path, buf, bufsiz);
864 }
865
866 char *_sys_getcwd(char *, size_t);
867 char *
868 STUB(getcwd)(char *buf, size_t size)
869 {
870 return _sys_getcwd(buf, size);
871 }
872 #else
873 #define STUB(fun) fun
874 #endif
875
876 FDCALL(ssize_t, STUB(read), DUALCALL_READ, \
877 (int fd, void *buf, size_t buflen), \
878 (int, void *, size_t), \
879 (fd, buf, buflen))
880
881 FDCALL(ssize_t, readv, DUALCALL_READV, \
882 (int fd, const struct iovec *iov, int iovcnt), \
883 (int, const struct iovec *, int), \
884 (fd, iov, iovcnt))
885
886 FDCALL(ssize_t, writev, DUALCALL_WRITEV, \
887 (int fd, const struct iovec *iov, int iovcnt), \
888 (int, const struct iovec *, int), \
889 (fd, iov, iovcnt))
890
891 FDCALL(int, close, DUALCALL_CLOSE, \
892 (int fd), \
893 (int), \
894 (fd))
895