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