Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_30.c revision 1.18
      1 /*	$NetBSD: netbsd32_compat_30.c,v 1.18 2007/03/10 21:40:25 dsl 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.18 2007/03/10 21:40:25 dsl 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/statvfs.h>
     51 #include <sys/syscallargs.h>
     52 #include <sys/proc.h>
     53 #include <sys/dirent.h>
     54 #include <sys/kauth.h>
     55 #include <sys/vfs_syscalls.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 compat_30_netbsd32_getdents(l, v, retval)
     65 	struct lwp *l;
     66 	void *v;
     67 	register_t *retval;
     68 {
     69 	struct compat_30_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 compat_30_netbsd32___stat13(l, v, retval)
    104 	struct lwp *l;
    105 	void *v;
    106 	register_t *retval;
    107 {
    108 	struct compat_30_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 	void *sg;
    116 	const char *path;
    117 	struct proc *p = l->l_proc;
    118 
    119 	path = (char *)NETBSD32PTR64(SCARG(uap, path));
    120 	sg = stackgap_init(p, 0);
    121 	CHECK_ALT_EXIST(l, &sg, path);
    122 
    123 	error = do_sys_stat(l, path, FOLLOW, &sb);
    124 	if (error)
    125 		return (error);
    126 	netbsd32_from___stat13(&sb, &sb32);
    127 	error = copyout(&sb32, (void *)NETBSD32PTR64(SCARG(uap, ub)),
    128 	    sizeof(sb32));
    129 	return (error);
    130 }
    131 
    132 int
    133 compat_30_netbsd32___fstat13(l, v, retval)
    134 	struct lwp *l;
    135 	void *v;
    136 	register_t *retval;
    137 {
    138 	struct compat_30_netbsd32___fstat13_args /* {
    139 		syscallarg(int) fd;
    140 		syscallarg(netbsd32_stat13p_t) sb;
    141 	} */ *uap = v;
    142 	int fd = SCARG(uap, fd);
    143 	struct proc *p = l->l_proc;
    144 	struct filedesc *fdp = p->p_fd;
    145 	struct file *fp;
    146 	struct netbsd32_stat13 sb32;
    147 	struct stat ub;
    148 	int error = 0;
    149 
    150 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    151 		return (EBADF);
    152 
    153 	FILE_USE(fp);
    154 	error = (*fp->f_ops->fo_stat)(fp, &ub, l);
    155 	FILE_UNUSE(fp, l);
    156 
    157 	if (error == 0) {
    158 		netbsd32_from___stat13(&ub, &sb32);
    159 		error = copyout(&sb32, (void *)NETBSD32PTR64(SCARG(uap, sb)),
    160 		    sizeof(sb32));
    161 	}
    162 	return (error);
    163 }
    164 
    165 int
    166 compat_30_netbsd32___lstat13(l, v, retval)
    167 	struct lwp *l;
    168 	void *v;
    169 	register_t *retval;
    170 {
    171 	struct compat_30_netbsd32___lstat13_args /* {
    172 		syscallarg(const netbsd32_charp) path;
    173 		syscallarg(netbsd32_stat13p_t) ub;
    174 	} */ *uap = v;
    175 	struct netbsd32_stat13 sb32;
    176 	struct stat sb;
    177 	int error;
    178 	void *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 	error = do_sys_stat(l, path, NOFOLLOW, &sb);
    187 	if (error)
    188 		return (error);
    189 	netbsd32_from___stat13(&sb, &sb32);
    190 	error = copyout(&sb32, (void *)NETBSD32PTR64(SCARG(uap, ub)),
    191 	    sizeof(sb32));
    192 	return (error);
    193 }
    194 
    195 int
    196 compat_30_netbsd32_fhstat(l, v, retval)
    197 	struct lwp *l;
    198 	void *v;
    199 	register_t *retval;
    200 {
    201 	struct compat_30_netbsd32_fhstat_args /* {
    202 		syscallarg(const netbsd32_fhandlep_t) fhp;
    203 		syscallarg(netbsd32_stat13p_t) sb);
    204 	} */ *uap = v;
    205 	struct stat sb;
    206 	struct netbsd32_stat13 sb32;
    207 	int error;
    208 	struct compat_30_fhandle fh;
    209 	struct mount *mp;
    210 	struct vnode *vp;
    211 
    212 	/*
    213 	 * Must be super user
    214 	 */
    215 	if ((error = kauth_authorize_system(l->l_cred,
    216 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
    217 		return (error);
    218 
    219 	if ((error = copyin(NETBSD32PTR64(SCARG(uap, fhp)), &fh,
    220 	    sizeof(fh))) != 0)
    221 		return (error);
    222 
    223 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
    224 		return (ESTALE);
    225 	if (mp->mnt_op->vfs_fhtovp == NULL)
    226 		return EOPNOTSUPP;
    227 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
    228 		return (error);
    229 	error = vn_stat(vp, &sb, l);
    230 	vput(vp);
    231 	if (error)
    232 		return (error);
    233 	netbsd32_from___stat13(&sb, &sb32);
    234 	error = copyout(&sb32, NETBSD32PTR64(SCARG(uap, sb)), sizeof(sb));
    235 	return (error);
    236 }
    237 
    238 int
    239 compat_30_netbsd32_fhstatvfs1(l, v, retval)
    240 	struct lwp *l;
    241 	void *v;
    242 	register_t *retval;
    243 {
    244 	struct compat_30_netbsd32_fhstatvfs1_args /* {
    245 		syscallarg(const netbsd32_fhandlep_t) fhp;
    246 		syscallarg(netbsd32_statvfsp_t) buf;
    247 		syscallarg(int) flags;
    248 	} */ *uap = v;
    249 	struct statvfs *sbuf;
    250 	struct netbsd32_statvfs *s32;
    251 	fhandle_t *fh;
    252 	struct vnode *vp;
    253 	int error;
    254 
    255 	/*
    256 	 * Must be super user
    257 	 */
    258 	if ((error = kauth_authorize_system(l->l_cred,
    259 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
    260 		return error;
    261 
    262 	if ((error = vfs_copyinfh_alloc(NETBSD32PTR64(SCARG(uap, fhp)),
    263 	    FHANDLE_SIZE_COMPAT, &fh)) != 0)
    264 		goto bad;
    265 	if ((error = vfs_fhtovp(fh, &vp)) != 0)
    266 		goto bad;
    267 
    268 	sbuf = (struct statvfs *)malloc(sizeof(struct statvfs), M_TEMP,
    269 	    M_WAITOK);
    270 	error = dostatvfs(vp->v_mount, sbuf, l, SCARG(uap, flags), 1);
    271 	vput(vp);
    272 	if (error != 0)
    273 		goto out;
    274 
    275 	s32 = (struct netbsd32_statvfs *)
    276 	    malloc(sizeof(struct netbsd32_statvfs), M_TEMP, M_WAITOK);
    277 	netbsd32_from_statvfs(sbuf, s32);
    278 	error = copyout(s32, (void *)NETBSD32PTR64(SCARG(uap, buf)),
    279 	    sizeof(struct netbsd32_statvfs));
    280 	free(s32, M_TEMP);
    281 
    282 out:
    283 	free(sbuf, M_TEMP);
    284 bad:
    285 	vfs_copyinfh_free(fh);
    286 	return (error);
    287 }
    288 
    289 int
    290 compat_30_netbsd32_socket(l, v, retval)
    291 	struct lwp *l;
    292 	void *v;
    293 	register_t *retval;
    294 {
    295 	struct compat_30_netbsd32_socket_args /* {
    296 		syscallarg(int) domain;
    297 		syscallarg(int) type;
    298 		syscallarg(int) protocol;
    299 	} */ *uap = v;
    300 	struct compat_30_sys_socket_args ua;
    301 
    302 	NETBSD32TO64_UAP(domain);
    303 	NETBSD32TO64_UAP(type);
    304 	NETBSD32TO64_UAP(protocol);
    305 	return (compat_30_sys_socket(l, &ua, retval));
    306 }
    307 
    308 int
    309 compat_30_netbsd32_getfh(l, v, retval)
    310 	struct lwp *l;
    311 	void *v;
    312 	register_t *retval;
    313 {
    314 	struct compat_30_netbsd32_getfh_args /* {
    315 		syscallarg(const netbsd32_charp) fname;
    316 		syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
    317 	} */ *uap = v;
    318 	struct compat_30_sys_getfh_args ua;
    319 
    320 	NETBSD32TOP_UAP(fname, const char);
    321 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
    322 	/* Lucky for us a fhandle_t doesn't change sizes */
    323 	return (compat_30_sys_getfh(l, &ua, retval));
    324 }
    325 
    326 
    327 int compat_30_netbsd32_sys___fhstat30(l, v, retval)
    328 	struct lwp *l;
    329 	void *v;
    330 	register_t *retval;
    331 {
    332 	struct compat_30_netbsd32_sys___fhstat30_args /* {
    333 		syscallarg(const netbsd32_fhandlep_t) fhp;
    334 		syscallarg(netbsd32_statp_t) sb;
    335 	} */ *uap = v;
    336 	struct stat sb;
    337 	struct netbsd32_stat sb32;
    338 	int error;
    339 	fhandle_t *fh;
    340 	struct vnode *vp;
    341 
    342 	/*
    343 	 * Must be super user
    344 	 */
    345 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
    346 	    0, NULL, NULL, NULL)))
    347 		return error;
    348 
    349 	if ((error = vfs_copyinfh_alloc(NETBSD32PTR64(SCARG(uap, fhp)),
    350 	    FHANDLE_SIZE_COMPAT, &fh)) != 0)
    351 		goto bad;
    352 
    353 	if ((error = vfs_fhtovp(fh, &vp)) != 0)
    354 		goto bad;
    355 
    356 	error = vn_stat(vp, &sb, l);
    357 	vput(vp);
    358 	if (error)
    359 		goto bad;
    360 	netbsd32_from___stat30(&sb, &sb32);
    361 	error = copyout(&sb32, NETBSD32PTR64(SCARG(uap, sb)), sizeof(sb));
    362 bad:
    363 	vfs_copyinfh_free(fh);
    364 	return error;
    365 }
    366 
    367 /*
    368  * Open a file given a file handle.
    369  *
    370  * Check permissions, allocate an open file structure,
    371  * and call the device open routine if any.
    372  */
    373 int
    374 compat_30_netbsd32_fhopen(l, v, retval)
    375 	struct lwp *l;
    376 	void *v;
    377 	register_t *retval;
    378 {
    379 	struct compat_30_netbsd32_fhopen_args /* {
    380 		syscallarg(const fhandle_t *) fhp;
    381 		syscallarg(int) flags;
    382 	} */ *uap = v;
    383 	struct compat_30_sys_fhopen_args ua;
    384 
    385 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
    386 	NETBSD32TO64_UAP(flags);
    387 	return (compat_30_sys_fhopen(l, &ua, retval));
    388 }
    389