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