1 /* $NetBSD: netbsd32_compat_30.c,v 1.8 2006/05/27 23:46:49 simonb 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.8 2006/05/27 23:46:49 simonb 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 netbsd32_size_t count; 75 struct proc *p = l->l_proc; 76 77 /* Limit the size on any kernel buffers used by VOP_READDIR */ 78 count = min(MAXBSIZE, SCARG(uap, count)); 79 80 /* getvnode() will use the descriptor for us */ 81 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) 82 return (error); 83 if ((fp->f_flag & FREAD) == 0) { 84 error = EBADF; 85 goto out; 86 } 87 buf = malloc(count, M_TEMP, M_WAITOK); 88 error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0); 89 if (error == 0) { 90 *retval = netbsd32_to_dirent12(buf, done); 91 error = copyout(buf, NETBSD32PTR64(SCARG(uap, buf)), *retval); 92 } 93 free(buf, M_TEMP); 94 out: 95 FILE_UNUSE(fp, l); 96 return (error); 97 } 98 99 int 100 netbsd32___stat13(l, v, retval) 101 struct lwp *l; 102 void *v; 103 register_t *retval; 104 { 105 struct netbsd32___stat13_args /* { 106 syscallarg(const netbsd32_charp) path; 107 syscallarg(netbsd32_stat13p_t) ub; 108 } */ *uap = v; 109 struct netbsd32_stat13 sb32; 110 struct stat sb; 111 int error; 112 struct nameidata nd; 113 caddr_t sg; 114 const char *path; 115 struct proc *p = l->l_proc; 116 117 path = (char *)NETBSD32PTR64(SCARG(uap, path)); 118 sg = stackgap_init(p, 0); 119 CHECK_ALT_EXIST(l, &sg, path); 120 121 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, path, l); 122 if ((error = namei(&nd)) != 0) 123 return (error); 124 error = vn_stat(nd.ni_vp, &sb, l); 125 vput(nd.ni_vp); 126 if (error) 127 return (error); 128 netbsd32_from___stat13(&sb, &sb32); 129 error = copyout(&sb32, (caddr_t)NETBSD32PTR64(SCARG(uap, ub)), 130 sizeof(sb32)); 131 return (error); 132 } 133 134 int 135 netbsd32___fstat13(l, v, retval) 136 struct lwp *l; 137 void *v; 138 register_t *retval; 139 { 140 struct netbsd32___fstat13_args /* { 141 syscallarg(int) fd; 142 syscallarg(netbsd32_stat13p_t) sb; 143 } */ *uap = v; 144 int fd = SCARG(uap, fd); 145 struct proc *p = l->l_proc; 146 struct filedesc *fdp = p->p_fd; 147 struct file *fp; 148 struct netbsd32_stat13 sb32; 149 struct stat ub; 150 int error = 0; 151 152 if ((fp = fd_getfile(fdp, fd)) == NULL) 153 return (EBADF); 154 155 FILE_USE(fp); 156 error = (*fp->f_ops->fo_stat)(fp, &ub, l); 157 FILE_UNUSE(fp, l); 158 159 if (error == 0) { 160 netbsd32_from___stat13(&ub, &sb32); 161 error = copyout(&sb32, (caddr_t)NETBSD32PTR64(SCARG(uap, sb)), 162 sizeof(sb32)); 163 } 164 return (error); 165 } 166 167 int 168 netbsd32___lstat13(l, v, retval) 169 struct lwp *l; 170 void *v; 171 register_t *retval; 172 { 173 struct netbsd32___lstat13_args /* { 174 syscallarg(const netbsd32_charp) path; 175 syscallarg(netbsd32_stat13p_t) ub; 176 } */ *uap = v; 177 struct netbsd32_stat13 sb32; 178 struct stat sb; 179 int error; 180 struct nameidata nd; 181 caddr_t sg; 182 const char *path; 183 struct proc *p = l->l_proc; 184 185 path = (char *)NETBSD32PTR64(SCARG(uap, path)); 186 sg = stackgap_init(p, 0); 187 CHECK_ALT_EXIST(l, &sg, path); 188 189 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, path, l); 190 if ((error = namei(&nd)) != 0) 191 return (error); 192 error = vn_stat(nd.ni_vp, &sb, l); 193 vput(nd.ni_vp); 194 if (error) 195 return (error); 196 netbsd32_from___stat13(&sb, &sb32); 197 error = copyout(&sb32, (caddr_t)NETBSD32PTR64(SCARG(uap, ub)), 198 sizeof(sb32)); 199 return (error); 200 } 201 202 int 203 compat_30_netbsd32_fhstat(l, v, retval) 204 struct lwp *l; 205 void *v; 206 register_t *retval; 207 { 208 struct compat_30_netbsd32_fhstat_args /* { 209 syscallarg(const netbsd32_fhandlep_t) fhp; 210 syscallarg(netbsd32_stat13p_t) sb); 211 } */ *uap = v; 212 struct proc *p = l->l_proc; 213 struct stat sb; 214 struct netbsd32_stat13 sb32; 215 int error; 216 fhandle_t fh; 217 struct mount *mp; 218 struct vnode *vp; 219 220 /* 221 * Must be super user 222 */ 223 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, 224 &p->p_acflag))) 225 return (error); 226 227 if ((error = copyin(NETBSD32PTR64(SCARG(uap, fhp)), &fh, 228 sizeof(fhandle_t))) != 0) 229 return (error); 230 231 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) 232 return (ESTALE); 233 if (mp->mnt_op->vfs_fhtovp == NULL) 234 return EOPNOTSUPP; 235 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp))) 236 return (error); 237 error = vn_stat(vp, &sb, l); 238 vput(vp); 239 if (error) 240 return (error); 241 netbsd32_from___stat13(&sb, &sb32); 242 error = copyout(&sb32, NETBSD32PTR64(SCARG(uap, sb)), sizeof(sb)); 243 return (error); 244 } 245