17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d67944fbSScott Rotondo  * Common Development and Distribution License (the "License").
6*d67944fbSScott Rotondo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d67944fbSScott Rotondo  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * 4.3BSD signal compatibility functions
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * the implementation interprets signal masks equal to -1 as "all of the
387c478bd9Sstevel@tonic-gate  * signals in the signal set", thereby allowing signals with numbers
397c478bd9Sstevel@tonic-gate  * above 32 to be blocked when referenced in code such as:
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *	for (i = 0; i < NSIG; i++)
427c478bd9Sstevel@tonic-gate  *		mask |= sigmask(i)
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <ucontext.h>
477c478bd9Sstevel@tonic-gate #include <signal.h>
487c478bd9Sstevel@tonic-gate #include <errno.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #undef	BUS_OBJERR	/* namespace conflict */
517c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
527c478bd9Sstevel@tonic-gate #include "libc.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #pragma weak sigvechandler = _sigvechandler
557c478bd9Sstevel@tonic-gate #pragma weak sigsetmask = _sigsetmask
567c478bd9Sstevel@tonic-gate #pragma weak sigblock = _sigblock
577c478bd9Sstevel@tonic-gate #pragma weak sigpause = usigpause
587c478bd9Sstevel@tonic-gate #pragma weak sigvec = _sigvec
597c478bd9Sstevel@tonic-gate #pragma weak sigstack = _sigstack
607c478bd9Sstevel@tonic-gate #pragma weak signal = usignal
617c478bd9Sstevel@tonic-gate #pragma weak siginterrupt = _siginterrupt
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	set2mask(setp) ((setp)->__sigbits[0])
647c478bd9Sstevel@tonic-gate #define	mask2set(mask, setp) \
657c478bd9Sstevel@tonic-gate 	((mask) == -1 ? sigfillset(setp) : \
667c478bd9Sstevel@tonic-gate 	    (sigemptyset(setp), (((setp)->__sigbits[0]) = (int)(mask))))
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate void (*_siguhandler[NSIG])() = { 0 };
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /* forward declarations */
717c478bd9Sstevel@tonic-gate int ucbsigsetmask(int);
727c478bd9Sstevel@tonic-gate int ucbsigblock(int);
737c478bd9Sstevel@tonic-gate int ucbsigvec(int, struct sigvec *, struct sigvec *);
747c478bd9Sstevel@tonic-gate int ucbsigpause(int);
757c478bd9Sstevel@tonic-gate int ucbsiginterrupt(int, int);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * sigvechandler is the real signal handler installed for all
797c478bd9Sstevel@tonic-gate  * signals handled in the 4.3BSD compatibility interface - it translates
807c478bd9Sstevel@tonic-gate  * SVR4 signal hander arguments into 4.3BSD signal handler arguments
817c478bd9Sstevel@tonic-gate  * and then calls the real handler
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
84*d67944fbSScott Rotondo static void ucbsigvechandler();
857c478bd9Sstevel@tonic-gate void
_sigvechandler(int sig,siginfo_t * sip,ucontext_t * ucp)867c478bd9Sstevel@tonic-gate _sigvechandler(int sig, siginfo_t *sip, ucontext_t *ucp)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	ucbsigvechandler(sig, sip, ucp);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static void
ucbsigvechandler(int sig,siginfo_t * sip,ucontext_t * ucp)937c478bd9Sstevel@tonic-gate ucbsigvechandler(int sig, siginfo_t *sip, ucontext_t *ucp)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	struct sigcontext sc;
967c478bd9Sstevel@tonic-gate 	int code;
977c478bd9Sstevel@tonic-gate 	char *addr;
987c478bd9Sstevel@tonic-gate 	int i, j;
997c478bd9Sstevel@tonic-gate 	int gwinswitch = 0;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	sc.sc_onstack = ((ucp->uc_stack.ss_flags & SS_ONSTACK) != 0);
1027c478bd9Sstevel@tonic-gate 	sc.sc_mask = set2mask(&ucp->uc_sigmask);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #if defined(__amd64)
1057c478bd9Sstevel@tonic-gate 	sc.sc_sp = (long)ucp->uc_mcontext.gregs[REG_RSP];
1067c478bd9Sstevel@tonic-gate 	sc.sc_pc = (long)ucp->uc_mcontext.gregs[REG_RIP];
1077c478bd9Sstevel@tonic-gate 	sc.sc_ps = (long)ucp->uc_mcontext.gregs[REG_RFL];
1087c478bd9Sstevel@tonic-gate 	sc.sc_r0 = (long)ucp->uc_mcontext.gregs[REG_RAX];
1097c478bd9Sstevel@tonic-gate 	sc.sc_r1 = (long)ucp->uc_mcontext.gregs[REG_RDX];
1107c478bd9Sstevel@tonic-gate #else
1117c478bd9Sstevel@tonic-gate 	sc.sc_sp = (int)ucp->uc_mcontext.gregs[UESP];
1127c478bd9Sstevel@tonic-gate 	sc.sc_pc = (int)ucp->uc_mcontext.gregs[EIP];
1137c478bd9Sstevel@tonic-gate 	sc.sc_ps = (int)ucp->uc_mcontext.gregs[EFL];
1147c478bd9Sstevel@tonic-gate 	sc.sc_r0 = (int)ucp->uc_mcontext.gregs[EAX];
1157c478bd9Sstevel@tonic-gate 	sc.sc_r1 = (int)ucp->uc_mcontext.gregs[EDX];
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/*
1197c478bd9Sstevel@tonic-gate 	 * Translate signal codes from new to old.
1207c478bd9Sstevel@tonic-gate 	 * /usr/include/sys/siginfo.h contains new codes.
1217c478bd9Sstevel@tonic-gate 	 * /usr/ucbinclude/sys/signal.h contains old codes.
1227c478bd9Sstevel@tonic-gate 	 */
1237c478bd9Sstevel@tonic-gate 	code = 0;
1247c478bd9Sstevel@tonic-gate 	addr = SIG_NOADDR;
1257c478bd9Sstevel@tonic-gate 	if (sip != NULL && SI_FROMKERNEL(sip)) {
1267c478bd9Sstevel@tonic-gate 		addr = sip->si_addr;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		switch (sig) {
1297c478bd9Sstevel@tonic-gate 		case SIGILL:
1307c478bd9Sstevel@tonic-gate 		case SIGFPE:
1317c478bd9Sstevel@tonic-gate 			code = ILL_ILLINSTR_FAULT;
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		case SIGBUS:
1357c478bd9Sstevel@tonic-gate 			switch (sip->si_code) {
1367c478bd9Sstevel@tonic-gate 			case BUS_ADRALN:
1377c478bd9Sstevel@tonic-gate 				code = BUS_ALIGN;
1387c478bd9Sstevel@tonic-gate 				break;
1397c478bd9Sstevel@tonic-gate 			case BUS_ADRERR:
1407c478bd9Sstevel@tonic-gate 				code = BUS_HWERR;
1417c478bd9Sstevel@tonic-gate 				break;
1427c478bd9Sstevel@tonic-gate 			default:	/* BUS_OBJERR */
1437c478bd9Sstevel@tonic-gate 				code = FC_MAKE_ERR(sip->si_errno);
1447c478bd9Sstevel@tonic-gate 				break;
1457c478bd9Sstevel@tonic-gate 			}
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		case SIGSEGV:
1497c478bd9Sstevel@tonic-gate 			switch (sip->si_code) {
1507c478bd9Sstevel@tonic-gate 			case SEGV_MAPERR:
1517c478bd9Sstevel@tonic-gate 				code = SEGV_NOMAP;
1527c478bd9Sstevel@tonic-gate 				break;
1537c478bd9Sstevel@tonic-gate 			case SEGV_ACCERR:
1547c478bd9Sstevel@tonic-gate 				code = SEGV_PROT;
1557c478bd9Sstevel@tonic-gate 				break;
1567c478bd9Sstevel@tonic-gate 			default:
1577c478bd9Sstevel@tonic-gate 				code = FC_MAKE_ERR(sip->si_errno);
1587c478bd9Sstevel@tonic-gate 				break;
1597c478bd9Sstevel@tonic-gate 			}
1607c478bd9Sstevel@tonic-gate 			break;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 		default:
1637c478bd9Sstevel@tonic-gate 			addr = SIG_NOADDR;
1647c478bd9Sstevel@tonic-gate 			break;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	(*_siguhandler[sig])(sig, code, &sc, addr);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (sc.sc_onstack)
1717c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_flags |= SS_ONSTACK;
1727c478bd9Sstevel@tonic-gate 	else
1737c478bd9Sstevel@tonic-gate 		ucp->uc_stack.ss_flags &= ~SS_ONSTACK;
1747c478bd9Sstevel@tonic-gate 	mask2set(sc.sc_mask, &ucp->uc_sigmask);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate #if defined(__amd64)
1777c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_RSP] = (long)sc.sc_sp;
1787c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_RIP] = (long)sc.sc_pc;
1797c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_RFL] = (long)sc.sc_ps;
1807c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_RAX] = (long)sc.sc_r0;
1817c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[REG_RDX] = (long)sc.sc_r1;
1827c478bd9Sstevel@tonic-gate #else
1837c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[UESP] = (int)sc.sc_sp;
1847c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EIP] = (int)sc.sc_pc;
1857c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EFL] = (int)sc.sc_ps;
1867c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EAX] = (int)sc.sc_r0;
1877c478bd9Sstevel@tonic-gate 	ucp->uc_mcontext.gregs[EDX] = (int)sc.sc_r1;
1887c478bd9Sstevel@tonic-gate #endif
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	setcontext(ucp);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate int
_sigsetmask(int mask)1947c478bd9Sstevel@tonic-gate _sigsetmask(int mask)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	return (ucbsigsetmask(mask));
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate int
ucbsigsetmask(int mask)2007c478bd9Sstevel@tonic-gate ucbsigsetmask(int mask)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	sigset_t oset;
2037c478bd9Sstevel@tonic-gate 	sigset_t nset;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &nset);
2067c478bd9Sstevel@tonic-gate 	mask2set(mask, &nset);
2077c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
2087c478bd9Sstevel@tonic-gate 	return (set2mask(&oset));
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate int
_sigblock(int mask)2127c478bd9Sstevel@tonic-gate _sigblock(int mask)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	return (ucbsigblock(mask));
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate int
ucbsigblock(int mask)2187c478bd9Sstevel@tonic-gate ucbsigblock(int mask)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	sigset_t oset;
2217c478bd9Sstevel@tonic-gate 	sigset_t nset;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &nset);
2247c478bd9Sstevel@tonic-gate 	mask2set(mask, &nset);
2257c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &nset, &oset);
2267c478bd9Sstevel@tonic-gate 	return (set2mask(&oset));
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate int
usigpause(int mask)2307c478bd9Sstevel@tonic-gate usigpause(int mask)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	return (ucbsigpause(mask));
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate int
ucbsigpause(int mask)2367c478bd9Sstevel@tonic-gate ucbsigpause(int mask)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	sigset_t set, oset;
2397c478bd9Sstevel@tonic-gate 	int ret;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	(void) sigprocmask(0, (sigset_t *)0, &set);
2427c478bd9Sstevel@tonic-gate 	oset = set;
2437c478bd9Sstevel@tonic-gate 	mask2set(mask, &set);
2447c478bd9Sstevel@tonic-gate 	ret = sigsuspend(&set);
2457c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oset, (sigset_t *)0);
2467c478bd9Sstevel@tonic-gate 	return (ret);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate int
_sigvec(int sig,struct sigvec * nvec,struct sigvec * ovec)2507c478bd9Sstevel@tonic-gate _sigvec(int sig, struct sigvec *nvec, struct sigvec *ovec)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	return (ucbsigvec(sig, nvec, ovec));
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate int
ucbsigvec(int sig,struct sigvec * nvec,struct sigvec * ovec)2567c478bd9Sstevel@tonic-gate ucbsigvec(int sig, struct sigvec *nvec, struct sigvec *ovec)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	struct sigaction nact;
2597c478bd9Sstevel@tonic-gate 	struct sigaction oact;
2607c478bd9Sstevel@tonic-gate 	struct sigaction *nactp;
2617c478bd9Sstevel@tonic-gate 	void (*ohandler)(), (*nhandler)();
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (sig <= 0 || sig >= NSIG) {
2647c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2657c478bd9Sstevel@tonic-gate 		return (-1);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if ((intptr_t)ovec == -1 || (intptr_t)nvec == -1) {
2697c478bd9Sstevel@tonic-gate 		errno = EFAULT;
2707c478bd9Sstevel@tonic-gate 		return (-1);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	ohandler = _siguhandler[sig];
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (nvec) {
2767c478bd9Sstevel@tonic-gate 		_sigaction(sig, (struct sigaction *)0, &nact);
2777c478bd9Sstevel@tonic-gate 		nhandler = nvec->sv_handler;
2787c478bd9Sstevel@tonic-gate 		/*
2797c478bd9Sstevel@tonic-gate 		 * To be compatible with the behavior of SunOS 4.x:
2807c478bd9Sstevel@tonic-gate 		 * If the new signal handler is SIG_IGN or SIG_DFL,
2817c478bd9Sstevel@tonic-gate 		 * do not change the signal's entry in the handler array.
2827c478bd9Sstevel@tonic-gate 		 * This allows a child of vfork(2) to set signal handlers
2837c478bd9Sstevel@tonic-gate 		 * to SIG_IGN or SIG_DFL without affecting the parent.
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		if (nhandler != SIG_DFL && nhandler != SIG_IGN) {
2867c478bd9Sstevel@tonic-gate 			_siguhandler[sig] = nhandler;
2877c478bd9Sstevel@tonic-gate 			nact.sa_handler = (void (*)())ucbsigvechandler;
2887c478bd9Sstevel@tonic-gate 		} else {
2897c478bd9Sstevel@tonic-gate 			nact.sa_handler = nhandler;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 		mask2set(nvec->sv_mask, &nact.sa_mask);
2927c478bd9Sstevel@tonic-gate 		if (sig == SIGKILL || sig == SIGSTOP)
2937c478bd9Sstevel@tonic-gate 			nact.sa_handler = SIG_DFL;
2947c478bd9Sstevel@tonic-gate 		nact.sa_flags = SA_SIGINFO;
2957c478bd9Sstevel@tonic-gate 		if (!(nvec->sv_flags & SV_INTERRUPT))
2967c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_RESTART;
2977c478bd9Sstevel@tonic-gate 		if (nvec->sv_flags & SV_RESETHAND)
2987c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_RESETHAND;
2997c478bd9Sstevel@tonic-gate 		if (nvec->sv_flags & SV_ONSTACK)
3007c478bd9Sstevel@tonic-gate 			nact.sa_flags |= SA_ONSTACK;
3017c478bd9Sstevel@tonic-gate 		nactp = &nact;
3027c478bd9Sstevel@tonic-gate 	} else
3037c478bd9Sstevel@tonic-gate 		nactp = (struct sigaction *)0;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if (_sigaction(sig, nactp, &oact) < 0) {
3067c478bd9Sstevel@tonic-gate 		_siguhandler[sig] = ohandler;
3077c478bd9Sstevel@tonic-gate 		return (-1);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	if (ovec) {
3117c478bd9Sstevel@tonic-gate 		if (oact.sa_handler == SIG_DFL || oact.sa_handler == SIG_IGN)
3127c478bd9Sstevel@tonic-gate 			ovec->sv_handler = oact.sa_handler;
3137c478bd9Sstevel@tonic-gate 		else
3147c478bd9Sstevel@tonic-gate 			ovec->sv_handler = ohandler;
3157c478bd9Sstevel@tonic-gate 		ovec->sv_mask = set2mask(&oact.sa_mask);
3167c478bd9Sstevel@tonic-gate 		ovec->sv_flags = 0;
3177c478bd9Sstevel@tonic-gate 		if (oact.sa_flags & SA_ONSTACK)
3187c478bd9Sstevel@tonic-gate 			ovec->sv_flags |= SV_ONSTACK;
3197c478bd9Sstevel@tonic-gate 		if (oact.sa_flags & SA_RESETHAND)
3207c478bd9Sstevel@tonic-gate 			ovec->sv_flags |= SV_RESETHAND;
3217c478bd9Sstevel@tonic-gate 		if (!(oact.sa_flags & SA_RESTART))
3227c478bd9Sstevel@tonic-gate 			ovec->sv_flags |= SV_INTERRUPT;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	return (0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate int
_sigstack(struct sigstack * nss,struct sigstack * oss)3297c478bd9Sstevel@tonic-gate _sigstack(struct sigstack *nss, struct sigstack *oss)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	struct sigaltstack nalt;
3327c478bd9Sstevel@tonic-gate 	struct sigaltstack oalt;
3337c478bd9Sstevel@tonic-gate 	struct sigaltstack *naltp;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (nss) {
3367c478bd9Sstevel@tonic-gate 		/*
3377c478bd9Sstevel@tonic-gate 		 * assumes stack growth is down (like sparc and x86)
3387c478bd9Sstevel@tonic-gate 		 */
3397c478bd9Sstevel@tonic-gate 		nalt.ss_sp = nss->ss_sp - SIGSTKSZ;
3407c478bd9Sstevel@tonic-gate 		nalt.ss_size = SIGSTKSZ;
3417c478bd9Sstevel@tonic-gate 		nalt.ss_flags = 0;
3427c478bd9Sstevel@tonic-gate 		naltp = &nalt;
3437c478bd9Sstevel@tonic-gate 	} else
3447c478bd9Sstevel@tonic-gate 		naltp = (struct sigaltstack *)0;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (sigaltstack(naltp, &oalt) < 0)
3477c478bd9Sstevel@tonic-gate 		return (-1);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (oss) {
3507c478bd9Sstevel@tonic-gate 		/*
3517c478bd9Sstevel@tonic-gate 		 * assumes stack growth is down (like sparc and x86)
3527c478bd9Sstevel@tonic-gate 		 */
3537c478bd9Sstevel@tonic-gate 		oss->ss_sp = oalt.ss_sp + oalt.ss_size;
3547c478bd9Sstevel@tonic-gate 		oss->ss_onstack = ((oalt.ss_flags & SS_ONSTACK) != 0);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	return (0);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate void (*
ucbsignal(int s,void (* a)())3617c478bd9Sstevel@tonic-gate ucbsignal(int s, void (*a)()))()
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	struct sigvec osv;
3647c478bd9Sstevel@tonic-gate 	struct sigvec nsv;
3657c478bd9Sstevel@tonic-gate 	static int mask[NSIG];
3667c478bd9Sstevel@tonic-gate 	static int flags[NSIG];
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	nsv.sv_handler = a;
3697c478bd9Sstevel@tonic-gate 	nsv.sv_mask = mask[s];
3707c478bd9Sstevel@tonic-gate 	nsv.sv_flags = flags[s];
3717c478bd9Sstevel@tonic-gate 	if (ucbsigvec(s, &nsv, &osv) < 0)
3727c478bd9Sstevel@tonic-gate 		return (SIG_ERR);
3737c478bd9Sstevel@tonic-gate 	if (nsv.sv_mask != osv.sv_mask || nsv.sv_flags != osv.sv_flags) {
3747c478bd9Sstevel@tonic-gate 		mask[s] = nsv.sv_mask = osv.sv_mask;
3757c478bd9Sstevel@tonic-gate 		flags[s] = nsv.sv_flags =
376*d67944fbSScott Rotondo 		    osv.sv_flags & ~(SV_RESETHAND|SV_INTERRUPT);
3777c478bd9Sstevel@tonic-gate 		if (ucbsigvec(s, &nsv, (struct sigvec *)0) < 0)
3787c478bd9Sstevel@tonic-gate 			return (SIG_ERR);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	return (osv.sv_handler);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate void (*
usignal(int s,void (* a)())3847c478bd9Sstevel@tonic-gate usignal(int s, void (*a)()))()
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	return (ucbsignal(s, a));
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * Set signal state to prevent restart of system calls
3917c478bd9Sstevel@tonic-gate  * after an instance of the indicated signal.
3927c478bd9Sstevel@tonic-gate  */
3937c478bd9Sstevel@tonic-gate int
_siginterrupt(int sig,int flag)3947c478bd9Sstevel@tonic-gate _siginterrupt(int sig, int flag)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	return (ucbsiginterrupt(sig, flag));
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate int
ucbsiginterrupt(int sig,int flag)4007c478bd9Sstevel@tonic-gate ucbsiginterrupt(int sig, int flag)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	struct sigvec sv;
4037c478bd9Sstevel@tonic-gate 	int ret;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if ((ret = ucbsigvec(sig, 0, &sv)) < 0)
4067c478bd9Sstevel@tonic-gate 		return (ret);
4077c478bd9Sstevel@tonic-gate 	if (flag)
4087c478bd9Sstevel@tonic-gate 		sv.sv_flags |= SV_INTERRUPT;
4097c478bd9Sstevel@tonic-gate 	else
4107c478bd9Sstevel@tonic-gate 		sv.sv_flags &= ~SV_INTERRUPT;
4117c478bd9Sstevel@tonic-gate 	return (ucbsigvec(sig, &sv, 0));
4127c478bd9Sstevel@tonic-gate }
413