Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_30.c revision 1.23
      1 /*	$NetBSD: netbsd32_compat_30.c,v 1.23 2007/12/20 23:03:01 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.23 2007/12/20 23:03:01 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(struct lwp *l, const struct compat_30_netbsd32_getdents_args *uap, register_t *retval)
     65 {
     66 	/* {
     67 		syscallarg(int) fd;
     68 		syscallarg(netbsd32_charp) buf;
     69 		syscallarg(netbsd32_size_t) count;
     70 	} */
     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, SCARG_P32(uap, buf), *retval);
     92 	}
     93 	free(buf, M_TEMP);
     94  out:
     95 	FILE_UNUSE(fp, l);
     96 	return (error);
     97 }
     98 
     99 int
    100 compat_30_netbsd32___stat13(struct lwp *l, const struct compat_30_netbsd32___stat13_args *uap, register_t *retval)
    101 {
    102 	/* {
    103 		syscallarg(const netbsd32_charp) path;
    104 		syscallarg(netbsd32_stat13p_t) ub;
    105 	} */
    106 	struct netbsd32_stat13 sb32;
    107 	struct stat sb;
    108 	int error;
    109 	const char *path;
    110 
    111 	path = SCARG_P32(uap, path);
    112 
    113 	error = do_sys_stat(l, path, FOLLOW, &sb);
    114 	if (error)
    115 		return (error);
    116 	netbsd32_from___stat13(&sb, &sb32);
    117 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    118 	return (error);
    119 }
    120 
    121 int
    122 compat_30_netbsd32___fstat13(struct lwp *l, const struct compat_30_netbsd32___fstat13_args *uap, register_t *retval)
    123 {
    124 	/* {
    125 		syscallarg(int) fd;
    126 		syscallarg(netbsd32_stat13p_t) sb;
    127 	} */
    128 	int fd = SCARG(uap, fd);
    129 	struct proc *p = l->l_proc;
    130 	struct filedesc *fdp = p->p_fd;
    131 	struct file *fp;
    132 	struct netbsd32_stat13 sb32;
    133 	struct stat ub;
    134 	int error = 0;
    135 
    136 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    137 		return (EBADF);
    138 
    139 	FILE_USE(fp);
    140 	error = (*fp->f_ops->fo_stat)(fp, &ub, l);
    141 	FILE_UNUSE(fp, l);
    142 
    143 	if (error == 0) {
    144 		netbsd32_from___stat13(&ub, &sb32);
    145 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    146 	}
    147 	return (error);
    148 }
    149 
    150 int
    151 compat_30_netbsd32___lstat13(struct lwp *l, const struct compat_30_netbsd32___lstat13_args *uap, register_t *retval)
    152 {
    153 	/* {
    154 		syscallarg(const netbsd32_charp) path;
    155 		syscallarg(netbsd32_stat13p_t) ub;
    156 	} */
    157 	struct netbsd32_stat13 sb32;
    158 	struct stat sb;
    159 	int error;
    160 	const char *path;
    161 
    162 	path = SCARG_P32(uap, path);
    163 
    164 	error = do_sys_stat(l, path, NOFOLLOW, &sb);
    165 	if (error)
    166 		return (error);
    167 	netbsd32_from___stat13(&sb, &sb32);
    168 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    169 	return (error);
    170 }
    171 
    172 int
    173 compat_30_netbsd32_fhstat(struct lwp *l, const struct compat_30_netbsd32_fhstat_args *uap, register_t *retval)
    174 {
    175 	/* {
    176 		syscallarg(const netbsd32_fhandlep_t) fhp;
    177 		syscallarg(netbsd32_stat13p_t) sb;
    178 	} */
    179 	struct stat sb;
    180 	struct netbsd32_stat13 sb32;
    181 	int error;
    182 	struct compat_30_fhandle fh;
    183 	struct mount *mp;
    184 	struct vnode *vp;
    185 
    186 	/*
    187 	 * Must be super user
    188 	 */
    189 	if ((error = kauth_authorize_system(l->l_cred,
    190 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
    191 		return (error);
    192 
    193 	if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
    194 		return (error);
    195 
    196 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
    197 		return (ESTALE);
    198 	if (mp->mnt_op->vfs_fhtovp == NULL)
    199 		return EOPNOTSUPP;
    200 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
    201 		return (error);
    202 	error = vn_stat(vp, &sb, l);
    203 	vput(vp);
    204 	if (error)
    205 		return (error);
    206 	netbsd32_from___stat13(&sb, &sb32);
    207 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
    208 	return (error);
    209 }
    210 
    211 int
    212 compat_30_netbsd32_fhstatvfs1(struct lwp *l, const struct compat_30_netbsd32_fhstatvfs1_args *uap, register_t *retval)
    213 {
    214 	/* {
    215 		syscallarg(const netbsd32_fhandlep_t) fhp;
    216 		syscallarg(netbsd32_statvfsp_t) buf;
    217 		syscallarg(int) flags;
    218 	} */
    219 	struct statvfs *sbuf;
    220 	struct netbsd32_statvfs *s32;
    221 	int error;
    222 
    223 	sbuf = STATVFSBUF_GET();
    224 	error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
    225 	    SCARG(uap, flags));
    226 
    227 	if (error != 0) {
    228 		s32 = malloc(sizeof *s32, M_TEMP, M_WAITOK);
    229 		netbsd32_from_statvfs(sbuf, s32);
    230 		error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
    231 		free(s32, M_TEMP);
    232 	}
    233 	STATVFSBUF_PUT(sbuf);
    234 
    235 	return (error);
    236 }
    237 
    238 int
    239 compat_30_netbsd32_socket(struct lwp *l, const struct compat_30_netbsd32_socket_args *uap, register_t *retval)
    240 {
    241 	/* {
    242 		syscallarg(int) domain;
    243 		syscallarg(int) type;
    244 		syscallarg(int) protocol;
    245 	} */
    246 	struct compat_30_sys_socket_args ua;
    247 
    248 	NETBSD32TO64_UAP(domain);
    249 	NETBSD32TO64_UAP(type);
    250 	NETBSD32TO64_UAP(protocol);
    251 	return (compat_30_sys_socket(l, &ua, retval));
    252 }
    253 
    254 int
    255 compat_30_netbsd32_getfh(struct lwp *l, const struct compat_30_netbsd32_getfh_args *uap, register_t *retval)
    256 {
    257 	/* {
    258 		syscallarg(const netbsd32_charp) fname;
    259 		syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
    260 	} */
    261 	struct compat_30_sys_getfh_args ua;
    262 
    263 	NETBSD32TOP_UAP(fname, const char);
    264 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
    265 	/* Lucky for us a fhandle_t doesn't change sizes */
    266 	return (compat_30_sys_getfh(l, &ua, retval));
    267 }
    268 
    269 
    270 int
    271 compat_30_netbsd32_sys___fhstat30(struct lwp *l, const struct compat_30_netbsd32_sys___fhstat30_args *uap, register_t *retval)
    272 {
    273 	/* {
    274 		syscallarg(const netbsd32_fhandlep_t) fhp;
    275 		syscallarg(netbsd32_statp_t) sb;
    276 	} */
    277 	struct stat sb;
    278 	struct netbsd32_stat sb32;
    279 	int error;
    280 
    281 	error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
    282 	if (error)
    283 		return error;
    284 
    285 	netbsd32_from___stat30(&sb, &sb32);
    286 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    287 	return error;
    288 }
    289 
    290 /*
    291  * Open a file given a file handle.
    292  *
    293  * Check permissions, allocate an open file structure,
    294  * and call the device open routine if any.
    295  */
    296 int
    297 compat_30_netbsd32_fhopen(struct lwp *l, const struct compat_30_netbsd32_fhopen_args *uap, register_t *retval)
    298 {
    299 	/* {
    300 		syscallarg(const fhandle_t *) fhp;
    301 		syscallarg(int) flags;
    302 	} */
    303 	struct compat_30_sys_fhopen_args ua;
    304 
    305 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
    306 	NETBSD32TO64_UAP(flags);
    307 	return (compat_30_sys_fhopen(l, &ua, retval));
    308 }
    309