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