xref: /netbsd-src/sys/compat/netbsd32/netbsd32_compat_30.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: netbsd32_compat_30.c,v 1.30 2010/04/23 15:19:20 rmind 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_30.c,v 1.30 2010/04/23 15:19:20 rmind Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mount.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <sys/ktrace.h>
40 #include <sys/resourcevar.h>
41 #include <sys/vnode.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/namei.h>
45 #include <sys/statvfs.h>
46 #include <sys/syscallargs.h>
47 #include <sys/proc.h>
48 #include <sys/dirent.h>
49 #include <sys/kauth.h>
50 #include <sys/vfs_syscalls.h>
51 
52 #include <compat/netbsd32/netbsd32.h>
53 #include <compat/netbsd32/netbsd32_syscallargs.h>
54 #include <compat/netbsd32/netbsd32_conv.h>
55 #include <compat/sys/mount.h>
56 
57 
58 int
59 compat_30_netbsd32_getdents(struct lwp *l, const struct compat_30_netbsd32_getdents_args *uap, register_t *retval)
60 {
61 	/* {
62 		syscallarg(int) fd;
63 		syscallarg(netbsd32_charp) buf;
64 		syscallarg(netbsd32_size_t) count;
65 	} */
66 	file_t *fp;
67 	int error, done;
68 	char  *buf;
69 	netbsd32_size_t count;
70 
71 	/* Limit the size on any kernel buffers used by VOP_READDIR */
72 	count = min(MAXBSIZE, SCARG(uap, count));
73 
74 	/* fd_getvnode() will use the descriptor for us */
75 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
76 		return (error);
77 	if ((fp->f_flag & FREAD) == 0) {
78 		error = EBADF;
79 		goto out;
80 	}
81 	buf = kmem_alloc(count, KM_SLEEP);
82 	error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0);
83 	if (error == 0) {
84 		*retval = netbsd32_to_dirent12(buf, done);
85 		error = copyout(buf, SCARG_P32(uap, buf), *retval);
86 	}
87 	kmem_free(buf, count);
88  out:
89  	fd_putfile(SCARG(uap, fd));
90 	return (error);
91 }
92 
93 int
94 compat_30_netbsd32___stat13(struct lwp *l, const struct compat_30_netbsd32___stat13_args *uap, register_t *retval)
95 {
96 	/* {
97 		syscallarg(const netbsd32_charp) path;
98 		syscallarg(netbsd32_stat13p_t) ub;
99 	} */
100 	struct netbsd32_stat13 sb32;
101 	struct stat sb;
102 	int error;
103 	const char *path;
104 
105 	path = SCARG_P32(uap, path);
106 
107 	error = do_sys_stat(path, FOLLOW, &sb);
108 	if (error)
109 		return (error);
110 	netbsd32_from___stat13(&sb, &sb32);
111 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
112 	return (error);
113 }
114 
115 int
116 compat_30_netbsd32___fstat13(struct lwp *l, const struct compat_30_netbsd32___fstat13_args *uap, register_t *retval)
117 {
118 	/* {
119 		syscallarg(int) fd;
120 		syscallarg(netbsd32_stat13p_t) sb;
121 	} */
122 	struct netbsd32_stat13 sb32;
123 	struct stat ub;
124 	int error;
125 
126 	error = do_sys_fstat(SCARG(uap, fd), &ub);
127 	if (error == 0) {
128 		netbsd32_from___stat13(&ub, &sb32);
129 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
130 	}
131 	return (error);
132 }
133 
134 int
135 compat_30_netbsd32___lstat13(struct lwp *l, const struct compat_30_netbsd32___lstat13_args *uap, register_t *retval)
136 {
137 	/* {
138 		syscallarg(const netbsd32_charp) path;
139 		syscallarg(netbsd32_stat13p_t) ub;
140 	} */
141 	struct netbsd32_stat13 sb32;
142 	struct stat sb;
143 	int error;
144 	const char *path;
145 
146 	path = SCARG_P32(uap, path);
147 
148 	error = do_sys_stat(path, NOFOLLOW, &sb);
149 	if (error)
150 		return (error);
151 	netbsd32_from___stat13(&sb, &sb32);
152 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
153 	return (error);
154 }
155 
156 int
157 compat_30_netbsd32_fhstat(struct lwp *l, const struct compat_30_netbsd32_fhstat_args *uap, register_t *retval)
158 {
159 	/* {
160 		syscallarg(const netbsd32_fhandlep_t) fhp;
161 		syscallarg(netbsd32_stat13p_t) sb;
162 	} */
163 	struct stat sb;
164 	struct netbsd32_stat13 sb32;
165 	int error;
166 	struct compat_30_fhandle fh;
167 	struct mount *mp;
168 	struct vnode *vp;
169 
170 	/*
171 	 * Must be super user
172 	 */
173 	if ((error = kauth_authorize_system(l->l_cred,
174 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
175 		return (error);
176 
177 	if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
178 		return (error);
179 
180 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
181 		return (ESTALE);
182 	if (mp->mnt_op->vfs_fhtovp == NULL)
183 		return EOPNOTSUPP;
184 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
185 		return (error);
186 	error = vn_stat(vp, &sb);
187 	vput(vp);
188 	if (error)
189 		return (error);
190 	netbsd32_from___stat13(&sb, &sb32);
191 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
192 	return (error);
193 }
194 
195 int
196 compat_30_netbsd32_fhstatvfs1(struct lwp *l, const struct compat_30_netbsd32_fhstatvfs1_args *uap, register_t *retval)
197 {
198 	/* {
199 		syscallarg(const netbsd32_fhandlep_t) fhp;
200 		syscallarg(netbsd32_statvfsp_t) buf;
201 		syscallarg(int) flags;
202 	} */
203 	struct statvfs *sbuf;
204 	struct netbsd32_statvfs *s32;
205 	int error;
206 
207 	sbuf = STATVFSBUF_GET();
208 	error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
209 	    SCARG(uap, flags));
210 
211 	if (error != 0) {
212 		s32 = kmem_alloc(sizeof(*s32), KM_SLEEP);
213 		netbsd32_from_statvfs(sbuf, s32);
214 		error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
215 		kmem_free(s32, sizeof(*s32));
216 	}
217 	STATVFSBUF_PUT(sbuf);
218 
219 	return (error);
220 }
221 
222 int
223 compat_30_netbsd32_socket(struct lwp *l, const struct compat_30_netbsd32_socket_args *uap, register_t *retval)
224 {
225 	/* {
226 		syscallarg(int) domain;
227 		syscallarg(int) type;
228 		syscallarg(int) protocol;
229 	} */
230 	struct compat_30_sys_socket_args ua;
231 
232 	NETBSD32TO64_UAP(domain);
233 	NETBSD32TO64_UAP(type);
234 	NETBSD32TO64_UAP(protocol);
235 	return (compat_30_sys_socket(l, &ua, retval));
236 }
237 
238 int
239 compat_30_netbsd32_getfh(struct lwp *l, const struct compat_30_netbsd32_getfh_args *uap, register_t *retval)
240 {
241 	/* {
242 		syscallarg(const netbsd32_charp) fname;
243 		syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
244 	} */
245 	struct compat_30_sys_getfh_args ua;
246 
247 	NETBSD32TOP_UAP(fname, const char);
248 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
249 	/* Lucky for us a fhandle_t doesn't change sizes */
250 	return (compat_30_sys_getfh(l, &ua, retval));
251 }
252 
253 
254 int
255 compat_30_netbsd32___fhstat30(struct lwp *l, const struct compat_30_netbsd32___fhstat30_args *uap, register_t *retval)
256 {
257 	/* {
258 		syscallarg(const netbsd32_fhandlep_t) fhp;
259 		syscallarg(netbsd32_statp_t) sb;
260 	} */
261 	struct stat sb;
262 	struct netbsd32_stat50 sb32;
263 	int error;
264 
265 	error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
266 	if (error)
267 		return error;
268 
269 	netbsd32_from___stat50(&sb, &sb32);
270 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
271 	return error;
272 }
273 
274 /*
275  * Open a file given a file handle.
276  *
277  * Check permissions, allocate an open file structure,
278  * and call the device open routine if any.
279  */
280 int
281 compat_30_netbsd32_fhopen(struct lwp *l, const struct compat_30_netbsd32_fhopen_args *uap, register_t *retval)
282 {
283 	/* {
284 		syscallarg(const fhandle_t *) fhp;
285 		syscallarg(int) flags;
286 	} */
287 	struct compat_30_sys_fhopen_args ua;
288 
289 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
290 	NETBSD32TO64_UAP(flags);
291 	return (compat_30_sys_fhopen(l, &ua, retval));
292 }
293