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