netbsd32_compat_30.c revision 1.4.8.2 1 /* $NetBSD: netbsd32_compat_30.c,v 1.4.8.2 2006/05/24 10:57:31 yamt 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.4.8.2 2006/05/24 10:57:31 yamt Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/mount.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/ktrace.h>
43 #include <sys/resourcevar.h>
44 #include <sys/vnode.h>
45 #include <sys/file.h>
46 #include <sys/filedesc.h>
47 #include <sys/namei.h>
48 #include <sys/sa.h>
49 #include <sys/statvfs.h>
50 #include <sys/syscallargs.h>
51 #include <sys/proc.h>
52 #include <sys/dirent.h>
53 #include <sys/kauth.h>
54
55 #include <compat/netbsd32/netbsd32.h>
56 #include <compat/netbsd32/netbsd32_syscallargs.h>
57 #include <compat/netbsd32/netbsd32_conv.h>
58
59
60 int
61 netbsd32_getdents(l, v, retval)
62 struct lwp *l;
63 void *v;
64 register_t *retval;
65 {
66 struct 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 struct proc *p = l->l_proc;
75
76 /* getvnode() will use the descriptor for us */
77 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
78 return (error);
79 if ((fp->f_flag & FREAD) == 0) {
80 error = EBADF;
81 goto out;
82 }
83 buf = malloc(SCARG(uap, count), M_TEMP, M_WAITOK);
84 error = vn_readdir(fp, buf,
85 UIO_SYSSPACE, SCARG(uap, count), &done, l, 0, 0);
86 if (error == 0) {
87 *retval = netbsd32_to_dirent12(buf, done);
88 error = copyout(buf, NETBSD32PTR64(SCARG(uap, buf)), *retval);
89 }
90 free(buf, M_TEMP);
91 out:
92 FILE_UNUSE(fp, l);
93 return (error);
94 }
95
96 int
97 netbsd32___stat13(l, v, retval)
98 struct lwp *l;
99 void *v;
100 register_t *retval;
101 {
102 struct 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 struct nameidata nd;
110 caddr_t sg;
111 const char *path;
112 struct proc *p = l->l_proc;
113
114 path = (char *)NETBSD32PTR64(SCARG(uap, path));
115 sg = stackgap_init(p, 0);
116 CHECK_ALT_EXIST(l, &sg, path);
117
118 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, path, l);
119 if ((error = namei(&nd)) != 0)
120 return (error);
121 error = vn_stat(nd.ni_vp, &sb, l);
122 vput(nd.ni_vp);
123 if (error)
124 return (error);
125 netbsd32_from___stat13(&sb, &sb32);
126 error = copyout(&sb32, (caddr_t)NETBSD32PTR64(SCARG(uap, ub)),
127 sizeof(sb32));
128 return (error);
129 }
130
131 int
132 netbsd32___fstat13(l, v, retval)
133 struct lwp *l;
134 void *v;
135 register_t *retval;
136 {
137 struct netbsd32___fstat13_args /* {
138 syscallarg(int) fd;
139 syscallarg(netbsd32_stat13p_t) sb;
140 } */ *uap = v;
141 int fd = SCARG(uap, fd);
142 struct proc *p = l->l_proc;
143 struct filedesc *fdp = p->p_fd;
144 struct file *fp;
145 struct netbsd32_stat13 sb32;
146 struct stat ub;
147 int error = 0;
148
149 if ((fp = fd_getfile(fdp, fd)) == NULL)
150 return (EBADF);
151
152 FILE_USE(fp);
153 error = (*fp->f_ops->fo_stat)(fp, &ub, l);
154 FILE_UNUSE(fp, l);
155
156 if (error == 0) {
157 netbsd32_from___stat13(&ub, &sb32);
158 error = copyout(&sb32, (caddr_t)NETBSD32PTR64(SCARG(uap, sb)),
159 sizeof(sb32));
160 }
161 return (error);
162 }
163
164 int
165 netbsd32___lstat13(l, v, retval)
166 struct lwp *l;
167 void *v;
168 register_t *retval;
169 {
170 struct netbsd32___lstat13_args /* {
171 syscallarg(const netbsd32_charp) path;
172 syscallarg(netbsd32_stat13p_t) ub;
173 } */ *uap = v;
174 struct netbsd32_stat13 sb32;
175 struct stat sb;
176 int error;
177 struct nameidata nd;
178 caddr_t sg;
179 const char *path;
180 struct proc *p = l->l_proc;
181
182 path = (char *)NETBSD32PTR64(SCARG(uap, path));
183 sg = stackgap_init(p, 0);
184 CHECK_ALT_EXIST(l, &sg, path);
185
186 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, path, l);
187 if ((error = namei(&nd)) != 0)
188 return (error);
189 error = vn_stat(nd.ni_vp, &sb, l);
190 vput(nd.ni_vp);
191 if (error)
192 return (error);
193 netbsd32_from___stat13(&sb, &sb32);
194 error = copyout(&sb32, (caddr_t)NETBSD32PTR64(SCARG(uap, ub)),
195 sizeof(sb32));
196 return (error);
197 }
198
199 int
200 compat_30_netbsd32_fhstat(l, v, retval)
201 struct lwp *l;
202 void *v;
203 register_t *retval;
204 {
205 struct compat_30_netbsd32_fhstat_args /* {
206 syscallarg(const netbsd32_fhandlep_t) fhp;
207 syscallarg(netbsd32_stat13p_t) sb);
208 } */ *uap = v;
209 struct proc *p = l->l_proc;
210 struct stat sb;
211 struct netbsd32_stat13 sb32;
212 int error;
213 fhandle_t fh;
214 struct mount *mp;
215 struct vnode *vp;
216
217 /*
218 * Must be super user
219 */
220 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
221 &p->p_acflag)))
222 return (error);
223
224 if ((error = copyin(NETBSD32PTR64(SCARG(uap, fhp)), &fh,
225 sizeof(fhandle_t))) != 0)
226 return (error);
227
228 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
229 return (ESTALE);
230 if (mp->mnt_op->vfs_fhtovp == NULL)
231 return EOPNOTSUPP;
232 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
233 return (error);
234 error = vn_stat(vp, &sb, l);
235 vput(vp);
236 if (error)
237 return (error);
238 netbsd32_from___stat13(&sb, &sb32);
239 error = copyout(&sb32, NETBSD32PTR64(SCARG(uap, sb)), sizeof(sb));
240 return (error);
241 }
242