xref: /netbsd-src/sys/compat/netbsd32/netbsd32_compat_30.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: netbsd32_compat_30.c,v 1.29 2009/01/26 13:00:04 njoly 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.29 2009/01/26 13:00:04 njoly 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 	struct netbsd32_stat13 sb32;
124 	struct stat ub;
125 	int error;
126 
127 	error = do_sys_fstat(SCARG(uap, fd), &ub);
128 	if (error == 0) {
129 		netbsd32_from___stat13(&ub, &sb32);
130 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
131 	}
132 	return (error);
133 }
134 
135 int
136 compat_30_netbsd32___lstat13(struct lwp *l, const struct compat_30_netbsd32___lstat13_args *uap, register_t *retval)
137 {
138 	/* {
139 		syscallarg(const netbsd32_charp) path;
140 		syscallarg(netbsd32_stat13p_t) ub;
141 	} */
142 	struct netbsd32_stat13 sb32;
143 	struct stat sb;
144 	int error;
145 	const char *path;
146 
147 	path = SCARG_P32(uap, path);
148 
149 	error = do_sys_stat(path, NOFOLLOW, &sb);
150 	if (error)
151 		return (error);
152 	netbsd32_from___stat13(&sb, &sb32);
153 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
154 	return (error);
155 }
156 
157 int
158 compat_30_netbsd32_fhstat(struct lwp *l, const struct compat_30_netbsd32_fhstat_args *uap, register_t *retval)
159 {
160 	/* {
161 		syscallarg(const netbsd32_fhandlep_t) fhp;
162 		syscallarg(netbsd32_stat13p_t) sb;
163 	} */
164 	struct stat sb;
165 	struct netbsd32_stat13 sb32;
166 	int error;
167 	struct compat_30_fhandle fh;
168 	struct mount *mp;
169 	struct vnode *vp;
170 
171 	/*
172 	 * Must be super user
173 	 */
174 	if ((error = kauth_authorize_system(l->l_cred,
175 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
176 		return (error);
177 
178 	if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
179 		return (error);
180 
181 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
182 		return (ESTALE);
183 	if (mp->mnt_op->vfs_fhtovp == NULL)
184 		return EOPNOTSUPP;
185 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
186 		return (error);
187 	error = vn_stat(vp, &sb);
188 	vput(vp);
189 	if (error)
190 		return (error);
191 	netbsd32_from___stat13(&sb, &sb32);
192 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
193 	return (error);
194 }
195 
196 int
197 compat_30_netbsd32_fhstatvfs1(struct lwp *l, const struct compat_30_netbsd32_fhstatvfs1_args *uap, register_t *retval)
198 {
199 	/* {
200 		syscallarg(const netbsd32_fhandlep_t) fhp;
201 		syscallarg(netbsd32_statvfsp_t) buf;
202 		syscallarg(int) flags;
203 	} */
204 	struct statvfs *sbuf;
205 	struct netbsd32_statvfs *s32;
206 	int error;
207 
208 	sbuf = STATVFSBUF_GET();
209 	error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
210 	    SCARG(uap, flags));
211 
212 	if (error != 0) {
213 		s32 = malloc(sizeof *s32, M_TEMP, M_WAITOK);
214 		netbsd32_from_statvfs(sbuf, s32);
215 		error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
216 		free(s32, M_TEMP);
217 	}
218 	STATVFSBUF_PUT(sbuf);
219 
220 	return (error);
221 }
222 
223 int
224 compat_30_netbsd32_socket(struct lwp *l, const struct compat_30_netbsd32_socket_args *uap, register_t *retval)
225 {
226 	/* {
227 		syscallarg(int) domain;
228 		syscallarg(int) type;
229 		syscallarg(int) protocol;
230 	} */
231 	struct compat_30_sys_socket_args ua;
232 
233 	NETBSD32TO64_UAP(domain);
234 	NETBSD32TO64_UAP(type);
235 	NETBSD32TO64_UAP(protocol);
236 	return (compat_30_sys_socket(l, &ua, retval));
237 }
238 
239 int
240 compat_30_netbsd32_getfh(struct lwp *l, const struct compat_30_netbsd32_getfh_args *uap, register_t *retval)
241 {
242 	/* {
243 		syscallarg(const netbsd32_charp) fname;
244 		syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
245 	} */
246 	struct compat_30_sys_getfh_args ua;
247 
248 	NETBSD32TOP_UAP(fname, const char);
249 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
250 	/* Lucky for us a fhandle_t doesn't change sizes */
251 	return (compat_30_sys_getfh(l, &ua, retval));
252 }
253 
254 
255 int
256 compat_30_netbsd32___fhstat30(struct lwp *l, const struct compat_30_netbsd32___fhstat30_args *uap, register_t *retval)
257 {
258 	/* {
259 		syscallarg(const netbsd32_fhandlep_t) fhp;
260 		syscallarg(netbsd32_statp_t) sb;
261 	} */
262 	struct stat sb;
263 	struct netbsd32_stat50 sb32;
264 	int error;
265 
266 	error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
267 	if (error)
268 		return error;
269 
270 	netbsd32_from___stat50(&sb, &sb32);
271 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
272 	return error;
273 }
274 
275 /*
276  * Open a file given a file handle.
277  *
278  * Check permissions, allocate an open file structure,
279  * and call the device open routine if any.
280  */
281 int
282 compat_30_netbsd32_fhopen(struct lwp *l, const struct compat_30_netbsd32_fhopen_args *uap, register_t *retval)
283 {
284 	/* {
285 		syscallarg(const fhandle_t *) fhp;
286 		syscallarg(int) flags;
287 	} */
288 	struct compat_30_sys_fhopen_args ua;
289 
290 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
291 	NETBSD32TO64_UAP(flags);
292 	return (compat_30_sys_fhopen(l, &ua, retval));
293 }
294