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