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