Home | History | Annotate | Line # | Download | only in sunos32
sunos32_misc.c revision 1.1
      1 /*	$NetBSD: sunos32_misc.c,v 1.1 2001/02/02 07:28:54 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 int sunstatfs __P((struct statfs *, caddr_t));
    144 
    145 int
    146 sunos32_sys_stime(p, v, retval)
    147 	struct proc *p;
    148 	void *v;
    149 	register_t *retval;
    150 {
    151 	struct sunos32_sys_stime_args *uap = v;
    152 	struct sys_settimeofday_args ap;
    153 	caddr_t sg = stackgap_init(p->p_emul);
    154 	struct timeval tv, *sgtvp;
    155 	int error;
    156 
    157 	error = copyin((caddr_t)(u_long)SCARG(uap, tp), &tv.tv_sec, sizeof(tv.tv_sec));
    158 	if (error)
    159 		return error;
    160 	tv.tv_usec = 0;
    161 
    162 	SCARG(&ap, tv) = sgtvp = stackgap_alloc(&sg, sizeof(struct timeval));
    163 	SCARG(&ap, tzp) = NULL;
    164 
    165 	error = copyout(&tv, sgtvp, sizeof(struct timeval));
    166 	if (error)
    167 		return error;
    168 
    169 	return sys_settimeofday(p, &ap, retval);
    170 }
    171 
    172 int
    173 sunos32_sys_wait4(p, v, retval)
    174 	struct proc *p;
    175 	void *v;
    176 	register_t *retval;
    177 {
    178 	struct sunos32_sys_wait4_args *uap = v;
    179 
    180 	if (SCARG(uap, pid) == 0)
    181 		SCARG(uap, pid) = WAIT_ANY;
    182 	return (netbsd32_wait4(p, uap, retval));
    183 }
    184 
    185 int
    186 sunos32_sys_creat(p, v, retval)
    187 	struct proc *p;
    188 	void *v;
    189 	register_t *retval;
    190 {
    191 	struct sunos32_sys_creat_args *uap = v;
    192 	struct sys_open_args ua;
    193 	caddr_t sg = stackgap_init(p->p_emul);
    194 
    195 	SUNOS32_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    196 	SUNOS32TOP_UAP(path, const char);
    197 	SCARG(&ua, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    198 	SUNOS32TO64_UAP(mode);
    199 
    200 	return (sys_open(p, &ua, retval));
    201 }
    202 
    203 int
    204 sunos32_sys_access(p, v, retval)
    205 	struct proc *p;
    206 	void *v;
    207 	register_t *retval;
    208 {
    209 	struct sunos32_sys_access_args *uap = v;
    210 	struct sys_access_args ua;
    211 	caddr_t sg = stackgap_init(p->p_emul);
    212 
    213 	SUNOS32TOP_UAP(path, const char);
    214 	SUNOS32TO64_UAP(flags);
    215 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
    216 
    217 	return (sys_access(p, &ua, retval));
    218 }
    219 
    220 int
    221 sunos32_sys_stat(p, v, retval)
    222 	struct proc *p;
    223 	void *v;
    224 	register_t *retval;
    225 {
    226 	struct sunos32_sys_stat_args *uap = v;
    227 	struct netbsd32_stat43 *sp32;
    228 	struct stat43 sb43;
    229 	struct stat43 *sp43 = &sb43;
    230 	struct compat_43_sys_stat_args ua;
    231 	int rv;
    232 	caddr_t sg;
    233 
    234 	SUNOS32TOP_UAP(path, const char);
    235 	SCARG(&ua, ub) = &sb43;
    236 	sg = stackgap_init(p->p_emul);
    237 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
    238 
    239 	rv = compat_43_sys_stat(p, &ua, retval);
    240 
    241 	sp32 = (struct netbsd32_stat43 *)(u_long)SCARG(uap, ub);
    242 	netbsd32_from_stat43(sp43, sp32);
    243 
    244 	return (rv);
    245 }
    246 
    247 int
    248 sunos32_sys_lstat(p, v, retval)
    249 	struct proc *p;
    250 	void *v;
    251 	register_t *retval;
    252 {
    253 	struct sunos32_sys_lstat_args *uap = v;
    254 	struct compat_43_sys_lstat_args ua;
    255 	struct netbsd32_stat43 *sp32;
    256 	struct stat43 sb43;
    257 	struct stat43 *sp43 = &sb43;
    258 	caddr_t sg = stackgap_init(p->p_emul);
    259 	int rv;
    260 
    261 	SUNOS32TOP_UAP(path, const char);
    262 	SCARG(&ua, ub) = &sb43;
    263 	sg = stackgap_init(p->p_emul);
    264 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    265 
    266 	rv = compat_43_sys_stat(p, &ua, retval);
    267 
    268 	sp32 = (struct netbsd32_stat43 *)(u_long)SCARG(uap, ub);
    269 	netbsd32_from_stat43(sp43, sp32);
    270 
    271 	return (rv);
    272 }
    273 
    274 int
    275 sunos32_sys_execv(p, v, retval)
    276 	struct proc *p;
    277 	void *v;
    278 	register_t *retval;
    279 {
    280 	struct sunos32_sys_execv_args /* {
    281 		syscallarg(const char *) path;
    282 		syscallarg(char **) argv;
    283 	} */ *uap = v;
    284 	struct sys_execve_args ua;
    285 	caddr_t sg;
    286 
    287 	SUNOS32TOP_UAP(path, const char);
    288 	SUNOS32TOP_UAP(argp, char *);
    289 	SCARG(&ua, envp) = NULL;
    290 	sg = stackgap_init(p->p_emul);
    291 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
    292 
    293 	return netbsd32_execve2(p, &ua, retval);
    294 }
    295 
    296 int
    297 sunos32_sys_execve(p, v, retval)
    298 	struct proc *p;
    299 	void *v;
    300 	register_t *retval;
    301 {
    302 	struct sunos32_sys_execve_args /* {
    303 		syscallarg(const char *) path;
    304 		syscallarg(char **) argv;
    305 		syscallarg(char **) envp;
    306 	} */ *uap = v;
    307 	struct sys_execve_args ua;
    308 	caddr_t sg;
    309 
    310 	SUNOS32TOP_UAP(path, const char);
    311 	SUNOS32TOP_UAP(argp, char *);
    312 	SUNOS32TOP_UAP(envp, char *);
    313 	sg = stackgap_init(p->p_emul);
    314 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    315 
    316 	return netbsd32_execve2(p, &ua, retval);
    317 }
    318 
    319 int
    320 sunos32_sys_omsync(p, v, retval)
    321 	struct proc *p;
    322 	void *v;
    323 	register_t *retval;
    324 {
    325 	struct sunos32_sys_omsync_args *uap = v;
    326 	struct netbsd32___msync13_args ouap;
    327 
    328 	SCARG(&ouap, addr) = SCARG(uap, addr);
    329 	SCARG(&ouap, len) = SCARG(uap, len);
    330 	SCARG(&ouap, flags) = SCARG(uap, flags);
    331 
    332 	return (netbsd32___msync13(p, &ouap, retval));
    333 }
    334 
    335 int
    336 sunos32_sys_unmount(p, v, retval)
    337 	struct proc *p;
    338 	void *v;
    339 	register_t *retval;
    340 {
    341 	struct sunos32_sys_unmount_args *uap = v;
    342 	struct sys_unmount_args ua;
    343 
    344 	SUNOS32TOP_UAP(path, const char);
    345 	SCARG(&ua, flags) = 0;
    346 
    347 	return (sys_unmount(p, &ua, retval));
    348 }
    349 
    350 /*
    351  * Conversion table for SunOS NFS mount flags.
    352  */
    353 static struct {
    354 	int	sun_flg;
    355 	int	bsd_flg;
    356 } sunnfs_flgtab[] = {
    357 	{ SUNNFS_SOFT,		NFSMNT_SOFT },
    358 	{ SUNNFS_WSIZE,		NFSMNT_WSIZE },
    359 	{ SUNNFS_RSIZE,		NFSMNT_RSIZE },
    360 	{ SUNNFS_TIMEO,		NFSMNT_TIMEO },
    361 	{ SUNNFS_RETRANS,	NFSMNT_RETRANS },
    362 	{ SUNNFS_HOSTNAME,	0 },			/* Ignored */
    363 	{ SUNNFS_INT,		NFSMNT_INT },
    364 	{ SUNNFS_NOAC,		0 },			/* Ignored */
    365 	{ SUNNFS_ACREGMIN,	0 },			/* Ignored */
    366 	{ SUNNFS_ACREGMAX,	0 },			/* Ignored */
    367 	{ SUNNFS_ACDIRMIN,	0 },			/* Ignored */
    368 	{ SUNNFS_ACDIRMAX,	0 },			/* Ignored */
    369 	{ SUNNFS_SECURE,	0 },			/* Ignored */
    370 	{ SUNNFS_NOCTO,		0 },			/* Ignored */
    371 	{ SUNNFS_POSIX,		0 }			/* Ignored */
    372 };
    373 
    374 int
    375 sunos32_sys_mount(p, v, retval)
    376 	struct proc *p;
    377 	void *v;
    378 	register_t *retval;
    379 {
    380 	struct sunos32_sys_mount_args *uap = v;
    381 	struct sys_mount_args ua;
    382 	int oflags = SCARG(uap, flags), nflags, error;
    383 	char fsname[MFSNAMELEN];
    384 	caddr_t sg = stackgap_init(p->p_emul);
    385 
    386 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
    387 		return (EINVAL);
    388 	if ((oflags & SUNM_NEWTYPE) == 0)
    389 		return (EINVAL);
    390 	nflags = 0;
    391 	if (oflags & SUNM_RDONLY)
    392 		nflags |= MNT_RDONLY;
    393 	if (oflags & SUNM_NOSUID)
    394 		nflags |= MNT_NOSUID;
    395 	if (oflags & SUNM_REMOUNT)
    396 		nflags |= MNT_UPDATE;
    397 	SCARG(uap, flags) = nflags;
    398 
    399 	error = copyinstr((caddr_t)(u_long)SCARG(uap, type), fsname,
    400 	    sizeof fsname, (size_t *)0);
    401 	if (error)
    402 		return (error);
    403 
    404 	if (strncmp(fsname, "4.2", sizeof fsname) == 0) {
    405 		SCARG(uap, type) = (netbsd32_charp)(u_long)stackgap_alloc(&sg, sizeof("ffs"));
    406 		error = copyout("ffs", (caddr_t)(u_long)SCARG(uap, type), sizeof("ffs"));
    407 		if (error)
    408 			return (error);
    409 	} else if (strncmp(fsname, "nfs", sizeof fsname) == 0) {
    410 		struct sunos_nfs_args sna;
    411 		struct sockaddr_in sain;
    412 		struct nfs_args na;	/* XXX */
    413 		struct sockaddr sa;
    414 		int n;
    415 
    416 		error = copyin((caddr_t)(u_long)SCARG(uap, data), &sna, sizeof sna);
    417 		if (error)
    418 			return (error);
    419 		error = copyin(sna.addr, &sain, sizeof sain);
    420 		if (error)
    421 			return (error);
    422 		memcpy(&sa, &sain, sizeof sa);
    423 		sa.sa_len = sizeof(sain);
    424 		SCARG(uap, data) = (netbsd32_charp)(u_long)stackgap_alloc(&sg, sizeof(na));
    425 		na.version = NFS_ARGSVERSION;
    426 		na.addr = stackgap_alloc(&sg, sizeof(struct sockaddr));
    427 		na.addrlen = sizeof(struct sockaddr);
    428 		na.sotype = SOCK_DGRAM;
    429 		na.proto = IPPROTO_UDP;
    430 		na.fh = (void *)sna.fh;
    431 		na.fhsize = NFSX_V2FH;
    432 		na.flags = 0;
    433 		n = sizeof(sunnfs_flgtab) / sizeof(sunnfs_flgtab[0]);
    434 		while (--n >= 0)
    435 			if (sna.flags & sunnfs_flgtab[n].sun_flg)
    436 				na.flags |= sunnfs_flgtab[n].bsd_flg;
    437 		na.wsize = sna.wsize;
    438 		na.rsize = sna.rsize;
    439 		if (na.flags & NFSMNT_RSIZE) {
    440 			na.flags |= NFSMNT_READDIRSIZE;
    441 			na.readdirsize = na.rsize;
    442 		}
    443 		na.timeo = sna.timeo;
    444 		na.retrans = sna.retrans;
    445 		na.hostname = (char *)(u_long)sna.hostname;
    446 
    447 		error = copyout(&sa, na.addr, sizeof sa);
    448 		if (error)
    449 			return (error);
    450 		error = copyout(&na, (caddr_t)(u_long)SCARG(uap, data), sizeof na);
    451 		if (error)
    452 			return (error);
    453 	}
    454 	SUNOS32TOP_UAP(type, const char);
    455 	SUNOS32TOP_UAP(path, const char);
    456 	SUNOS32TO64_UAP(flags);
    457 	SUNOS32TOP_UAP(data, void);
    458 	return (sys_mount(p, &ua, retval));
    459 }
    460 
    461 #if defined(NFS)
    462 int
    463 async_daemon(p, v, retval)
    464 	struct proc *p;
    465 	void *v;
    466 	register_t *retval;
    467 {
    468 	struct netbsd32_nfssvc_args ouap;
    469 
    470 	SCARG(&ouap, flag) = NFSSVC_BIOD;
    471 	SCARG(&ouap, argp) = NULL;
    472 
    473 	return (netbsd32_nfssvc(p, &ouap, retval));
    474 }
    475 #endif /* NFS */
    476 
    477 void	native_to_sunos_sigset __P((const sigset_t *, int *));
    478 void	sunos_to_native_sigset __P((const int, sigset_t *));
    479 
    480 __inline__ void
    481 native_to_sunos_sigset(ss, mask)
    482 	const sigset_t *ss;
    483 	int *mask;
    484 {
    485 	*mask = ss->__bits[0];
    486 }
    487 
    488 __inline__ void
    489 sunos_to_native_sigset(mask, ss)
    490 	const int mask;
    491 	sigset_t *ss;
    492 {
    493 
    494 	ss->__bits[0] = mask;
    495 	ss->__bits[1] = 0;
    496 	ss->__bits[2] = 0;
    497 	ss->__bits[3] = 0;
    498 }
    499 
    500 int
    501 sunos32_sys_sigpending(p, v, retval)
    502 	struct proc *p;
    503 	void *v;
    504 	register_t *retval;
    505 {
    506 	struct sunos32_sys_sigpending_args *uap = v;
    507 	sigset_t ss;
    508 	int mask;
    509 
    510 	sigpending1(p, &ss);
    511 	native_to_sunos_sigset(&ss, &mask);
    512 
    513 	return (copyout((caddr_t)(u_long)&mask, (caddr_t)(u_long)SCARG(uap, mask), sizeof(int)));
    514 }
    515 
    516 int
    517 sunos32_sys_sigsuspend(p, v, retval)
    518 	struct proc *p;
    519 	void *v;
    520 	register_t *retval;
    521 {
    522 	struct sunos32_sys_sigsuspend_args /* {
    523 		syscallarg(int) mask;
    524 	} */ *uap = v;
    525 	int mask;
    526 	sigset_t ss;
    527 
    528 	mask = SCARG(uap, mask);
    529 	sunos_to_native_sigset(mask, &ss);
    530 	return (sigsuspend1(p, &ss));
    531 }
    532 
    533 /*
    534  * Read Sun-style directory entries.  We suck them into kernel space so
    535  * that they can be massaged before being copied out to user code.  Like
    536  * SunOS, we squish out `empty' entries.
    537  *
    538  * This is quite ugly, but what do you expect from compatibility code?
    539  */
    540 int
    541 sunos32_sys_getdents(p, v, retval)
    542 	struct proc *p;
    543 	void *v;
    544 	register_t *retval;
    545 {
    546 	struct sunos32_sys_getdents_args *uap = v;
    547 	struct dirent *bdp;
    548 	struct vnode *vp;
    549 	caddr_t inp, buf;	/* BSD-format */
    550 	int len, reclen;	/* BSD-format */
    551 	caddr_t outp;		/* Sun-format */
    552 	int resid, sunos_reclen;/* Sun-format */
    553 	struct file *fp;
    554 	struct uio auio;
    555 	struct iovec aiov;
    556 	struct sunos_dirent idb;
    557 	off_t off;			/* true file offset */
    558 	int buflen, error, eofflag;
    559 	off_t *cookiebuf, *cookie;
    560 	int ncookies;
    561 
    562 	/* getvnode() will use the descriptor for us */
    563 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    564 		return (error);
    565 
    566 	if ((fp->f_flag & FREAD) == 0) {
    567 		error = EBADF;
    568 		goto out1;
    569 	}
    570 
    571 	vp = (struct vnode *)fp->f_data;
    572 	if (vp->v_type != VDIR) {
    573 		error = EINVAL;
    574 		goto out1;
    575 	}
    576 
    577 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
    578 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    579 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    580 	off = fp->f_offset;
    581 again:
    582 	aiov.iov_base = buf;
    583 	aiov.iov_len = buflen;
    584 	auio.uio_iov = &aiov;
    585 	auio.uio_iovcnt = 1;
    586 	auio.uio_rw = UIO_READ;
    587 	auio.uio_segflg = UIO_SYSSPACE;
    588 	auio.uio_procp = p;
    589 	auio.uio_resid = buflen;
    590 	auio.uio_offset = off;
    591 	/*
    592 	 * First we read into the malloc'ed buffer, then
    593 	 * we massage it into user space, one record at a time.
    594 	 */
    595 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    596 	    &ncookies);
    597 	if (error)
    598 		goto out;
    599 
    600 	inp = buf;
    601 	outp = (caddr_t)(u_long)SCARG(uap, buf);
    602 	resid = SCARG(uap, nbytes);
    603 	if ((len = buflen - auio.uio_resid) == 0)
    604 		goto eof;
    605 
    606 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    607 		bdp = (struct dirent *)inp;
    608 		reclen = bdp->d_reclen;
    609 		if (reclen & 3)
    610 			panic("sunos_getdents");
    611 		if ((*cookie >> 32) != 0) {
    612 			compat_offseterr(vp, "sunos_getdents");
    613 			error = EINVAL;
    614 			goto out;
    615 		}
    616 		if (bdp->d_fileno == 0) {
    617 			inp += reclen;	/* it is a hole; squish it out */
    618 			off = *cookie++;
    619 			continue;
    620 		}
    621 		sunos_reclen = SUNOS_RECLEN(&idb, bdp->d_namlen);
    622 		if (reclen > len || resid < sunos_reclen) {
    623 			/* entry too big for buffer, so just stop */
    624 			outp++;
    625 			break;
    626 		}
    627 		off = *cookie++;	/* each entry points to next */
    628 		/*
    629 		 * Massage in place to make a Sun-shaped dirent (otherwise
    630 		 * we have to worry about touching user memory outside of
    631 		 * the copyout() call).
    632 		 */
    633 		idb.d_fileno = bdp->d_fileno;
    634 		idb.d_off = off;
    635 		idb.d_reclen = sunos_reclen;
    636 		idb.d_namlen = bdp->d_namlen;
    637 		strcpy(idb.d_name, bdp->d_name);
    638 		if ((error = copyout((caddr_t)&idb, outp, sunos_reclen)) != 0)
    639 			goto out;
    640 		/* advance past this real entry */
    641 		inp += reclen;
    642 		/* advance output past Sun-shaped entry */
    643 		outp += sunos_reclen;
    644 		resid -= sunos_reclen;
    645 	}
    646 
    647 	/* if we squished out the whole block, try again */
    648 	if (outp == (caddr_t)(u_long)SCARG(uap, buf))
    649 		goto again;
    650 	fp->f_offset = off;		/* update the vnode offset */
    651 
    652 eof:
    653 	*retval = SCARG(uap, nbytes) - resid;
    654 out:
    655 	VOP_UNLOCK(vp, 0);
    656 	free(cookiebuf, M_TEMP);
    657 	free(buf, M_TEMP);
    658  out1:
    659 	FILE_UNUSE(fp, p);
    660 	return (error);
    661 }
    662 
    663 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    664 
    665 int
    666 sunos32_sys_mmap(p, v, retval)
    667 	struct proc *p;
    668 	void *v;
    669 	register_t *retval;
    670 {
    671 	struct sunos32_sys_mmap_args *uap = v;
    672 	struct sys_mmap_args ua;
    673 	struct filedesc *fdp;
    674 	struct file *fp;
    675 	struct vnode *vp;
    676 
    677 	/*
    678 	 * Verify the arguments.
    679 	 */
    680 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    681 		return (EINVAL);			/* XXX still needed? */
    682 
    683 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
    684 		return (EINVAL);
    685 
    686 	SCARG(&ua, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
    687 	SUNOS32TOP_UAP(addr, void);
    688 
    689 	if ((SCARG(&ua, flags) & MAP_FIXED) == 0 &&
    690 	    SCARG(&ua, addr) != 0 &&
    691 	    SCARG(&ua, addr) < (void *)round_page((vaddr_t)p->p_vmspace->vm_daddr+MAXDSIZ))
    692 		SCARG(&ua, addr) = (void *)round_page((vaddr_t)p->p_vmspace->vm_daddr+MAXDSIZ);
    693 
    694 	SUNOS32TOX_UAP(len, size_t);
    695 	SUNOS32TO64_UAP(prot);
    696 	SUNOS32TO64_UAP(fd);
    697 	SUNOS32TO64_UAP(pos);
    698 
    699 	/*
    700 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
    701 	 */
    702 	fdp = p->p_fd;
    703 	if ((unsigned)SCARG(&ua, fd) < fdp->fd_nfiles &&		/*XXX*/
    704 	    (fp = fdp->fd_ofiles[SCARG(&ua, fd)]) != NULL &&		/*XXX*/
    705 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
    706 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
    707 	    iszerodev(vp->v_rdev)) {					/*XXX*/
    708 		SCARG(&ua, flags) |= MAP_ANON;
    709 		SCARG(&ua, fd) = -1;
    710 	}
    711 
    712 	return (sys_mmap(p, &ua, retval));
    713 }
    714 
    715 #define	MC_SYNC		1
    716 #define	MC_LOCK		2
    717 #define	MC_UNLOCK	3
    718 #define	MC_ADVISE	4
    719 #define	MC_LOCKAS	5
    720 #define	MC_UNLOCKAS	6
    721 
    722 int
    723 sunos32_sys_mctl(p, v, retval)
    724 	struct proc *p;
    725 	void *v;
    726 	register_t *retval;
    727 {
    728 	struct sunos32_sys_mctl_args *uap = v;
    729 
    730 	switch (SCARG(uap, func)) {
    731 	case MC_ADVISE:		/* ignore for now */
    732 		return (0);
    733 	case MC_SYNC:		/* translate to msync */
    734 		return (netbsd32___msync13(p, uap, retval));
    735 	default:
    736 		return (EINVAL);
    737 	}
    738 }
    739 
    740 int
    741 sunos32_sys_setsockopt(p, v, retval)
    742 	struct proc *p;
    743 	void *v;
    744 	register_t *retval;
    745 {
    746 	struct sunos32_sys_setsockopt_args *uap = v;
    747 	struct file *fp;
    748 	struct mbuf *m = NULL;
    749 	int error;
    750 
    751 	/* getsock() will use the descriptor for us */
    752 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    753 		return (error);
    754 #define	SO_DONTLINGER (~SO_LINGER)
    755 	if (SCARG(uap, name) == SO_DONTLINGER) {
    756 		m = m_get(M_WAIT, MT_SOOPTS);
    757 		mtod(m, struct linger *)->l_onoff = 0;
    758 		m->m_len = sizeof(struct linger);
    759 		error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    760 		    SO_LINGER, m);
    761 		goto out;
    762 	}
    763 	if (SCARG(uap, level) == IPPROTO_IP) {
    764 #define		SUNOS_IP_MULTICAST_IF		2
    765 #define		SUNOS_IP_MULTICAST_TTL		3
    766 #define		SUNOS_IP_MULTICAST_LOOP		4
    767 #define		SUNOS_IP_ADD_MEMBERSHIP		5
    768 #define		SUNOS_IP_DROP_MEMBERSHIP	6
    769 		static int ipoptxlat[] = {
    770 			IP_MULTICAST_IF,
    771 			IP_MULTICAST_TTL,
    772 			IP_MULTICAST_LOOP,
    773 			IP_ADD_MEMBERSHIP,
    774 			IP_DROP_MEMBERSHIP
    775 		};
    776 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
    777 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
    778 			SCARG(uap, name) =
    779 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
    780 		}
    781 	}
    782 	if (SCARG(uap, valsize) > MLEN) {
    783 		error = EINVAL;
    784 		goto out;
    785 	}
    786 	if (SCARG(uap, val)) {
    787 		m = m_get(M_WAIT, MT_SOOPTS);
    788 		error = copyin((caddr_t)(u_long)SCARG(uap, val), mtod(m, caddr_t),
    789 		    (u_int)SCARG(uap, valsize));
    790 		if (error) {
    791 			(void) m_free(m);
    792 			goto out;
    793 		}
    794 		m->m_len = SCARG(uap, valsize);
    795 	}
    796 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    797 	    SCARG(uap, name), m);
    798  out:
    799 	FILE_UNUSE(fp, p);
    800 	return (error);
    801 }
    802 
    803 /*
    804  * XXX: This needs cleaning up.
    805  */
    806 int
    807 sunos32_sys_auditsys(p, v, retval)
    808 	struct proc *p;
    809 	void *v;
    810 	register_t *retval;
    811 {
    812 	return 0;
    813 }
    814 
    815 int
    816 sunos32_sys_uname(p, v, retval)
    817 	struct proc *p;
    818 	void *v;
    819 	register_t *retval;
    820 {
    821 	struct sunos32_sys_uname_args *uap = v;
    822 	struct sunos_utsname sut;
    823 
    824 	memset(&sut, 0, sizeof(sut));
    825 
    826 	memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
    827 	memcpy(sut.nodename, hostname, sizeof(sut.nodename));
    828 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
    829 	memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
    830 	memcpy(sut.version, "1", sizeof(sut.version) - 1);
    831 	memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
    832 
    833 	return copyout((caddr_t)&sut, (caddr_t)(u_long)SCARG(uap, name),
    834 	    sizeof(struct sunos_utsname));
    835 }
    836 
    837 int
    838 sunos32_sys_setpgrp(p, v, retval)
    839 	struct proc *p;
    840 	void *v;
    841 	register_t *retval;
    842 {
    843 	struct sunos32_sys_setpgrp_args *uap = v;
    844 
    845 	/*
    846 	 * difference to our setpgid call is to include backwards
    847 	 * compatibility to pre-setsid() binaries. Do setsid()
    848 	 * instead of setpgid() in those cases where the process
    849 	 * tries to create a new session the old way.
    850 	 */
    851 	if (!SCARG(uap, pgid) &&
    852 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
    853 		return sys_setsid(p, uap, retval);
    854 	else
    855 		return netbsd32_setpgid(p, uap, retval);
    856 }
    857 
    858 int
    859 sunos32_sys_open(p, v, retval)
    860 	struct proc *p;
    861 	void *v;
    862 	register_t *retval;
    863 {
    864 	struct sunos32_sys_open_args *uap = v;
    865 	struct sys_open_args ua;
    866 	int l, r;
    867 	int noctty;
    868 	int ret;
    869 
    870 	caddr_t sg = stackgap_init(p->p_emul);
    871 
    872 	/* convert mode into NetBSD mode */
    873 	l = SCARG(uap, flags);
    874 	noctty = l & 0x8000;
    875 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
    876 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
    877 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
    878 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
    879 	r |=	((l & 0x2000) ? O_FSYNC : 0);
    880 
    881 	SUNOS32TOP_UAP(path, const char);
    882 	SCARG(&ua, flags) = r;
    883 	SUNOS32TO64_UAP(mode);
    884 
    885 	if (r & O_CREAT)
    886 		SUNOS32_CHECK_ALT_CREAT(p, &sg, SCARG(&ua, path));
    887 	else
    888 		SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(&ua, path));
    889 
    890 	ret = sys_open(p, &ua, retval);
    891 
    892 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
    893 		struct filedesc *fdp = p->p_fd;
    894 		struct file *fp = fdp->fd_ofiles[*retval];
    895 
    896 		/* ignore any error, just give it a try */
    897 		if (fp->f_type == DTYPE_VNODE)
    898 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
    899 	}
    900 	return ret;
    901 }
    902 
    903 #if defined (NFSSERVER)
    904 int
    905 sunos32_sys_nfssvc(p, v, retval)
    906 	struct proc *p;
    907 	void *v;
    908 	register_t *retval;
    909 {
    910 #if 0
    911 	struct sunos32_sys_nfssvc_args *uap = v;
    912 	struct emul *e = p->p_emul;
    913 	struct sys_nfssvc_args outuap;
    914 	struct sockaddr sa;
    915 	int error;
    916 	caddr_t sg = stackgap_init(p->p_emul);
    917 
    918 	memset(&outuap, 0, sizeof outuap);
    919 	SCARG(&outuap, fd) = SCARG(uap, fd);
    920 	SCARG(&outuap, mskval) = stackgap_alloc(&sg, sizeof(sa));
    921 	SCARG(&outuap, msklen) = sizeof(sa);
    922 	SCARG(&outuap, mtchval) = stackgap_alloc(&sg, sizeof(sa));
    923 	SCARG(&outuap, mtchlen) = sizeof(sa);
    924 
    925 	memset(&sa, 0, sizeof sa);
    926 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
    927 		return (error);
    928 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
    929 		return (error);
    930 
    931 	return nfssvc(p, &outuap, retval);
    932 #else
    933 	return (ENOSYS);
    934 #endif
    935 }
    936 #endif /* NFSSERVER */
    937 
    938 int
    939 sunos32_sys_ustat(p, v, retval)
    940 	struct proc *p;
    941 	void *v;
    942 	register_t *retval;
    943 {
    944 	struct sunos32_sys_ustat_args *uap = v;
    945 	struct sunos_ustat us;
    946 	int error;
    947 
    948 	memset(&us, 0, sizeof us);
    949 
    950 	/*
    951 	 * XXX: should set f_tfree and f_tinode at least
    952 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
    953 	 */
    954 
    955 	if ((error = copyout(&us, (caddr_t)(u_long)SCARG(uap, buf), sizeof us)) != 0)
    956 		return (error);
    957 	return 0;
    958 }
    959 
    960 int
    961 sunos32_sys_quotactl(p, v, retval)
    962 	struct proc *p;
    963 	void *v;
    964 	register_t *retval;
    965 {
    966 
    967 	return EINVAL;
    968 }
    969 
    970 int
    971 sunos32_sys_vhangup(p, v, retval)
    972 	struct proc *p;
    973 	void *v;
    974 	register_t *retval;
    975 {
    976 	struct session *sp = p->p_session;
    977 
    978 	if (sp->s_ttyvp == 0)
    979 		return 0;
    980 
    981 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
    982 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
    983 
    984 	(void) ttywait(sp->s_ttyp);
    985 	if (sp->s_ttyvp)
    986 		VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
    987 	if (sp->s_ttyvp)
    988 		vrele(sp->s_ttyvp);
    989 	sp->s_ttyvp = NULL;
    990 
    991 	return 0;
    992 }
    993 
    994 static int
    995 sunstatfs(sp, buf)
    996 	struct statfs *sp;
    997 	caddr_t buf;
    998 {
    999 	struct sunos_statfs ssfs;
   1000 
   1001 	memset(&ssfs, 0, sizeof ssfs);
   1002 	ssfs.f_type = 0;
   1003 	ssfs.f_bsize = sp->f_bsize;
   1004 	ssfs.f_blocks = sp->f_blocks;
   1005 	ssfs.f_bfree = sp->f_bfree;
   1006 	ssfs.f_bavail = sp->f_bavail;
   1007 	ssfs.f_files = sp->f_files;
   1008 	ssfs.f_ffree = sp->f_ffree;
   1009 	ssfs.f_fsid = sp->f_fsid;
   1010 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
   1011 }
   1012 
   1013 int
   1014 sunos32_sys_statfs(p, v, retval)
   1015 	struct proc *p;
   1016 	void *v;
   1017 	register_t *retval;
   1018 {
   1019 	struct sunos32_sys_statfs_args *uap = v;
   1020 	struct mount *mp;
   1021 	struct statfs *sp;
   1022 	int error;
   1023 	struct nameidata nd;
   1024 
   1025 	caddr_t sg = stackgap_init(p->p_emul);
   1026 	SUNOS32_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
   1027 
   1028 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, (caddr_t)(u_long)SCARG(uap, path), p);
   1029 	if ((error = namei(&nd)) != 0)
   1030 		return (error);
   1031 	mp = nd.ni_vp->v_mount;
   1032 	sp = &mp->mnt_stat;
   1033 	vrele(nd.ni_vp);
   1034 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
   1035 		return (error);
   1036 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   1037 	return sunstatfs(sp, (caddr_t)(u_long)SCARG(uap, buf));
   1038 }
   1039 
   1040 int
   1041 sunos32_sys_fstatfs(p, v, retval)
   1042 	struct proc *p;
   1043 	void *v;
   1044 	register_t *retval;
   1045 {
   1046 	struct sunos32_sys_fstatfs_args *uap = v;
   1047 	struct file *fp;
   1048 	struct mount *mp;
   1049 	struct statfs *sp;
   1050 	int error;
   1051 
   1052 	/* getvnode() will use the descriptor for us */
   1053 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1054 		return (error);
   1055 	mp = ((struct vnode *)fp->f_data)->v_mount;
   1056 	sp = &mp->mnt_stat;
   1057 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
   1058 		goto out;
   1059 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   1060 	error = sunstatfs(sp, (caddr_t)(u_long)SCARG(uap, buf));
   1061  out:
   1062 	FILE_UNUSE(fp, p);
   1063 	return (error);
   1064 }
   1065 
   1066 int
   1067 sunos32_sys_exportfs(p, v, retval)
   1068 	struct proc *p;
   1069 	void *v;
   1070 	register_t *retval;
   1071 {
   1072 	/*
   1073 	 * XXX: should perhaps translate into a mount(2)
   1074 	 * with MOUNT_EXPORT?
   1075 	 */
   1076 	return 0;
   1077 }
   1078 
   1079 int
   1080 sunos32_sys_mknod(p, v, retval)
   1081 	struct proc *p;
   1082 	void *v;
   1083 	register_t *retval;
   1084 {
   1085 	struct sunos32_sys_mknod_args *uap = v;
   1086 
   1087 	caddr_t sg = stackgap_init(p->p_emul);
   1088 	SUNOS32_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
   1089 
   1090 	/* netbsd32_mkfifo/mknod to not do alt checking */
   1091 	if (S_ISFIFO(SCARG(uap, mode)))
   1092 		return netbsd32_mkfifo(p, (struct netbsd32_mkfifo_args *)uap, retval);
   1093 
   1094 	return netbsd32_mknod(p, (struct netbsd32_mknod_args *)uap, retval);
   1095 }
   1096 
   1097 #define SUNOS_SC_ARG_MAX	1
   1098 #define SUNOS_SC_CHILD_MAX	2
   1099 #define SUNOS_SC_CLK_TCK	3
   1100 #define SUNOS_SC_NGROUPS_MAX	4
   1101 #define SUNOS_SC_OPEN_MAX	5
   1102 #define SUNOS_SC_JOB_CONTROL	6
   1103 #define SUNOS_SC_SAVED_IDS	7
   1104 #define SUNOS_SC_VERSION	8
   1105 
   1106 int
   1107 sunos32_sys_sysconf(p, v, retval)
   1108 	struct proc *p;
   1109 	void *v;
   1110 	register_t *retval;
   1111 {
   1112 	struct sunos32_sys_sysconf_args *uap = v;
   1113 	extern int maxfiles;
   1114 
   1115 	switch(SCARG(uap, name)) {
   1116 	case SUNOS_SC_ARG_MAX:
   1117 		*retval = ARG_MAX;
   1118 		break;
   1119 	case SUNOS_SC_CHILD_MAX:
   1120 		*retval = maxproc;
   1121 		break;
   1122 	case SUNOS_SC_CLK_TCK:
   1123 		*retval = 60;		/* should this be `hz', ie. 100? */
   1124 		break;
   1125 	case SUNOS_SC_NGROUPS_MAX:
   1126 		*retval = NGROUPS_MAX;
   1127 		break;
   1128 	case SUNOS_SC_OPEN_MAX:
   1129 		*retval = maxfiles;
   1130 		break;
   1131 	case SUNOS_SC_JOB_CONTROL:
   1132 		*retval = 1;
   1133 		break;
   1134 	case SUNOS_SC_SAVED_IDS:
   1135 #ifdef _POSIX_SAVED_IDS
   1136 		*retval = 1;
   1137 #else
   1138 		*retval = 0;
   1139 #endif
   1140 		break;
   1141 	case SUNOS_SC_VERSION:
   1142 		*retval = 198808;
   1143 		break;
   1144 	default:
   1145 		return EINVAL;
   1146 	}
   1147 	return 0;
   1148 }
   1149 
   1150 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
   1151 #define SUNOS_RLIM_NLIMITS	7
   1152 
   1153 int
   1154 sunos32_sys_getrlimit(p, v, retval)
   1155 	struct proc *p;
   1156 	void *v;
   1157 	register_t *retval;
   1158 {
   1159 	struct sunos32_sys_getrlimit_args *uap = v;
   1160 
   1161 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
   1162 		return EINVAL;
   1163 
   1164 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
   1165 		SCARG(uap, which) = RLIMIT_NOFILE;
   1166 
   1167 	return compat_43_netbsd32_ogetrlimit(p, uap, retval);
   1168 }
   1169 
   1170 int
   1171 sunos32_sys_setrlimit(p, v, retval)
   1172 	struct proc *p;
   1173 	void *v;
   1174 	register_t *retval;
   1175 {
   1176 	struct sunos32_sys_getrlimit_args *uap = v;
   1177 
   1178 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
   1179 		return EINVAL;
   1180 
   1181 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
   1182 		SCARG(uap, which) = RLIMIT_NOFILE;
   1183 
   1184 	return compat_43_netbsd32_osetrlimit(p, uap, retval);
   1185 }
   1186 
   1187 /* for the m68k machines */
   1188 #ifndef PT_GETFPREGS
   1189 #define PT_GETFPREGS -1
   1190 #endif
   1191 #ifndef PT_SETFPREGS
   1192 #define PT_SETFPREGS -1
   1193 #endif
   1194 
   1195 static int sreq2breq[] = {
   1196 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
   1197 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
   1198 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
   1199 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
   1200 };
   1201 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
   1202 
   1203 int
   1204 sunos32_sys_ptrace(p, v, retval)
   1205 	struct proc *p;
   1206 	void *v;
   1207 	register_t *retval;
   1208 {
   1209 	struct sunos32_sys_ptrace_args *uap = v;
   1210 	struct netbsd32_ptrace_args pa;
   1211 	int req;
   1212 
   1213 	req = SCARG(uap, req);
   1214 
   1215 	if (req < 0 || req >= nreqs)
   1216 		return (EINVAL);
   1217 
   1218 	req = sreq2breq[req];
   1219 	if (req == -1)
   1220 		return (EINVAL);
   1221 
   1222 	SCARG(&pa, req) = req;
   1223 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
   1224 	SCARG(&pa, addr) = SCARG(uap, addr);
   1225 	SCARG(&pa, data) = SCARG(uap, data);
   1226 
   1227 	return netbsd32_ptrace(p, &pa, retval);
   1228 }
   1229 
   1230 /*
   1231  * SunOS reboot system call (for compatibility).
   1232  * Sun lets you pass in a boot string which the PROM
   1233  * saves and provides to the next boot program.
   1234  */
   1235 
   1236 #define SUNOS_RB_ASKNAME	0x001
   1237 #define SUNOS_RB_SINGLE 	0x002
   1238 #define SUNOS_RB_NOSYNC		0x004
   1239 #define SUNOS_RB_HALT		0x008
   1240 #define SUNOS_RB_DUMP		0x080
   1241 #define	SUNOS_RB_STRING		0x200
   1242 
   1243 static struct sunos_howto_conv {
   1244 	int sun_howto;
   1245 	int bsd_howto;
   1246 } sunos_howto_conv[] = {
   1247 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
   1248 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
   1249 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
   1250 	{ SUNOS_RB_HALT,	RB_HALT },
   1251 	{ SUNOS_RB_DUMP,	RB_DUMP },
   1252 	{ SUNOS_RB_STRING,	RB_STRING },
   1253 	{ 0x000,		0 },
   1254 };
   1255 
   1256 int
   1257 sunos32_sys_reboot(p, v, retval)
   1258 	struct proc *p;
   1259 	void *v;
   1260 	register_t *retval;
   1261 {
   1262 	struct sunos32_sys_reboot_args *uap = v;
   1263 	struct sys_reboot_args ua;
   1264 	struct sunos_howto_conv *convp;
   1265 	int error, bsd_howto, sun_howto;
   1266 	char *bootstr;
   1267 
   1268 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1269 		return (error);
   1270 
   1271 	/*
   1272 	 * Convert howto bits to BSD format.
   1273 	 */
   1274 	sun_howto = SCARG(uap, howto);
   1275 	bsd_howto = 0;
   1276 	convp = sunos_howto_conv;
   1277 	while (convp->sun_howto) {
   1278 		if (sun_howto & convp->sun_howto)
   1279 			bsd_howto |= convp->bsd_howto;
   1280 		convp++;
   1281 	}
   1282 
   1283 	/*
   1284 	 * Sun RB_STRING (Get user supplied bootstring.)
   1285 	 * If the machine supports passing a string to the
   1286 	 * next booted kernel.
   1287 	 */
   1288 	if (sun_howto & SUNOS_RB_STRING)
   1289 		bootstr = (char *)(u_long)SCARG(uap, bootstr);
   1290 	else
   1291 		bootstr = NULL;
   1292 
   1293 	SCARG(&ua, opt) = bsd_howto;
   1294 	SCARG(&ua, bootstr) = bootstr;
   1295 	return (sys_reboot(p, &ua, retval));
   1296 }
   1297 
   1298 /*
   1299  * Generalized interface signal handler, 4.3-compatible.
   1300  */
   1301 /* ARGSUSED */
   1302 int
   1303 sunos32_sys_sigvec(p, v, retval)
   1304 	struct proc *p;
   1305 	void *v;
   1306 	register_t *retval;
   1307 {
   1308 	struct sunos32_sys_sigvec_args /* {
   1309 		syscallarg(int) signum;
   1310 		syscallarg(struct sigvec *) nsv;
   1311 		syscallarg(struct sigvec *) osv;
   1312 	} */ *uap = v;
   1313 	struct sigvec nsv, osv;
   1314 	struct sigaction nsa, osa;
   1315 	int error;
   1316 /*XXX*/extern	void compat_43_sigvec_to_sigaction
   1317 		__P((const struct sigvec *, struct sigaction *));
   1318 /*XXX*/extern	void compat_43_sigaction_to_sigvec
   1319 		__P((const struct sigaction *, struct sigvec *));
   1320 
   1321 	if (SCARG(uap, nsv)) {
   1322 		error = copyin((caddr_t)(u_long)SCARG(uap, nsv), &nsv, sizeof(nsv));
   1323 		if (error != 0)
   1324 			return (error);
   1325 
   1326 		/*
   1327 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
   1328 		 * meaning: `reset to SIG_DFL on delivery'.
   1329 		 * We support only the bits in: 0xF
   1330 		 * (those bits are the same as ours)
   1331 		 */
   1332 		if (nsv.sv_flags & ~0xF)
   1333 			return (EINVAL);
   1334 
   1335 		compat_43_sigvec_to_sigaction(&nsv, &nsa);
   1336 	}
   1337 	error = sigaction1(p, SCARG(uap, signum),
   1338 			   SCARG(uap, nsv) ? &nsa : 0,
   1339 			   SCARG(uap, osv) ? &osa : 0);
   1340 	if (error != 0)
   1341 		return (error);
   1342 
   1343 	if (SCARG(uap, osv)) {
   1344 		compat_43_sigaction_to_sigvec(&osa, &osv);
   1345 		error = copyout(&osv, (caddr_t)(u_long)SCARG(uap, osv), sizeof(osv));
   1346 		if (error != 0)
   1347 			return (error);
   1348 	}
   1349 
   1350 	return (0);
   1351 }
   1352