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