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