xref: /netbsd-src/sys/compat/netbsd32/netbsd32_compat_30.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: netbsd32_compat_30.c,v 1.22 2007/12/08 18:36:18 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.22 2007/12/08 18:36:18 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(struct lwp *l, void *v, register_t *retval)
65 {
66 	struct compat_30_netbsd32_getdents_args /* {
67 		syscallarg(int) fd;
68 		syscallarg(netbsd32_charp) buf;
69 		syscallarg(netbsd32_size_t) count;
70 	} */ *uap = v;
71 	struct file *fp;
72 	int error, done;
73 	char  *buf;
74 	netbsd32_size_t count;
75 	struct proc *p = l->l_proc;
76 
77 	/* Limit the size on any kernel buffers used by VOP_READDIR */
78 	count = min(MAXBSIZE, SCARG(uap, count));
79 
80 	/* getvnode() will use the descriptor for us */
81 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
82 		return (error);
83 	if ((fp->f_flag & FREAD) == 0) {
84 		error = EBADF;
85 		goto out;
86 	}
87 	buf = malloc(count, M_TEMP, M_WAITOK);
88 	error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0);
89 	if (error == 0) {
90 		*retval = netbsd32_to_dirent12(buf, done);
91 		error = copyout(buf, SCARG_P32(uap, buf), *retval);
92 	}
93 	free(buf, M_TEMP);
94  out:
95 	FILE_UNUSE(fp, l);
96 	return (error);
97 }
98 
99 int
100 compat_30_netbsd32___stat13(struct lwp *l, void *v, register_t *retval)
101 {
102 	struct compat_30_netbsd32___stat13_args /* {
103 		syscallarg(const netbsd32_charp) path;
104 		syscallarg(netbsd32_stat13p_t) ub;
105 	} */ *uap = v;
106 	struct netbsd32_stat13 sb32;
107 	struct stat sb;
108 	int error;
109 	const char *path;
110 
111 	path = SCARG_P32(uap, path);
112 
113 	error = do_sys_stat(l, path, FOLLOW, &sb);
114 	if (error)
115 		return (error);
116 	netbsd32_from___stat13(&sb, &sb32);
117 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
118 	return (error);
119 }
120 
121 int
122 compat_30_netbsd32___fstat13(struct lwp *l, void *v, register_t *retval)
123 {
124 	struct compat_30_netbsd32___fstat13_args /* {
125 		syscallarg(int) fd;
126 		syscallarg(netbsd32_stat13p_t) sb;
127 	} */ *uap = v;
128 	int fd = SCARG(uap, fd);
129 	struct proc *p = l->l_proc;
130 	struct filedesc *fdp = p->p_fd;
131 	struct file *fp;
132 	struct netbsd32_stat13 sb32;
133 	struct stat ub;
134 	int error = 0;
135 
136 	if ((fp = fd_getfile(fdp, fd)) == NULL)
137 		return (EBADF);
138 
139 	FILE_USE(fp);
140 	error = (*fp->f_ops->fo_stat)(fp, &ub, l);
141 	FILE_UNUSE(fp, l);
142 
143 	if (error == 0) {
144 		netbsd32_from___stat13(&ub, &sb32);
145 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
146 	}
147 	return (error);
148 }
149 
150 int
151 compat_30_netbsd32___lstat13(struct lwp *l, void *v, register_t *retval)
152 {
153 	struct compat_30_netbsd32___lstat13_args /* {
154 		syscallarg(const netbsd32_charp) path;
155 		syscallarg(netbsd32_stat13p_t) ub;
156 	} */ *uap = v;
157 	struct netbsd32_stat13 sb32;
158 	struct stat sb;
159 	int error;
160 	const char *path;
161 
162 	path = SCARG_P32(uap, path);
163 
164 	error = do_sys_stat(l, path, NOFOLLOW, &sb);
165 	if (error)
166 		return (error);
167 	netbsd32_from___stat13(&sb, &sb32);
168 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
169 	return (error);
170 }
171 
172 int
173 compat_30_netbsd32_fhstat(struct lwp *l, void *v, register_t *retval)
174 {
175 	struct compat_30_netbsd32_fhstat_args /* {
176 		syscallarg(const netbsd32_fhandlep_t) fhp;
177 		syscallarg(netbsd32_stat13p_t) sb;
178 	} */ *uap = v;
179 	struct stat sb;
180 	struct netbsd32_stat13 sb32;
181 	int error;
182 	struct compat_30_fhandle fh;
183 	struct mount *mp;
184 	struct vnode *vp;
185 
186 	/*
187 	 * Must be super user
188 	 */
189 	if ((error = kauth_authorize_system(l->l_cred,
190 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
191 		return (error);
192 
193 	if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
194 		return (error);
195 
196 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
197 		return (ESTALE);
198 	if (mp->mnt_op->vfs_fhtovp == NULL)
199 		return EOPNOTSUPP;
200 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
201 		return (error);
202 	error = vn_stat(vp, &sb, l);
203 	vput(vp);
204 	if (error)
205 		return (error);
206 	netbsd32_from___stat13(&sb, &sb32);
207 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
208 	return (error);
209 }
210 
211 int
212 compat_30_netbsd32_fhstatvfs1(struct lwp *l, void *v, register_t *retval)
213 {
214 	struct compat_30_netbsd32_fhstatvfs1_args /* {
215 		syscallarg(const netbsd32_fhandlep_t) fhp;
216 		syscallarg(netbsd32_statvfsp_t) buf;
217 		syscallarg(int) flags;
218 	} */ *uap = v;
219 	struct statvfs *sbuf;
220 	struct netbsd32_statvfs *s32;
221 	int error;
222 
223 	sbuf = STATVFSBUF_GET();
224 	error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
225 	    SCARG(uap, flags));
226 
227 	if (error != 0) {
228 		s32 = malloc(sizeof *s32, M_TEMP, M_WAITOK);
229 		netbsd32_from_statvfs(sbuf, s32);
230 		error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
231 		free(s32, M_TEMP);
232 	}
233 	STATVFSBUF_PUT(sbuf);
234 
235 	return (error);
236 }
237 
238 int
239 compat_30_netbsd32_socket(struct lwp *l, void *v, register_t *retval)
240 {
241 	struct compat_30_netbsd32_socket_args /* {
242 		syscallarg(int) domain;
243 		syscallarg(int) type;
244 		syscallarg(int) protocol;
245 	} */ *uap = v;
246 	struct compat_30_sys_socket_args ua;
247 
248 	NETBSD32TO64_UAP(domain);
249 	NETBSD32TO64_UAP(type);
250 	NETBSD32TO64_UAP(protocol);
251 	return (compat_30_sys_socket(l, &ua, retval));
252 }
253 
254 int
255 compat_30_netbsd32_getfh(struct lwp *l, void *v, register_t *retval)
256 {
257 	struct compat_30_netbsd32_getfh_args /* {
258 		syscallarg(const netbsd32_charp) fname;
259 		syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
260 	} */ *uap = v;
261 	struct compat_30_sys_getfh_args ua;
262 
263 	NETBSD32TOP_UAP(fname, const char);
264 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
265 	/* Lucky for us a fhandle_t doesn't change sizes */
266 	return (compat_30_sys_getfh(l, &ua, retval));
267 }
268 
269 
270 int compat_30_netbsd32_sys___fhstat30(l, v, retval)
271 	struct lwp *l;
272 	void *v;
273 	register_t *retval;
274 {
275 	struct compat_30_netbsd32_sys___fhstat30_args /* {
276 		syscallarg(const netbsd32_fhandlep_t) fhp;
277 		syscallarg(netbsd32_statp_t) sb;
278 	} */ *uap = v;
279 	struct stat sb;
280 	struct netbsd32_stat sb32;
281 	int error;
282 
283 	error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
284 	if (error)
285 		return error;
286 
287 	netbsd32_from___stat30(&sb, &sb32);
288 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
289 	return error;
290 }
291 
292 /*
293  * Open a file given a file handle.
294  *
295  * Check permissions, allocate an open file structure,
296  * and call the device open routine if any.
297  */
298 int
299 compat_30_netbsd32_fhopen(struct lwp *l, void *v, register_t *retval)
300 {
301 	struct compat_30_netbsd32_fhopen_args /* {
302 		syscallarg(const fhandle_t *) fhp;
303 		syscallarg(int) flags;
304 	} */ *uap = v;
305 	struct compat_30_sys_fhopen_args ua;
306 
307 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
308 	NETBSD32TO64_UAP(flags);
309 	return (compat_30_sys_fhopen(l, &ua, retval));
310 }
311