Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_compat_30.c revision 1.31.16.5
      1 /*	$NetBSD: netbsd32_compat_30.c,v 1.31.16.5 2018/09/11 04:53:42 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.5 2018/09/11 04:53:42 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 
     62 int
     63 compat_30_netbsd32_getdents(struct lwp *l, const struct compat_30_netbsd32_getdents_args *uap, register_t *retval)
     64 {
     65 	/* {
     66 		syscallarg(int) fd;
     67 		syscallarg(netbsd32_charp) buf;
     68 		syscallarg(netbsd32_size_t) count;
     69 	} */
     70 	file_t *fp;
     71 	int error, done;
     72 	char  *buf;
     73 	netbsd32_size_t count;
     74 
     75 	/* Limit the size on any kernel buffers used by VOP_READDIR */
     76 	count = uimin(MAXBSIZE, SCARG(uap, count));
     77 
     78 	/* fd_getvnode() will use the descriptor for us */
     79 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
     80 		return (error);
     81 	if ((fp->f_flag & FREAD) == 0) {
     82 		error = EBADF;
     83 		goto out;
     84 	}
     85 	if (count == 0)
     86 		goto out;
     87 
     88 	buf = kmem_alloc(count, KM_SLEEP);
     89 	error = vn_readdir(fp, buf, UIO_SYSSPACE, count, &done, l, 0, 0);
     90 	if (error == 0) {
     91 		*retval = netbsd32_to_dirent12(buf, done);
     92 		error = copyout(buf, SCARG_P32(uap, buf), *retval);
     93 	}
     94 	kmem_free(buf, count);
     95  out:
     96  	fd_putfile(SCARG(uap, fd));
     97 	return (error);
     98 }
     99 
    100 int
    101 compat_30_netbsd32___stat13(struct lwp *l, const struct compat_30_netbsd32___stat13_args *uap, register_t *retval)
    102 {
    103 	/* {
    104 		syscallarg(const netbsd32_charp) path;
    105 		syscallarg(netbsd32_stat13p_t) ub;
    106 	} */
    107 	struct netbsd32_stat13 sb32;
    108 	struct stat sb;
    109 	int error;
    110 	const char *path;
    111 
    112 	path = SCARG_P32(uap, path);
    113 
    114 	error = do_sys_stat(path, FOLLOW, &sb);
    115 	if (error)
    116 		return (error);
    117 	netbsd32_from___stat13(&sb, &sb32);
    118 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    119 	return (error);
    120 }
    121 
    122 int
    123 compat_30_netbsd32___fstat13(struct lwp *l, const struct compat_30_netbsd32___fstat13_args *uap, register_t *retval)
    124 {
    125 	/* {
    126 		syscallarg(int) fd;
    127 		syscallarg(netbsd32_stat13p_t) sb;
    128 	} */
    129 	struct netbsd32_stat13 sb32;
    130 	struct stat ub;
    131 	int error;
    132 
    133 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    134 	if (error == 0) {
    135 		netbsd32_from___stat13(&ub, &sb32);
    136 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    137 	}
    138 	return (error);
    139 }
    140 
    141 int
    142 compat_30_netbsd32___lstat13(struct lwp *l, const struct compat_30_netbsd32___lstat13_args *uap, register_t *retval)
    143 {
    144 	/* {
    145 		syscallarg(const netbsd32_charp) path;
    146 		syscallarg(netbsd32_stat13p_t) ub;
    147 	} */
    148 	struct netbsd32_stat13 sb32;
    149 	struct stat sb;
    150 	int error;
    151 	const char *path;
    152 
    153 	path = SCARG_P32(uap, path);
    154 
    155 	error = do_sys_stat(path, NOFOLLOW, &sb);
    156 	if (error)
    157 		return (error);
    158 	netbsd32_from___stat13(&sb, &sb32);
    159 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    160 	return (error);
    161 }
    162 
    163 int
    164 compat_30_netbsd32_fhstat(struct lwp *l, const struct compat_30_netbsd32_fhstat_args *uap, register_t *retval)
    165 {
    166 	/* {
    167 		syscallarg(const netbsd32_fhandlep_t) fhp;
    168 		syscallarg(netbsd32_stat13p_t) sb;
    169 	} */
    170 	struct stat sb;
    171 	struct netbsd32_stat13 sb32;
    172 	int error;
    173 	struct compat_30_fhandle fh;
    174 	struct mount *mp;
    175 	struct vnode *vp;
    176 
    177 	/*
    178 	 * Must be super user
    179 	 */
    180 	if ((error = kauth_authorize_system(l->l_cred,
    181 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)))
    182 		return (error);
    183 
    184 	if ((error = copyin(SCARG_P32(uap, fhp), &fh, sizeof(fh))) != 0)
    185 		return (error);
    186 
    187 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
    188 		return (ESTALE);
    189 	if (mp->mnt_op->vfs_fhtovp == NULL)
    190 		return EOPNOTSUPP;
    191 	if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
    192 		return (error);
    193 	error = vn_stat(vp, &sb);
    194 	vput(vp);
    195 	if (error)
    196 		return (error);
    197 	netbsd32_from___stat13(&sb, &sb32);
    198 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
    199 	return (error);
    200 }
    201 
    202 int
    203 compat_30_netbsd32_fhstatvfs1(struct lwp *l, const struct compat_30_netbsd32_fhstatvfs1_args *uap, register_t *retval)
    204 {
    205 	/* {
    206 		syscallarg(const netbsd32_fhandlep_t) fhp;
    207 		syscallarg(netbsd32_statvfsp_t) buf;
    208 		syscallarg(int) flags;
    209 	} */
    210 	struct statvfs *sbuf;
    211 	struct netbsd32_statvfs *s32;
    212 	int error;
    213 
    214 	sbuf = STATVFSBUF_GET();
    215 	error = do_fhstatvfs(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, sbuf,
    216 	    SCARG(uap, flags));
    217 
    218 	if (error != 0) {
    219 		s32 = kmem_alloc(sizeof(*s32), KM_SLEEP);
    220 		netbsd32_from_statvfs(sbuf, s32);
    221 		error = copyout(s32, SCARG_P32(uap, buf), sizeof *s32);
    222 		kmem_free(s32, sizeof(*s32));
    223 	}
    224 	STATVFSBUF_PUT(sbuf);
    225 
    226 	return (error);
    227 }
    228 
    229 int
    230 compat_30_netbsd32_socket(struct lwp *l, const struct compat_30_netbsd32_socket_args *uap, register_t *retval)
    231 {
    232 	/* {
    233 		syscallarg(int) domain;
    234 		syscallarg(int) type;
    235 		syscallarg(int) protocol;
    236 	} */
    237 	struct compat_30_sys_socket_args ua;
    238 
    239 	NETBSD32TO64_UAP(domain);
    240 	NETBSD32TO64_UAP(type);
    241 	NETBSD32TO64_UAP(protocol);
    242 	return (compat_30_sys_socket(l, &ua, retval));
    243 }
    244 
    245 int
    246 compat_30_netbsd32_getfh(struct lwp *l, const struct compat_30_netbsd32_getfh_args *uap, register_t *retval)
    247 {
    248 	/* {
    249 		syscallarg(const netbsd32_charp) fname;
    250 		syscallarg(netbsd32_compat_30_fhandlep_t) fhp;
    251 	} */
    252 	struct compat_30_sys_getfh_args ua;
    253 
    254 	NETBSD32TOP_UAP(fname, const char);
    255 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
    256 	/* Lucky for us a fhandle_t doesn't change sizes */
    257 	return (compat_30_sys_getfh(l, &ua, retval));
    258 }
    259 
    260 
    261 int
    262 compat_30_netbsd32___fhstat30(struct lwp *l, const struct compat_30_netbsd32___fhstat30_args *uap, register_t *retval)
    263 {
    264 	/* {
    265 		syscallarg(const netbsd32_fhandlep_t) fhp;
    266 		syscallarg(netbsd32_statp_t) sb;
    267 	} */
    268 	struct stat sb;
    269 	struct netbsd32_stat50 sb32;
    270 	int error;
    271 
    272 	error = do_fhstat(l, SCARG_P32(uap, fhp), FHANDLE_SIZE_COMPAT, &sb);
    273 	if (error)
    274 		return error;
    275 
    276 	netbsd32_from___stat50(&sb, &sb32);
    277 	error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    278 	return error;
    279 }
    280 
    281 /*
    282  * Open a file given a file handle.
    283  *
    284  * Check permissions, allocate an open file structure,
    285  * and call the device open routine if any.
    286  */
    287 int
    288 compat_30_netbsd32_fhopen(struct lwp *l, const struct compat_30_netbsd32_fhopen_args *uap, register_t *retval)
    289 {
    290 	/* {
    291 		syscallarg(const fhandle_t *) fhp;
    292 		syscallarg(int) flags;
    293 	} */
    294 	struct compat_30_sys_fhopen_args ua;
    295 
    296 	NETBSD32TOP_UAP(fhp, struct compat_30_fhandle);
    297 	NETBSD32TO64_UAP(flags);
    298 	return (compat_30_sys_fhopen(l, &ua, retval));
    299 }
    300 
    301 static struct syscall_package compat_netbsd32_30_syscalls[] = {
    302 	{ NETBSD32_SYS_compat_30_netbsd32_getdents, 0,
    303 	    (sy_call_t *)compat_30_netbsd32_getdents },
    304 	{ NETBSD32_SYS_compat_30_netbsd32___stat13, 0,
    305 	    (sy_call_t *)compat_30_netbsd32___stat13 },
    306 	{ NETBSD32_SYS_compat_30_netbsd32___fstat13, 0,
    307 	    (sy_call_t *)compat_30_netbsd32___fstat13 },
    308 	{ NETBSD32_SYS_compat_30_netbsd32___lstat13, 0,
    309 	    (sy_call_t *)compat_30_netbsd32___lstat13 },
    310 	{ NETBSD32_SYS_compat_30_netbsd32_fhstat, 0,
    311 	    (sy_call_t *)compat_30_netbsd32_fhstat },
    312 	{ NETBSD32_SYS_compat_30_netbsd32_fhstatvfs1, 0,
    313 	    (sy_call_t *)compat_30_netbsd32_fhstatvfs1 },
    314 	{ NETBSD32_SYS_compat_30_netbsd32_socket, 0,
    315 	    (sy_call_t *)compat_30_netbsd32_socket },
    316 	{ NETBSD32_SYS_compat_30_netbsd32_getfh, 0,
    317 	    (sy_call_t *)compat_30_netbsd32_getfh },
    318 	{ NETBSD32_SYS_compat_30_netbsd32___fhstat30, 0,
    319 	    (sy_call_t *)compat_30_netbsd32___fhstat30 },
    320 	{ NETBSD32_SYS_compat_30_netbsd32_fhopen, 0,
    321 	    (sy_call_t *)compat_30_netbsd32_fhopen },
    322 	{ 0, 0, NULL }
    323 };
    324 
    325 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_30, "compat_netbsd32,compat_30");
    326 
    327 static int
    328 compat_netbsd32_30_modcmd(modcmd_t cmd, void *arg)
    329 {
    330 
    331 	switch (cmd) {
    332 	case MODULE_CMD_INIT:
    333 		return syscall_establish(&emul_netbsd32,
    334 		    compat_netbsd32_30_syscalls);
    335 
    336 	case MODULE_CMD_FINI:
    337 		return syscall_disestablish(&emul_netbsd32,
    338 		    compat_netbsd32_30_syscalls);
    339 
    340 	default:
    341 		return ENOTTY;
    342 	}
    343 }
    344