netbsd32_compat_30.c revision 1.31.16.3 1 1.31.16.3 pgoyette /* $NetBSD: netbsd32_compat_30.c,v 1.31.16.3 2018/09/11 01:52:00 pgoyette Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1998, 2001 Matthew R. Green
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 christos * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 christos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 christos * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 christos * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 christos * SUCH DAMAGE.
27 1.1 christos */
28 1.1 christos
29 1.1 christos #include <sys/cdefs.h>
30 1.31.16.3 pgoyette __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_30.c,v 1.31.16.3 2018/09/11 01:52:00 pgoyette Exp $");
31 1.1 christos
32 1.1 christos #include <sys/param.h>
33 1.1 christos #include <sys/systm.h>
34 1.1 christos #include <sys/mount.h>
35 1.31.16.2 pgoyette #include <sys/mount.h>
36 1.1 christos #include <sys/socket.h>
37 1.1 christos #include <sys/socketvar.h>
38 1.1 christos #include <sys/stat.h>
39 1.1 christos #include <sys/time.h>
40 1.1 christos #include <sys/ktrace.h>
41 1.1 christos #include <sys/resourcevar.h>
42 1.1 christos #include <sys/vnode.h>
43 1.1 christos #include <sys/file.h>
44 1.1 christos #include <sys/filedesc.h>
45 1.1 christos #include <sys/namei.h>
46 1.1 christos #include <sys/statvfs.h>
47 1.1 christos #include <sys/syscallargs.h>
48 1.31.16.3 pgoyette #include <sys/syscallvar.h>
49 1.1 christos #include <sys/proc.h>
50 1.2 christos #include <sys/dirent.h>
51 1.7 elad #include <sys/kauth.h>
52 1.18 dsl #include <sys/vfs_syscalls.h>
53 1.1 christos
54 1.1 christos #include <compat/netbsd32/netbsd32.h>
55 1.1 christos #include <compat/netbsd32/netbsd32_syscallargs.h>
56 1.1 christos #include <compat/netbsd32/netbsd32_conv.h>
57 1.9 martin #include <compat/sys/mount.h>
58 1.1 christos
59 1.1 christos
60 1.1 christos int
61 1.23 dsl compat_30_netbsd32_getdents(struct lwp *l, const struct compat_30_netbsd32_getdents_args *uap, register_t *retval)
62 1.1 christos {
63 1.23 dsl /* {
64 1.1 christos syscallarg(int) fd;
65 1.1 christos syscallarg(netbsd32_charp) buf;
66 1.1 christos syscallarg(netbsd32_size_t) count;
67 1.23 dsl } */
68 1.24 ad file_t *fp;
69 1.1 christos int error, done;
70 1.1 christos char *buf;
71 1.8 simonb netbsd32_size_t count;
72 1.1 christos
73 1.8 simonb /* Limit the size on any kernel buffers used by VOP_READDIR */
74 1.31.16.1 pgoyette count = uimin(MAXBSIZE, SCARG(uap, count));
75 1.8 simonb
76 1.26 ad /* fd_getvnode() will use the descriptor for us */
77 1.24 ad if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
78 1.1 christos return (error);
79 1.1 christos if ((fp->f_flag & FREAD) == 0) {
80 1.1 christos error = EBADF;
81 1.1 christos goto out;
82 1.1 christos }
83 1.31 maxv if (count == 0)
84 1.31 maxv goto out;
85 1.31 maxv
86 1.30 rmind buf = kmem_alloc(count, KM_SLEEP);
87 1.8 simonb error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0);
88 1.1 christos if (error == 0) {
89 1.1 christos *retval = netbsd32_to_dirent12(buf, done);
90 1.19 dsl error = copyout(buf, SCARG_P32(uap, buf), *retval);
91 1.1 christos }
92 1.30 rmind kmem_free(buf, count);
93 1.1 christos out:
94 1.24 ad fd_putfile(SCARG(uap, fd));
95 1.1 christos return (error);
96 1.1 christos }
97 1.1 christos
98 1.1 christos int
99 1.23 dsl compat_30_netbsd32___stat13(struct lwp *l, const struct compat_30_netbsd32___stat13_args *uap, register_t *retval)
100 1.1 christos {
101 1.23 dsl /* {
102 1.1 christos syscallarg(const netbsd32_charp) path;
103 1.1 christos syscallarg(netbsd32_stat13p_t) ub;
104 1.23 dsl } */
105 1.2 christos struct netbsd32_stat13 sb32;
106 1.1 christos struct stat sb;
107 1.1 christos int error;
108 1.1 christos const char *path;
109 1.1 christos
110 1.19 dsl path = SCARG_P32(uap, path);
111 1.1 christos
112 1.24 ad error = do_sys_stat(path, FOLLOW, &sb);
113 1.1 christos if (error)
114 1.1 christos return (error);
115 1.1 christos netbsd32_from___stat13(&sb, &sb32);
116 1.19 dsl error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
117 1.1 christos return (error);
118 1.1 christos }
119 1.1 christos
120 1.1 christos int
121 1.23 dsl compat_30_netbsd32___fstat13(struct lwp *l, const struct compat_30_netbsd32___fstat13_args *uap, register_t *retval)
122 1.1 christos {
123 1.23 dsl /* {
124 1.1 christos syscallarg(int) fd;
125 1.1 christos syscallarg(netbsd32_stat13p_t) sb;
126 1.23 dsl } */
127 1.2 christos struct netbsd32_stat13 sb32;
128 1.1 christos struct stat ub;
129 1.29 njoly int error;
130 1.1 christos
131 1.29 njoly error = do_sys_fstat(SCARG(uap, fd), &ub);
132 1.1 christos if (error == 0) {
133 1.1 christos netbsd32_from___stat13(&ub, &sb32);
134 1.19 dsl error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
135 1.1 christos }
136 1.1 christos return (error);
137 1.1 christos }
138 1.1 christos
139 1.1 christos int
140 1.23 dsl compat_30_netbsd32___lstat13(struct lwp *l, const struct compat_30_netbsd32___lstat13_args *uap, register_t *retval)
141 1.1 christos {
142 1.23 dsl /* {
143 1.1 christos syscallarg(const netbsd32_charp) path;
144 1.1 christos syscallarg(netbsd32_stat13p_t) ub;
145 1.23 dsl } */
146 1.2 christos struct netbsd32_stat13 sb32;
147 1.1 christos struct stat sb;
148 1.1 christos int error;
149 1.1 christos const char *path;
150 1.1 christos
151 1.19 dsl path = SCARG_P32(uap, path);
152 1.1 christos
153 1.24 ad error = do_sys_stat(path, NOFOLLOW, &sb);
154 1.1 christos if (error)
155 1.1 christos return (error);
156 1.1 christos netbsd32_from___stat13(&sb, &sb32);
157 1.19 dsl error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
158 1.1 christos return (error);
159 1.1 christos }
160 1.6 cube
161 1.6 cube int
162 1.23 dsl compat_30_netbsd32_fhstat(struct lwp *l, const struct compat_30_netbsd32_fhstat_args *uap, register_t *retval)
163 1.6 cube {
164 1.23 dsl /* {
165 1.6 cube syscallarg(const netbsd32_fhandlep_t) fhp;
166 1.19 dsl syscallarg(netbsd32_stat13p_t) sb;
167 1.23 dsl } */
168 1.6 cube struct stat sb;
169 1.6 cube struct netbsd32_stat13 sb32;
170 1.6 cube int error;
171 1.9 martin struct compat_30_fhandle fh;
172 1.6 cube struct mount *mp;
173 1.6 cube struct vnode *vp;
174 1.6 cube
175 1.6 cube /*
176 1.6 cube * Must be super user
177 1.6 cube */
178 1.15 elad if ((error = kauth_authorize_system(l->l_cred,
179 1.15 elad KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
180 1.6 cube return (error);
181 1.6 cube
182 1.19 dsl if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
183 1.6 cube return (error);
184 1.6 cube
185 1.6 cube if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
186 1.6 cube return (ESTALE);
187 1.6 cube if (mp->mnt_op->vfs_fhtovp == NULL)
188 1.6 cube return EOPNOTSUPP;
189 1.9 martin if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
190 1.6 cube return (error);
191 1.24 ad error = vn_stat(vp, &sb);
192 1.6 cube vput(vp);
193 1.6 cube if (error)
194 1.6 cube return (error);
195 1.6 cube netbsd32_from___stat13(&sb, &sb32);
196 1.19 dsl error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
197 1.6 cube return (error);
198 1.6 cube }
199 1.9 martin
200 1.9 martin int
201 1.23 dsl compat_30_netbsd32_fhstatvfs1(struct lwp *l, const struct compat_30_netbsd32_fhstatvfs1_args *uap, register_t *retval)
202 1.12 martin {
203 1.23 dsl /* {
204 1.12 martin syscallarg(const netbsd32_fhandlep_t) fhp;
205 1.12 martin syscallarg(netbsd32_statvfsp_t) buf;
206 1.12 martin syscallarg(int) flags;
207 1.23 dsl } */
208 1.12 martin struct statvfs *sbuf;
209 1.12 martin struct netbsd32_statvfs *s32;
210 1.12 martin int error;
211 1.12 martin
212 1.21 dsl sbuf = STATVFSBUF_GET();
213 1.21 dsl error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
214 1.21 dsl SCARG(uap, flags));
215 1.21 dsl
216 1.21 dsl if (error != 0) {
217 1.30 rmind s32 = kmem_alloc(sizeof(*s32), KM_SLEEP);
218 1.21 dsl netbsd32_from_statvfs(sbuf, s32);
219 1.21 dsl error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
220 1.30 rmind kmem_free(s32, sizeof(*s32));
221 1.21 dsl }
222 1.21 dsl STATVFSBUF_PUT(sbuf);
223 1.12 martin
224 1.12 martin return (error);
225 1.12 martin }
226 1.12 martin
227 1.12 martin int
228 1.23 dsl compat_30_netbsd32_socket(struct lwp *l, const struct compat_30_netbsd32_socket_args *uap, register_t *retval)
229 1.9 martin {
230 1.23 dsl /* {
231 1.9 martin syscallarg(int) domain;
232 1.9 martin syscallarg(int) type;
233 1.9 martin syscallarg(int) protocol;
234 1.23 dsl } */
235 1.9 martin struct compat_30_sys_socket_args ua;
236 1.9 martin
237 1.9 martin NETBSD32TO64_UAP(domain);
238 1.9 martin NETBSD32TO64_UAP(type);
239 1.9 martin NETBSD32TO64_UAP(protocol);
240 1.9 martin return (compat_30_sys_socket(l, &ua, retval));
241 1.9 martin }
242 1.9 martin
243 1.9 martin int
244 1.23 dsl compat_30_netbsd32_getfh(struct lwp *l, const struct compat_30_netbsd32_getfh_args *uap, register_t *retval)
245 1.9 martin {
246 1.23 dsl /* {
247 1.9 martin syscallarg(const netbsd32_charp) fname;
248 1.9 martin syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
249 1.23 dsl } */
250 1.9 martin struct compat_30_sys_getfh_args ua;
251 1.9 martin
252 1.9 martin NETBSD32TOP_UAP(fname, const char);
253 1.9 martin NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
254 1.9 martin /* Lucky for us a fhandle_t doesn't change sizes */
255 1.9 martin return (compat_30_sys_getfh(l, &ua, retval));
256 1.9 martin }
257 1.12 martin
258 1.12 martin
259 1.23 dsl int
260 1.28 christos compat_30_netbsd32___fhstat30(struct lwp *l, const struct compat_30_netbsd32___fhstat30_args *uap, register_t *retval)
261 1.12 martin {
262 1.23 dsl /* {
263 1.12 martin syscallarg(const netbsd32_fhandlep_t) fhp;
264 1.12 martin syscallarg(netbsd32_statp_t) sb;
265 1.23 dsl } */
266 1.12 martin struct stat sb;
267 1.28 christos struct netbsd32_stat50 sb32;
268 1.12 martin int error;
269 1.12 martin
270 1.21 dsl error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
271 1.21 dsl if (error)
272 1.12 martin return error;
273 1.12 martin
274 1.28 christos netbsd32_from___stat50(&sb, &sb32);
275 1.21 dsl error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
276 1.12 martin return error;
277 1.12 martin }
278 1.12 martin
279 1.12 martin /*
280 1.12 martin * Open a file given a file handle.
281 1.12 martin *
282 1.12 martin * Check permissions, allocate an open file structure,
283 1.12 martin * and call the device open routine if any.
284 1.12 martin */
285 1.12 martin int
286 1.23 dsl compat_30_netbsd32_fhopen(struct lwp *l, const struct compat_30_netbsd32_fhopen_args *uap, register_t *retval)
287 1.12 martin {
288 1.23 dsl /* {
289 1.12 martin syscallarg(const fhandle_t *) fhp;
290 1.12 martin syscallarg(int) flags;
291 1.23 dsl } */
292 1.12 martin struct compat_30_sys_fhopen_args ua;
293 1.12 martin
294 1.12 martin NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
295 1.12 martin NETBSD32TO64_UAP(flags);
296 1.12 martin return (compat_30_sys_fhopen(l, &ua, retval));
297 1.12 martin }
298 1.31.16.2 pgoyette
299 1.31.16.2 pgoyette static struct syscall_package compat_netbsd32_30_syscalls[] = {
300 1.31.16.2 pgoyette { NETBSD32_SYS_getdents, 0,
301 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32_getdents },
302 1.31.16.2 pgoyette { NETBSD32_SYS_stat13, 0,
303 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32___stat13 },
304 1.31.16.2 pgoyette { NETBSD32_SYS_fstat13, 0,
305 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32___fstat13 },
306 1.31.16.2 pgoyette { NETBSD32_SYS_lstat13, 0,
307 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32___lstat13 },
308 1.31.16.2 pgoyette { NETBSD32_SYS_fhstat, 0,
309 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32_fhstat },
310 1.31.16.2 pgoyette { NETBSD32_SYS_fhstatvfs1, 0,
311 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32_fhstatvfs1 },
312 1.31.16.2 pgoyette { NETBSD32_SYS_socket, 0,
313 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32_socket },
314 1.31.16.2 pgoyette { NETBSD32_SYS_getfh, 0,
315 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32_getfh },
316 1.31.16.2 pgoyette { NETBSD32_SYS_fhstat30, 0,
317 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32___fhstat30 },
318 1.31.16.2 pgoyette { NETBSD32_SYS_fhopen, 0,
319 1.31.16.2 pgoyette (sy_call_t *)compat_30_netbsd32___fhopen },
320 1.31.16.2 pgoyette { 0, 0, NULL }
321 1.31.16.2 pgoyette };
322 1.31.16.2 pgoyette
323 1.31.16.2 pgoyette MODULE(MODULE_CLASS_EXEC, compat_netbsd32_30, "compat_netbsd32,compat_30");
324 1.31.16.2 pgoyette
325 1.31.16.2 pgoyette static int
326 1.31.16.2 pgoyette compat_netbsd32_30_modcmd(modcmd_t cmd, void *arg)
327 1.31.16.2 pgoyette {
328 1.31.16.2 pgoyette
329 1.31.16.2 pgoyette switch (cmd) {
330 1.31.16.2 pgoyette case MODULE_CMD_INIT:
331 1.31.16.2 pgoyette return syscall_establish(NULL, compat_netbsd32_30_syscalls);
332 1.31.16.2 pgoyette
333 1.31.16.2 pgoyette case MODULE_CMD_FINI:
334 1.31.16.2 pgoyette return syscall_disestablish(NULL, compat_netbsd32_30_syscalls);
335 1.31.16.2 pgoyette
336 1.31.16.2 pgoyette default:
337 1.31.16.2 pgoyette return ENOTTY;
338 1.31.16.2 pgoyette }
339 1.31.16.2 pgoyette }
340