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