Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_30.c revision 1.31.16.7
      1 /*	$NetBSD: netbsd32_compat_30.c,v 1.31.16.7 2018/09/11 21:18:32 pgoyette 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_30.c,v 1.31.16.7 2018/09/11 21:18:32 pgoyette Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/module.h>
     35 #include <sys/mount.h>
     36 #include <sys/mount.h>
     37 #include <sys/socket.h>
     38 #include <sys/socketvar.h>
     39 #include <sys/stat.h>
     40 #include <sys/time.h>
     41 #include <sys/ktrace.h>
     42 #include <sys/resourcevar.h>
     43 #include <sys/vnode.h>
     44 #include <sys/file.h>
     45 #include <sys/filedesc.h>
     46 #include <sys/namei.h>
     47 #include <sys/statvfs.h>
     48 #include <sys/syscallargs.h>
     49 #include <sys/syscallvar.h>
     50 #include <sys/proc.h>
     51 #include <sys/dirent.h>
     52 #include <sys/kauth.h>
     53 #include <sys/vfs_syscalls.h>
     54 
     55 #include <compat/netbsd32/netbsd32.h>
     56 #include <compat/netbsd32/netbsd32_syscall.h>
     57 #include <compat/netbsd32/netbsd32_syscallargs.h>
     58 #include <compat/netbsd32/netbsd32_conv.h>
     59 #include <compat/sys/mount.h>
     60 
     61 int
     62 compat_30_netbsd32_getdents(struct lwp *l, const struct compat_30_netbsd32_getdents_args *uap, register_t *retval)
     63 {
     64 	/* {
     65 		syscallarg(int) fd;
     66 		syscallarg(netbsd32_charp) buf;
     67 		syscallarg(netbsd32_size_t) count;
     68 	} */
     69 	file_t *fp;
     70 	int error, done;
     71 	char  *buf;
     72 	netbsd32_size_t count;
     73 
     74 	/* Limit the size on any kernel buffers used by VOP_READDIR */
     75 	count = uimin(MAXBSIZE, SCARG(uap, count));
     76 
     77 	/* fd_getvnode() will use the descriptor for us */
     78 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
     79 		return (error);
     80 	if ((fp->f_flag & FREAD) == 0) {
     81 		error = EBADF;
     82 		goto out;
     83 	}
     84 	if (count == 0)
     85 		goto out;
     86 
     87 	buf = kmem_alloc(count, KM_SLEEP);
     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 	kmem_free(buf, count);
     94  out:
     95  	fd_putfile(SCARG(uap, fd));
     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(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 	struct netbsd32_stat13 sb32;
    129 	struct stat ub;
    130 	int error;
    131 
    132 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    133 	if (error == 0) {
    134 		netbsd32_from___stat13(&ub, &sb32);
    135 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    136 	}
    137 	return (error);
    138 }
    139 
    140 int
    141 compat_30_netbsd32___lstat13(struct lwp *l, const struct compat_30_netbsd32___lstat13_args *uap, register_t *retval)
    142 {
    143 	/* {
    144 		syscallarg(const netbsd32_charp) path;
    145 		syscallarg(netbsd32_stat13p_t) ub;
    146 	} */
    147 	struct netbsd32_stat13 sb32;
    148 	struct stat sb;
    149 	int error;
    150 	const char *path;
    151 
    152 	path = SCARG_P32(uap, path);
    153 
    154 	error = do_sys_stat(path, NOFOLLOW, &sb);
    155 	if (error)
    156 		return (error);
    157 	netbsd32_from___stat13(&sb, &sb32);
    158 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    159 	return (error);
    160 }
    161 
    162 int
    163 compat_30_netbsd32_fhstat(struct lwp *l, const struct compat_30_netbsd32_fhstat_args *uap, register_t *retval)
    164 {
    165 	/* {
    166 		syscallarg(const netbsd32_fhandlep_t) fhp;
    167 		syscallarg(netbsd32_stat13p_t) sb;
    168 	} */
    169 	struct stat sb;
    170 	struct netbsd32_stat13 sb32;
    171 	int error;
    172 	struct compat_30_fhandle fh;
    173 	struct mount *mp;
    174 	struct vnode *vp;
    175 
    176 	/*
    177 	 * Must be super user
    178 	 */
    179 	if ((error = kauth_authorize_system(l->l_cred,
    180 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
    181 		return (error);
    182 
    183 	if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
    184 		return (error);
    185 
    186 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
    187 		return (ESTALE);
    188 	if (mp->mnt_op->vfs_fhtovp == NULL)
    189 		return EOPNOTSUPP;
    190 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
    191 		return (error);
    192 	error = vn_stat(vp, &sb);
    193 	vput(vp);
    194 	if (error)
    195 		return (error);
    196 	netbsd32_from___stat13(&sb, &sb32);
    197 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
    198 	return (error);
    199 }
    200 
    201 int
    202 compat_30_netbsd32_fhstatvfs1(struct lwp *l, const struct compat_30_netbsd32_fhstatvfs1_args *uap, register_t *retval)
    203 {
    204 	/* {
    205 		syscallarg(const netbsd32_fhandlep_t) fhp;
    206 		syscallarg(netbsd32_statvfsp_t) buf;
    207 		syscallarg(int) flags;
    208 	} */
    209 	struct statvfs *sbuf;
    210 	struct netbsd32_statvfs *s32;
    211 	int error;
    212 
    213 	sbuf = STATVFSBUF_GET();
    214 	error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
    215 	    SCARG(uap, flags));
    216 
    217 	if (error != 0) {
    218 		s32 = kmem_alloc(sizeof(*s32), KM_SLEEP);
    219 		netbsd32_from_statvfs(sbuf, s32);
    220 		error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
    221 		kmem_free(s32, sizeof(*s32));
    222 	}
    223 	STATVFSBUF_PUT(sbuf);
    224 
    225 	return (error);
    226 }
    227 
    228 int
    229 compat_30_netbsd32_socket(struct lwp *l, const struct compat_30_netbsd32_socket_args *uap, register_t *retval)
    230 {
    231 	/* {
    232 		syscallarg(int) domain;
    233 		syscallarg(int) type;
    234 		syscallarg(int) protocol;
    235 	} */
    236 	struct compat_30_sys_socket_args ua;
    237 
    238 	NETBSD32TO64_UAP(domain);
    239 	NETBSD32TO64_UAP(type);
    240 	NETBSD32TO64_UAP(protocol);
    241 	return (compat_30_sys_socket(l, &ua, retval));
    242 }
    243 
    244 int
    245 compat_30_netbsd32_getfh(struct lwp *l, const struct compat_30_netbsd32_getfh_args *uap, register_t *retval)
    246 {
    247 	/* {
    248 		syscallarg(const netbsd32_charp) fname;
    249 		syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
    250 	} */
    251 	struct compat_30_sys_getfh_args ua;
    252 
    253 	NETBSD32TOP_UAP(fname, const char);
    254 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
    255 	/* Lucky for us a fhandle_t doesn't change sizes */
    256 	return (compat_30_sys_getfh(l, &ua, retval));
    257 }
    258 
    259 
    260 int
    261 compat_30_netbsd32___fhstat30(struct lwp *l, const struct compat_30_netbsd32___fhstat30_args *uap, register_t *retval)
    262 {
    263 	/* {
    264 		syscallarg(const netbsd32_fhandlep_t) fhp;
    265 		syscallarg(netbsd32_statp_t) sb;
    266 	} */
    267 	struct stat sb;
    268 	struct netbsd32_stat50 sb32;
    269 	int error;
    270 
    271 	error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
    272 	if (error)
    273 		return error;
    274 
    275 	netbsd32_from___stat50(&sb, &sb32);
    276 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    277 	return error;
    278 }
    279 
    280 /*
    281  * Open a file given a file handle.
    282  *
    283  * Check permissions, allocate an open file structure,
    284  * and call the device open routine if any.
    285  */
    286 int
    287 compat_30_netbsd32_fhopen(struct lwp *l, const struct compat_30_netbsd32_fhopen_args *uap, register_t *retval)
    288 {
    289 	/* {
    290 		syscallarg(const fhandle_t *) fhp;
    291 		syscallarg(int) flags;
    292 	} */
    293 	struct compat_30_sys_fhopen_args ua;
    294 
    295 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
    296 	NETBSD32TO64_UAP(flags);
    297 	return (compat_30_sys_fhopen(l, &ua, retval));
    298 }
    299 
    300 static struct syscall_package compat_netbsd32_30_syscalls[] = {
    301 	{ NETBSD32_SYS_compat_30_netbsd32_getdents, 0,
    302 	    (sy_call_t *)compat_30_netbsd32_getdents },
    303 	{ NETBSD32_SYS_compat_30_netbsd32___stat13, 0,
    304 	    (sy_call_t *)compat_30_netbsd32___stat13 },
    305 	{ NETBSD32_SYS_compat_30_netbsd32___fstat13, 0,
    306 	    (sy_call_t *)compat_30_netbsd32___fstat13 },
    307 	{ NETBSD32_SYS_compat_30_netbsd32___lstat13, 0,
    308 	    (sy_call_t *)compat_30_netbsd32___lstat13 },
    309 	{ NETBSD32_SYS_compat_30_netbsd32_fhstat, 0,
    310 	    (sy_call_t *)compat_30_netbsd32_fhstat },
    311 	{ NETBSD32_SYS_compat_30_netbsd32_fhstatvfs1, 0,
    312 	    (sy_call_t *)compat_30_netbsd32_fhstatvfs1 },
    313 	{ NETBSD32_SYS_compat_30_netbsd32_socket, 0,
    314 	    (sy_call_t *)compat_30_netbsd32_socket },
    315 	{ NETBSD32_SYS_compat_30_netbsd32_getfh, 0,
    316 	    (sy_call_t *)compat_30_netbsd32_getfh },
    317 	{ NETBSD32_SYS_compat_30_netbsd32___fhstat30, 0,
    318 	    (sy_call_t *)compat_30_netbsd32___fhstat30 },
    319 	{ NETBSD32_SYS_compat_30_netbsd32_fhopen, 0,
    320 	    (sy_call_t *)compat_30_netbsd32_fhopen },
    321 	{ 0, 0, NULL }
    322 };
    323 
    324 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_30, "compat_netbsd32,compat_30");
    325 
    326 static int
    327 compat_netbsd32_30_modcmd(modcmd_t cmd, void *arg)
    328 {
    329 
    330 	switch (cmd) {
    331 	case MODULE_CMD_INIT:
    332 		return syscall_establish(&emul_netbsd32,
    333 		    compat_netbsd32_30_syscalls);
    334 
    335 	case MODULE_CMD_FINI:
    336 		return syscall_disestablish(&emul_netbsd32,
    337 		    compat_netbsd32_30_syscalls);
    338 
    339 	default:
    340 		return ENOTTY;
    341 	}
    342 }
    343