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