Home | History | Annotate | Line # | Download | only in sunos32
sunos32_misc.c revision 1.42
      1 /*	$NetBSD: sunos32_misc.c,v 1.42 2006/11/14 13:34:30 elad Exp $	*/
      2 /* from :NetBSD: sunos_misc.c,v 1.107 2000/12/01 19:25:10 jdolecek Exp	*/
      3 
      4 /*
      5  * Copyright (c) 2001 Matthew R. Green
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1992, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * This software was developed by the Computer Systems Engineering group
     37  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     38  * contributed to Berkeley.
     39  *
     40  * All advertising materials mentioning features or use of this software
     41  * must display the following acknowledgement:
     42  *	This product includes software developed by the University of
     43  *	California, Lawrence Berkeley Laboratory.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. Neither the name of the University nor the names of its contributors
     54  *    may be used to endorse or promote products derived from this software
     55  *    without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  *
     69  *	@(#)sunos_misc.c	8.1 (Berkeley) 6/18/93
     70  *
     71  *	Header: sunos_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
     72  */
     73 
     74 /*
     75  * SunOS compatibility module, 64-bit kernel version
     76  *
     77  * SunOS system calls that are implemented differently in BSD are
     78  * handled here.
     79  */
     80 
     81 #include <sys/cdefs.h>
     82 __KERNEL_RCSID(0, "$NetBSD: sunos32_misc.c,v 1.42 2006/11/14 13:34:30 elad Exp $");
     83 
     84 #define COMPAT_SUNOS 1
     85 
     86 #if defined(_KERNEL_OPT)
     87 #include "opt_nfsserver.h"
     88 #include "opt_compat_43.h"
     89 #include "opt_compat_netbsd.h"
     90 #include "opt_ptrace.h"
     91 #include "fs_nfs.h"
     92 #endif
     93 
     94 #include <sys/param.h>
     95 #include <sys/systm.h>
     96 #include <sys/namei.h>
     97 #include <sys/proc.h>
     98 #include <sys/dirent.h>
     99 #include <sys/file.h>
    100 #include <sys/stat.h>
    101 #include <sys/filedesc.h>
    102 #include <sys/ioctl.h>
    103 #include <sys/kernel.h>
    104 #include <sys/reboot.h>
    105 #include <sys/malloc.h>
    106 #include <sys/mbuf.h>
    107 #include <sys/mman.h>
    108 #include <sys/mount.h>
    109 #include <sys/ptrace.h>
    110 #include <sys/resource.h>
    111 #include <sys/resourcevar.h>
    112 #include <sys/signal.h>
    113 #include <sys/signalvar.h>
    114 #include <sys/socket.h>
    115 #include <sys/tty.h>
    116 #include <sys/vnode.h>
    117 #include <sys/uio.h>
    118 #include <sys/wait.h>
    119 #include <sys/utsname.h>
    120 #include <sys/unistd.h>
    121 #include <sys/sa.h>
    122 #include <sys/syscall.h>
    123 #include <sys/syscallargs.h>
    124 #include <sys/conf.h>
    125 #include <sys/socketvar.h>
    126 #include <sys/exec.h>
    127 #include <sys/swap.h>
    128 #include <sys/kauth.h>
    129 
    130 #include <compat/netbsd32/netbsd32.h>
    131 #include <compat/netbsd32/netbsd32_syscallargs.h>
    132 #include <compat/sunos32/sunos32.h>
    133 #include <compat/sunos32/sunos32_dirent.h>
    134 #include <compat/sunos32/sunos32_syscallargs.h>
    135 #include <compat/common/compat_util.h>
    136 
    137 #include <netinet/in.h>
    138 
    139 #include <miscfs/specfs/specdev.h>
    140 
    141 #include <nfs/rpcv2.h>
    142 #include <nfs/nfsproto.h>
    143 #include <nfs/nfs.h>
    144 #include <nfs/nfsmount.h>
    145 
    146 static void sunos32_sigvec_to_sigaction(const struct netbsd32_sigvec *, struct sigaction *);
    147 static void sunos32_sigvec_from_sigaction(struct netbsd32_sigvec *, const struct sigaction *);
    148 
    149 static int sunstatfs __P((struct statvfs *, caddr_t));
    150 
    151 static void
    152 sunos32_sigvec_to_sigaction(sv, sa)
    153 	const struct netbsd32_sigvec *sv;
    154 	struct sigaction *sa;
    155 {
    156 /*XXX*/ extern void compat_43_sigmask_to_sigset __P((const int *, sigset_t *));
    157 
    158 	sa->sa_handler = (void *)(u_long)sv->sv_handler;
    159 	compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask);
    160 	sa->sa_flags = sv->sv_flags ^ SA_RESTART;
    161 }
    162 
    163 static
    164 void sunos32_sigvec_from_sigaction(sv, sa)
    165 	struct netbsd32_sigvec *sv;
    166 	const struct sigaction *sa;
    167 {
    168 /*XXX*/ extern void compat_43_sigset_to_sigmask __P((const sigset_t *, int *));
    169 
    170 	sv->sv_handler = (netbsd32_voidp)(u_long)sa->sa_handler;
    171 	compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask);
    172 	sv->sv_flags = sa->sa_flags ^ SA_RESTART;
    173 }
    174 
    175 int
    176 sunos32_sys_stime(l, v, retval)
    177 	struct lwp *l;
    178 	void *v;
    179 	register_t *retval;
    180 {
    181 	struct sunos32_sys_stime_args /* {
    182 		syscallarg(sunos32_time_tp) tp;
    183 	} */ *uap = v;
    184 	struct proc *p = l->l_proc;
    185 	struct sys_settimeofday_args ap;
    186 	caddr_t sg = stackgap_init(p, 0);
    187 	struct netbsd32_timeval ntv;
    188 	struct timeval tv, *sgtvp;
    189 	int error;
    190 
    191 	error = copyin((caddr_t)(u_long)SCARG(uap, tp), &ntv.tv_sec, sizeof(ntv.tv_sec));
    192 	if (error)
    193 		return error;
    194 	tv.tv_sec = ntv.tv_sec;
    195 	tv.tv_usec = 0;
    196 
    197 	SCARG(&ap, tv) = sgtvp = stackgap_alloc(p, &sg, sizeof(struct timeval));
    198 	SCARG(&ap, tzp) = NULL;
    199 
    200 	error = copyout(&tv, sgtvp, sizeof(struct timeval));
    201 	if (error)
    202 		return error;
    203 
    204 	return sys_settimeofday(l, &ap, retval);
    205 }
    206 
    207 int
    208 sunos32_sys_wait4(l, v, retval)
    209 	struct lwp *l;
    210 	void *v;
    211 	register_t *retval;
    212 {
    213 	struct sunos32_sys_wait4_args /* {
    214 		syscallarg(int) pid;
    215 		syscallarg(netbsd32_intp) status;
    216 		syscallarg(int) options;
    217 		syscallarg(netbsd32_rusagep_t) rusage;
    218 	} */ *uap = v;
    219 
    220 	if (SCARG(uap, pid) == 0)
    221 		SCARG(uap, pid) = WAIT_ANY;
    222 	return (netbsd32_wait4(l, uap, retval));
    223 }
    224 
    225 int
    226 sunos32_sys_creat(l, v, retval)
    227 	struct lwp *l;
    228 	void *v;
    229 	register_t *retval;
    230 {
    231 	struct sunos32_sys_creat_args /* {
    232 		syscallarg(const netbsd32_charp) path;
    233 		syscallarg(int) mode;
    234 	} */ *uap = v;
    235 	struct proc *p = l->l_proc;
    236 	struct sys_open_args ua;
    237 	caddr_t sg = stackgap_init(p, 0);
    238 
    239 	SUNOS32TOP_UAP(path, const char);
    240 	SCARG(&ua, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    241 	SUNOS32TO64_UAP(mode);
    242 
    243 	SUNOS32_CHECK_ALT_CREAT(l, &sg, SCARG(&ua, path));
    244 
    245 	return (sys_open(l, &ua, retval));
    246 }
    247 
    248 int
    249 sunos32_sys_access(l, v, retval)
    250 	struct lwp *l;
    251 	void *v;
    252 	register_t *retval;
    253 {
    254 	struct sunos32_sys_access_args /* {
    255 		syscallarg(const netbsd32_charp) path;
    256 		syscallarg(int) flags;
    257 	} */ *uap = v;
    258 	struct proc *p = l->l_proc;
    259 	struct sys_access_args ua;
    260 	caddr_t sg = stackgap_init(p, 0);
    261 
    262 	SUNOS32TOP_UAP(path, const char);
    263 	SUNOS32TO64_UAP(flags);
    264 	SUNOS32_CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
    265 
    266 	return (sys_access(l, &ua, retval));
    267 }
    268 
    269 static inline void sunos32_from___stat13 __P((struct stat *, struct netbsd32_stat43 *));
    270 
    271 static inline void
    272 sunos32_from___stat13(sbp, sb32p)
    273 	struct stat *sbp;
    274 	struct netbsd32_stat43 *sb32p;
    275 {
    276 	sb32p->st_dev = sbp->st_dev;
    277 	sb32p->st_ino = sbp->st_ino;
    278 	sb32p->st_mode = sbp->st_mode;
    279 	sb32p->st_nlink = sbp->st_nlink;
    280 	sb32p->st_uid = sbp->st_uid;
    281 	sb32p->st_gid = sbp->st_gid;
    282 	sb32p->st_rdev = sbp->st_rdev;
    283 	if (sbp->st_size < (quad_t)1 << 32)
    284 		sb32p->st_size = sbp->st_size;
    285 	else
    286 		sb32p->st_size = -2;
    287 	sb32p->st_atimespec.tv_sec = (netbsd32_time_t)sbp->st_atimespec.tv_sec;
    288 	sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
    289 	sb32p->st_mtimespec.tv_sec = (netbsd32_time_t)sbp->st_mtimespec.tv_sec;
    290 	sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
    291 	sb32p->st_ctimespec.tv_sec = (netbsd32_time_t)sbp->st_ctimespec.tv_sec;
    292 	sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
    293 	sb32p->st_blksize = sbp->st_blksize;
    294 	sb32p->st_blocks = sbp->st_blocks;
    295 	sb32p->st_flags = sbp->st_flags;
    296 	sb32p->st_gen = sbp->st_gen;
    297 }
    298 
    299 
    300 int
    301 sunos32_sys_stat(l, v, retval)
    302 	struct lwp *l;
    303 	void *v;
    304 	register_t *retval;
    305 {
    306 	struct sunos32_sys_stat_args /* {
    307 		syscallarg(const netbsd32_charp) path;
    308 		syscallarg(netbsd32_stat43p_t) ub;
    309 	} */ *uap = v;
    310 	struct proc *p = l->l_proc;
    311 	struct netbsd32_stat43 sb32;
    312 	struct stat sb;
    313 	struct nameidata nd;
    314 	caddr_t sg;
    315 	const char *path;
    316 	int error;
    317 
    318 	path = (char *)(u_long)SCARG(uap, path);
    319 	sg = stackgap_init(p, 0);
    320 	SUNOS32_CHECK_ALT_EXIST(l, &sg, path);
    321 
    322 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, path, l);
    323 	if ((error = namei(&nd)) != 0)
    324 		return (error);
    325 	error = vn_stat(nd.ni_vp, &sb, l);
    326 	vput(nd.ni_vp);
    327 	if (error)
    328 		return (error);
    329 	sunos32_from___stat13(&sb, &sb32);
    330 	error = copyout((caddr_t)&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof (sb32));
    331 	return (error);
    332 }
    333 
    334 int
    335 sunos32_sys_lstat(l, v, retval)
    336 	struct lwp *l;
    337 	void *v;
    338 	register_t *retval;
    339 {
    340 	struct sunos32_sys_lstat_args /* {
    341 		syscallarg(const netbsd32_charp) path;
    342 		syscallarg(netbsd32_stat43p_t) ub;
    343 	} */ *uap = v;
    344 	struct vnode *vp, *dvp;
    345 	struct proc *p = l->l_proc;
    346 	struct stat sb, sb1;
    347 	struct netbsd32_stat43 sb32;
    348 	int error;
    349 	struct nameidata nd;
    350 	int ndflags;
    351 	const char *path;
    352 	caddr_t sg;
    353 
    354 	path = (char *)(u_long)SCARG(uap, path);
    355 	sg = stackgap_init(p, 0);
    356 	SUNOS32_CHECK_ALT_EXIST(l, &sg, path);
    357 
    358 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT;
    359 again:
    360 	NDINIT(&nd, LOOKUP, ndflags, UIO_USERSPACE, path, l);
    361 	if ((error = namei(&nd))) {
    362 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
    363 			/*
    364 			 * Should only happen on '/'. Retry without LOCKPARENT;
    365 			 * this is safe since the vnode won't be a VLNK.
    366 			 */
    367 			ndflags &= ~LOCKPARENT;
    368 			goto again;
    369 		}
    370 		return (error);
    371 	}
    372 	/*
    373 	 * For symbolic links, always return the attributes of its
    374 	 * containing directory, except for mode, size, and links.
    375 	 */
    376 	vp = nd.ni_vp;
    377 	dvp = nd.ni_dvp;
    378 	if (vp->v_type != VLNK) {
    379 		if ((ndflags & LOCKPARENT) != 0) {
    380 			if (dvp == vp)
    381 				vrele(dvp);
    382 			else
    383 				vput(dvp);
    384 		}
    385 		error = vn_stat(vp, &sb, l);
    386 		vput(vp);
    387 		if (error)
    388 			return (error);
    389 	} else {
    390 		error = vn_stat(dvp, &sb, l);
    391 		vput(dvp);
    392 		if (error) {
    393 			vput(vp);
    394 			return (error);
    395 		}
    396 		error = vn_stat(vp, &sb1, l);
    397 		vput(vp);
    398 		if (error)
    399 			return (error);
    400 		sb.st_mode &= ~S_IFDIR;
    401 		sb.st_mode |= S_IFLNK;
    402 		sb.st_nlink = sb1.st_nlink;
    403 		sb.st_size = sb1.st_size;
    404 		sb.st_blocks = sb1.st_blocks;
    405 	}
    406 	sunos32_from___stat13(&sb, &sb32);
    407 	error = copyout((caddr_t)&sb32, (caddr_t)(u_long)SCARG(uap, ub), sizeof (sb32));
    408 	return (error);
    409 }
    410 
    411 static int
    412 sunos32_execve_fetch_element(char * const *array, size_t index, char **value)
    413 {
    414 	int error;
    415 	netbsd32_charp const *a32 = (void const *)array;
    416 	netbsd32_charp e;
    417 
    418 	error = copyin(a32 + index, &e, sizeof(e));
    419 	if (error)
    420 		return error;
    421 	*value = (char *)(u_long)(u_int)e;
    422 	return 0;
    423 }
    424 
    425 int
    426 sunos32_sys_execv(l, v, retval)
    427 	struct lwp *l;
    428 	void *v;
    429 	register_t *retval;
    430 {
    431 	struct sunos32_sys_execv_args /* {
    432 		syscallarg(const char *) path;
    433 		syscallarg(char **) argv;
    434 	} */ *uap = v;
    435 	struct proc *p = l->l_proc;
    436 	const char *path = (const char *)(u_long)(u_int)SCARG(uap, path);
    437 	caddr_t sg;
    438 
    439 	sg = stackgap_init(p, 0);
    440 	SUNOS32_CHECK_ALT_EXIST(l, &sg, path);
    441 
    442 	return execve1(l, path, (char **)(u_long)(u_int)SCARG(uap, argp), NULL,
    443 	    sunos32_execve_fetch_element);
    444 }
    445 
    446 int
    447 sunos32_sys_execve(l, v, retval)
    448 	struct lwp *l;
    449 	void *v;
    450 	register_t *retval;
    451 {
    452 	struct sunos32_sys_execve_args /* {
    453 		syscallarg(const char *) path;
    454 		syscallarg(char **) argv;
    455 		syscallarg(char **) envp;
    456 	} */ *uap = v;
    457 	struct proc *p = l->l_proc;
    458 	const char *path = (const char *)(u_long)(u_int)SCARG(uap, path);
    459 	caddr_t sg;
    460 
    461 	sg = stackgap_init(p, 0);
    462 	SUNOS32_CHECK_ALT_EXIST(l, &sg, path);
    463 
    464 	return execve1(l, path, (char **)(u_long)(u_int)SCARG(uap, argp),
    465 	    (char **)(u_long)(u_int)SCARG(uap, envp),
    466 	    sunos32_execve_fetch_element);
    467 }
    468 
    469 int
    470 sunos32_sys_omsync(l, v, retval)
    471 	struct lwp *l;
    472 	void *v;
    473 	register_t *retval;
    474 {
    475 	struct sunos32_sys_omsync_args /* {
    476 		syscallarg(netbsd32_caddr_t) addr;
    477 		syscallarg(netbsd32_size_t) len;
    478 		syscallarg(int) flags;
    479 	} */ *uap = v;
    480 	struct netbsd32___msync13_args ouap;
    481 
    482 	SCARG(&ouap, addr) = SCARG(uap, addr);
    483 	SCARG(&ouap, len) = SCARG(uap, len);
    484 	SCARG(&ouap, flags) = SCARG(uap, flags);
    485 
    486 	return (netbsd32___msync13(l, &ouap, retval));
    487 }
    488 
    489 int
    490 sunos32_sys_unmount(l, v, retval)
    491 	struct lwp *l;
    492 	void *v;
    493 	register_t *retval;
    494 {
    495 	struct sunos32_sys_unmount_args /* {
    496 		syscallarg(netbsd32_charp) path;
    497 	} */ *uap = v;
    498 	struct sys_unmount_args ua;
    499 
    500 	SUNOS32TOP_UAP(path, const char);
    501 	SCARG(&ua, flags) = 0;
    502 
    503 	return (sys_unmount(l, &ua, retval));
    504 }
    505 
    506 /*
    507  * Conversion table for SunOS NFS mount flags.
    508  */
    509 static struct {
    510 	int	sun_flg;
    511 	int	bsd_flg;
    512 } sunnfs_flgtab[] = {
    513 	{ SUNNFS_SOFT,		NFSMNT_SOFT },
    514 	{ SUNNFS_WSIZE,		NFSMNT_WSIZE },
    515 	{ SUNNFS_RSIZE,		NFSMNT_RSIZE },
    516 	{ SUNNFS_TIMEO,		NFSMNT_TIMEO },
    517 	{ SUNNFS_RETRANS,	NFSMNT_RETRANS },
    518 	{ SUNNFS_HOSTNAME,	0 },			/* Ignored */
    519 	{ SUNNFS_INT,		NFSMNT_INT },
    520 	{ SUNNFS_NOAC,		0 },			/* Ignored */
    521 	{ SUNNFS_ACREGMIN,	0 },			/* Ignored */
    522 	{ SUNNFS_ACREGMAX,	0 },			/* Ignored */
    523 	{ SUNNFS_ACDIRMIN,	0 },			/* Ignored */
    524 	{ SUNNFS_ACDIRMAX,	0 },			/* Ignored */
    525 	{ SUNNFS_SECURE,	0 },			/* Ignored */
    526 	{ SUNNFS_NOCTO,		0 },			/* Ignored */
    527 	{ SUNNFS_POSIX,		0 }			/* Ignored */
    528 };
    529 
    530 int
    531 sunos32_sys_mount(l, v, retval)
    532 	struct lwp *l;
    533 	void *v;
    534 	register_t *retval;
    535 {
    536 	struct sunos32_sys_mount_args /* {
    537 		syscallarg(netbsd32_charp) type;
    538 		syscallarg(netbsd32_charp) path;
    539 		syscallarg(int) flags;
    540 		syscallarg(netbsd32_caddr_t) data;
    541 	} */ *uap = v;
    542 	struct proc *p = l->l_proc;
    543 	struct sys_mount_args ua;
    544 	int oflags = SCARG(uap, flags), nflags, error;
    545 	char fsname[MFSNAMELEN];
    546 	caddr_t sg = stackgap_init(p, 0);
    547 
    548 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
    549 		return (EINVAL);
    550 	if ((oflags & SUNM_NEWTYPE) == 0)
    551 		return (EINVAL);
    552 	nflags = 0;
    553 	if (oflags & SUNM_RDONLY)
    554 		nflags |= MNT_RDONLY;
    555 	if (oflags & SUNM_NOSUID)
    556 		nflags |= MNT_NOSUID;
    557 	if (oflags & SUNM_REMOUNT)
    558 		nflags |= MNT_UPDATE;
    559 	SCARG(uap, flags) = nflags;
    560 
    561 	error = copyinstr((caddr_t)(u_long)SCARG(uap, type), fsname,
    562 	    sizeof fsname, (size_t *)0);
    563 	if (error)
    564 		return (error);
    565 
    566 	if (strncmp(fsname, "4.2", sizeof fsname) == 0) {
    567 		SCARG(uap, type) = (netbsd32_charp)(u_long)stackgap_alloc(p, &sg, sizeof("ffs"));
    568 		error = copyout("ffs", (caddr_t)(u_long)SCARG(uap, type), sizeof("ffs"));
    569 		if (error)
    570 			return (error);
    571 	} else if (strncmp(fsname, "nfs", sizeof fsname) == 0) {
    572 		struct sunos_nfs_args sna;
    573 		struct sockaddr_in sain;
    574 		struct nfs_args na;	/* XXX */
    575 		struct sockaddr sa;
    576 		int n;
    577 
    578 		error = copyin((caddr_t)(u_long)SCARG(uap, data), &sna, sizeof sna);
    579 		if (error)
    580 			return (error);
    581 		error = copyin(sna.addr, &sain, sizeof sain);
    582 		if (error)
    583 			return (error);
    584 		memcpy(&sa, &sain, sizeof sa);
    585 		sa.sa_len = sizeof(sain);
    586 		SCARG(uap, data) = (netbsd32_charp)(u_long)stackgap_alloc(p, &sg, sizeof(na));
    587 		na.version = NFS_ARGSVERSION;
    588 		na.addr = stackgap_alloc(p, &sg, sizeof(struct sockaddr));
    589 		na.addrlen = sizeof(struct sockaddr);
    590 		na.sotype = SOCK_DGRAM;
    591 		na.proto = IPPROTO_UDP;
    592 		na.fh = (void *)sna.fh;
    593 		na.fhsize = NFSX_V2FH;
    594 		na.flags = 0;
    595 		n = sizeof(sunnfs_flgtab) / sizeof(sunnfs_flgtab[0]);
    596 		while (--n >= 0)
    597 			if (sna.flags & sunnfs_flgtab[n].sun_flg)
    598 				na.flags |= sunnfs_flgtab[n].bsd_flg;
    599 		na.wsize = sna.wsize;
    600 		na.rsize = sna.rsize;
    601 		if (na.flags & NFSMNT_RSIZE) {
    602 			na.flags |= NFSMNT_READDIRSIZE;
    603 			na.readdirsize = na.rsize;
    604 		}
    605 		na.timeo = sna.timeo;
    606 		na.retrans = sna.retrans;
    607 		na.hostname = (char *)(u_long)sna.hostname;
    608 
    609 		error = copyout(&sa, na.addr, sizeof sa);
    610 		if (error)
    611 			return (error);
    612 		error = copyout(&na, (caddr_t)(u_long)SCARG(uap, data), sizeof na);
    613 		if (error)
    614 			return (error);
    615 	}
    616 	SUNOS32TOP_UAP(type, const char);
    617 	SUNOS32TOP_UAP(path, const char);
    618 	SUNOS32TO64_UAP(flags);
    619 	SUNOS32TOP_UAP(data, void);
    620 	return (sys_mount(l, &ua, retval));
    621 }
    622 
    623 #if defined(NFS)
    624 int
    625 async_daemon(l, v, retval)
    626 	struct lwp *l;
    627 	void *v;
    628 	register_t *retval;
    629 {
    630 	struct netbsd32_nfssvc_args ouap;
    631 
    632 	SCARG(&ouap, flag) = NFSSVC_BIOD;
    633 	SCARG(&ouap, argp) = 0;
    634 
    635 	return (netbsd32_nfssvc(l, &ouap, retval));
    636 }
    637 #endif /* NFS */
    638 
    639 void	native_to_sunos_sigset __P((const sigset_t *, int *));
    640 void	sunos_to_native_sigset __P((const int, sigset_t *));
    641 
    642 inline void
    643 native_to_sunos_sigset(ss, mask)
    644 	const sigset_t *ss;
    645 	int *mask;
    646 {
    647 	*mask = ss->__bits[0];
    648 }
    649 
    650 inline void
    651 sunos_to_native_sigset(mask, ss)
    652 	const int mask;
    653 	sigset_t *ss;
    654 {
    655 
    656 	ss->__bits[0] = mask;
    657 	ss->__bits[1] = 0;
    658 	ss->__bits[2] = 0;
    659 	ss->__bits[3] = 0;
    660 }
    661 
    662 int
    663 sunos32_sys_sigpending(l, v, retval)
    664 	struct lwp *l;
    665 	void *v;
    666 	register_t *retval;
    667 {
    668 	struct sunos32_sys_sigpending_args /* {
    669 		syscallarg(netbsd32_intp) mask;
    670 	} */ *uap = v;
    671 	struct proc *p = l->l_proc;
    672 	sigset_t ss;
    673 	int mask;
    674 
    675 	sigpending1(p, &ss);
    676 	native_to_sunos_sigset(&ss, &mask);
    677 
    678 	return (copyout((caddr_t)(u_long)&mask, (caddr_t)(u_long)SCARG(uap, mask), sizeof(int)));
    679 }
    680 
    681 int
    682 sunos32_sys_sigsuspend(l, v, retval)
    683 	struct lwp *l;
    684 	void *v;
    685 	register_t *retval;
    686 {
    687 	struct sunos32_sys_sigsuspend_args /* {
    688 		syscallarg(int) mask;
    689 	} */ *uap = v;
    690 	int mask;
    691 	struct proc *p = l->l_proc;
    692 	sigset_t ss;
    693 
    694 	mask = SCARG(uap, mask);
    695 	sunos_to_native_sigset(mask, &ss);
    696 	return (sigsuspend1(p, &ss));
    697 }
    698 
    699 /*
    700  * Read Sun-style directory entries.  We suck them into kernel space so
    701  * that they can be massaged before being copied out to user code.  Like
    702  * SunOS, we squish out `empty' entries.
    703  *
    704  * This is quite ugly, but what do you expect from compatibility code?
    705  */
    706 int
    707 sunos32_sys_getdents(l, v, retval)
    708 	struct lwp *l;
    709 	void *v;
    710 	register_t *retval;
    711 {
    712 	struct sunos32_sys_getdents_args /* {
    713 		syscallarg(int) fd;
    714 		syscallarg(netbsd32_charp) buf;
    715 		syscallarg(int) nbytes;
    716 	} */ *uap = v;
    717 	struct proc *p = l->l_proc;
    718 	struct dirent *bdp;
    719 	struct vnode *vp;
    720 	caddr_t inp, sbuf;	/* BSD-format */
    721 	int len, reclen;	/* BSD-format */
    722 	caddr_t outp;		/* Sun-format */
    723 	int resid, sunos_reclen;/* Sun-format */
    724 	struct file *fp;
    725 	struct uio auio;
    726 	struct iovec aiov;
    727 	struct sunos32_dirent idb;
    728 	off_t off;			/* true file offset */
    729 	int buflen, error, eofflag;
    730 	off_t *cookiebuf, *cookie;
    731 	int ncookies;
    732 
    733 	/* getvnode() will use the descriptor for us */
    734 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    735 		return (error);
    736 
    737 	if ((fp->f_flag & FREAD) == 0) {
    738 		error = EBADF;
    739 		goto out1;
    740 	}
    741 
    742 	vp = (struct vnode *)fp->f_data;
    743 	if (vp->v_type != VDIR) {
    744 		error = EINVAL;
    745 		goto out1;
    746 	}
    747 
    748 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
    749 	sbuf = malloc(buflen, M_TEMP, M_WAITOK);
    750 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    751 	off = fp->f_offset;
    752 again:
    753 	aiov.iov_base = sbuf;
    754 	aiov.iov_len = buflen;
    755 	auio.uio_iov = &aiov;
    756 	auio.uio_iovcnt = 1;
    757 	auio.uio_rw = UIO_READ;
    758 	auio.uio_resid = buflen;
    759 	auio.uio_offset = off;
    760 	UIO_SETUP_SYSSPACE(&auio);
    761 	/*
    762 	 * First we read into the malloc'ed buffer, then
    763 	 * we massage it into user space, one record at a time.
    764 	 */
    765 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    766 	    &ncookies);
    767 	if (error)
    768 		goto out;
    769 
    770 	inp = sbuf;
    771 	outp = (caddr_t)(u_long)SCARG(uap, buf);
    772 	resid = SCARG(uap, nbytes);
    773 	if ((len = buflen - auio.uio_resid) == 0)
    774 		goto eof;
    775 
    776 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    777 		bdp = (struct dirent *)inp;
    778 		reclen = bdp->d_reclen;
    779 		if (reclen & 3)
    780 			panic("sunos_getdents");
    781 		if (cookie && (*cookie >> 32) != 0) {
    782 			compat_offseterr(vp, "sunos_getdents");
    783 			error = EINVAL;
    784 			goto out;
    785 		}
    786 		if (bdp->d_fileno == 0) {
    787 			inp += reclen;	/* it is a hole; squish it out */
    788 			if (cookie)
    789 				off = *cookie++;
    790 			else
    791 				off += reclen;
    792 			continue;
    793 		}
    794 		sunos_reclen = SUNOS32_RECLEN(&idb, bdp->d_namlen);
    795 		if (reclen > len || resid < sunos_reclen) {
    796 			/* entry too big for buffer, so just stop */
    797 			outp++;
    798 			break;
    799 		}
    800 		if (cookie)
    801 			off = *cookie++;	/* each entry points to next */
    802 		else
    803 			off += reclen;
    804 		/*
    805 		 * Massage in place to make a Sun-shaped dirent (otherwise
    806 		 * we have to worry about touching user memory outside of
    807 		 * the copyout() call).
    808 		 */
    809 		idb.d_fileno = bdp->d_fileno;
    810 		idb.d_off = off;
    811 		idb.d_reclen = sunos_reclen;
    812 		idb.d_namlen = bdp->d_namlen;
    813 		strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
    814 		if ((error = copyout((caddr_t)&idb, outp, sunos_reclen)) != 0)
    815 			goto out;
    816 		/* advance past this real entry */
    817 		inp += reclen;
    818 		/* advance output past Sun-shaped entry */
    819 		outp += sunos_reclen;
    820 		resid -= sunos_reclen;
    821 	}
    822 
    823 	/* if we squished out the whole block, try again */
    824 	if (outp == (caddr_t)(u_long)SCARG(uap, buf))
    825 		goto again;
    826 	fp->f_offset = off;		/* update the vnode offset */
    827 
    828 eof:
    829 	*retval = SCARG(uap, nbytes) - resid;
    830 out:
    831 	VOP_UNLOCK(vp, 0);
    832 	free(cookiebuf, M_TEMP);
    833 	free(sbuf, M_TEMP);
    834  out1:
    835 	FILE_UNUSE(fp, l);
    836 	return (error);
    837 }
    838 
    839 #define	SUNOS32__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    840 
    841 int
    842 sunos32_sys_mmap(l, v, retval)
    843 	struct lwp *l;
    844 	void *v;
    845 	register_t *retval;
    846 {
    847 	struct sunos32_sys_mmap_args /* {
    848 		syscallarg(netbsd32_voidp) addr;
    849 		syscallarg(netbsd32_size_t) len;
    850 		syscallarg(int) prot;
    851 		syscallarg(int) flags;
    852 		syscallarg(int) fd;
    853 		syscallarg(netbsd32_long) pos;
    854 	} */ *uap = v;
    855 	struct sys_mmap_args ua;
    856 	int error;
    857 
    858 	/*
    859 	 * Verify the arguments.
    860 	 */
    861 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    862 		return (EINVAL);			/* XXX still needed? */
    863 
    864 	if ((SCARG(uap, flags) & SUNOS32__MAP_NEW) == 0)
    865 		return (EINVAL);
    866 
    867 	SUNOS32TOP_UAP(addr, void);
    868 	SUNOS32TOX_UAP(len, size_t);
    869 	SUNOS32TO64_UAP(prot);
    870 	SCARG(&ua, flags) = SCARG(uap, flags) & ~SUNOS32__MAP_NEW;
    871 	SUNOS32TO64_UAP(fd);
    872 	SCARG(&ua, pad) = 0;
    873 	SUNOS32TOX_UAP(pos, off_t);
    874 
    875 	error = sys_mmap(l, &ua, retval);
    876 	if ((u_long)*retval > (u_long)UINT_MAX) {
    877 		printf("sunos32_mmap: retval out of range: 0x%lx",
    878 		       (u_long)*retval);
    879 		/* Should try to recover and return an error here. */
    880 	}
    881 	return (error);
    882 }
    883 
    884 #define	MC_SYNC		1
    885 #define	MC_LOCK		2
    886 #define	MC_UNLOCK	3
    887 #define	MC_ADVISE	4
    888 #define	MC_LOCKAS	5
    889 #define	MC_UNLOCKAS	6
    890 
    891 int
    892 sunos32_sys_mctl(l, v, retval)
    893 	struct lwp *l;
    894 	void *v;
    895 	register_t *retval;
    896 {
    897 	struct sunos32_sys_mctl_args /* {
    898 		syscallarg(netbsd32_voidp) addr;
    899 		syscallarg(int) len;
    900 		syscallarg(int) func;
    901 		syscallarg(netbsd32_voidp) arg;
    902 	} */ *uap = v;
    903 
    904 	switch (SCARG(uap, func)) {
    905 	case MC_ADVISE:		/* ignore for now */
    906 		return (0);
    907 	case MC_SYNC:		/* translate to msync */
    908 		return (netbsd32___msync13(l, uap, retval));
    909 	default:
    910 		return (EINVAL);
    911 	}
    912 }
    913 
    914 int
    915 sunos32_sys_setsockopt(l, v, retval)
    916 	struct lwp *l;
    917 	void *v;
    918 	register_t *retval;
    919 {
    920 	struct sunos32_sys_setsockopt_args /* {
    921 		syscallarg(int) s;
    922 		syscallarg(int) level;
    923 		syscallarg(int) name;
    924 		syscallarg(netbsd32_caddr_t) val;
    925 		syscallarg(int) valsize;
    926 	} */ *uap = v;
    927 	struct proc *p = l->l_proc;
    928 	struct file *fp;
    929 	struct mbuf *m = NULL;
    930 	int error;
    931 
    932 	/* getsock() will use the descriptor for us */
    933 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    934 		return (error);
    935 #define	SO_DONTLINGER (~SO_LINGER)
    936 	if (SCARG(uap, name) == SO_DONTLINGER) {
    937 		m = m_get(M_WAIT, MT_SOOPTS);
    938 		mtod(m, struct linger *)->l_onoff = 0;
    939 		m->m_len = sizeof(struct linger);
    940 		error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    941 		    SO_LINGER, m);
    942 		goto out;
    943 	}
    944 	if (SCARG(uap, level) == IPPROTO_IP) {
    945 #define		SUNOS_IP_MULTICAST_IF		2
    946 #define		SUNOS_IP_MULTICAST_TTL		3
    947 #define		SUNOS_IP_MULTICAST_LOOP		4
    948 #define		SUNOS_IP_ADD_MEMBERSHIP		5
    949 #define		SUNOS_IP_DROP_MEMBERSHIP	6
    950 		static const int ipoptxlat[] = {
    951 			IP_MULTICAST_IF,
    952 			IP_MULTICAST_TTL,
    953 			IP_MULTICAST_LOOP,
    954 			IP_ADD_MEMBERSHIP,
    955 			IP_DROP_MEMBERSHIP
    956 		};
    957 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
    958 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
    959 			SCARG(uap, name) =
    960 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
    961 		}
    962 	}
    963 	if (SCARG(uap, valsize) > MLEN) {
    964 		error = EINVAL;
    965 		goto out;
    966 	}
    967 	if (SCARG(uap, val)) {
    968 		m = m_get(M_WAIT, MT_SOOPTS);
    969 		error = copyin((caddr_t)(u_long)SCARG(uap, val), mtod(m, caddr_t),
    970 		    (u_int)SCARG(uap, valsize));
    971 		if (error) {
    972 			(void) m_free(m);
    973 			goto out;
    974 		}
    975 		m->m_len = SCARG(uap, valsize);
    976 	}
    977 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    978 	    SCARG(uap, name), m);
    979  out:
    980 	FILE_UNUSE(fp, l);
    981 	return (error);
    982 }
    983 
    984 static inline int sunos32_sys_socket_common(struct lwp *, register_t *,
    985 					      int type);
    986 static inline int
    987 sunos32_sys_socket_common(l, retval, type)
    988 	struct lwp *l;
    989 	register_t *retval;
    990 	int type;
    991 {
    992 	struct socket *so;
    993 	struct proc *p = l->l_proc;
    994 	struct file *fp;
    995 	int error, fd;
    996 
    997 	/* getsock() will use the descriptor for us */
    998 	fd = (int)*retval;
    999 	if ((error = getsock(p->p_fd, fd, &fp)) == 0) {
   1000 		so = (struct socket *)fp->f_data;
   1001 		if (type == SOCK_DGRAM)
   1002 			so->so_options |= SO_BROADCAST;
   1003 	}
   1004 	FILE_UNUSE(fp, l);
   1005 	return (error);
   1006 }
   1007 
   1008 int
   1009 sunos32_sys_socket(l, v, retval)
   1010 	struct lwp *l;
   1011 	void *v;
   1012 	register_t *retval;
   1013 {
   1014 	struct sunos32_sys_socket_args /* {
   1015 		syscallarg(int) domain;
   1016 		syscallarg(int) type;
   1017 		syscallarg(int) protocol;
   1018 	} */ *uap = v;
   1019 	int error;
   1020 
   1021 	error = netbsd32_sys___socket30(l, v, retval);
   1022 	if (error)
   1023 		return (error);
   1024 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
   1025 }
   1026 
   1027 int
   1028 sunos32_sys_socketpair(l, v, retval)
   1029 	struct lwp *l;
   1030 	void *v;
   1031 	register_t *retval;
   1032 {
   1033 	struct sunos32_sys_socketpair_args /* {
   1034 		syscallarg(int) domain;
   1035 		syscallarg(int) type;
   1036 		syscallarg(int) protocol;
   1037 		syscallarg(int *) rsv;
   1038 	} */ *uap = v;
   1039 	int error;
   1040 
   1041 	error = netbsd32_socketpair(l, v, retval);
   1042 	if (error)
   1043 		return (error);
   1044 	return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
   1045 }
   1046 
   1047 
   1048 /*
   1049  * XXX: This needs cleaning up.
   1050  */
   1051 int
   1052 sunos32_sys_auditsys(l, v, retval)
   1053 	struct lwp *l;
   1054 	void *v;
   1055 	register_t *retval;
   1056 {
   1057 	return 0;
   1058 }
   1059 
   1060 int
   1061 sunos32_sys_uname(l, v, retval)
   1062 	struct lwp *l;
   1063 	void *v;
   1064 	register_t *retval;
   1065 {
   1066 	struct sunos32_sys_uname_args /* {
   1067 		syscallarg(sunos32_utsnamep_t) name;
   1068 	} */ *uap = v;
   1069 	struct sunos_utsname sut;
   1070 
   1071 	memset(&sut, 0, sizeof(sut));
   1072 
   1073 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
   1074 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
   1075 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
   1076 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
   1077 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
   1078 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
   1079 
   1080 	return copyout((caddr_t)&sut, (caddr_t)(u_long)SCARG(uap, name),
   1081 	    sizeof(struct sunos_utsname));
   1082 }
   1083 
   1084 int
   1085 sunos32_sys_setpgrp(l, v, retval)
   1086 	struct lwp *l;
   1087 	void *v;
   1088 	register_t *retval;
   1089 {
   1090 	struct sunos32_sys_setpgrp_args /* {
   1091 		syscallarg(int) pid;
   1092 		syscallarg(int) pgid;
   1093 	} */ *uap = v;
   1094 	struct proc *p = l->l_proc;
   1095 
   1096 	/*
   1097 	 * difference to our setpgid call is to include backwards
   1098 	 * compatibility to pre-setsid() binaries. Do setsid()
   1099 	 * instead of setpgid() in those cases where the process
   1100 	 * tries to create a new session the old way.
   1101 	 */
   1102 	if (!SCARG(uap, pgid) &&
   1103 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
   1104 		return sys_setsid(l, uap, retval);
   1105 	else
   1106 		return netbsd32_setpgid(l, uap, retval);
   1107 }
   1108 
   1109 int
   1110 sunos32_sys_open(l, v, retval)
   1111 	struct lwp *l;
   1112 	void *v;
   1113 	register_t *retval;
   1114 {
   1115 	struct sunos32_sys_open_args /* {
   1116 		syscallarg(const netbsd32_charp) path;
   1117 		syscallarg(int) flags;
   1118 		syscallarg(int) mode;
   1119 	} */ *uap = v;
   1120 	struct proc *p = l->l_proc;
   1121 	struct sys_open_args ua;
   1122 	int lf, r;
   1123 	int noctty;
   1124 	int ret;
   1125 	caddr_t sg = stackgap_init(p, 0);
   1126 
   1127 	/* convert mode into NetBSD mode */
   1128 	lf = SCARG(uap, flags);
   1129 	noctty = lf & 0x8000;
   1130 	r =	(lf & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
   1131 	r |=	((lf & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
   1132 	r |=	((lf & 0x0080) ? O_SHLOCK : 0);
   1133 	r |=	((lf & 0x0100) ? O_EXLOCK : 0);
   1134 	r |=	((lf & 0x2000) ? O_FSYNC : 0);
   1135 
   1136 	SUNOS32TOP_UAP(path, const char);
   1137 	SCARG(&ua, flags) = r;
   1138 	SUNOS32TO64_UAP(mode);
   1139 
   1140 	if (r & O_CREAT)
   1141 		SUNOS32_CHECK_ALT_CREAT(l, &sg, SCARG(&ua, path));
   1142 	else
   1143 		SUNOS32_CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
   1144 
   1145 	ret = sys_open(l, &ua, retval);
   1146 
   1147 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
   1148 		struct filedesc *fdp = p->p_fd;
   1149 		struct file *fp;
   1150 
   1151 		fp = fd_getfile(fdp, *retval);
   1152 
   1153 		/* ignore any error, just give it a try */
   1154 		if (fp != NULL && fp->f_type == DTYPE_VNODE)
   1155 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, l);
   1156 	}
   1157 	return ret;
   1158 }
   1159 
   1160 #if defined (NFSSERVER)
   1161 int
   1162 sunos32_sys_nfssvc(l, v, retval)
   1163 	struct lwp *l;
   1164 	void *v;
   1165 	register_t *retval;
   1166 {
   1167 #if 0
   1168 	struct sunos32_sys_nfssvc_args *uap = v;
   1169 	struct emul *e = p->p_emul;
   1170 	struct sys_nfssvc_args outuap;
   1171 	struct sockaddr sa;
   1172 	int error;
   1173 	caddr_t sg = stackgap_init(p, 0);
   1174 
   1175 	memset(&outuap, 0, sizeof outuap);
   1176 	SCARG(&outuap, fd) = SCARG(uap, fd);
   1177 	SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa));
   1178 	SCARG(&outuap, msklen) = sizeof(sa);
   1179 	SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa));
   1180 	SCARG(&outuap, mtchlen) = sizeof(sa);
   1181 
   1182 	memset(&sa, 0, sizeof sa);
   1183 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
   1184 		return (error);
   1185 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
   1186 		return (error);
   1187 
   1188 	return nfssvc(l, &outuap, retval);
   1189 #else
   1190 	return (ENOSYS);
   1191 #endif
   1192 }
   1193 #endif /* NFSSERVER */
   1194 
   1195 int
   1196 sunos32_sys_ustat(l, v, retval)
   1197 	struct lwp *l;
   1198 	void *v;
   1199 	register_t *retval;
   1200 {
   1201 	struct sunos32_sys_ustat_args /* {
   1202 		syscallarg(int) dev;
   1203 		syscallarg(sunos32_ustatp_t) buf;
   1204 	} */ *uap = v;
   1205 	struct sunos_ustat us;
   1206 	int error;
   1207 
   1208 	memset(&us, 0, sizeof us);
   1209 
   1210 	/*
   1211 	 * XXX: should set f_tfree and f_tinode at least
   1212 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
   1213 	 */
   1214 
   1215 	if ((error = copyout(&us, (caddr_t)(u_long)SCARG(uap, buf), sizeof us)) != 0)
   1216 		return (error);
   1217 	return 0;
   1218 }
   1219 
   1220 int
   1221 sunos32_sys_quotactl(l, v, retval)
   1222 	struct lwp *l;
   1223 	void *v;
   1224 	register_t *retval;
   1225 {
   1226 
   1227 	return EINVAL;
   1228 }
   1229 
   1230 int
   1231 sunos32_sys_vhangup(l, v, retval)
   1232 	struct lwp *l;
   1233 	void *v;
   1234 	register_t *retval;
   1235 {
   1236 	struct proc *p = l->l_proc;
   1237 	struct session *sp = p->p_session;
   1238 
   1239 	if (sp->s_ttyvp == 0)
   1240 		return 0;
   1241 
   1242 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
   1243 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
   1244 
   1245 	(void) ttywait(sp->s_ttyp);
   1246 	if (sp->s_ttyvp)
   1247 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
   1248 	if (sp->s_ttyvp)
   1249 		vrele(sp->s_ttyvp);
   1250 	sp->s_ttyvp = NULL;
   1251 
   1252 	return 0;
   1253 }
   1254 
   1255 static int
   1256 sunstatfs(sp, sbuf)
   1257 	struct statvfs *sp;
   1258 	caddr_t sbuf;
   1259 {
   1260 	struct sunos_statfs ssfs;
   1261 
   1262 	memset(&ssfs, 0, sizeof ssfs);
   1263 	ssfs.f_type = 0;
   1264 	ssfs.f_bsize = sp->f_bsize;
   1265 	ssfs.f_blocks = sp->f_blocks;
   1266 	ssfs.f_bfree = sp->f_bfree;
   1267 	ssfs.f_bavail = sp->f_bavail;
   1268 	ssfs.f_files = sp->f_files;
   1269 	ssfs.f_ffree = sp->f_ffree;
   1270 	ssfs.f_fsid = sp->f_fsidx;
   1271 	return copyout((caddr_t)&ssfs, sbuf, sizeof ssfs);
   1272 }
   1273 
   1274 int
   1275 sunos32_sys_statfs(l, v, retval)
   1276 	struct lwp *l;
   1277 	void *v;
   1278 	register_t *retval;
   1279 {
   1280 	struct sunos32_sys_statfs_args /* {
   1281 		syscallarg(const netbsd32_charp) path;
   1282 		syscallarg(sunos32_statfsp_t) buf;
   1283 	} */ *uap = v;
   1284 	struct proc *p = l->l_proc;
   1285 	struct mount *mp;
   1286 	struct statvfs *sp;
   1287 	int error;
   1288 	struct nameidata nd;
   1289 	struct sys_statvfs1_args ua;
   1290 	caddr_t sg;
   1291 
   1292 	sg = stackgap_init(p, 0);
   1293 	SUNOS32TOP_UAP(path, const char);
   1294 	SUNOS32_CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
   1295 
   1296 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(&ua, path), l);
   1297 	if ((error = namei(&nd)) != 0)
   1298 		return (error);
   1299 	mp = nd.ni_vp->v_mount;
   1300 	sp = &mp->mnt_stat;
   1301 	vrele(nd.ni_vp);
   1302 	if ((error = VFS_STATVFS(mp, sp, l)) != 0)
   1303 		return (error);
   1304 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
   1305 	return sunstatfs(sp, (caddr_t)(u_long)SCARG(uap, buf));
   1306 }
   1307 
   1308 int
   1309 sunos32_sys_fstatfs(l, v, retval)
   1310 	struct lwp *l;
   1311 	void *v;
   1312 	register_t *retval;
   1313 {
   1314 	struct sunos32_sys_fstatfs_args /* {
   1315 		syscallarg(int) fd;
   1316 		syscallarg(sunos32_statfsp_t) buf;
   1317 	} */ *uap = v;
   1318 	struct proc *p = l->l_proc;
   1319 	struct file *fp;
   1320 	struct mount *mp;
   1321 	struct statvfs *sp;
   1322 	int error;
   1323 
   1324 	/* getvnode() will use the descriptor for us */
   1325 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1326 		return (error);
   1327 	mp = ((struct vnode *)fp->f_data)->v_mount;
   1328 	sp = &mp->mnt_stat;
   1329 	if ((error = VFS_STATVFS(mp, sp, l)) != 0)
   1330 		goto out;
   1331 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
   1332 	error = sunstatfs(sp, (caddr_t)(u_long)SCARG(uap, buf));
   1333  out:
   1334 	FILE_UNUSE(fp, l);
   1335 	return (error);
   1336 }
   1337 
   1338 int
   1339 sunos32_sys_exportfs(l, v, retval)
   1340 	struct lwp *l;
   1341 	void *v;
   1342 	register_t *retval;
   1343 {
   1344 	/*
   1345 	 * XXX: should perhaps translate into a mount(2)
   1346 	 * with MOUNT_EXPORT?
   1347 	 */
   1348 	return 0;
   1349 }
   1350 
   1351 int
   1352 sunos32_sys_mknod(l, v, retval)
   1353 	struct lwp *l;
   1354 	void *v;
   1355 	register_t *retval;
   1356 {
   1357 	struct sunos32_sys_mknod_args /* {
   1358 		syscallarg(const netbsd32_charp) path;
   1359 		syscallarg(int) mode;
   1360 		syscallarg(int) dev;
   1361 	} */ *uap = v;
   1362 	struct proc *p = l->l_proc;
   1363 	struct sys_mknod_args ua;
   1364 	caddr_t sg;
   1365 
   1366 	sg = stackgap_init(p, 0);
   1367 	SUNOS32TOP_UAP(path, const char);
   1368 	SUNOS32TO64_UAP(mode);
   1369 	SUNOS32TO64_UAP(dev);
   1370 	SUNOS32_CHECK_ALT_CREAT(l, &sg, SCARG(&ua, path));
   1371 
   1372 	/* netbsd32_mkfifo/mknod to not do alt checking */
   1373 	if (S_ISFIFO(SCARG(uap, mode)))
   1374 		return netbsd32_mkfifo(l, (struct netbsd32_mkfifo_args *)uap, retval);
   1375 
   1376 	return netbsd32_mknod(l, (struct netbsd32_mknod_args *)uap, retval);
   1377 }
   1378 
   1379 #define SUNOS_SC_ARG_MAX	1
   1380 #define SUNOS_SC_CHILD_MAX	2
   1381 #define SUNOS_SC_CLK_TCK	3
   1382 #define SUNOS_SC_NGROUPS_MAX	4
   1383 #define SUNOS_SC_OPEN_MAX	5
   1384 #define SUNOS_SC_JOB_CONTROL	6
   1385 #define SUNOS_SC_SAVED_IDS	7
   1386 #define SUNOS_SC_VERSION	8
   1387 
   1388 int
   1389 sunos32_sys_sysconf(l, v, retval)
   1390 	struct lwp *l;
   1391 	void *v;
   1392 	register_t *retval;
   1393 {
   1394 	struct sunos32_sys_sysconf_args /* {
   1395 		syscallarg(int) name;
   1396 	} */ *uap = v;
   1397 	extern int maxfiles;
   1398 
   1399 	switch(SCARG(uap, name)) {
   1400 	case SUNOS_SC_ARG_MAX:
   1401 		*retval = ARG_MAX;
   1402 		break;
   1403 	case SUNOS_SC_CHILD_MAX:
   1404 		*retval = maxproc;
   1405 		break;
   1406 	case SUNOS_SC_CLK_TCK:
   1407 		*retval = 60;		/* should this be `hz', ie. 100? */
   1408 		break;
   1409 	case SUNOS_SC_NGROUPS_MAX:
   1410 		*retval = NGROUPS_MAX;
   1411 		break;
   1412 	case SUNOS_SC_OPEN_MAX:
   1413 		*retval = maxfiles;
   1414 		break;
   1415 	case SUNOS_SC_JOB_CONTROL:
   1416 		*retval = 1;
   1417 		break;
   1418 	case SUNOS_SC_SAVED_IDS:
   1419 #ifdef _POSIX_SAVED_IDS
   1420 		*retval = 1;
   1421 #else
   1422 		*retval = 0;
   1423 #endif
   1424 		break;
   1425 	case SUNOS_SC_VERSION:
   1426 		*retval = 198808;
   1427 		break;
   1428 	default:
   1429 		return EINVAL;
   1430 	}
   1431 	return 0;
   1432 }
   1433 
   1434 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
   1435 #define SUNOS_RLIM_NLIMITS	7
   1436 
   1437 int
   1438 sunos32_sys_getrlimit(l, v, retval)
   1439 	struct lwp *l;
   1440 	void *v;
   1441 	register_t *retval;
   1442 {
   1443 	struct sunos32_sys_getrlimit_args /* {
   1444 		syscallarg(u_int) which;
   1445 		syscallarg(netbsd32_orlimitp_t) rlp;
   1446 	} */ *uap = v;
   1447 
   1448 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
   1449 		return EINVAL;
   1450 
   1451 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
   1452 		SCARG(uap, which) = RLIMIT_NOFILE;
   1453 
   1454 	return compat_43_netbsd32_ogetrlimit(l, uap, retval);
   1455 }
   1456 
   1457 int
   1458 sunos32_sys_setrlimit(l, v, retval)
   1459 	struct lwp *l;
   1460 	void *v;
   1461 	register_t *retval;
   1462 {
   1463 	struct sunos32_sys_setrlimit_args /* {
   1464 		syscallarg(u_int) which;
   1465 		syscallarg(netbsd32_orlimitp_t) rlp;
   1466 	} */ *uap = v;
   1467 
   1468 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
   1469 		return EINVAL;
   1470 
   1471 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
   1472 		SCARG(uap, which) = RLIMIT_NOFILE;
   1473 
   1474 	return compat_43_netbsd32_osetrlimit(l, uap, retval);
   1475 }
   1476 
   1477 #if defined(PTRACE) || defined(_LKM)
   1478 /* for the m68k machines */
   1479 #ifndef PT_GETFPREGS
   1480 #define PT_GETFPREGS -1
   1481 #endif
   1482 #ifndef PT_SETFPREGS
   1483 #define PT_SETFPREGS -1
   1484 #endif
   1485 
   1486 static const int sreq2breq[] = {
   1487 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
   1488 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
   1489 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
   1490 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
   1491 };
   1492 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
   1493 #endif
   1494 
   1495 int
   1496 sunos32_sys_ptrace(l, v, retval)
   1497 	struct lwp *l;
   1498 	void *v;
   1499 	register_t *retval;
   1500 {
   1501 #if defined(PTRACE) || defined(_LKM)
   1502 	struct sunos32_sys_ptrace_args /* {
   1503 		syscallarg(int) req;
   1504 		syscallarg(pid_t) pid;
   1505 		syscallarg(netbsd32_caddr_t) addr;
   1506 		syscallarg(int) data;
   1507 		syscallarg(netbsd32_charp) addr2;
   1508 	} */ *uap = v;
   1509 	struct netbsd32_ptrace_args pa;
   1510 	int req;
   1511 
   1512 #ifdef _LKM
   1513 #define sys_ptrace sysent[SYS_ptrace].sy_call
   1514 	if (sys_ptrace == sys_nosys)
   1515 		return ENOSYS;
   1516 #endif
   1517 
   1518 	req = SCARG(uap, req);
   1519 	if ((unsigned int)req >= nreqs)
   1520 		return (EINVAL);
   1521 
   1522 	req = sreq2breq[req];
   1523 	if (req == -1)
   1524 		return (EINVAL);
   1525 
   1526 	SCARG(&pa, req) = req;
   1527 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
   1528 	SCARG(&pa, addr) = SCARG(uap, addr);
   1529 	SCARG(&pa, data) = SCARG(uap, data);
   1530 
   1531 	return netbsd32_ptrace(l, &pa, retval);
   1532 #else
   1533 	return (ENOSYS);
   1534 #endif /* PTRACE || _LKM */
   1535 }
   1536 
   1537 /*
   1538  * SunOS reboot system call (for compatibility).
   1539  * Sun lets you pass in a boot string which the PROM
   1540  * saves and provides to the next boot program.
   1541  */
   1542 
   1543 #define SUNOS_RB_ASKNAME	0x001
   1544 #define SUNOS_RB_SINGLE 	0x002
   1545 #define SUNOS_RB_NOSYNC		0x004
   1546 #define SUNOS_RB_HALT		0x008
   1547 #define SUNOS_RB_DUMP		0x080
   1548 #define	SUNOS_RB_STRING		0x200
   1549 
   1550 static struct sunos_howto_conv {
   1551 	int sun_howto;
   1552 	int bsd_howto;
   1553 } sunos_howto_conv[] = {
   1554 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
   1555 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
   1556 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
   1557 	{ SUNOS_RB_HALT,	RB_HALT },
   1558 	{ SUNOS_RB_DUMP,	RB_DUMP },
   1559 	{ SUNOS_RB_STRING,	RB_STRING },
   1560 	{ 0x000,		0 },
   1561 };
   1562 
   1563 int
   1564 sunos32_sys_reboot(l, v, retval)
   1565 	struct lwp *l;
   1566 	void *v;
   1567 	register_t *retval;
   1568 {
   1569 	struct sunos32_sys_reboot_args /* {
   1570 		syscallarg(int) howto;
   1571 		syscallarg(netbsd32_charp) bootstr;
   1572 	} */ *uap = v;
   1573 	struct sys_reboot_args ua;
   1574 	struct sunos_howto_conv *convp;
   1575 	int error, bsd_howto, sun_howto;
   1576 	char *bootstr;
   1577 
   1578 	if ((error = kauth_authorize_system(l->l_cred,
   1579 	    KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
   1580 		return (error);
   1581 
   1582 	/*
   1583 	 * Convert howto bits to BSD format.
   1584 	 */
   1585 	sun_howto = SCARG(uap, howto);
   1586 	bsd_howto = 0;
   1587 	convp = sunos_howto_conv;
   1588 	while (convp->sun_howto) {
   1589 		if (sun_howto & convp->sun_howto)
   1590 			bsd_howto |= convp->bsd_howto;
   1591 		convp++;
   1592 	}
   1593 
   1594 	/*
   1595 	 * Sun RB_STRING (Get user supplied bootstring.)
   1596 	 * If the machine supports passing a string to the
   1597 	 * next booted kernel.
   1598 	 */
   1599 	if (sun_howto & SUNOS_RB_STRING)
   1600 		bootstr = (char *)(u_long)SCARG(uap, bootstr);
   1601 	else
   1602 		bootstr = NULL;
   1603 
   1604 	SCARG(&ua, opt) = bsd_howto;
   1605 	SCARG(&ua, bootstr) = bootstr;
   1606 	return (sys_reboot(l, &ua, retval));
   1607 }
   1608 
   1609 /*
   1610  * Generalized interface signal handler, 4.3-compatible.
   1611  */
   1612 /* ARGSUSED */
   1613 int
   1614 sunos32_sys_sigvec(l, v, retval)
   1615 	struct lwp *l;
   1616 	void *v;
   1617 	register_t *retval;
   1618 {
   1619 	struct sunos32_sys_sigvec_args /* {
   1620 		syscallarg(int) signum;
   1621 		syscallarg(struct sigvec *) nsv;
   1622 		syscallarg(struct sigvec *) osv;
   1623 	} */ *uap = v;
   1624 	struct proc *p = l->l_proc;
   1625 	struct netbsd32_sigvec sv;
   1626 	struct sigaction nsa, osa;
   1627 	int error;
   1628 
   1629 	if (SCARG(uap, nsv)) {
   1630 		error = copyin((caddr_t)(u_long)SCARG(uap, nsv), &sv, sizeof(sv));
   1631 		if (error != 0)
   1632 			return (error);
   1633 
   1634 		/*
   1635 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
   1636 		 * meaning: `reset to SIG_DFL on delivery'.
   1637 		 * We support only the bits in: 0xF
   1638 		 * (those bits are the same as ours)
   1639 		 */
   1640 		if (sv.sv_flags & ~0xF)
   1641 			return (EINVAL);
   1642 
   1643 		sunos32_sigvec_to_sigaction(&sv, &nsa);
   1644 	}
   1645 	error = sigaction1(p, SCARG(uap, signum),
   1646 			   SCARG(uap, nsv) ? &nsa : 0,
   1647 			   SCARG(uap, osv) ? &osa : 0,
   1648 			   NULL, 0);
   1649 	if (error != 0)
   1650 		return (error);
   1651 
   1652 	if (SCARG(uap, osv)) {
   1653 		sunos32_sigvec_from_sigaction(&sv, &osa);
   1654 		error = copyout(&sv, (caddr_t)(u_long)SCARG(uap, osv), sizeof(sv));
   1655 		if (error != 0)
   1656 			return (error);
   1657 	}
   1658 
   1659 	return (0);
   1660 }
   1661