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