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