hijack.c revision 1.61 1 1.61 pooka /* $NetBSD: hijack.c,v 1.61 2011/02/21 12:55:21 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.61 pooka __RCSID("$NetBSD: hijack.c,v 1.61 2011/02/21 12:55:21 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.48 pooka #include <sys/mount.h>
38 1.48 pooka #include <sys/poll.h>
39 1.1 pooka #include <sys/socket.h>
40 1.45 pooka #include <sys/statvfs.h>
41 1.1 pooka
42 1.1 pooka #include <rump/rumpclient.h>
43 1.1 pooka #include <rump/rump_syscalls.h>
44 1.1 pooka
45 1.1 pooka #include <assert.h>
46 1.1 pooka #include <dlfcn.h>
47 1.1 pooka #include <err.h>
48 1.1 pooka #include <errno.h>
49 1.1 pooka #include <fcntl.h>
50 1.1 pooka #include <poll.h>
51 1.1 pooka #include <pthread.h>
52 1.3 pooka #include <signal.h>
53 1.1 pooka #include <stdarg.h>
54 1.8 pooka #include <stdbool.h>
55 1.1 pooka #include <stdio.h>
56 1.1 pooka #include <stdlib.h>
57 1.28 pooka #include <string.h>
58 1.3 pooka #include <time.h>
59 1.1 pooka #include <unistd.h>
60 1.1 pooka
61 1.17 pooka enum dualcall {
62 1.60 pooka DUALCALL_WRITE, DUALCALL_WRITEV, DUALCALL_PWRITE, DUALCALL_PWRITEV,
63 1.17 pooka DUALCALL_IOCTL, DUALCALL_FCNTL,
64 1.17 pooka DUALCALL_SOCKET, DUALCALL_ACCEPT, DUALCALL_BIND, DUALCALL_CONNECT,
65 1.17 pooka DUALCALL_GETPEERNAME, DUALCALL_GETSOCKNAME, DUALCALL_LISTEN,
66 1.17 pooka DUALCALL_RECVFROM, DUALCALL_RECVMSG,
67 1.17 pooka DUALCALL_SENDTO, DUALCALL_SENDMSG,
68 1.17 pooka DUALCALL_GETSOCKOPT, DUALCALL_SETSOCKOPT,
69 1.17 pooka DUALCALL_SHUTDOWN,
70 1.60 pooka DUALCALL_READ, DUALCALL_READV, DUALCALL_PREAD, DUALCALL_PREADV,
71 1.41 pooka DUALCALL_DUP2,
72 1.34 pooka DUALCALL_CLOSE,
73 1.17 pooka DUALCALL_POLLTS,
74 1.34 pooka DUALCALL_KEVENT,
75 1.45 pooka DUALCALL_STAT, DUALCALL_LSTAT, DUALCALL_FSTAT,
76 1.45 pooka DUALCALL_CHMOD, DUALCALL_LCHMOD, DUALCALL_FCHMOD,
77 1.45 pooka DUALCALL_CHOWN, DUALCALL_LCHOWN, DUALCALL_FCHOWN,
78 1.45 pooka DUALCALL_OPEN,
79 1.45 pooka DUALCALL_STATVFS1, DUALCALL_FSTATVFS1,
80 1.45 pooka DUALCALL_CHDIR, DUALCALL_FCHDIR,
81 1.45 pooka DUALCALL_LSEEK,
82 1.45 pooka DUALCALL_GETDENTS,
83 1.45 pooka DUALCALL_UNLINK, DUALCALL_SYMLINK, DUALCALL_READLINK,
84 1.45 pooka DUALCALL_RENAME,
85 1.45 pooka DUALCALL_MKDIR, DUALCALL_RMDIR,
86 1.45 pooka DUALCALL_UTIMES, DUALCALL_LUTIMES, DUALCALL_FUTIMES,
87 1.45 pooka DUALCALL_TRUNCATE, DUALCALL_FTRUNCATE,
88 1.45 pooka DUALCALL_FSYNC, DUALCALL_FSYNC_RANGE,
89 1.48 pooka DUALCALL_MOUNT, DUALCALL_UNMOUNT,
90 1.57 pooka DUALCALL___GETCWD,
91 1.60 pooka DUALCALL_CHFLAGS, DUALCALL_LCHFLAGS, DUALCALL_FCHFLAGS,
92 1.17 pooka DUALCALL__NUM
93 1.1 pooka };
94 1.1 pooka
95 1.8 pooka #define RSYS_STRING(a) __STRING(a)
96 1.8 pooka #define RSYS_NAME(a) RSYS_STRING(__CONCAT(RUMP_SYS_RENAME_,a))
97 1.8 pooka
98 1.1 pooka /*
99 1.14 pooka * Would be nice to get this automatically in sync with libc.
100 1.14 pooka * Also, this does not work for compat-using binaries!
101 1.14 pooka */
102 1.14 pooka #if !__NetBSD_Prereq__(5,99,7)
103 1.29 pooka #define REALSELECT select
104 1.29 pooka #define REALPOLLTS pollts
105 1.34 pooka #define REALKEVENT kevent
106 1.45 pooka #define REALSTAT __stat30
107 1.45 pooka #define REALLSTAT __lstat30
108 1.45 pooka #define REALFSTAT __fstat30
109 1.45 pooka #define REALUTIMES utimes
110 1.45 pooka #define REALLUTIMES lutimes
111 1.45 pooka #define REALFUTIMES futimes
112 1.14 pooka #else
113 1.29 pooka #define REALSELECT _sys___select50
114 1.29 pooka #define REALPOLLTS _sys___pollts50
115 1.34 pooka #define REALKEVENT _sys___kevent50
116 1.45 pooka #define REALSTAT __stat50
117 1.45 pooka #define REALLSTAT __lstat50
118 1.45 pooka #define REALFSTAT __fstat50
119 1.45 pooka #define REALUTIMES __utimes50
120 1.45 pooka #define REALLUTIMES __lutimes50
121 1.45 pooka #define REALFUTIMES __futimes50
122 1.17 pooka #endif
123 1.31 pooka #define REALREAD _sys_read
124 1.60 pooka #define REALPREAD _sys_pread
125 1.60 pooka #define REALPWRITE _sys_pwrite
126 1.45 pooka #define REALGETDENTS __getdents30
127 1.48 pooka #define REALMOUNT __mount50
128 1.14 pooka
129 1.29 pooka int REALSELECT(int, fd_set *, fd_set *, fd_set *, struct timeval *);
130 1.29 pooka int REALPOLLTS(struct pollfd *, nfds_t,
131 1.20 pooka const struct timespec *, const sigset_t *);
132 1.34 pooka int REALKEVENT(int, const struct kevent *, size_t, struct kevent *, size_t,
133 1.34 pooka const struct timespec *);
134 1.31 pooka ssize_t REALREAD(int, void *, size_t);
135 1.60 pooka ssize_t REALPREAD(int, void *, size_t, off_t);
136 1.60 pooka ssize_t REALPWRITE(int, const void *, size_t, off_t);
137 1.45 pooka int REALSTAT(const char *, struct stat *);
138 1.45 pooka int REALLSTAT(const char *, struct stat *);
139 1.45 pooka int REALFSTAT(int, struct stat *);
140 1.45 pooka int REALGETDENTS(int, char *, size_t);
141 1.45 pooka int REALUTIMES(const char *, const struct timeval [2]);
142 1.45 pooka int REALLUTIMES(const char *, const struct timeval [2]);
143 1.45 pooka int REALFUTIMES(int, const struct timeval [2]);
144 1.48 pooka int REALMOUNT(const char *, const char *, int, void *, size_t);
145 1.57 pooka int __getcwd(char *, size_t);
146 1.17 pooka
147 1.17 pooka #define S(a) __STRING(a)
148 1.17 pooka struct sysnames {
149 1.17 pooka enum dualcall scm_callnum;
150 1.17 pooka const char *scm_hostname;
151 1.17 pooka const char *scm_rumpname;
152 1.17 pooka } syscnames[] = {
153 1.17 pooka { DUALCALL_SOCKET, "__socket30", RSYS_NAME(SOCKET) },
154 1.17 pooka { DUALCALL_ACCEPT, "accept", RSYS_NAME(ACCEPT) },
155 1.17 pooka { DUALCALL_BIND, "bind", RSYS_NAME(BIND) },
156 1.17 pooka { DUALCALL_CONNECT, "connect", RSYS_NAME(CONNECT) },
157 1.17 pooka { DUALCALL_GETPEERNAME, "getpeername", RSYS_NAME(GETPEERNAME) },
158 1.17 pooka { DUALCALL_GETSOCKNAME, "getsockname", RSYS_NAME(GETSOCKNAME) },
159 1.17 pooka { DUALCALL_LISTEN, "listen", RSYS_NAME(LISTEN) },
160 1.17 pooka { DUALCALL_RECVFROM, "recvfrom", RSYS_NAME(RECVFROM) },
161 1.17 pooka { DUALCALL_RECVMSG, "recvmsg", RSYS_NAME(RECVMSG) },
162 1.17 pooka { DUALCALL_SENDTO, "sendto", RSYS_NAME(SENDTO) },
163 1.17 pooka { DUALCALL_SENDMSG, "sendmsg", RSYS_NAME(SENDMSG) },
164 1.17 pooka { DUALCALL_GETSOCKOPT, "getsockopt", RSYS_NAME(GETSOCKOPT) },
165 1.17 pooka { DUALCALL_SETSOCKOPT, "setsockopt", RSYS_NAME(SETSOCKOPT) },
166 1.17 pooka { DUALCALL_SHUTDOWN, "shutdown", RSYS_NAME(SHUTDOWN) },
167 1.31 pooka { DUALCALL_READ, S(REALREAD), RSYS_NAME(READ) },
168 1.17 pooka { DUALCALL_READV, "readv", RSYS_NAME(READV) },
169 1.60 pooka { DUALCALL_PREAD, S(REALPREAD), RSYS_NAME(PREAD) },
170 1.60 pooka { DUALCALL_PREADV, "preadv", RSYS_NAME(PREADV) },
171 1.17 pooka { DUALCALL_WRITE, "write", RSYS_NAME(WRITE) },
172 1.17 pooka { DUALCALL_WRITEV, "writev", RSYS_NAME(WRITEV) },
173 1.60 pooka { DUALCALL_PWRITE, S(REALPWRITE), RSYS_NAME(PWRITE) },
174 1.60 pooka { DUALCALL_PWRITEV, "pwritev", RSYS_NAME(PWRITEV) },
175 1.17 pooka { DUALCALL_IOCTL, "ioctl", RSYS_NAME(IOCTL) },
176 1.17 pooka { DUALCALL_FCNTL, "fcntl", RSYS_NAME(FCNTL) },
177 1.17 pooka { DUALCALL_DUP2, "dup2", RSYS_NAME(DUP2) },
178 1.17 pooka { DUALCALL_CLOSE, "close", RSYS_NAME(CLOSE) },
179 1.29 pooka { DUALCALL_POLLTS, S(REALPOLLTS), RSYS_NAME(POLLTS) },
180 1.34 pooka { DUALCALL_KEVENT, S(REALKEVENT), RSYS_NAME(KEVENT) },
181 1.45 pooka { DUALCALL_STAT, S(REALSTAT), RSYS_NAME(STAT) },
182 1.45 pooka { DUALCALL_LSTAT, S(REALLSTAT), RSYS_NAME(LSTAT) },
183 1.45 pooka { DUALCALL_FSTAT, S(REALFSTAT), RSYS_NAME(FSTAT) },
184 1.45 pooka { DUALCALL_CHOWN, "chown", RSYS_NAME(CHOWN) },
185 1.45 pooka { DUALCALL_LCHOWN, "lchown", RSYS_NAME(LCHOWN) },
186 1.45 pooka { DUALCALL_FCHOWN, "fchown", RSYS_NAME(FCHOWN) },
187 1.45 pooka { DUALCALL_CHMOD, "chmod", RSYS_NAME(CHMOD) },
188 1.45 pooka { DUALCALL_LCHMOD, "lchmod", RSYS_NAME(LCHMOD) },
189 1.45 pooka { DUALCALL_FCHMOD, "fchmod", RSYS_NAME(FCHMOD) },
190 1.45 pooka { DUALCALL_UTIMES, S(REALUTIMES), RSYS_NAME(UTIMES) },
191 1.45 pooka { DUALCALL_LUTIMES, S(REALLUTIMES), RSYS_NAME(LUTIMES) },
192 1.45 pooka { DUALCALL_FUTIMES, S(REALFUTIMES), RSYS_NAME(FUTIMES) },
193 1.45 pooka { DUALCALL_OPEN, "open", RSYS_NAME(OPEN) },
194 1.45 pooka { DUALCALL_STATVFS1, "statvfs1", RSYS_NAME(STATVFS1) },
195 1.45 pooka { DUALCALL_FSTATVFS1, "fstatvfs1", RSYS_NAME(FSTATVFS1) },
196 1.45 pooka { DUALCALL_CHDIR, "chdir", RSYS_NAME(CHDIR) },
197 1.45 pooka { DUALCALL_FCHDIR, "fchdir", RSYS_NAME(FCHDIR) },
198 1.61 pooka { DUALCALL_LSEEK, "lseek", RSYS_NAME(LSEEK) },
199 1.45 pooka { DUALCALL_GETDENTS, "__getdents30", RSYS_NAME(GETDENTS) },
200 1.45 pooka { DUALCALL_UNLINK, "unlink", RSYS_NAME(UNLINK) },
201 1.45 pooka { DUALCALL_SYMLINK, "symlink", RSYS_NAME(SYMLINK) },
202 1.45 pooka { DUALCALL_READLINK, "readlink", RSYS_NAME(READLINK) },
203 1.45 pooka { DUALCALL_RENAME, "rename", RSYS_NAME(RENAME) },
204 1.45 pooka { DUALCALL_MKDIR, "mkdir", RSYS_NAME(MKDIR) },
205 1.45 pooka { DUALCALL_RMDIR, "rmdir", RSYS_NAME(RMDIR) },
206 1.45 pooka { DUALCALL_TRUNCATE, "truncate", RSYS_NAME(TRUNCATE) },
207 1.45 pooka { DUALCALL_FTRUNCATE, "ftruncate", RSYS_NAME(FTRUNCATE) },
208 1.45 pooka { DUALCALL_FSYNC, "fsync", RSYS_NAME(FSYNC) },
209 1.45 pooka { DUALCALL_FSYNC_RANGE, "fsync_range", RSYS_NAME(FSYNC_RANGE) },
210 1.48 pooka { DUALCALL_MOUNT, S(REALMOUNT), RSYS_NAME(MOUNT) },
211 1.48 pooka { DUALCALL_UNMOUNT, "unmount", RSYS_NAME(UNMOUNT) },
212 1.57 pooka { DUALCALL___GETCWD, "__getcwd", RSYS_NAME(__GETCWD) },
213 1.60 pooka { DUALCALL_CHFLAGS, "chflags", RSYS_NAME(CHFLAGS) },
214 1.60 pooka { DUALCALL_LCHFLAGS, "lchflags", RSYS_NAME(LCHFLAGS) },
215 1.60 pooka { DUALCALL_FCHFLAGS, "fchflags", RSYS_NAME(FCHFLAGS) },
216 1.17 pooka };
217 1.17 pooka #undef S
218 1.17 pooka
219 1.17 pooka struct bothsys {
220 1.17 pooka void *bs_host;
221 1.17 pooka void *bs_rump;
222 1.17 pooka } syscalls[DUALCALL__NUM];
223 1.17 pooka #define GETSYSCALL(which, name) syscalls[DUALCALL_##name].bs_##which
224 1.17 pooka
225 1.25 pooka pid_t (*host_fork)(void);
226 1.25 pooka int (*host_daemon)(int, int);
227 1.39 pooka int (*host_execve)(const char *, char *const[], char *const[]);
228 1.17 pooka
229 1.47 pooka /* ok, we need *two* bits per dup2'd fd to track fd+HIJACKOFF aliases */
230 1.40 pooka static uint32_t dup2mask;
231 1.47 pooka #define ISDUP2D(fd) (((fd) < 16) && (1<<(fd) & dup2mask))
232 1.40 pooka #define SETDUP2(fd) \
233 1.47 pooka do { if ((fd) < 16) dup2mask |= (1<<(fd)); } while (/*CONSTCOND*/0)
234 1.40 pooka #define CLRDUP2(fd) \
235 1.47 pooka do { if ((fd) < 16) dup2mask &= ~(1<<(fd)); } while (/*CONSTCOND*/0)
236 1.47 pooka #define ISDUP2ALIAS(fd) (((fd) < 16) && (1<<((fd)+16) & dup2mask))
237 1.47 pooka #define SETDUP2ALIAS(fd) \
238 1.47 pooka do { if ((fd) < 16) dup2mask |= (1<<((fd)+16)); } while (/*CONSTCOND*/0)
239 1.47 pooka #define CLRDUP2ALIAS(fd) \
240 1.47 pooka do { if ((fd) < 16) dup2mask &= ~(1<<((fd)+16)); } while (/*CONSTCOND*/0)
241 1.17 pooka
242 1.17 pooka //#define DEBUGJACK
243 1.17 pooka #ifdef DEBUGJACK
244 1.17 pooka #define DPRINTF(x) mydprintf x
245 1.17 pooka static void
246 1.17 pooka mydprintf(const char *fmt, ...)
247 1.17 pooka {
248 1.17 pooka va_list ap;
249 1.17 pooka
250 1.17 pooka if (ISDUP2D(STDERR_FILENO))
251 1.17 pooka return;
252 1.17 pooka
253 1.17 pooka va_start(ap, fmt);
254 1.17 pooka vfprintf(stderr, fmt, ap);
255 1.17 pooka va_end(ap);
256 1.17 pooka }
257 1.17 pooka
258 1.17 pooka #else
259 1.17 pooka #define DPRINTF(x)
260 1.14 pooka #endif
261 1.14 pooka
262 1.17 pooka #define FDCALL(type, name, rcname, args, proto, vars) \
263 1.17 pooka type name args \
264 1.17 pooka { \
265 1.17 pooka type (*fun) proto; \
266 1.17 pooka \
267 1.33 pooka DPRINTF(("%s -> %d\n", __STRING(name), fd)); \
268 1.17 pooka if (fd_isrump(fd)) { \
269 1.17 pooka fun = syscalls[rcname].bs_rump; \
270 1.17 pooka fd = fd_host2rump(fd); \
271 1.17 pooka } else { \
272 1.17 pooka fun = syscalls[rcname].bs_host; \
273 1.17 pooka } \
274 1.17 pooka \
275 1.17 pooka return fun vars; \
276 1.17 pooka }
277 1.17 pooka
278 1.45 pooka #define PATHCALL(type, name, rcname, args, proto, vars) \
279 1.45 pooka type name args \
280 1.45 pooka { \
281 1.45 pooka type (*fun) proto; \
282 1.45 pooka \
283 1.45 pooka DPRINTF(("%s -> %s\n", __STRING(name), path)); \
284 1.45 pooka if (path_isrump(path)) { \
285 1.45 pooka fun = syscalls[rcname].bs_rump; \
286 1.45 pooka path = path_host2rump(path); \
287 1.45 pooka } else { \
288 1.45 pooka fun = syscalls[rcname].bs_host; \
289 1.45 pooka } \
290 1.45 pooka \
291 1.45 pooka return fun vars; \
292 1.45 pooka }
293 1.45 pooka
294 1.14 pooka /*
295 1.1 pooka * This is called from librumpclient in case of LD_PRELOAD.
296 1.1 pooka * It ensures correct RTLD_NEXT.
297 1.32 pooka *
298 1.32 pooka * ... except, it's apparently extremely difficult to force
299 1.32 pooka * at least gcc to generate an actual stack frame here. So
300 1.32 pooka * sprinkle some volatile foobar and baz to throw the optimizer
301 1.32 pooka * off the scent and generate a variable assignment with the
302 1.32 pooka * return value. The posterboy for this meltdown is amd64
303 1.32 pooka * with -O2. At least with gcc 4.1.3 i386 works regardless of
304 1.32 pooka * optimization.
305 1.1 pooka */
306 1.32 pooka volatile int rumphijack_unrope; /* there, unhang yourself */
307 1.1 pooka static void *
308 1.1 pooka hijackdlsym(void *handle, const char *symbol)
309 1.1 pooka {
310 1.32 pooka void *rv;
311 1.32 pooka
312 1.32 pooka rv = dlsym(handle, symbol);
313 1.32 pooka rumphijack_unrope = *(volatile int *)rv;
314 1.1 pooka
315 1.32 pooka return (void *)rv;
316 1.1 pooka }
317 1.1 pooka
318 1.49 pooka /*
319 1.49 pooka * This tracks if our process is in a subdirectory of /rump.
320 1.49 pooka * It's preserved over exec.
321 1.49 pooka */
322 1.49 pooka static bool pwdinrump = false;
323 1.49 pooka
324 1.49 pooka /*
325 1.49 pooka * These variables are set from the RUMPHIJACK string and control
326 1.49 pooka * which operations can product rump kernel file descriptors.
327 1.49 pooka * This should be easily extendable for future needs.
328 1.49 pooka */
329 1.49 pooka #define RUMPHIJACK_DEFAULT "path=/rump,socket=all:nolocal"
330 1.49 pooka static bool rumpsockets[PF_MAX];
331 1.49 pooka static const char *rumpprefix;
332 1.49 pooka static size_t rumpprefixlen;
333 1.49 pooka
334 1.49 pooka static struct {
335 1.49 pooka int pf;
336 1.49 pooka const char *name;
337 1.49 pooka } socketmap[] = {
338 1.51 pooka { PF_LOCAL, "local" },
339 1.49 pooka { PF_INET, "inet" },
340 1.49 pooka { PF_LINK, "link" },
341 1.55 pooka #ifdef PF_OROUTE
342 1.56 pooka { PF_OROUTE, "oroute" },
343 1.56 pooka #endif
344 1.49 pooka { PF_ROUTE, "route" },
345 1.49 pooka { PF_INET6, "inet6" },
346 1.55 pooka #ifdef PF_MPLS
347 1.55 pooka { PF_MPLS, "mpls" },
348 1.55 pooka #endif
349 1.49 pooka { -1, NULL }
350 1.49 pooka };
351 1.49 pooka
352 1.49 pooka static void
353 1.49 pooka sockparser(char *buf)
354 1.49 pooka {
355 1.49 pooka char *p, *l;
356 1.49 pooka bool value;
357 1.49 pooka int i;
358 1.49 pooka
359 1.49 pooka /* if "all" is present, it must be specified first */
360 1.49 pooka if (strncmp(buf, "all", strlen("all")) == 0) {
361 1.50 pooka for (i = 0; i < (int)__arraycount(rumpsockets); i++) {
362 1.49 pooka rumpsockets[i] = true;
363 1.49 pooka }
364 1.49 pooka buf += strlen("all");
365 1.49 pooka if (*buf == ':')
366 1.49 pooka buf++;
367 1.49 pooka }
368 1.49 pooka
369 1.49 pooka for (p = strtok_r(buf, ":", &l); p; p = strtok_r(NULL, ":", &l)) {
370 1.49 pooka value = true;
371 1.49 pooka if (strncmp(p, "no", strlen("no")) == 0) {
372 1.49 pooka value = false;
373 1.49 pooka p += strlen("no");
374 1.49 pooka }
375 1.45 pooka
376 1.49 pooka for (i = 0; socketmap[i].name; i++) {
377 1.49 pooka if (strcmp(p, socketmap[i].name) == 0) {
378 1.49 pooka rumpsockets[socketmap[i].pf] = value;
379 1.49 pooka break;
380 1.49 pooka }
381 1.49 pooka }
382 1.49 pooka if (socketmap[i].name == NULL) {
383 1.49 pooka warnx("invalid socket specifier %s", p);
384 1.49 pooka }
385 1.49 pooka }
386 1.49 pooka }
387 1.49 pooka
388 1.49 pooka static void
389 1.49 pooka pathparser(char *buf)
390 1.49 pooka {
391 1.49 pooka
392 1.57 pooka /* sanity-check */
393 1.49 pooka if (*buf != '/')
394 1.49 pooka errx(1, "hijack path specifier must begin with ``/''");
395 1.57 pooka rumpprefixlen = strlen(buf);
396 1.57 pooka if (rumpprefixlen < 2)
397 1.57 pooka errx(1, "invalid hijack prefix: %s", buf);
398 1.57 pooka if (buf[rumpprefixlen-1] == '/' && strspn(buf, "/") != rumpprefixlen)
399 1.57 pooka errx(1, "hijack prefix may end in slash only if pure "
400 1.57 pooka "slash, gave %s", buf);
401 1.49 pooka
402 1.49 pooka if ((rumpprefix = strdup(buf)) == NULL)
403 1.49 pooka err(1, "strdup");
404 1.49 pooka rumpprefixlen = strlen(rumpprefix);
405 1.49 pooka }
406 1.49 pooka
407 1.49 pooka static struct {
408 1.49 pooka void (*parsefn)(char *);
409 1.49 pooka const char *name;
410 1.49 pooka } hijackparse[] = {
411 1.49 pooka { sockparser, "socket" },
412 1.49 pooka { pathparser, "path" },
413 1.49 pooka { NULL, NULL },
414 1.49 pooka };
415 1.49 pooka
416 1.49 pooka static void
417 1.49 pooka parsehijack(char *hijack)
418 1.49 pooka {
419 1.49 pooka char *p, *p2, *l;
420 1.49 pooka const char *hijackcopy;
421 1.49 pooka int i;
422 1.49 pooka
423 1.49 pooka if ((hijackcopy = strdup(hijack)) == NULL)
424 1.49 pooka err(1, "strdup");
425 1.49 pooka
426 1.49 pooka /* disable everything explicitly */
427 1.49 pooka for (i = 0; i < PF_MAX; i++)
428 1.49 pooka rumpsockets[i] = false;
429 1.49 pooka
430 1.49 pooka for (p = strtok_r(hijack, ",", &l); p; p = strtok_r(NULL, ",", &l)) {
431 1.49 pooka p2 = strchr(p, '=');
432 1.49 pooka if (!p2)
433 1.49 pooka errx(1, "invalid hijack specifier: %s", hijackcopy);
434 1.49 pooka
435 1.49 pooka for (i = 0; hijackparse[i].parsefn; i++) {
436 1.49 pooka if (strncmp(hijackparse[i].name, p,
437 1.49 pooka (size_t)(p2-p)) == 0) {
438 1.49 pooka hijackparse[i].parsefn(p2+1);
439 1.49 pooka break;
440 1.49 pooka }
441 1.49 pooka }
442 1.49 pooka }
443 1.49 pooka
444 1.49 pooka }
445 1.7 pooka
446 1.1 pooka static void __attribute__((constructor))
447 1.1 pooka rcinit(void)
448 1.1 pooka {
449 1.49 pooka char buf[1024];
450 1.23 pooka extern void *(*rumpclient_dlsym)(void *, const char *);
451 1.19 pooka unsigned i, j;
452 1.1 pooka
453 1.23 pooka rumpclient_dlsym = hijackdlsym;
454 1.17 pooka host_fork = dlsym(RTLD_NEXT, "fork");
455 1.25 pooka host_daemon = dlsym(RTLD_NEXT, "daemon");
456 1.39 pooka host_execve = dlsym(RTLD_NEXT, "execve");
457 1.17 pooka
458 1.17 pooka /*
459 1.17 pooka * In theory cannot print anything during lookups because
460 1.17 pooka * we might not have the call vector set up. so, the errx()
461 1.17 pooka * is a bit of a strech, but it might work.
462 1.17 pooka */
463 1.1 pooka
464 1.17 pooka for (i = 0; i < DUALCALL__NUM; i++) {
465 1.17 pooka /* build runtime O(1) access */
466 1.17 pooka for (j = 0; j < __arraycount(syscnames); j++) {
467 1.17 pooka if (syscnames[j].scm_callnum == i)
468 1.17 pooka break;
469 1.17 pooka }
470 1.17 pooka
471 1.17 pooka if (j == __arraycount(syscnames))
472 1.17 pooka errx(1, "rumphijack error: syscall pos %d missing", i);
473 1.17 pooka
474 1.23 pooka syscalls[i].bs_host = dlsym(RTLD_NEXT,
475 1.23 pooka syscnames[j].scm_hostname);
476 1.17 pooka if (syscalls[i].bs_host == NULL)
477 1.17 pooka errx(1, "hostcall %s not found missing",
478 1.17 pooka syscnames[j].scm_hostname);
479 1.17 pooka
480 1.23 pooka syscalls[i].bs_rump = dlsym(RTLD_NEXT,
481 1.23 pooka syscnames[j].scm_rumpname);
482 1.17 pooka if (syscalls[i].bs_rump == NULL)
483 1.17 pooka errx(1, "rumpcall %s not found missing",
484 1.17 pooka syscnames[j].scm_rumpname);
485 1.1 pooka }
486 1.1 pooka
487 1.22 pooka if (rumpclient_init() == -1)
488 1.1 pooka err(1, "rumpclient init");
489 1.28 pooka
490 1.49 pooka /* check which syscalls we're supposed to hijack */
491 1.49 pooka if (getenv_r("RUMPHIJACK", buf, sizeof(buf)) == -1) {
492 1.49 pooka strcpy(buf, RUMPHIJACK_DEFAULT);
493 1.49 pooka }
494 1.49 pooka parsehijack(buf);
495 1.49 pooka
496 1.28 pooka /* set client persistence level */
497 1.44 pooka if (getenv_r("RUMPHIJACK_RETRYCONNECT", buf, sizeof(buf)) != -1) {
498 1.28 pooka if (strcmp(buf, "die") == 0)
499 1.28 pooka rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_DIE);
500 1.28 pooka else if (strcmp(buf, "inftime") == 0)
501 1.28 pooka rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_INFTIME);
502 1.28 pooka else if (strcmp(buf, "once") == 0)
503 1.28 pooka rumpclient_setconnretry(RUMPCLIENT_RETRYCONN_ONCE);
504 1.28 pooka else {
505 1.28 pooka time_t timeout;
506 1.44 pooka char *ep;
507 1.28 pooka
508 1.44 pooka timeout = (time_t)strtoll(buf, &ep, 10);
509 1.44 pooka if (timeout <= 0 || ep != buf + strlen(buf))
510 1.44 pooka errx(1, "RUMPHIJACK_RETRYCONNECT must be "
511 1.44 pooka "keyword or integer, got: %s", buf);
512 1.28 pooka
513 1.28 pooka rumpclient_setconnretry(timeout);
514 1.28 pooka }
515 1.28 pooka }
516 1.39 pooka
517 1.39 pooka if (getenv_r("RUMPHIJACK__DUP2MASK", buf, sizeof(buf)) == 0) {
518 1.40 pooka dup2mask = strtoul(buf, NULL, 10);
519 1.45 pooka unsetenv("RUMPHIJACK__DUP2MASK");
520 1.45 pooka }
521 1.45 pooka if (getenv_r("RUMPHIJACK__PWDINRUMP", buf, sizeof(buf)) == 0) {
522 1.49 pooka pwdinrump = true;
523 1.45 pooka unsetenv("RUMPHIJACK__PWDINRUMP");
524 1.39 pooka }
525 1.1 pooka }
526 1.1 pooka
527 1.2 pooka /* XXX: need runtime selection. low for now due to FD_SETSIZE */
528 1.2 pooka #define HIJACK_FDOFF 128
529 1.2 pooka static int
530 1.2 pooka fd_rump2host(int fd)
531 1.2 pooka {
532 1.2 pooka
533 1.2 pooka if (fd == -1)
534 1.2 pooka return fd;
535 1.2 pooka
536 1.2 pooka if (!ISDUP2D(fd))
537 1.2 pooka fd += HIJACK_FDOFF;
538 1.2 pooka
539 1.2 pooka return fd;
540 1.2 pooka }
541 1.2 pooka
542 1.2 pooka static int
543 1.2 pooka fd_host2rump(int fd)
544 1.2 pooka {
545 1.2 pooka
546 1.2 pooka if (!ISDUP2D(fd))
547 1.2 pooka fd -= HIJACK_FDOFF;
548 1.2 pooka return fd;
549 1.2 pooka }
550 1.2 pooka
551 1.2 pooka static bool
552 1.2 pooka fd_isrump(int fd)
553 1.2 pooka {
554 1.2 pooka
555 1.2 pooka return ISDUP2D(fd) || fd >= HIJACK_FDOFF;
556 1.2 pooka }
557 1.2 pooka
558 1.40 pooka #define assertfd(_fd_) assert(ISDUP2D(_fd_) || (_fd_) >= HIJACK_FDOFF)
559 1.40 pooka
560 1.49 pooka static bool
561 1.45 pooka path_isrump(const char *path)
562 1.45 pooka {
563 1.45 pooka
564 1.49 pooka if (rumpprefix == NULL)
565 1.49 pooka return false;
566 1.49 pooka
567 1.45 pooka if (*path == '/') {
568 1.49 pooka if (strncmp(path, rumpprefix, rumpprefixlen) == 0)
569 1.49 pooka return true;
570 1.49 pooka return false;
571 1.45 pooka } else {
572 1.45 pooka return pwdinrump;
573 1.45 pooka }
574 1.45 pooka }
575 1.45 pooka
576 1.45 pooka static const char *rootpath = "/";
577 1.45 pooka static const char *
578 1.45 pooka path_host2rump(const char *path)
579 1.45 pooka {
580 1.45 pooka const char *rv;
581 1.45 pooka
582 1.45 pooka if (*path == '/') {
583 1.49 pooka rv = path + rumpprefixlen;
584 1.45 pooka if (*rv == '\0')
585 1.45 pooka rv = rootpath;
586 1.45 pooka } else {
587 1.45 pooka rv = path;
588 1.45 pooka }
589 1.45 pooka
590 1.45 pooka return rv;
591 1.45 pooka }
592 1.45 pooka
593 1.40 pooka static int
594 1.40 pooka dodup(int oldd, int minfd)
595 1.40 pooka {
596 1.40 pooka int (*op_fcntl)(int, int, ...);
597 1.40 pooka int newd;
598 1.40 pooka int isrump;
599 1.40 pooka
600 1.40 pooka DPRINTF(("dup -> %d (minfd %d)\n", oldd, minfd));
601 1.40 pooka if (fd_isrump(oldd)) {
602 1.40 pooka op_fcntl = GETSYSCALL(rump, FCNTL);
603 1.40 pooka oldd = fd_host2rump(oldd);
604 1.40 pooka isrump = 1;
605 1.40 pooka } else {
606 1.40 pooka op_fcntl = GETSYSCALL(host, FCNTL);
607 1.40 pooka isrump = 0;
608 1.40 pooka }
609 1.40 pooka
610 1.40 pooka newd = op_fcntl(oldd, F_DUPFD, minfd);
611 1.40 pooka
612 1.40 pooka if (isrump)
613 1.40 pooka newd = fd_rump2host(newd);
614 1.40 pooka DPRINTF(("dup <- %d\n", newd));
615 1.40 pooka
616 1.40 pooka return newd;
617 1.40 pooka }
618 1.2 pooka
619 1.47 pooka /*
620 1.47 pooka * dup a host file descriptor so that it doesn't collide with the dup2mask
621 1.47 pooka */
622 1.47 pooka static int
623 1.47 pooka fd_dupgood(int fd)
624 1.47 pooka {
625 1.47 pooka int (*op_fcntl)(int, int, ...) = GETSYSCALL(host, FCNTL);
626 1.47 pooka int (*op_close)(int) = GETSYSCALL(host, CLOSE);
627 1.47 pooka int ofd, i;
628 1.47 pooka
629 1.47 pooka for (i = 1; ISDUP2D(fd); i++) {
630 1.47 pooka ofd = fd;
631 1.47 pooka fd = op_fcntl(ofd, F_DUPFD, i);
632 1.47 pooka op_close(ofd);
633 1.47 pooka }
634 1.47 pooka
635 1.47 pooka return fd;
636 1.47 pooka }
637 1.47 pooka
638 1.45 pooka int
639 1.45 pooka open(const char *path, int flags, ...)
640 1.45 pooka {
641 1.45 pooka int (*op_open)(const char *, int, ...);
642 1.45 pooka bool isrump;
643 1.45 pooka va_list ap;
644 1.45 pooka int fd;
645 1.45 pooka
646 1.45 pooka if (path_isrump(path)) {
647 1.45 pooka path = path_host2rump(path);
648 1.45 pooka op_open = GETSYSCALL(rump, OPEN);
649 1.45 pooka isrump = true;
650 1.45 pooka } else {
651 1.45 pooka op_open = GETSYSCALL(host, OPEN);
652 1.45 pooka isrump = false;
653 1.45 pooka }
654 1.45 pooka
655 1.45 pooka va_start(ap, flags);
656 1.45 pooka fd = op_open(path, flags, va_arg(ap, mode_t));
657 1.45 pooka va_end(ap);
658 1.45 pooka
659 1.45 pooka if (isrump)
660 1.45 pooka fd = fd_rump2host(fd);
661 1.47 pooka else
662 1.47 pooka fd = fd_dupgood(fd);
663 1.45 pooka return fd;
664 1.45 pooka }
665 1.45 pooka
666 1.45 pooka int
667 1.45 pooka chdir(const char *path)
668 1.45 pooka {
669 1.45 pooka int (*op_chdir)(const char *);
670 1.45 pooka bool isrump;
671 1.45 pooka int rv;
672 1.45 pooka
673 1.45 pooka if (path_isrump(path)) {
674 1.45 pooka op_chdir = GETSYSCALL(rump, CHDIR);
675 1.45 pooka isrump = true;
676 1.45 pooka path = path_host2rump(path);
677 1.45 pooka } else {
678 1.45 pooka op_chdir = GETSYSCALL(host, CHDIR);
679 1.45 pooka isrump = false;
680 1.45 pooka }
681 1.45 pooka
682 1.45 pooka rv = op_chdir(path);
683 1.45 pooka if (rv == 0) {
684 1.45 pooka if (isrump)
685 1.45 pooka pwdinrump = true;
686 1.45 pooka else
687 1.45 pooka pwdinrump = false;
688 1.45 pooka }
689 1.45 pooka
690 1.45 pooka return rv;
691 1.45 pooka }
692 1.45 pooka
693 1.45 pooka int
694 1.45 pooka fchdir(int fd)
695 1.45 pooka {
696 1.45 pooka int (*op_fchdir)(int);
697 1.45 pooka bool isrump;
698 1.45 pooka int rv;
699 1.45 pooka
700 1.45 pooka if (fd_isrump(fd)) {
701 1.45 pooka op_fchdir = GETSYSCALL(rump, FCHDIR);
702 1.45 pooka isrump = true;
703 1.45 pooka fd = fd_host2rump(fd);
704 1.45 pooka } else {
705 1.45 pooka op_fchdir = GETSYSCALL(host, FCHDIR);
706 1.45 pooka isrump = false;
707 1.45 pooka }
708 1.45 pooka
709 1.45 pooka rv = op_fchdir(fd);
710 1.45 pooka if (rv == 0) {
711 1.45 pooka if (isrump)
712 1.45 pooka pwdinrump = true;
713 1.45 pooka else
714 1.45 pooka pwdinrump = false;
715 1.45 pooka }
716 1.45 pooka
717 1.45 pooka return rv;
718 1.45 pooka }
719 1.45 pooka
720 1.52 pooka int
721 1.57 pooka __getcwd(char *bufp, size_t len)
722 1.57 pooka {
723 1.57 pooka int (*op___getcwd)(char *, size_t);
724 1.57 pooka int rv;
725 1.57 pooka
726 1.57 pooka if (pwdinrump) {
727 1.57 pooka size_t prefixgap;
728 1.57 pooka bool iamslash;
729 1.57 pooka
730 1.57 pooka if (rumpprefix[rumpprefixlen-1] == '/')
731 1.57 pooka iamslash = true;
732 1.57 pooka else
733 1.57 pooka iamslash = false;
734 1.57 pooka
735 1.57 pooka if (iamslash)
736 1.57 pooka prefixgap = rumpprefixlen - 1; /* ``//+path'' */
737 1.57 pooka else
738 1.57 pooka prefixgap = rumpprefixlen; /* ``/pfx+/path'' */
739 1.57 pooka if (len <= prefixgap) {
740 1.57 pooka return ERANGE;
741 1.57 pooka }
742 1.57 pooka
743 1.57 pooka op___getcwd = GETSYSCALL(rump, __GETCWD);
744 1.57 pooka rv = op___getcwd(bufp + prefixgap, len - prefixgap);
745 1.57 pooka if (rv == -1)
746 1.57 pooka return rv;
747 1.57 pooka
748 1.57 pooka /* augment the "/" part only for a non-root path */
749 1.57 pooka memcpy(bufp, rumpprefix, rumpprefixlen);
750 1.57 pooka
751 1.57 pooka /* append / only to non-root cwd */
752 1.57 pooka if (rv != 2)
753 1.57 pooka bufp[prefixgap] = '/';
754 1.57 pooka
755 1.57 pooka /* don't append extra slash in the purely-slash case */
756 1.57 pooka if (rv == 2 && !iamslash)
757 1.57 pooka bufp[rumpprefixlen] = '\0';
758 1.57 pooka
759 1.57 pooka return rv;
760 1.57 pooka } else {
761 1.57 pooka op___getcwd = GETSYSCALL(host, __GETCWD);
762 1.57 pooka return op___getcwd(bufp, len);
763 1.57 pooka }
764 1.57 pooka }
765 1.57 pooka
766 1.57 pooka int
767 1.52 pooka rename(const char *from, const char *to)
768 1.52 pooka {
769 1.52 pooka int (*op_rename)(const char *, const char *);
770 1.52 pooka
771 1.52 pooka if (path_isrump(from)) {
772 1.52 pooka if (!path_isrump(to))
773 1.52 pooka return EXDEV;
774 1.52 pooka
775 1.52 pooka from = path_host2rump(from);
776 1.52 pooka to = path_host2rump(to);
777 1.52 pooka op_rename = GETSYSCALL(rump, RENAME);
778 1.52 pooka } else {
779 1.53 pooka if (path_isrump(to))
780 1.53 pooka return EXDEV;
781 1.53 pooka
782 1.52 pooka op_rename = GETSYSCALL(host, RENAME);
783 1.52 pooka }
784 1.52 pooka
785 1.52 pooka return op_rename(from, to);
786 1.52 pooka }
787 1.52 pooka
788 1.1 pooka int __socket30(int, int, int);
789 1.1 pooka int
790 1.1 pooka __socket30(int domain, int type, int protocol)
791 1.1 pooka {
792 1.17 pooka int (*op_socket)(int, int, int);
793 1.1 pooka int fd;
794 1.49 pooka bool isrump;
795 1.7 pooka
796 1.49 pooka isrump = domain < PF_MAX && rumpsockets[domain];
797 1.1 pooka
798 1.49 pooka if (isrump)
799 1.49 pooka op_socket = GETSYSCALL(rump, SOCKET);
800 1.49 pooka else
801 1.17 pooka op_socket = GETSYSCALL(host, SOCKET);
802 1.17 pooka fd = op_socket(domain, type, protocol);
803 1.2 pooka
804 1.49 pooka if (isrump)
805 1.7 pooka fd = fd_rump2host(fd);
806 1.47 pooka else
807 1.47 pooka fd = fd_dupgood(fd);
808 1.7 pooka DPRINTF(("socket <- %d\n", fd));
809 1.2 pooka
810 1.7 pooka return fd;
811 1.1 pooka }
812 1.1 pooka
813 1.1 pooka int
814 1.1 pooka accept(int s, struct sockaddr *addr, socklen_t *addrlen)
815 1.1 pooka {
816 1.17 pooka int (*op_accept)(int, struct sockaddr *, socklen_t *);
817 1.1 pooka int fd;
818 1.7 pooka bool isrump;
819 1.7 pooka
820 1.7 pooka isrump = fd_isrump(s);
821 1.1 pooka
822 1.2 pooka DPRINTF(("accept -> %d", s));
823 1.7 pooka if (isrump) {
824 1.17 pooka op_accept = GETSYSCALL(rump, ACCEPT);
825 1.7 pooka s = fd_host2rump(s);
826 1.7 pooka } else {
827 1.17 pooka op_accept = GETSYSCALL(host, ACCEPT);
828 1.7 pooka }
829 1.17 pooka fd = op_accept(s, addr, addrlen);
830 1.7 pooka if (fd != -1 && isrump)
831 1.7 pooka fd = fd_rump2host(fd);
832 1.47 pooka else
833 1.47 pooka fd = fd_dupgood(fd);
834 1.7 pooka
835 1.7 pooka DPRINTF((" <- %d\n", fd));
836 1.2 pooka
837 1.7 pooka return fd;
838 1.1 pooka }
839 1.1 pooka
840 1.17 pooka /*
841 1.17 pooka * ioctl and fcntl are varargs calls and need special treatment
842 1.17 pooka */
843 1.1 pooka int
844 1.17 pooka ioctl(int fd, unsigned long cmd, ...)
845 1.1 pooka {
846 1.17 pooka int (*op_ioctl)(int, unsigned long cmd, ...);
847 1.17 pooka va_list ap;
848 1.17 pooka int rv;
849 1.1 pooka
850 1.17 pooka DPRINTF(("ioctl -> %d\n", fd));
851 1.17 pooka if (fd_isrump(fd)) {
852 1.17 pooka fd = fd_host2rump(fd);
853 1.17 pooka op_ioctl = GETSYSCALL(rump, IOCTL);
854 1.7 pooka } else {
855 1.17 pooka op_ioctl = GETSYSCALL(host, IOCTL);
856 1.7 pooka }
857 1.1 pooka
858 1.17 pooka va_start(ap, cmd);
859 1.17 pooka rv = op_ioctl(fd, cmd, va_arg(ap, void *));
860 1.17 pooka va_end(ap);
861 1.17 pooka return rv;
862 1.1 pooka }
863 1.1 pooka
864 1.40 pooka #include <syslog.h>
865 1.1 pooka int
866 1.17 pooka fcntl(int fd, int cmd, ...)
867 1.1 pooka {
868 1.17 pooka int (*op_fcntl)(int, int, ...);
869 1.17 pooka va_list ap;
870 1.40 pooka int rv, minfd, i;
871 1.40 pooka
872 1.40 pooka DPRINTF(("fcntl -> %d (cmd %d)\n", fd, cmd));
873 1.40 pooka
874 1.40 pooka switch (cmd) {
875 1.40 pooka case F_DUPFD:
876 1.40 pooka va_start(ap, cmd);
877 1.40 pooka minfd = va_arg(ap, int);
878 1.40 pooka va_end(ap);
879 1.40 pooka return dodup(fd, minfd);
880 1.40 pooka
881 1.40 pooka case F_CLOSEM:
882 1.40 pooka /*
883 1.40 pooka * So, if fd < HIJACKOFF, we want to do a host closem.
884 1.40 pooka */
885 1.40 pooka
886 1.40 pooka if (fd < HIJACK_FDOFF) {
887 1.40 pooka int closemfd = fd;
888 1.1 pooka
889 1.40 pooka if (rumpclient__closenotify(&closemfd,
890 1.39 pooka RUMPCLIENT_CLOSE_FCLOSEM) == -1)
891 1.39 pooka return -1;
892 1.40 pooka op_fcntl = GETSYSCALL(host, FCNTL);
893 1.40 pooka rv = op_fcntl(closemfd, cmd);
894 1.40 pooka if (rv)
895 1.40 pooka return rv;
896 1.40 pooka }
897 1.40 pooka
898 1.40 pooka /*
899 1.40 pooka * Additionally, we want to do a rump closem, but only
900 1.40 pooka * for the file descriptors not within the dup2mask.
901 1.40 pooka */
902 1.40 pooka
903 1.40 pooka /* why don't we offer fls()? */
904 1.47 pooka for (i = 15; i >= 0; i--) {
905 1.47 pooka if (ISDUP2D(i))
906 1.40 pooka break;
907 1.40 pooka }
908 1.40 pooka
909 1.40 pooka if (fd >= HIJACK_FDOFF)
910 1.40 pooka fd -= HIJACK_FDOFF;
911 1.40 pooka else
912 1.40 pooka fd = 0;
913 1.40 pooka fd = MAX(i+1, fd);
914 1.40 pooka
915 1.40 pooka /* hmm, maybe we should close rump fd's not within dup2mask? */
916 1.40 pooka
917 1.40 pooka return rump_sys_fcntl(fd, F_CLOSEM);
918 1.40 pooka
919 1.40 pooka case F_MAXFD:
920 1.40 pooka /*
921 1.40 pooka * For maxfd, if there's a rump kernel fd, return
922 1.40 pooka * it hostified. Otherwise, return host's MAXFD
923 1.40 pooka * return value.
924 1.40 pooka */
925 1.40 pooka if ((rv = rump_sys_fcntl(fd, F_MAXFD)) != -1) {
926 1.40 pooka /*
927 1.40 pooka * This might go a little wrong in case
928 1.40 pooka * of dup2 to [012], but I'm not sure if
929 1.40 pooka * there's a justification for tracking
930 1.40 pooka * that info. Consider e.g.
931 1.40 pooka * dup2(rumpfd, 2) followed by rump_sys_open()
932 1.40 pooka * returning 1. We should return 1+HIJACKOFF,
933 1.40 pooka * not 2+HIJACKOFF. However, if [01] is not
934 1.40 pooka * open, the correct return value is 2.
935 1.40 pooka */
936 1.40 pooka return fd_rump2host(fd);
937 1.40 pooka } else {
938 1.40 pooka op_fcntl = GETSYSCALL(host, FCNTL);
939 1.40 pooka return op_fcntl(fd, F_MAXFD);
940 1.40 pooka }
941 1.40 pooka /*NOTREACHED*/
942 1.40 pooka
943 1.40 pooka default:
944 1.40 pooka if (fd_isrump(fd)) {
945 1.40 pooka fd = fd_host2rump(fd);
946 1.40 pooka op_fcntl = GETSYSCALL(rump, FCNTL);
947 1.40 pooka } else {
948 1.40 pooka op_fcntl = GETSYSCALL(host, FCNTL);
949 1.40 pooka }
950 1.40 pooka
951 1.40 pooka va_start(ap, cmd);
952 1.40 pooka rv = op_fcntl(fd, cmd, va_arg(ap, void *));
953 1.40 pooka va_end(ap);
954 1.40 pooka return rv;
955 1.7 pooka }
956 1.40 pooka /*NOTREACHED*/
957 1.1 pooka }
958 1.1 pooka
959 1.39 pooka int
960 1.39 pooka close(int fd)
961 1.39 pooka {
962 1.39 pooka int (*op_close)(int);
963 1.39 pooka int rv;
964 1.39 pooka
965 1.39 pooka DPRINTF(("close -> %d\n", fd));
966 1.39 pooka if (fd_isrump(fd)) {
967 1.39 pooka int undup2 = 0;
968 1.39 pooka
969 1.47 pooka fd = fd_host2rump(fd);
970 1.47 pooka if (ISDUP2ALIAS(fd)) {
971 1.47 pooka _DIAGASSERT(ISDUP2D(fd));
972 1.47 pooka CLRDUP2ALIAS(fd);
973 1.47 pooka return 0;
974 1.47 pooka }
975 1.47 pooka
976 1.39 pooka if (ISDUP2D(fd))
977 1.39 pooka undup2 = 1;
978 1.39 pooka op_close = GETSYSCALL(rump, CLOSE);
979 1.39 pooka rv = op_close(fd);
980 1.39 pooka if (rv == 0 && undup2)
981 1.40 pooka CLRDUP2(fd);
982 1.39 pooka } else {
983 1.39 pooka if (rumpclient__closenotify(&fd, RUMPCLIENT_CLOSE_CLOSE) == -1)
984 1.39 pooka return -1;
985 1.39 pooka op_close = GETSYSCALL(host, CLOSE);
986 1.39 pooka rv = op_close(fd);
987 1.39 pooka }
988 1.39 pooka
989 1.39 pooka return rv;
990 1.39 pooka }
991 1.39 pooka
992 1.17 pooka /*
993 1.17 pooka * write cannot issue a standard debug printf due to recursion
994 1.17 pooka */
995 1.1 pooka ssize_t
996 1.17 pooka write(int fd, const void *buf, size_t blen)
997 1.1 pooka {
998 1.17 pooka ssize_t (*op_write)(int, const void *, size_t);
999 1.1 pooka
1000 1.17 pooka if (fd_isrump(fd)) {
1001 1.17 pooka fd = fd_host2rump(fd);
1002 1.17 pooka op_write = GETSYSCALL(rump, WRITE);
1003 1.16 pooka } else {
1004 1.17 pooka op_write = GETSYSCALL(host, WRITE);
1005 1.16 pooka }
1006 1.1 pooka
1007 1.17 pooka return op_write(fd, buf, blen);
1008 1.2 pooka }
1009 1.2 pooka
1010 1.2 pooka /*
1011 1.2 pooka * dup2 is special. we allow dup2 of a rump kernel fd to 0-2 since
1012 1.2 pooka * many programs do that. dup2 of a rump kernel fd to another value
1013 1.2 pooka * not >= fdoff is an error.
1014 1.2 pooka *
1015 1.2 pooka * Note: cannot rump2host newd, because it is often hardcoded.
1016 1.2 pooka */
1017 1.2 pooka int
1018 1.2 pooka dup2(int oldd, int newd)
1019 1.2 pooka {
1020 1.17 pooka int (*host_dup2)(int, int);
1021 1.2 pooka int rv;
1022 1.2 pooka
1023 1.2 pooka DPRINTF(("dup2 -> %d (o) -> %d (n)\n", oldd, newd));
1024 1.2 pooka
1025 1.2 pooka if (fd_isrump(oldd)) {
1026 1.2 pooka if (!(newd >= 0 && newd <= 2))
1027 1.2 pooka return EBADF;
1028 1.2 pooka oldd = fd_host2rump(oldd);
1029 1.47 pooka if (oldd == newd) {
1030 1.47 pooka SETDUP2(newd);
1031 1.47 pooka SETDUP2ALIAS(newd);
1032 1.47 pooka return newd;
1033 1.47 pooka }
1034 1.2 pooka rv = rump_sys_dup2(oldd, newd);
1035 1.2 pooka if (rv != -1)
1036 1.40 pooka SETDUP2(newd);
1037 1.2 pooka } else {
1038 1.17 pooka host_dup2 = syscalls[DUALCALL_DUP2].bs_host;
1039 1.39 pooka if (rumpclient__closenotify(&newd, RUMPCLIENT_CLOSE_DUP2) == -1)
1040 1.39 pooka return -1;
1041 1.10 pooka rv = host_dup2(oldd, newd);
1042 1.2 pooka }
1043 1.10 pooka
1044 1.10 pooka return rv;
1045 1.2 pooka }
1046 1.2 pooka
1047 1.34 pooka int
1048 1.34 pooka dup(int oldd)
1049 1.34 pooka {
1050 1.34 pooka
1051 1.40 pooka return dodup(oldd, 0);
1052 1.34 pooka }
1053 1.34 pooka
1054 1.2 pooka pid_t
1055 1.2 pooka fork()
1056 1.2 pooka {
1057 1.2 pooka pid_t rv;
1058 1.2 pooka
1059 1.2 pooka DPRINTF(("fork\n"));
1060 1.2 pooka
1061 1.43 pooka rv = rumpclient__dofork(host_fork);
1062 1.2 pooka
1063 1.2 pooka DPRINTF(("fork returns %d\n", rv));
1064 1.2 pooka return rv;
1065 1.1 pooka }
1066 1.43 pooka /* we do not have the luxury of not requiring a stackframe */
1067 1.43 pooka __strong_alias(__vfork14,fork);
1068 1.1 pooka
1069 1.25 pooka int
1070 1.25 pooka daemon(int nochdir, int noclose)
1071 1.25 pooka {
1072 1.25 pooka struct rumpclient_fork *rf;
1073 1.25 pooka
1074 1.25 pooka if ((rf = rumpclient_prefork()) == NULL)
1075 1.25 pooka return -1;
1076 1.25 pooka
1077 1.25 pooka if (host_daemon(nochdir, noclose) == -1)
1078 1.25 pooka return -1;
1079 1.25 pooka
1080 1.25 pooka if (rumpclient_fork_init(rf) == -1)
1081 1.25 pooka return -1;
1082 1.25 pooka
1083 1.25 pooka return 0;
1084 1.25 pooka }
1085 1.25 pooka
1086 1.39 pooka int
1087 1.42 pooka execve(const char *path, char *const argv[], char *const envp[])
1088 1.39 pooka {
1089 1.39 pooka char buf[128];
1090 1.39 pooka char *dup2str;
1091 1.49 pooka const char *pwdinrumpstr;
1092 1.42 pooka char **newenv;
1093 1.42 pooka size_t nelem;
1094 1.42 pooka int rv, sverrno;
1095 1.45 pooka int bonus = 1, i = 0;
1096 1.45 pooka
1097 1.45 pooka if (dup2mask) {
1098 1.45 pooka snprintf(buf, sizeof(buf), "RUMPHIJACK__DUP2MASK=%u", dup2mask);
1099 1.45 pooka dup2str = malloc(strlen(buf)+1);
1100 1.45 pooka if (dup2str == NULL)
1101 1.45 pooka return ENOMEM;
1102 1.45 pooka strcpy(dup2str, buf);
1103 1.45 pooka bonus++;
1104 1.45 pooka } else {
1105 1.45 pooka dup2str = NULL;
1106 1.45 pooka }
1107 1.39 pooka
1108 1.45 pooka if (pwdinrump) {
1109 1.49 pooka pwdinrumpstr = "RUMPHIJACK__PWDINRUMP=true";
1110 1.45 pooka bonus++;
1111 1.45 pooka } else {
1112 1.45 pooka pwdinrumpstr = NULL;
1113 1.45 pooka }
1114 1.39 pooka
1115 1.42 pooka for (nelem = 0; envp && envp[nelem]; nelem++)
1116 1.42 pooka continue;
1117 1.45 pooka newenv = malloc(sizeof(*newenv) * nelem+bonus);
1118 1.42 pooka if (newenv == NULL) {
1119 1.39 pooka free(dup2str);
1120 1.42 pooka return ENOMEM;
1121 1.39 pooka }
1122 1.42 pooka memcpy(newenv, envp, nelem*sizeof(*newenv));
1123 1.45 pooka if (dup2str) {
1124 1.45 pooka newenv[nelem+i] = dup2str;
1125 1.45 pooka i++;
1126 1.45 pooka }
1127 1.45 pooka if (pwdinrumpstr) {
1128 1.49 pooka newenv[nelem+i] = __UNCONST(pwdinrumpstr);
1129 1.45 pooka i++;
1130 1.45 pooka }
1131 1.45 pooka newenv[nelem+i] = NULL;
1132 1.45 pooka _DIAGASSERT(i < bonus);
1133 1.42 pooka
1134 1.42 pooka rv = rumpclient_exec(path, argv, newenv);
1135 1.42 pooka
1136 1.42 pooka _DIAGASSERT(rv != 0);
1137 1.42 pooka sverrno = errno;
1138 1.42 pooka free(newenv);
1139 1.42 pooka free(dup2str);
1140 1.42 pooka errno = sverrno;
1141 1.39 pooka return rv;
1142 1.39 pooka }
1143 1.39 pooka
1144 1.1 pooka /*
1145 1.17 pooka * select is done by calling poll.
1146 1.1 pooka */
1147 1.1 pooka int
1148 1.29 pooka REALSELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
1149 1.4 pooka struct timeval *timeout)
1150 1.1 pooka {
1151 1.4 pooka struct pollfd *pfds;
1152 1.4 pooka struct timespec ts, *tsp = NULL;
1153 1.19 pooka nfds_t realnfds;
1154 1.19 pooka int i, j;
1155 1.4 pooka int rv, incr;
1156 1.4 pooka
1157 1.7 pooka DPRINTF(("select\n"));
1158 1.7 pooka
1159 1.4 pooka /*
1160 1.4 pooka * Well, first we must scan the fds to figure out how many
1161 1.4 pooka * fds there really are. This is because up to and including
1162 1.17 pooka * nb5 poll() silently refuses nfds > process_maxopen_fds.
1163 1.4 pooka * Seems to be fixed in current, thank the maker.
1164 1.4 pooka * god damn cluster...bomb.
1165 1.4 pooka */
1166 1.4 pooka
1167 1.4 pooka for (i = 0, realnfds = 0; i < nfds; i++) {
1168 1.4 pooka if (readfds && FD_ISSET(i, readfds)) {
1169 1.4 pooka realnfds++;
1170 1.4 pooka continue;
1171 1.4 pooka }
1172 1.4 pooka if (writefds && FD_ISSET(i, writefds)) {
1173 1.4 pooka realnfds++;
1174 1.4 pooka continue;
1175 1.4 pooka }
1176 1.4 pooka if (exceptfds && FD_ISSET(i, exceptfds)) {
1177 1.4 pooka realnfds++;
1178 1.4 pooka continue;
1179 1.1 pooka }
1180 1.1 pooka }
1181 1.1 pooka
1182 1.6 pooka if (realnfds) {
1183 1.38 pooka pfds = calloc(realnfds, sizeof(*pfds));
1184 1.6 pooka if (!pfds)
1185 1.6 pooka return -1;
1186 1.6 pooka } else {
1187 1.6 pooka pfds = NULL;
1188 1.6 pooka }
1189 1.1 pooka
1190 1.4 pooka for (i = 0, j = 0; i < nfds; i++) {
1191 1.4 pooka incr = 0;
1192 1.4 pooka if (readfds && FD_ISSET(i, readfds)) {
1193 1.4 pooka pfds[j].fd = i;
1194 1.4 pooka pfds[j].events |= POLLIN;
1195 1.4 pooka incr=1;
1196 1.4 pooka }
1197 1.4 pooka if (writefds && FD_ISSET(i, writefds)) {
1198 1.4 pooka pfds[j].fd = i;
1199 1.4 pooka pfds[j].events |= POLLOUT;
1200 1.4 pooka incr=1;
1201 1.4 pooka }
1202 1.4 pooka if (exceptfds && FD_ISSET(i, exceptfds)) {
1203 1.4 pooka pfds[j].fd = i;
1204 1.4 pooka pfds[j].events |= POLLHUP|POLLERR;
1205 1.4 pooka incr=1;
1206 1.1 pooka }
1207 1.4 pooka if (incr)
1208 1.4 pooka j++;
1209 1.1 pooka }
1210 1.37 pooka assert(j == (int)realnfds);
1211 1.1 pooka
1212 1.4 pooka if (timeout) {
1213 1.4 pooka TIMEVAL_TO_TIMESPEC(timeout, &ts);
1214 1.4 pooka tsp = &ts;
1215 1.4 pooka }
1216 1.29 pooka rv = REALPOLLTS(pfds, realnfds, tsp, NULL);
1217 1.36 pooka /*
1218 1.36 pooka * "If select() returns with an error the descriptor sets
1219 1.36 pooka * will be unmodified"
1220 1.36 pooka */
1221 1.36 pooka if (rv < 0)
1222 1.4 pooka goto out;
1223 1.4 pooka
1224 1.4 pooka /*
1225 1.36 pooka * zero out results (can't use FD_ZERO for the
1226 1.36 pooka * obvious select-me-not reason). whee.
1227 1.36 pooka *
1228 1.36 pooka * We do this here since some software ignores the return
1229 1.36 pooka * value of select, and hence if the timeout expires, it may
1230 1.36 pooka * assume all input descriptors have activity.
1231 1.4 pooka */
1232 1.4 pooka for (i = 0; i < nfds; i++) {
1233 1.4 pooka if (readfds)
1234 1.4 pooka FD_CLR(i, readfds);
1235 1.4 pooka if (writefds)
1236 1.4 pooka FD_CLR(i, writefds);
1237 1.4 pooka if (exceptfds)
1238 1.4 pooka FD_CLR(i, exceptfds);
1239 1.1 pooka }
1240 1.36 pooka if (rv == 0)
1241 1.36 pooka goto out;
1242 1.1 pooka
1243 1.36 pooka /*
1244 1.36 pooka * We have >0 fds with activity. Harvest the results.
1245 1.36 pooka */
1246 1.19 pooka for (i = 0; i < (int)realnfds; i++) {
1247 1.4 pooka if (readfds) {
1248 1.4 pooka if (pfds[i].revents & POLLIN) {
1249 1.4 pooka FD_SET(pfds[i].fd, readfds);
1250 1.4 pooka }
1251 1.4 pooka }
1252 1.4 pooka if (writefds) {
1253 1.4 pooka if (pfds[i].revents & POLLOUT) {
1254 1.4 pooka FD_SET(pfds[i].fd, writefds);
1255 1.4 pooka }
1256 1.4 pooka }
1257 1.4 pooka if (exceptfds) {
1258 1.4 pooka if (pfds[i].revents & (POLLHUP|POLLERR)) {
1259 1.4 pooka FD_SET(pfds[i].fd, exceptfds);
1260 1.4 pooka }
1261 1.4 pooka }
1262 1.1 pooka }
1263 1.1 pooka
1264 1.4 pooka out:
1265 1.4 pooka free(pfds);
1266 1.1 pooka return rv;
1267 1.1 pooka }
1268 1.1 pooka
1269 1.1 pooka static void
1270 1.1 pooka checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
1271 1.1 pooka {
1272 1.1 pooka nfds_t i;
1273 1.1 pooka
1274 1.1 pooka for (i = 0; i < nfds; i++) {
1275 1.12 pooka if (fds[i].fd == -1)
1276 1.12 pooka continue;
1277 1.12 pooka
1278 1.2 pooka if (fd_isrump(fds[i].fd))
1279 1.2 pooka (*rumpcall)++;
1280 1.2 pooka else
1281 1.1 pooka (*hostcall)++;
1282 1.1 pooka }
1283 1.1 pooka }
1284 1.1 pooka
1285 1.1 pooka static void
1286 1.2 pooka adjustpoll(struct pollfd *fds, nfds_t nfds, int (*fdadj)(int))
1287 1.1 pooka {
1288 1.1 pooka nfds_t i;
1289 1.1 pooka
1290 1.1 pooka for (i = 0; i < nfds; i++) {
1291 1.2 pooka fds[i].fd = fdadj(fds[i].fd);
1292 1.1 pooka }
1293 1.1 pooka }
1294 1.1 pooka
1295 1.1 pooka /*
1296 1.1 pooka * poll is easy as long as the call comes in the fds only in one
1297 1.1 pooka * kernel. otherwise its quite tricky...
1298 1.1 pooka */
1299 1.1 pooka struct pollarg {
1300 1.1 pooka struct pollfd *pfds;
1301 1.1 pooka nfds_t nfds;
1302 1.3 pooka const struct timespec *ts;
1303 1.3 pooka const sigset_t *sigmask;
1304 1.1 pooka int pipefd;
1305 1.1 pooka int errnum;
1306 1.1 pooka };
1307 1.1 pooka
1308 1.1 pooka static void *
1309 1.1 pooka hostpoll(void *arg)
1310 1.1 pooka {
1311 1.17 pooka int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
1312 1.17 pooka const sigset_t *);
1313 1.1 pooka struct pollarg *parg = arg;
1314 1.1 pooka intptr_t rv;
1315 1.1 pooka
1316 1.35 pooka op_pollts = GETSYSCALL(host, POLLTS);
1317 1.17 pooka rv = op_pollts(parg->pfds, parg->nfds, parg->ts, parg->sigmask);
1318 1.1 pooka if (rv == -1)
1319 1.1 pooka parg->errnum = errno;
1320 1.1 pooka rump_sys_write(parg->pipefd, &rv, sizeof(rv));
1321 1.1 pooka
1322 1.1 pooka return (void *)(intptr_t)rv;
1323 1.1 pooka }
1324 1.1 pooka
1325 1.1 pooka int
1326 1.29 pooka REALPOLLTS(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
1327 1.3 pooka const sigset_t *sigmask)
1328 1.1 pooka {
1329 1.3 pooka int (*op_pollts)(struct pollfd *, nfds_t, const struct timespec *,
1330 1.3 pooka const sigset_t *);
1331 1.17 pooka int (*host_close)(int);
1332 1.1 pooka int hostcall = 0, rumpcall = 0;
1333 1.1 pooka pthread_t pt;
1334 1.1 pooka nfds_t i;
1335 1.1 pooka int rv;
1336 1.1 pooka
1337 1.2 pooka DPRINTF(("poll\n"));
1338 1.1 pooka checkpoll(fds, nfds, &hostcall, &rumpcall);
1339 1.1 pooka
1340 1.1 pooka if (hostcall && rumpcall) {
1341 1.1 pooka struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
1342 1.1 pooka int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
1343 1.1 pooka struct pollarg parg;
1344 1.1 pooka uintptr_t lrv;
1345 1.1 pooka int sverrno = 0, trv;
1346 1.1 pooka
1347 1.1 pooka /*
1348 1.1 pooka * ok, this is where it gets tricky. We must support
1349 1.1 pooka * this since it's a very common operation in certain
1350 1.1 pooka * types of software (telnet, netcat, etc). We allocate
1351 1.1 pooka * two vectors and run two poll commands in separate
1352 1.1 pooka * threads. Whichever returns first "wins" and the
1353 1.1 pooka * other kernel's fds won't show activity.
1354 1.1 pooka */
1355 1.1 pooka rv = -1;
1356 1.1 pooka
1357 1.1 pooka /* allocate full vector for O(n) joining after call */
1358 1.1 pooka pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
1359 1.1 pooka if (!pfd_host)
1360 1.1 pooka goto out;
1361 1.1 pooka pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
1362 1.1 pooka if (!pfd_rump) {
1363 1.1 pooka goto out;
1364 1.1 pooka }
1365 1.1 pooka
1366 1.59 pooka /*
1367 1.59 pooka * then, open two pipes, one for notifications
1368 1.59 pooka * to each kernel.
1369 1.59 pooka */
1370 1.59 pooka if ((rv = rump_sys_pipe(rpipe)) == -1) {
1371 1.59 pooka sverrno = errno;
1372 1.59 pooka }
1373 1.59 pooka if (rv == 0 && (rv = pipe(hpipe)) == -1) {
1374 1.59 pooka sverrno = errno;
1375 1.59 pooka }
1376 1.59 pooka
1377 1.59 pooka /* split vectors (or signal errors) */
1378 1.1 pooka for (i = 0; i < nfds; i++) {
1379 1.59 pooka int fd;
1380 1.59 pooka
1381 1.59 pooka fds[i].revents = 0;
1382 1.3 pooka if (fds[i].fd == -1) {
1383 1.3 pooka pfd_host[i].fd = -1;
1384 1.3 pooka pfd_rump[i].fd = -1;
1385 1.3 pooka } else if (fd_isrump(fds[i].fd)) {
1386 1.2 pooka pfd_host[i].fd = -1;
1387 1.59 pooka fd = fd_host2rump(fds[i].fd);
1388 1.59 pooka if (fd == rpipe[0] || fd == rpipe[1]) {
1389 1.59 pooka fds[i].revents = POLLNVAL;
1390 1.59 pooka if (rv != -1)
1391 1.59 pooka rv++;
1392 1.59 pooka }
1393 1.59 pooka pfd_rump[i].fd = fd;
1394 1.2 pooka pfd_rump[i].events = fds[i].events;
1395 1.2 pooka } else {
1396 1.2 pooka pfd_rump[i].fd = -1;
1397 1.59 pooka fd = fds[i].fd;
1398 1.59 pooka if (fd == hpipe[0] || fd == hpipe[1]) {
1399 1.59 pooka fds[i].revents = POLLNVAL;
1400 1.59 pooka if (rv != -1)
1401 1.59 pooka rv++;
1402 1.59 pooka }
1403 1.59 pooka pfd_host[i].fd = fd;
1404 1.1 pooka pfd_host[i].events = fds[i].events;
1405 1.1 pooka }
1406 1.39 pooka pfd_rump[i].revents = pfd_host[i].revents = 0;
1407 1.1 pooka }
1408 1.59 pooka if (rv) {
1409 1.1 pooka goto out;
1410 1.59 pooka }
1411 1.1 pooka
1412 1.1 pooka pfd_host[nfds].fd = hpipe[0];
1413 1.1 pooka pfd_host[nfds].events = POLLIN;
1414 1.1 pooka pfd_rump[nfds].fd = rpipe[0];
1415 1.1 pooka pfd_rump[nfds].events = POLLIN;
1416 1.1 pooka
1417 1.1 pooka /*
1418 1.1 pooka * then, create a thread to do host part and meanwhile
1419 1.1 pooka * do rump kernel part right here
1420 1.1 pooka */
1421 1.1 pooka
1422 1.1 pooka parg.pfds = pfd_host;
1423 1.1 pooka parg.nfds = nfds+1;
1424 1.3 pooka parg.ts = ts;
1425 1.3 pooka parg.sigmask = sigmask;
1426 1.1 pooka parg.pipefd = rpipe[1];
1427 1.1 pooka pthread_create(&pt, NULL, hostpoll, &parg);
1428 1.1 pooka
1429 1.35 pooka op_pollts = GETSYSCALL(rump, POLLTS);
1430 1.3 pooka lrv = op_pollts(pfd_rump, nfds+1, ts, NULL);
1431 1.1 pooka sverrno = errno;
1432 1.1 pooka write(hpipe[1], &rv, sizeof(rv));
1433 1.1 pooka pthread_join(pt, (void *)&trv);
1434 1.1 pooka
1435 1.1 pooka /* check who "won" and merge results */
1436 1.1 pooka if (lrv != 0 && pfd_host[nfds].revents & POLLIN) {
1437 1.1 pooka rv = trv;
1438 1.1 pooka
1439 1.1 pooka for (i = 0; i < nfds; i++) {
1440 1.1 pooka if (pfd_rump[i].fd != -1)
1441 1.1 pooka fds[i].revents = pfd_rump[i].revents;
1442 1.1 pooka }
1443 1.1 pooka sverrno = parg.errnum;
1444 1.1 pooka } else if (trv != 0 && pfd_rump[nfds].revents & POLLIN) {
1445 1.1 pooka rv = trv;
1446 1.1 pooka
1447 1.1 pooka for (i = 0; i < nfds; i++) {
1448 1.1 pooka if (pfd_host[i].fd != -1)
1449 1.1 pooka fds[i].revents = pfd_host[i].revents;
1450 1.1 pooka }
1451 1.1 pooka } else {
1452 1.1 pooka rv = 0;
1453 1.1 pooka }
1454 1.1 pooka
1455 1.1 pooka out:
1456 1.35 pooka host_close = GETSYSCALL(host, CLOSE);
1457 1.1 pooka if (rpipe[0] != -1)
1458 1.1 pooka rump_sys_close(rpipe[0]);
1459 1.1 pooka if (rpipe[1] != -1)
1460 1.1 pooka rump_sys_close(rpipe[1]);
1461 1.1 pooka if (hpipe[0] != -1)
1462 1.9 pooka host_close(hpipe[0]);
1463 1.1 pooka if (hpipe[1] != -1)
1464 1.9 pooka host_close(hpipe[1]);
1465 1.1 pooka free(pfd_host);
1466 1.1 pooka free(pfd_rump);
1467 1.1 pooka errno = sverrno;
1468 1.1 pooka } else {
1469 1.1 pooka if (hostcall) {
1470 1.35 pooka op_pollts = GETSYSCALL(host, POLLTS);
1471 1.1 pooka } else {
1472 1.35 pooka op_pollts = GETSYSCALL(rump, POLLTS);
1473 1.2 pooka adjustpoll(fds, nfds, fd_host2rump);
1474 1.1 pooka }
1475 1.1 pooka
1476 1.3 pooka rv = op_pollts(fds, nfds, ts, sigmask);
1477 1.1 pooka if (rumpcall)
1478 1.2 pooka adjustpoll(fds, nfds, fd_rump2host);
1479 1.1 pooka }
1480 1.1 pooka
1481 1.1 pooka return rv;
1482 1.1 pooka }
1483 1.1 pooka
1484 1.1 pooka int
1485 1.24 pooka poll(struct pollfd *fds, nfds_t nfds, int timeout)
1486 1.1 pooka {
1487 1.3 pooka struct timespec ts;
1488 1.3 pooka struct timespec *tsp = NULL;
1489 1.3 pooka
1490 1.3 pooka if (timeout != INFTIM) {
1491 1.3 pooka ts.tv_sec = timeout / 1000;
1492 1.11 pooka ts.tv_nsec = (timeout % 1000) * 1000*1000;
1493 1.3 pooka
1494 1.3 pooka tsp = &ts;
1495 1.3 pooka }
1496 1.1 pooka
1497 1.29 pooka return REALPOLLTS(fds, nfds, tsp, NULL);
1498 1.1 pooka }
1499 1.10 pooka
1500 1.10 pooka int
1501 1.34 pooka REALKEVENT(int kq, const struct kevent *changelist, size_t nchanges,
1502 1.34 pooka struct kevent *eventlist, size_t nevents,
1503 1.34 pooka const struct timespec *timeout)
1504 1.10 pooka {
1505 1.34 pooka int (*op_kevent)(int, const struct kevent *, size_t,
1506 1.34 pooka struct kevent *, size_t, const struct timespec *);
1507 1.34 pooka const struct kevent *ev;
1508 1.34 pooka size_t i;
1509 1.10 pooka
1510 1.34 pooka /*
1511 1.34 pooka * Check that we don't attempt to kevent rump kernel fd's.
1512 1.34 pooka * That needs similar treatment to select/poll, but is slightly
1513 1.34 pooka * trickier since we need to manage to different kq descriptors.
1514 1.34 pooka * (TODO, in case you're wondering).
1515 1.34 pooka */
1516 1.34 pooka for (i = 0; i < nchanges; i++) {
1517 1.34 pooka ev = &changelist[i];
1518 1.34 pooka if (ev->filter == EVFILT_READ || ev->filter == EVFILT_WRITE ||
1519 1.34 pooka ev->filter == EVFILT_VNODE) {
1520 1.40 pooka if (fd_isrump((int)ev->ident))
1521 1.34 pooka return ENOTSUP;
1522 1.34 pooka }
1523 1.27 pooka }
1524 1.10 pooka
1525 1.35 pooka op_kevent = GETSYSCALL(host, KEVENT);
1526 1.34 pooka return op_kevent(kq, changelist, nchanges, eventlist, nevents, timeout);
1527 1.10 pooka }
1528 1.17 pooka
1529 1.17 pooka /*
1530 1.17 pooka * Rest are std type calls.
1531 1.17 pooka */
1532 1.17 pooka
1533 1.17 pooka FDCALL(int, bind, DUALCALL_BIND, \
1534 1.17 pooka (int fd, const struct sockaddr *name, socklen_t namelen), \
1535 1.17 pooka (int, const struct sockaddr *, socklen_t), \
1536 1.17 pooka (fd, name, namelen))
1537 1.17 pooka
1538 1.17 pooka FDCALL(int, connect, DUALCALL_CONNECT, \
1539 1.17 pooka (int fd, const struct sockaddr *name, socklen_t namelen), \
1540 1.17 pooka (int, const struct sockaddr *, socklen_t), \
1541 1.17 pooka (fd, name, namelen))
1542 1.17 pooka
1543 1.17 pooka FDCALL(int, getpeername, DUALCALL_GETPEERNAME, \
1544 1.17 pooka (int fd, struct sockaddr *name, socklen_t *namelen), \
1545 1.17 pooka (int, struct sockaddr *, socklen_t *), \
1546 1.17 pooka (fd, name, namelen))
1547 1.17 pooka
1548 1.17 pooka FDCALL(int, getsockname, DUALCALL_GETSOCKNAME, \
1549 1.17 pooka (int fd, struct sockaddr *name, socklen_t *namelen), \
1550 1.17 pooka (int, struct sockaddr *, socklen_t *), \
1551 1.17 pooka (fd, name, namelen))
1552 1.17 pooka
1553 1.17 pooka FDCALL(int, listen, DUALCALL_LISTEN, \
1554 1.17 pooka (int fd, int backlog), \
1555 1.17 pooka (int, int), \
1556 1.17 pooka (fd, backlog))
1557 1.17 pooka
1558 1.17 pooka FDCALL(ssize_t, recvfrom, DUALCALL_RECVFROM, \
1559 1.17 pooka (int fd, void *buf, size_t len, int flags, \
1560 1.17 pooka struct sockaddr *from, socklen_t *fromlen), \
1561 1.17 pooka (int, void *, size_t, int, struct sockaddr *, socklen_t *), \
1562 1.17 pooka (fd, buf, len, flags, from, fromlen))
1563 1.17 pooka
1564 1.17 pooka FDCALL(ssize_t, sendto, DUALCALL_SENDTO, \
1565 1.17 pooka (int fd, const void *buf, size_t len, int flags, \
1566 1.17 pooka const struct sockaddr *to, socklen_t tolen), \
1567 1.17 pooka (int, const void *, size_t, int, \
1568 1.17 pooka const struct sockaddr *, socklen_t), \
1569 1.17 pooka (fd, buf, len, flags, to, tolen))
1570 1.17 pooka
1571 1.17 pooka FDCALL(ssize_t, recvmsg, DUALCALL_RECVMSG, \
1572 1.17 pooka (int fd, struct msghdr *msg, int flags), \
1573 1.17 pooka (int, struct msghdr *, int), \
1574 1.17 pooka (fd, msg, flags))
1575 1.17 pooka
1576 1.17 pooka FDCALL(ssize_t, sendmsg, DUALCALL_SENDMSG, \
1577 1.17 pooka (int fd, const struct msghdr *msg, int flags), \
1578 1.17 pooka (int, const struct msghdr *, int), \
1579 1.17 pooka (fd, msg, flags))
1580 1.17 pooka
1581 1.17 pooka FDCALL(int, getsockopt, DUALCALL_GETSOCKOPT, \
1582 1.17 pooka (int fd, int level, int optn, void *optval, socklen_t *optlen), \
1583 1.17 pooka (int, int, int, void *, socklen_t *), \
1584 1.17 pooka (fd, level, optn, optval, optlen))
1585 1.17 pooka
1586 1.17 pooka FDCALL(int, setsockopt, DUALCALL_SETSOCKOPT, \
1587 1.17 pooka (int fd, int level, int optn, \
1588 1.17 pooka const void *optval, socklen_t optlen), \
1589 1.17 pooka (int, int, int, const void *, socklen_t), \
1590 1.17 pooka (fd, level, optn, optval, optlen))
1591 1.17 pooka
1592 1.17 pooka FDCALL(int, shutdown, DUALCALL_SHUTDOWN, \
1593 1.17 pooka (int fd, int how), \
1594 1.17 pooka (int, int), \
1595 1.17 pooka (fd, how))
1596 1.17 pooka
1597 1.21 christos #if _FORTIFY_SOURCE > 0
1598 1.21 christos #define STUB(fun) __ssp_weak_name(fun)
1599 1.21 christos ssize_t _sys_readlink(const char * __restrict, char * __restrict, size_t);
1600 1.21 christos ssize_t
1601 1.21 christos STUB(readlink)(const char * __restrict path, char * __restrict buf,
1602 1.21 christos size_t bufsiz)
1603 1.21 christos {
1604 1.21 christos return _sys_readlink(path, buf, bufsiz);
1605 1.21 christos }
1606 1.21 christos
1607 1.21 christos char *_sys_getcwd(char *, size_t);
1608 1.21 christos char *
1609 1.21 christos STUB(getcwd)(char *buf, size_t size)
1610 1.21 christos {
1611 1.21 christos return _sys_getcwd(buf, size);
1612 1.21 christos }
1613 1.21 christos #else
1614 1.21 christos #define STUB(fun) fun
1615 1.21 christos #endif
1616 1.21 christos
1617 1.31 pooka FDCALL(ssize_t, REALREAD, DUALCALL_READ, \
1618 1.17 pooka (int fd, void *buf, size_t buflen), \
1619 1.17 pooka (int, void *, size_t), \
1620 1.17 pooka (fd, buf, buflen))
1621 1.17 pooka
1622 1.18 pooka FDCALL(ssize_t, readv, DUALCALL_READV, \
1623 1.17 pooka (int fd, const struct iovec *iov, int iovcnt), \
1624 1.17 pooka (int, const struct iovec *, int), \
1625 1.17 pooka (fd, iov, iovcnt))
1626 1.17 pooka
1627 1.60 pooka FDCALL(ssize_t, REALPREAD, DUALCALL_PREAD, \
1628 1.60 pooka (int fd, void *buf, size_t nbytes, off_t offset), \
1629 1.60 pooka (int, void *, size_t, off_t), \
1630 1.60 pooka (fd, buf, nbytes, offset))
1631 1.60 pooka
1632 1.60 pooka FDCALL(ssize_t, preadv, DUALCALL_PREADV, \
1633 1.60 pooka (int fd, const struct iovec *iov, int iovcnt, off_t offset), \
1634 1.60 pooka (int, const struct iovec *, int, off_t), \
1635 1.60 pooka (fd, iov, iovcnt, offset))
1636 1.60 pooka
1637 1.17 pooka FDCALL(ssize_t, writev, DUALCALL_WRITEV, \
1638 1.17 pooka (int fd, const struct iovec *iov, int iovcnt), \
1639 1.17 pooka (int, const struct iovec *, int), \
1640 1.17 pooka (fd, iov, iovcnt))
1641 1.45 pooka
1642 1.60 pooka FDCALL(ssize_t, REALPWRITE, DUALCALL_PWRITE, \
1643 1.60 pooka (int fd, const void *buf, size_t nbytes, off_t offset), \
1644 1.60 pooka (int, const void *, size_t, off_t), \
1645 1.60 pooka (fd, buf, nbytes, offset))
1646 1.60 pooka
1647 1.60 pooka FDCALL(ssize_t, pwritev, DUALCALL_PWRITEV, \
1648 1.60 pooka (int fd, const struct iovec *iov, int iovcnt, off_t offset), \
1649 1.60 pooka (int, const struct iovec *, int, off_t), \
1650 1.60 pooka (fd, iov, iovcnt, offset))
1651 1.60 pooka
1652 1.45 pooka FDCALL(int, REALFSTAT, DUALCALL_FSTAT, \
1653 1.45 pooka (int fd, struct stat *sb), \
1654 1.45 pooka (int, struct stat *), \
1655 1.45 pooka (fd, sb))
1656 1.45 pooka
1657 1.45 pooka FDCALL(int, fstatvfs1, DUALCALL_FSTATVFS1, \
1658 1.45 pooka (int fd, struct statvfs *buf, int flags), \
1659 1.45 pooka (int, struct statvfs *, int), \
1660 1.45 pooka (fd, buf, flags))
1661 1.45 pooka
1662 1.61 pooka FDCALL(off_t, lseek, DUALCALL_LSEEK, \
1663 1.45 pooka (int fd, off_t offset, int whence), \
1664 1.45 pooka (int, off_t, int), \
1665 1.45 pooka (fd, offset, whence))
1666 1.61 pooka __strong_alias(_lseek,lseek);
1667 1.45 pooka
1668 1.45 pooka FDCALL(int, REALGETDENTS, DUALCALL_GETDENTS, \
1669 1.45 pooka (int fd, char *buf, size_t nbytes), \
1670 1.45 pooka (int, char *, size_t), \
1671 1.45 pooka (fd, buf, nbytes))
1672 1.45 pooka
1673 1.45 pooka FDCALL(int, fchown, DUALCALL_FCHOWN, \
1674 1.45 pooka (int fd, uid_t owner, gid_t group), \
1675 1.45 pooka (int, uid_t, gid_t), \
1676 1.45 pooka (fd, owner, group))
1677 1.45 pooka
1678 1.45 pooka FDCALL(int, fchmod, DUALCALL_FCHMOD, \
1679 1.45 pooka (int fd, mode_t mode), \
1680 1.45 pooka (int, mode_t), \
1681 1.45 pooka (fd, mode))
1682 1.45 pooka
1683 1.45 pooka FDCALL(int, ftruncate, DUALCALL_FTRUNCATE, \
1684 1.45 pooka (int fd, off_t length), \
1685 1.45 pooka (int, off_t), \
1686 1.45 pooka (fd, length))
1687 1.45 pooka
1688 1.45 pooka FDCALL(int, fsync, DUALCALL_FSYNC, \
1689 1.45 pooka (int fd), \
1690 1.45 pooka (int), \
1691 1.45 pooka (fd))
1692 1.45 pooka
1693 1.45 pooka FDCALL(int, fsync_range, DUALCALL_FSYNC_RANGE, \
1694 1.45 pooka (int fd, int how, off_t start, off_t length), \
1695 1.45 pooka (int, int, off_t, off_t), \
1696 1.45 pooka (fd, how, start, length))
1697 1.45 pooka
1698 1.45 pooka FDCALL(int, futimes, DUALCALL_FUTIMES, \
1699 1.45 pooka (int fd, const struct timeval *tv), \
1700 1.45 pooka (int, const struct timeval *), \
1701 1.45 pooka (fd, tv))
1702 1.45 pooka
1703 1.60 pooka FDCALL(int, fchflags, DUALCALL_FCHFLAGS, \
1704 1.60 pooka (int fd, u_long flags), \
1705 1.60 pooka (int, u_long), \
1706 1.60 pooka (fd, flags))
1707 1.60 pooka
1708 1.45 pooka /*
1709 1.45 pooka * path-based selectors
1710 1.45 pooka */
1711 1.45 pooka
1712 1.45 pooka PATHCALL(int, REALSTAT, DUALCALL_STAT, \
1713 1.45 pooka (const char *path, struct stat *sb), \
1714 1.45 pooka (const char *, struct stat *), \
1715 1.45 pooka (path, sb))
1716 1.45 pooka
1717 1.45 pooka PATHCALL(int, REALLSTAT, DUALCALL_LSTAT, \
1718 1.45 pooka (const char *path, struct stat *sb), \
1719 1.45 pooka (const char *, struct stat *), \
1720 1.45 pooka (path, sb))
1721 1.45 pooka
1722 1.45 pooka PATHCALL(int, chown, DUALCALL_CHOWN, \
1723 1.45 pooka (const char *path, uid_t owner, gid_t group), \
1724 1.45 pooka (const char *, uid_t, gid_t), \
1725 1.45 pooka (path, owner, group))
1726 1.45 pooka
1727 1.45 pooka PATHCALL(int, lchown, DUALCALL_LCHOWN, \
1728 1.45 pooka (const char *path, uid_t owner, gid_t group), \
1729 1.45 pooka (const char *, uid_t, gid_t), \
1730 1.45 pooka (path, owner, group))
1731 1.45 pooka
1732 1.45 pooka PATHCALL(int, chmod, DUALCALL_CHMOD, \
1733 1.45 pooka (const char *path, mode_t mode), \
1734 1.45 pooka (const char *, mode_t), \
1735 1.45 pooka (path, mode))
1736 1.45 pooka
1737 1.45 pooka PATHCALL(int, lchmod, DUALCALL_LCHMOD, \
1738 1.45 pooka (const char *path, mode_t mode), \
1739 1.45 pooka (const char *, mode_t), \
1740 1.45 pooka (path, mode))
1741 1.45 pooka
1742 1.45 pooka PATHCALL(int, statvfs1, DUALCALL_STATVFS1, \
1743 1.45 pooka (const char *path, struct statvfs *buf, int flags), \
1744 1.45 pooka (const char *, struct statvfs *, int), \
1745 1.45 pooka (path, buf, flags))
1746 1.45 pooka
1747 1.45 pooka PATHCALL(int, unlink, DUALCALL_UNLINK, \
1748 1.45 pooka (const char *path), \
1749 1.45 pooka (const char *), \
1750 1.45 pooka (path))
1751 1.45 pooka
1752 1.45 pooka PATHCALL(int, symlink, DUALCALL_SYMLINK, \
1753 1.58 pooka (const char *target, const char *path), \
1754 1.45 pooka (const char *, const char *), \
1755 1.58 pooka (target, path))
1756 1.45 pooka
1757 1.46 pooka PATHCALL(ssize_t, readlink, DUALCALL_READLINK, \
1758 1.45 pooka (const char *path, char *buf, size_t bufsiz), \
1759 1.45 pooka (const char *, char *, size_t), \
1760 1.45 pooka (path, buf, bufsiz))
1761 1.45 pooka
1762 1.45 pooka PATHCALL(int, mkdir, DUALCALL_MKDIR, \
1763 1.45 pooka (const char *path, mode_t mode), \
1764 1.45 pooka (const char *, mode_t), \
1765 1.45 pooka (path, mode))
1766 1.45 pooka
1767 1.45 pooka PATHCALL(int, rmdir, DUALCALL_RMDIR, \
1768 1.45 pooka (const char *path), \
1769 1.45 pooka (const char *), \
1770 1.45 pooka (path))
1771 1.45 pooka
1772 1.45 pooka PATHCALL(int, utimes, DUALCALL_UTIMES, \
1773 1.45 pooka (const char *path, const struct timeval *tv), \
1774 1.45 pooka (const char *, const struct timeval *), \
1775 1.45 pooka (path, tv))
1776 1.45 pooka
1777 1.45 pooka PATHCALL(int, lutimes, DUALCALL_LUTIMES, \
1778 1.45 pooka (const char *path, const struct timeval *tv), \
1779 1.45 pooka (const char *, const struct timeval *), \
1780 1.45 pooka (path, tv))
1781 1.45 pooka
1782 1.60 pooka PATHCALL(int, chflags, DUALCALL_CHFLAGS, \
1783 1.60 pooka (const char *path, u_long flags), \
1784 1.60 pooka (const char *, u_long), \
1785 1.60 pooka (path, flags))
1786 1.60 pooka
1787 1.60 pooka PATHCALL(int, lchflags, DUALCALL_LCHFLAGS, \
1788 1.60 pooka (const char *path, u_long flags), \
1789 1.60 pooka (const char *, u_long), \
1790 1.60 pooka (path, flags))
1791 1.60 pooka
1792 1.45 pooka PATHCALL(int, truncate, DUALCALL_TRUNCATE, \
1793 1.45 pooka (const char *path, off_t length), \
1794 1.45 pooka (const char *, off_t), \
1795 1.45 pooka (path, length))
1796 1.48 pooka
1797 1.48 pooka /*
1798 1.48 pooka * Note: with mount the decisive parameter is the mount
1799 1.48 pooka * destination directory. This is because we don't really know
1800 1.48 pooka * about the "source" directory in a generic call (and besides,
1801 1.48 pooka * it might not even exist, cf. nfs).
1802 1.48 pooka */
1803 1.48 pooka PATHCALL(int, REALMOUNT, DUALCALL_MOUNT, \
1804 1.48 pooka (const char *type, const char *path, int flags, \
1805 1.48 pooka void *data, size_t dlen), \
1806 1.48 pooka (const char *, const char *, int, void *, size_t), \
1807 1.48 pooka (type, path, flags, data, dlen))
1808 1.48 pooka
1809 1.48 pooka PATHCALL(int, unmount, DUALCALL_UNMOUNT, \
1810 1.48 pooka (const char *path, int flags), \
1811 1.48 pooka (const char *, int), \
1812 1.48 pooka (path, flags))
1813