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