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