netbsd32_compat_30.c revision 1.9 1 /* $NetBSD: netbsd32_compat_30.c,v 1.9 2006/07/13 12:00:25 martin 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.9 2006/07/13 12:00:25 martin 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/sa.h>
51 #include <sys/statvfs.h>
52 #include <sys/syscallargs.h>
53 #include <sys/proc.h>
54 #include <sys/dirent.h>
55 #include <sys/kauth.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 netbsd32_getdents(l, v, retval)
65 struct lwp *l;
66 void *v;
67 register_t *retval;
68 {
69 struct 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, NETBSD32PTR64(SCARG(uap, buf)), *retval);
95 }
96 free(buf, M_TEMP);
97 out:
98 FILE_UNUSE(fp, l);
99 return (error);
100 }
101
102 int
103 netbsd32___stat13(l, v, retval)
104 struct lwp *l;
105 void *v;
106 register_t *retval;
107 {
108 struct 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 struct nameidata nd;
116 caddr_t sg;
117 const char *path;
118 struct proc *p = l->l_proc;
119
120 path = (char *)NETBSD32PTR64(SCARG(uap, path));
121 sg = stackgap_init(p, 0);
122 CHECK_ALT_EXIST(l, &sg, path);
123
124 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, path, l);
125 if ((error = namei(&nd)) != 0)
126 return (error);
127 error = vn_stat(nd.ni_vp, &sb, l);
128 vput(nd.ni_vp);
129 if (error)
130 return (error);
131 netbsd32_from___stat13(&sb, &sb32);
132 error = copyout(&sb32, (caddr_t)NETBSD32PTR64(SCARG(uap, ub)),
133 sizeof(sb32));
134 return (error);
135 }
136
137 int
138 netbsd32___fstat13(l, v, retval)
139 struct lwp *l;
140 void *v;
141 register_t *retval;
142 {
143 struct netbsd32___fstat13_args /* {
144 syscallarg(int) fd;
145 syscallarg(netbsd32_stat13p_t) sb;
146 } */ *uap = v;
147 int fd = SCARG(uap, fd);
148 struct proc *p = l->l_proc;
149 struct filedesc *fdp = p->p_fd;
150 struct file *fp;
151 struct netbsd32_stat13 sb32;
152 struct stat ub;
153 int error = 0;
154
155 if ((fp = fd_getfile(fdp, fd)) == NULL)
156 return (EBADF);
157
158 FILE_USE(fp);
159 error = (*fp->f_ops->fo_stat)(fp, &ub, l);
160 FILE_UNUSE(fp, l);
161
162 if (error == 0) {
163 netbsd32_from___stat13(&ub, &sb32);
164 error = copyout(&sb32, (caddr_t)NETBSD32PTR64(SCARG(uap, sb)),
165 sizeof(sb32));
166 }
167 return (error);
168 }
169
170 int
171 netbsd32___lstat13(l, v, retval)
172 struct lwp *l;
173 void *v;
174 register_t *retval;
175 {
176 struct netbsd32___lstat13_args /* {
177 syscallarg(const netbsd32_charp) path;
178 syscallarg(netbsd32_stat13p_t) ub;
179 } */ *uap = v;
180 struct netbsd32_stat13 sb32;
181 struct stat sb;
182 int error;
183 struct nameidata nd;
184 caddr_t sg;
185 const char *path;
186 struct proc *p = l->l_proc;
187
188 path = (char *)NETBSD32PTR64(SCARG(uap, path));
189 sg = stackgap_init(p, 0);
190 CHECK_ALT_EXIST(l, &sg, path);
191
192 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, path, l);
193 if ((error = namei(&nd)) != 0)
194 return (error);
195 error = vn_stat(nd.ni_vp, &sb, l);
196 vput(nd.ni_vp);
197 if (error)
198 return (error);
199 netbsd32_from___stat13(&sb, &sb32);
200 error = copyout(&sb32, (caddr_t)NETBSD32PTR64(SCARG(uap, ub)),
201 sizeof(sb32));
202 return (error);
203 }
204
205 int
206 compat_30_netbsd32_fhstat(l, v, retval)
207 struct lwp *l;
208 void *v;
209 register_t *retval;
210 {
211 struct compat_30_netbsd32_fhstat_args /* {
212 syscallarg(const netbsd32_fhandlep_t) fhp;
213 syscallarg(netbsd32_stat13p_t) sb);
214 } */ *uap = v;
215 struct proc *p = l->l_proc;
216 struct stat sb;
217 struct netbsd32_stat13 sb32;
218 int error;
219 struct compat_30_fhandle fh;
220 struct mount *mp;
221 struct vnode *vp;
222
223 /*
224 * Must be super user
225 */
226 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
227 &p->p_acflag)))
228 return (error);
229
230 if ((error = copyin(NETBSD32PTR64(SCARG(uap, fhp)), &fh,
231 sizeof(fh))) != 0)
232 return (error);
233
234 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
235 return (ESTALE);
236 if (mp->mnt_op->vfs_fhtovp == NULL)
237 return EOPNOTSUPP;
238 if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
239 return (error);
240 error = vn_stat(vp, &sb, l);
241 vput(vp);
242 if (error)
243 return (error);
244 netbsd32_from___stat13(&sb, &sb32);
245 error = copyout(&sb32, NETBSD32PTR64(SCARG(uap, sb)), sizeof(sb));
246 return (error);
247 }
248
249 int
250 compat_30_netbsd32_socket(l, v, retval)
251 struct lwp *l;
252 void *v;
253 register_t *retval;
254 {
255 struct compat_30_netbsd32_socket_args /* {
256 syscallarg(int) domain;
257 syscallarg(int) type;
258 syscallarg(int) protocol;
259 } */ *uap = v;
260 struct compat_30_sys_socket_args ua;
261
262 NETBSD32TO64_UAP(domain);
263 NETBSD32TO64_UAP(type);
264 NETBSD32TO64_UAP(protocol);
265 return (compat_30_sys_socket(l, &ua, retval));
266 }
267
268 #if defined(NFS) || defined(NFSSERVER)
269 int
270 compat_30_netbsd32_getfh(l, v, retval)
271 struct lwp *l;
272 void *v;
273 register_t *retval;
274 {
275 struct compat_30_netbsd32_getfh_args /* {
276 syscallarg(const netbsd32_charp) fname;
277 syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
278 } */ *uap = v;
279 struct compat_30_sys_getfh_args ua;
280
281 NETBSD32TOP_UAP(fname, const char);
282 NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
283 /* Lucky for us a fhandle_t doesn't change sizes */
284 return (compat_30_sys_getfh(l, &ua, retval));
285 }
286 #endif
287