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
534709573Sraf  * Common Development and Distribution License (the "License").
634709573Sraf  * 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  */
210293487cSraf 
227c478bd9Sstevel@tonic-gate /*
23ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "lint.h"
307c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
31f841f6adSraf #include "asyncio.h"
327c478bd9Sstevel@tonic-gate #include <signal.h>
337c478bd9Sstevel@tonic-gate #include <siginfo.h>
347c478bd9Sstevel@tonic-gate #include <ucontext.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate const sigset_t maskset = {MASKSET0, MASKSET1, 0, 0};	/* maskable signals */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Return true if the valid signal bits in both sets are the same.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate int
437c478bd9Sstevel@tonic-gate sigequalset(const sigset_t *s1, const sigset_t *s2)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	/*
467c478bd9Sstevel@tonic-gate 	 * We only test valid signal bits, not rubbish following MAXSIG
477c478bd9Sstevel@tonic-gate 	 * (for speed).  Algorithm:
487c478bd9Sstevel@tonic-gate 	 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
497c478bd9Sstevel@tonic-gate 	 */
507c478bd9Sstevel@tonic-gate 	return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) |
517c478bd9Sstevel@tonic-gate 	    ((s1->__sigbits[1] ^ s2->__sigbits[1]) & FILLSET1)));
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * Common code for calling the user-specified signal handler.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate void
587c478bd9Sstevel@tonic-gate call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
617c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
627c478bd9Sstevel@tonic-gate 	struct sigaction uact;
637c478bd9Sstevel@tonic-gate 	volatile struct sigaction *sap;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	/*
667c478bd9Sstevel@tonic-gate 	 * If we are taking a signal while parked or about to be parked
677c478bd9Sstevel@tonic-gate 	 * on __lwp_park() then remove ourself from the sleep queue so
687c478bd9Sstevel@tonic-gate 	 * that we can grab locks.  The code in mutex_lock_queue() and
697c478bd9Sstevel@tonic-gate 	 * cond_wait_common() will detect this and deal with it when
707c478bd9Sstevel@tonic-gate 	 * __lwp_park() returns.
717c478bd9Sstevel@tonic-gate 	 */
727c478bd9Sstevel@tonic-gate 	unsleep_self();
737c478bd9Sstevel@tonic-gate 	set_parking_flag(self, 0);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (__td_event_report(self, TD_CATCHSIG, udp)) {
767c478bd9Sstevel@tonic-gate 		self->ul_td_evbuf.eventnum = TD_CATCHSIG;
777c478bd9Sstevel@tonic-gate 		self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig;
787c478bd9Sstevel@tonic-gate 		tdb_event(TD_CATCHSIG, udp);
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	/*
827c478bd9Sstevel@tonic-gate 	 * Get a self-consistent set of flags, handler, and mask
837c478bd9Sstevel@tonic-gate 	 * while holding the sig's sig_lock for the least possible time.
847c478bd9Sstevel@tonic-gate 	 * We must acquire the sig's sig_lock because some thread running
857c478bd9Sstevel@tonic-gate 	 * in sigaction() might be establishing a new signal handler.
86*41efec22Sraf 	 * The code in sigaction() acquires the writer lock; here
87*41efec22Sraf 	 * we acquire the readers lock to ehance concurrency in the
88*41efec22Sraf 	 * face of heavy signal traffic, such as generated by java.
897c478bd9Sstevel@tonic-gate 	 *
907c478bd9Sstevel@tonic-gate 	 * Locking exceptions:
917c478bd9Sstevel@tonic-gate 	 * No locking for a child of vfork().
927c478bd9Sstevel@tonic-gate 	 * If the signal is SIGPROF with an si_code of PROF_SIG,
937c478bd9Sstevel@tonic-gate 	 * then we assume that this signal was generated by
947c478bd9Sstevel@tonic-gate 	 * setitimer(ITIMER_REALPROF) set up by the dbx collector.
957c478bd9Sstevel@tonic-gate 	 * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
967c478bd9Sstevel@tonic-gate 	 * then we assume that the signal was generated by
977c478bd9Sstevel@tonic-gate 	 * a hardware performance counter overflow.
987c478bd9Sstevel@tonic-gate 	 * In these cases, assume that we need no locking.  It is the
997c478bd9Sstevel@tonic-gate 	 * monitoring program's responsibility to ensure correctness.
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 	sap = &udp->siguaction[sig].sig_uaction;
1027c478bd9Sstevel@tonic-gate 	if (self->ul_vfork ||
1037c478bd9Sstevel@tonic-gate 	    (sip != NULL &&
1047c478bd9Sstevel@tonic-gate 	    ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
1057c478bd9Sstevel@tonic-gate 	    (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
1067c478bd9Sstevel@tonic-gate 		/* we wish this assignment could be atomic */
1070293487cSraf 		(void) _private_memcpy(&uact, (void *)sap, sizeof (uact));
1087c478bd9Sstevel@tonic-gate 	} else {
109*41efec22Sraf 		rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
110*41efec22Sraf 		lrw_rdlock(rwlp);
1110293487cSraf 		(void) _private_memcpy(&uact, (void *)sap, sizeof (uact));
1127c478bd9Sstevel@tonic-gate 		if (sig == SIGCANCEL && (sap->sa_flags & SA_RESETHAND))
1137c478bd9Sstevel@tonic-gate 			sap->sa_sigaction = SIG_DFL;
114*41efec22Sraf 		lrw_unlock(rwlp);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * Set the proper signal mask and call the user's signal handler.
1197c478bd9Sstevel@tonic-gate 	 * (We overrode the user-requested signal mask with maskset
1207c478bd9Sstevel@tonic-gate 	 * so we currently have all blockable signals blocked.)
1217c478bd9Sstevel@tonic-gate 	 *
1227c478bd9Sstevel@tonic-gate 	 * We would like to ASSERT() that the signal is not a member of the
1237c478bd9Sstevel@tonic-gate 	 * signal mask at the previous level (ucp->uc_sigmask) or the specified
1247c478bd9Sstevel@tonic-gate 	 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
1257c478bd9Sstevel@tonic-gate 	 * /proc can override this via PCSSIG, so we don't bother.
1267c478bd9Sstevel@tonic-gate 	 *
1277c478bd9Sstevel@tonic-gate 	 * We would also like to ASSERT() that the signal mask at the previous
1287c478bd9Sstevel@tonic-gate 	 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
1297c478bd9Sstevel@tonic-gate 	 * but /proc can change the thread's signal mask via PCSHOLD, so we
1307c478bd9Sstevel@tonic-gate 	 * don't bother with that either.
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	ASSERT(ucp->uc_flags & UC_SIGMASK);
1337c478bd9Sstevel@tonic-gate 	if (self->ul_sigsuspend) {
1347c478bd9Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
1357c478bd9Sstevel@tonic-gate 		self->ul_sigsuspend = 0;
1367c478bd9Sstevel@tonic-gate 		/* the sigsuspend() or pollsys() signal mask */
1377c478bd9Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &self->ul_tmpmask);
1387c478bd9Sstevel@tonic-gate 	} else {
1397c478bd9Sstevel@tonic-gate 		/* the signal mask at the previous level */
1407c478bd9Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &ucp->uc_sigmask);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_NODEFER))	/* add current signal */
1437c478bd9Sstevel@tonic-gate 		(void) _private_sigaddset(&uact.sa_mask, sig);
1447c478bd9Sstevel@tonic-gate 	self->ul_sigmask = uact.sa_mask;
1457c478bd9Sstevel@tonic-gate 	self->ul_siglink = ucp;
1467c478bd9Sstevel@tonic-gate 	(void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * If this thread has been sent SIGCANCEL from the kernel
1507c478bd9Sstevel@tonic-gate 	 * or from pthread_cancel(), it is being asked to exit.
1517c478bd9Sstevel@tonic-gate 	 * The kernel may send SIGCANCEL without a siginfo struct.
1527c478bd9Sstevel@tonic-gate 	 * If the SIGCANCEL is process-directed (from kill() or
1537c478bd9Sstevel@tonic-gate 	 * sigqueue()), treat it as an ordinary signal.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	if (sig == SIGCANCEL) {
1567c478bd9Sstevel@tonic-gate 		if (sip == NULL || SI_FROMKERNEL(sip) ||
1577c478bd9Sstevel@tonic-gate 		    sip->si_code == SI_LWP) {
1587c478bd9Sstevel@tonic-gate 			do_sigcancel();
1597c478bd9Sstevel@tonic-gate 			goto out;
1607c478bd9Sstevel@tonic-gate 		}
161f841f6adSraf 		/* SIGCANCEL is ignored by default */
162f841f6adSraf 		if (uact.sa_sigaction == SIG_DFL ||
163f841f6adSraf 		    uact.sa_sigaction == SIG_IGN)
164f841f6adSraf 			goto out;
165f841f6adSraf 	}
166f841f6adSraf 
167f841f6adSraf 	/*
168f841f6adSraf 	 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
169f841f6adSraf 	 * we are an aio worker thread, cancel the aio request.
170f841f6adSraf 	 */
171f841f6adSraf 	if (sig == SIGAIOCANCEL) {
172f841f6adSraf 		aio_worker_t *aiowp = _pthread_getspecific(_aio_key);
173f841f6adSraf 
174f841f6adSraf 		if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
175f841f6adSraf 			_siglongjmp(aiowp->work_jmp_buf, 1);
176f841f6adSraf 		/* SIGLWP is ignored by default */
1777c478bd9Sstevel@tonic-gate 		if (uact.sa_sigaction == SIG_DFL ||
1787c478bd9Sstevel@tonic-gate 		    uact.sa_sigaction == SIG_IGN)
1797c478bd9Sstevel@tonic-gate 			goto out;
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_SIGINFO))
1837c478bd9Sstevel@tonic-gate 		sip = NULL;
1847c478bd9Sstevel@tonic-gate 	__sighndlr(sig, sip, ucp, uact.sa_sigaction);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate #if defined(sparc) || defined(__sparc)
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * If this is a floating point exception and the queue
1897c478bd9Sstevel@tonic-gate 	 * is non-empty, pop the top entry from the queue.  This
1907c478bd9Sstevel@tonic-gate 	 * is to maintain expected behavior.
1917c478bd9Sstevel@tonic-gate 	 */
1927c478bd9Sstevel@tonic-gate 	if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
1937c478bd9Sstevel@tonic-gate 		fpregset_t *fp = &ucp->uc_mcontext.fpregs;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		if (--fp->fpu_qcnt > 0) {
1967c478bd9Sstevel@tonic-gate 			unsigned char i;
1977c478bd9Sstevel@tonic-gate 			struct fq *fqp;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 			fqp = fp->fpu_q;
2007c478bd9Sstevel@tonic-gate 			for (i = 0; i < fp->fpu_qcnt; i++)
2017c478bd9Sstevel@tonic-gate 				fqp[i] = fqp[i+1];
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate #endif	/* sparc */
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate out:
2077c478bd9Sstevel@tonic-gate 	(void) _private_setcontext(ucp);
2087c478bd9Sstevel@tonic-gate 	thr_panic("call_user_handler(): _setcontext() returned");
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * take_deferred_signal() is called when ul_critical and ul_sigdefer become
2137c478bd9Sstevel@tonic-gate  * zero and a deferred signal has been recorded on the current thread.
2147c478bd9Sstevel@tonic-gate  * We are out of the critical region and are ready to take a signal.
2157c478bd9Sstevel@tonic-gate  * The kernel has all signals blocked on this lwp, but our value of
2167c478bd9Sstevel@tonic-gate  * ul_sigmask is the correct signal mask for the previous context.
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate void
2197c478bd9Sstevel@tonic-gate take_deferred_signal(int sig)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
2227c478bd9Sstevel@tonic-gate 	siginfo_t siginfo;
2237c478bd9Sstevel@tonic-gate 	siginfo_t *sip;
2247c478bd9Sstevel@tonic-gate 	ucontext_t uc;
2257c478bd9Sstevel@tonic-gate 	volatile int returning;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
2287c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer == 0);
2297c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_cursig == 0);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	returning = 0;
2327c478bd9Sstevel@tonic-gate 	uc.uc_flags = UC_ALL;
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * We call _private_getcontext (a libc-private synonym for
2357c478bd9Sstevel@tonic-gate 	 * _getcontext) rather than _getcontext because we need to
2367c478bd9Sstevel@tonic-gate 	 * avoid the dynamic linker and link auditing problems here.
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 	(void) _private_getcontext(&uc);
2397c478bd9Sstevel@tonic-gate 	/*
2407c478bd9Sstevel@tonic-gate 	 * If the application signal handler calls setcontext() on
2417c478bd9Sstevel@tonic-gate 	 * the ucontext we give it, it returns here, then we return.
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 	if (returning)
2447c478bd9Sstevel@tonic-gate 		return;
2457c478bd9Sstevel@tonic-gate 	returning = 1;
2467c478bd9Sstevel@tonic-gate 	ASSERT(sigequalset(&uc.uc_sigmask, &maskset));
2477c478bd9Sstevel@tonic-gate 	if (self->ul_siginfo.si_signo == 0)
2487c478bd9Sstevel@tonic-gate 		sip = NULL;
2497c478bd9Sstevel@tonic-gate 	else {
2500293487cSraf 		(void) _private_memcpy(&siginfo,
2510293487cSraf 		    &self->ul_siginfo, sizeof (siginfo));
2527c478bd9Sstevel@tonic-gate 		sip = &siginfo;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 	uc.uc_sigmask = self->ul_sigmask;
2557c478bd9Sstevel@tonic-gate 	call_user_handler(sig, sip, &uc);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate void
2597c478bd9Sstevel@tonic-gate sigacthandler(int sig, siginfo_t *sip, void *uvp)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	ucontext_t *ucp = uvp;
2627c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * Do this in case we took a signal while in a cancelable system call.
2667c478bd9Sstevel@tonic-gate 	 * It does no harm if we were not in such a system call.
2677c478bd9Sstevel@tonic-gate 	 */
2687c478bd9Sstevel@tonic-gate 	self->ul_sp = 0;
2697c478bd9Sstevel@tonic-gate 	if (sig != SIGCANCEL)
2707c478bd9Sstevel@tonic-gate 		self->ul_cancel_async = self->ul_save_async;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * If we are not in a critical region and are
2747c478bd9Sstevel@tonic-gate 	 * not deferring signals, take the signal now.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	if ((self->ul_critical + self->ul_sigdefer) == 0) {
2777c478bd9Sstevel@tonic-gate 		call_user_handler(sig, sip, ucp);
2787c478bd9Sstevel@tonic-gate 		return;	/* call_user_handler() cannot return */
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * We are in a critical region or we are deferring signals.  When
2837c478bd9Sstevel@tonic-gate 	 * we emerge from the region we will call take_deferred_signal().
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_cursig == 0);
2867c478bd9Sstevel@tonic-gate 	self->ul_cursig = (char)sig;
2877c478bd9Sstevel@tonic-gate 	if (sip != NULL)
2880293487cSraf 		(void) _private_memcpy(&self->ul_siginfo,
2890293487cSraf 		    sip, sizeof (siginfo_t));
2907c478bd9Sstevel@tonic-gate 	else
2917c478bd9Sstevel@tonic-gate 		self->ul_siginfo.si_signo = 0;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
2957c478bd9Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
2967c478bd9Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	set_parking_flag(self, 0);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	/*
3017c478bd9Sstevel@tonic-gate 	 * Return to the previous context with all signals blocked.
3027c478bd9Sstevel@tonic-gate 	 * We will restore the signal mask in take_deferred_signal().
3037c478bd9Sstevel@tonic-gate 	 * Note that we are calling the system call trap here, not
3047c478bd9Sstevel@tonic-gate 	 * the _setcontext() wrapper.  We don't want to change the
3057c478bd9Sstevel@tonic-gate 	 * thread's ul_sigmask by this operation.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	ucp->uc_sigmask = maskset;
3087c478bd9Sstevel@tonic-gate 	(void) __setcontext_syscall(ucp);
3097c478bd9Sstevel@tonic-gate 	thr_panic("sigacthandler(): __setcontext() returned");
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3129acbbeafSnn #pragma weak sigaction = _libc_sigaction
3139acbbeafSnn #pragma weak _sigaction = _libc_sigaction
3147c478bd9Sstevel@tonic-gate int
3159acbbeafSnn _libc_sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
3187c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
3197c478bd9Sstevel@tonic-gate 	struct sigaction oaction;
3207c478bd9Sstevel@tonic-gate 	struct sigaction tact;
3217c478bd9Sstevel@tonic-gate 	struct sigaction *tactp = NULL;
3227c478bd9Sstevel@tonic-gate 	int rv;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	if (sig <= 0 || sig >= NSIG) {
3257c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3267c478bd9Sstevel@tonic-gate 		return (-1);
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (!self->ul_vfork)
330*41efec22Sraf 		lrw_wrlock(&udp->siguaction[sig].sig_lock);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	oaction = udp->siguaction[sig].sig_uaction;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (nact != NULL) {
3357c478bd9Sstevel@tonic-gate 		tact = *nact;	/* make a copy so we can modify it */
3367c478bd9Sstevel@tonic-gate 		tactp = &tact;
3377c478bd9Sstevel@tonic-gate 		delete_reserved_signals(&tact.sa_mask);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate #if !defined(_LP64)
3407c478bd9Sstevel@tonic-gate 		tact.sa_resv[0] = tact.sa_resv[1] = 0;	/* cleanliness */
3417c478bd9Sstevel@tonic-gate #endif
3427c478bd9Sstevel@tonic-gate 		/*
3437c478bd9Sstevel@tonic-gate 		 * To be compatible with the behavior of SunOS 4.x:
3447c478bd9Sstevel@tonic-gate 		 * If the new signal handler is SIG_IGN or SIG_DFL, do
3457c478bd9Sstevel@tonic-gate 		 * not change the signal's entry in the siguaction array.
3467c478bd9Sstevel@tonic-gate 		 * This allows a child of vfork(2) to set signal handlers
3477c478bd9Sstevel@tonic-gate 		 * to SIG_IGN or SIG_DFL without affecting the parent.
3487c478bd9Sstevel@tonic-gate 		 *
3497c478bd9Sstevel@tonic-gate 		 * This also covers a race condition with some thread
3507c478bd9Sstevel@tonic-gate 		 * setting the signal action to SIG_DFL or SIG_IGN
3517c478bd9Sstevel@tonic-gate 		 * when the thread has also received and deferred
3527c478bd9Sstevel@tonic-gate 		 * that signal.  When the thread takes the deferred
3537c478bd9Sstevel@tonic-gate 		 * signal, even though it has set the action to SIG_DFL
3547c478bd9Sstevel@tonic-gate 		 * or SIG_IGN, it will execute the old signal handler
3557c478bd9Sstevel@tonic-gate 		 * anyway.  This is an inherent signaling race condition
3567c478bd9Sstevel@tonic-gate 		 * and is not a bug.
3577c478bd9Sstevel@tonic-gate 		 *
3587c478bd9Sstevel@tonic-gate 		 * A child of vfork() is not allowed to change signal
3597c478bd9Sstevel@tonic-gate 		 * handlers to anything other than SIG_DFL or SIG_IGN.
3607c478bd9Sstevel@tonic-gate 		 */
3617c478bd9Sstevel@tonic-gate 		if (self->ul_vfork) {
3627c478bd9Sstevel@tonic-gate 			if (tact.sa_sigaction != SIG_IGN)
3637c478bd9Sstevel@tonic-gate 				tact.sa_sigaction = SIG_DFL;
364f841f6adSraf 		} else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
3657c478bd9Sstevel@tonic-gate 			/*
366f841f6adSraf 			 * Always catch these signals.
367f841f6adSraf 			 * We need SIGCANCEL for pthread_cancel() to work.
368f841f6adSraf 			 * We need SIGAIOCANCEL for aio_cancel() to work.
3697c478bd9Sstevel@tonic-gate 			 */
3707c478bd9Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
3717c478bd9Sstevel@tonic-gate 			if (tact.sa_sigaction == SIG_DFL ||
3727c478bd9Sstevel@tonic-gate 			    tact.sa_sigaction == SIG_IGN)
3737c478bd9Sstevel@tonic-gate 				tact.sa_flags = SA_SIGINFO;
3747c478bd9Sstevel@tonic-gate 			else {
3757c478bd9Sstevel@tonic-gate 				tact.sa_flags |= SA_SIGINFO;
3767c478bd9Sstevel@tonic-gate 				tact.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
3777c478bd9Sstevel@tonic-gate 			}
3787c478bd9Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
3797c478bd9Sstevel@tonic-gate 			tact.sa_mask = maskset;
3807c478bd9Sstevel@tonic-gate 		} else if (tact.sa_sigaction != SIG_DFL &&
3817c478bd9Sstevel@tonic-gate 		    tact.sa_sigaction != SIG_IGN) {
3827c478bd9Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
3837c478bd9Sstevel@tonic-gate 			tact.sa_flags &= ~SA_NODEFER;
3847c478bd9Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
3857c478bd9Sstevel@tonic-gate 			tact.sa_mask = maskset;
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if ((rv = __sigaction(sig, tactp, oact)) != 0)
3907c478bd9Sstevel@tonic-gate 		udp->siguaction[sig].sig_uaction = oaction;
3917c478bd9Sstevel@tonic-gate 	else if (oact != NULL &&
3927c478bd9Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_DFL &&
3937c478bd9Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_IGN)
3947c478bd9Sstevel@tonic-gate 		*oact = oaction;
3957c478bd9Sstevel@tonic-gate 
396f841f6adSraf 	/*
397f841f6adSraf 	 * We detect setting the disposition of SIGIO just to set the
398f841f6adSraf 	 * _sigio_enabled flag for the asynchronous i/o (aio) code.
399f841f6adSraf 	 */
400f841f6adSraf 	if (sig == SIGIO && rv == 0 && tactp != NULL) {
401f841f6adSraf 		_sigio_enabled =
402f841f6adSraf 		    (tactp->sa_handler != SIG_DFL &&
403f841f6adSraf 		    tactp->sa_handler != SIG_IGN);
404f841f6adSraf 	}
405f841f6adSraf 
4067c478bd9Sstevel@tonic-gate 	if (!self->ul_vfork)
407*41efec22Sraf 		lrw_unlock(&udp->siguaction[sig].sig_lock);
4087c478bd9Sstevel@tonic-gate 	return (rv);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4119acbbeafSnn void
4129acbbeafSnn setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
4139acbbeafSnn     void (**osigacthandler)(int, siginfo_t *, void *))
4149acbbeafSnn {
4159acbbeafSnn 	ulwp_t *self = curthread;
4169acbbeafSnn 	uberdata_t *udp = self->ul_uberdata;
4179acbbeafSnn 
4189acbbeafSnn 	if (osigacthandler != NULL)
4199acbbeafSnn 		*osigacthandler = udp->sigacthandler;
4209acbbeafSnn 
4219acbbeafSnn 	udp->sigacthandler = nsigacthandler;
4229acbbeafSnn }
4239acbbeafSnn 
4247c478bd9Sstevel@tonic-gate /*
4257c478bd9Sstevel@tonic-gate  * Calling set_parking_flag(curthread, 1) informs the kernel that we are
4267c478bd9Sstevel@tonic-gate  * calling __lwp_park or ___lwp_cond_wait().  If we take a signal in
4277c478bd9Sstevel@tonic-gate  * the unprotected (from signals) interval before reaching the kernel,
4287c478bd9Sstevel@tonic-gate  * sigacthandler() will call set_parking_flag(curthread, 0) to inform
4297c478bd9Sstevel@tonic-gate  * the kernel to return immediately from these system calls, giving us
4307c478bd9Sstevel@tonic-gate  * a spurious wakeup but not a deadlock.
4317c478bd9Sstevel@tonic-gate  */
4327c478bd9Sstevel@tonic-gate void
4337c478bd9Sstevel@tonic-gate set_parking_flag(ulwp_t *self, int park)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate 	volatile sc_shared_t *scp;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	enter_critical(self);
4387c478bd9Sstevel@tonic-gate 	if ((scp = self->ul_schedctl) != NULL ||
4397c478bd9Sstevel@tonic-gate 	    (scp = setup_schedctl()) != NULL)
4407c478bd9Sstevel@tonic-gate 		scp->sc_park = park;
4417c478bd9Sstevel@tonic-gate 	else if (park == 0)	/* schedctl failed, do it the long way */
4427c478bd9Sstevel@tonic-gate 		__lwp_unpark(self->ul_lwpid);
4437c478bd9Sstevel@tonic-gate 	exit_critical(self);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * Tell the kernel to block all signals.
4487c478bd9Sstevel@tonic-gate  * Use the schedctl interface, or failing that, use __lwp_sigmask().
4497c478bd9Sstevel@tonic-gate  * This action can be rescinded only by making a system call that
4507c478bd9Sstevel@tonic-gate  * sets the signal mask:
4517c478bd9Sstevel@tonic-gate  *	__lwp_sigmask(), __sigprocmask(), __setcontext(),
4527c478bd9Sstevel@tonic-gate  *	__sigsuspend() or __pollsys().
4537c478bd9Sstevel@tonic-gate  * In particular, this action cannot be reversed by assigning
4547c478bd9Sstevel@tonic-gate  * scp->sc_sigblock = 0.  That would be a way to lose signals.
4557c478bd9Sstevel@tonic-gate  * See the definition of restore_signals(self).
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate void
4587c478bd9Sstevel@tonic-gate block_all_signals(ulwp_t *self)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	volatile sc_shared_t *scp;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	enter_critical(self);
4637c478bd9Sstevel@tonic-gate 	if ((scp = self->ul_schedctl) != NULL ||
4647c478bd9Sstevel@tonic-gate 	    (scp = setup_schedctl()) != NULL)
4657c478bd9Sstevel@tonic-gate 		scp->sc_sigblock = 1;
4667c478bd9Sstevel@tonic-gate 	else
4677c478bd9Sstevel@tonic-gate 		(void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL);
4687c478bd9Sstevel@tonic-gate 	exit_critical(self);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4719acbbeafSnn /*
4729acbbeafSnn  * _private_setcontext has code that forcibly restores the curthread
4739acbbeafSnn  * pointer in a context passed to the setcontext(2) syscall.
4749acbbeafSnn  *
4759acbbeafSnn  * Certain processes may need to disable this feature, so these routines
4769acbbeafSnn  * provide the mechanism to do so.
4779acbbeafSnn  *
4789acbbeafSnn  * (As an example, branded 32-bit x86 processes may use %gs for their own
4799acbbeafSnn  * purposes, so they need to be able to specify a %gs value to be restored
4809acbbeafSnn  * on return from a signal handler via the passed ucontext_t.)
4819acbbeafSnn  */
4829acbbeafSnn static int setcontext_enforcement = 1;
4839acbbeafSnn 
4849acbbeafSnn void
4859acbbeafSnn set_setcontext_enforcement(int on)
4869acbbeafSnn {
4879acbbeafSnn 	setcontext_enforcement = on;
4889acbbeafSnn }
4899acbbeafSnn 
4907c478bd9Sstevel@tonic-gate #pragma weak setcontext = _private_setcontext
4917c478bd9Sstevel@tonic-gate #pragma weak _setcontext = _private_setcontext
4927c478bd9Sstevel@tonic-gate int
4937c478bd9Sstevel@tonic-gate _private_setcontext(const ucontext_t *ucp)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
4967c478bd9Sstevel@tonic-gate 	int ret;
4977c478bd9Sstevel@tonic-gate 	ucontext_t uc;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/*
5007c478bd9Sstevel@tonic-gate 	 * Returning from the main context (uc_link == NULL) causes
5017c478bd9Sstevel@tonic-gate 	 * the thread to exit.  See setcontext(2) and makecontext(3C).
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	if (ucp == NULL)
5047c478bd9Sstevel@tonic-gate 		_thr_exit(NULL);
5050293487cSraf 	(void) _private_memcpy(&uc, ucp, sizeof (uc));
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/*
5087c478bd9Sstevel@tonic-gate 	 * Restore previous signal mask and context link.
5097c478bd9Sstevel@tonic-gate 	 */
5107c478bd9Sstevel@tonic-gate 	if (uc.uc_flags & UC_SIGMASK) {
5117c478bd9Sstevel@tonic-gate 		block_all_signals(self);
5127c478bd9Sstevel@tonic-gate 		delete_reserved_signals(&uc.uc_sigmask);
5137c478bd9Sstevel@tonic-gate 		self->ul_sigmask = uc.uc_sigmask;
5147c478bd9Sstevel@tonic-gate 		if (self->ul_cursig) {
5157c478bd9Sstevel@tonic-gate 			/*
5167c478bd9Sstevel@tonic-gate 			 * We have a deferred signal present.
5177c478bd9Sstevel@tonic-gate 			 * The signal mask will be set when the
5187c478bd9Sstevel@tonic-gate 			 * signal is taken in take_deferred_signal().
5197c478bd9Sstevel@tonic-gate 			 */
5207c478bd9Sstevel@tonic-gate 			ASSERT(self->ul_critical + self->ul_sigdefer != 0);
5217c478bd9Sstevel@tonic-gate 			uc.uc_flags &= ~UC_SIGMASK;
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 	self->ul_siglink = uc.uc_link;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	/*
5277c478bd9Sstevel@tonic-gate 	 * We don't know where this context structure has been.
5287c478bd9Sstevel@tonic-gate 	 * Preserve the curthread pointer, at least.
5299acbbeafSnn 	 *
5309acbbeafSnn 	 * Allow this feature to be disabled if a particular process
5319acbbeafSnn 	 * requests it.
5327c478bd9Sstevel@tonic-gate 	 */
5339acbbeafSnn 	if (setcontext_enforcement) {
5347c478bd9Sstevel@tonic-gate #if defined(__sparc)
5359acbbeafSnn 		uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
5367c478bd9Sstevel@tonic-gate #elif defined(__amd64)
537ae115bc7Smrj 		uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
5387c478bd9Sstevel@tonic-gate #elif defined(__i386)
539ae115bc7Smrj 		uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
5407c478bd9Sstevel@tonic-gate #else
5417c478bd9Sstevel@tonic-gate #error "none of __sparc, __amd64, __i386 defined"
5427c478bd9Sstevel@tonic-gate #endif
5439acbbeafSnn 	}
5449acbbeafSnn 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
5477c478bd9Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
5487c478bd9Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	set_parking_flag(self, 0);
5517c478bd9Sstevel@tonic-gate 	self->ul_sp = 0;
5527c478bd9Sstevel@tonic-gate 	ret = __setcontext_syscall(&uc);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * It is OK for setcontext() to return if the user has not specified
5567c478bd9Sstevel@tonic-gate 	 * UC_CPU.
5577c478bd9Sstevel@tonic-gate 	 */
5587c478bd9Sstevel@tonic-gate 	if (uc.uc_flags & UC_CPU)
5597c478bd9Sstevel@tonic-gate 		thr_panic("setcontext(): __setcontext() returned");
5607c478bd9Sstevel@tonic-gate 	return (ret);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate #pragma weak thr_sigsetmask = _thr_sigsetmask
5647c478bd9Sstevel@tonic-gate #pragma weak pthread_sigmask = _thr_sigsetmask
5657c478bd9Sstevel@tonic-gate #pragma weak _pthread_sigmask = _thr_sigsetmask
5667c478bd9Sstevel@tonic-gate int
5677c478bd9Sstevel@tonic-gate _thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
5707c478bd9Sstevel@tonic-gate 	sigset_t saveset;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (set == NULL) {
5737c478bd9Sstevel@tonic-gate 		enter_critical(self);
5747c478bd9Sstevel@tonic-gate 		if (oset != NULL)
5757c478bd9Sstevel@tonic-gate 			*oset = self->ul_sigmask;
5767c478bd9Sstevel@tonic-gate 		exit_critical(self);
5777c478bd9Sstevel@tonic-gate 	} else {
5787c478bd9Sstevel@tonic-gate 		switch (how) {
5797c478bd9Sstevel@tonic-gate 		case SIG_BLOCK:
5807c478bd9Sstevel@tonic-gate 		case SIG_UNBLOCK:
5817c478bd9Sstevel@tonic-gate 		case SIG_SETMASK:
5827c478bd9Sstevel@tonic-gate 			break;
5837c478bd9Sstevel@tonic-gate 		default:
5847c478bd9Sstevel@tonic-gate 			return (EINVAL);
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		/*
5887c478bd9Sstevel@tonic-gate 		 * The assignments to self->ul_sigmask must be protected from
5897c478bd9Sstevel@tonic-gate 		 * signals.  The nuances of this code are subtle.  Be careful.
5907c478bd9Sstevel@tonic-gate 		 */
5917c478bd9Sstevel@tonic-gate 		block_all_signals(self);
5927c478bd9Sstevel@tonic-gate 		if (oset != NULL)
5937c478bd9Sstevel@tonic-gate 			saveset = self->ul_sigmask;
5947c478bd9Sstevel@tonic-gate 		switch (how) {
5957c478bd9Sstevel@tonic-gate 		case SIG_BLOCK:
5967c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
5977c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
5987c478bd9Sstevel@tonic-gate 			break;
5997c478bd9Sstevel@tonic-gate 		case SIG_UNBLOCK:
6007c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
6017c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
6027c478bd9Sstevel@tonic-gate 			break;
6037c478bd9Sstevel@tonic-gate 		case SIG_SETMASK:
6047c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
6057c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
6067c478bd9Sstevel@tonic-gate 			break;
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 		delete_reserved_signals(&self->ul_sigmask);
6097c478bd9Sstevel@tonic-gate 		if (oset != NULL)
6107c478bd9Sstevel@tonic-gate 			*oset = saveset;
6117c478bd9Sstevel@tonic-gate 		restore_signals(self);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	return (0);
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate #pragma weak sigprocmask = _sigprocmask
6187c478bd9Sstevel@tonic-gate int
6197c478bd9Sstevel@tonic-gate _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	int error;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/*
6247c478bd9Sstevel@tonic-gate 	 * Guard against children of vfork().
6257c478bd9Sstevel@tonic-gate 	 */
6267c478bd9Sstevel@tonic-gate 	if (curthread->ul_vfork)
6277c478bd9Sstevel@tonic-gate 		return (__lwp_sigmask(how, set, oset));
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if ((error = _thr_sigsetmask(how, set, oset)) != 0) {
6307c478bd9Sstevel@tonic-gate 		errno = error;
6317c478bd9Sstevel@tonic-gate 		return (-1);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	return (0);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  * Called at library initialization to set up signal handling.
639*41efec22Sraf  * All we really do is initialize the sig_lock rwlocks.
6407c478bd9Sstevel@tonic-gate  * All signal handlers are either SIG_DFL or SIG_IGN on exec().
6417c478bd9Sstevel@tonic-gate  * However, if any signal handlers were established on alternate
6427c478bd9Sstevel@tonic-gate  * link maps before the primary link map has been initialized,
6437c478bd9Sstevel@tonic-gate  * then inform the kernel of the new sigacthandler.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate void
6467c478bd9Sstevel@tonic-gate signal_init()
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
6497c478bd9Sstevel@tonic-gate 	struct sigaction *sap;
6507c478bd9Sstevel@tonic-gate 	struct sigaction act;
651*41efec22Sraf 	rwlock_t *rwlp;
6527c478bd9Sstevel@tonic-gate 	int sig;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	for (sig = 0; sig < NSIG; sig++) {
655*41efec22Sraf 		rwlp = &udp->siguaction[sig].sig_lock;
656*41efec22Sraf 		rwlp->rwlock_magic = RWL_MAGIC;
657*41efec22Sraf 		rwlp->mutex.mutex_flag = LOCK_INITED;
658*41efec22Sraf 		rwlp->mutex.mutex_magic = MUTEX_MAGIC;
6597c478bd9Sstevel@tonic-gate 		sap = &udp->siguaction[sig].sig_uaction;
6607c478bd9Sstevel@tonic-gate 		if (sap->sa_sigaction != SIG_DFL &&
6617c478bd9Sstevel@tonic-gate 		    sap->sa_sigaction != SIG_IGN &&
6627c478bd9Sstevel@tonic-gate 		    __sigaction(sig, NULL, &act) == 0 &&
6637c478bd9Sstevel@tonic-gate 		    act.sa_sigaction != SIG_DFL &&
6647c478bd9Sstevel@tonic-gate 		    act.sa_sigaction != SIG_IGN) {
6657c478bd9Sstevel@tonic-gate 			act = *sap;
6667c478bd9Sstevel@tonic-gate 			act.sa_flags &= ~SA_NODEFER;
6677c478bd9Sstevel@tonic-gate 			act.sa_sigaction = udp->sigacthandler;
6687c478bd9Sstevel@tonic-gate 			act.sa_mask = maskset;
6697c478bd9Sstevel@tonic-gate 			(void) __sigaction(sig, &act, NULL);
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate /*
6757c478bd9Sstevel@tonic-gate  * Common code for cancelling self in _sigcancel() and pthread_cancel().
6767c478bd9Sstevel@tonic-gate  * If the thread is at a cancellation point (ul_cancelable) then just
6777c478bd9Sstevel@tonic-gate  * return and let _canceloff() do the exit, else exit immediately if
6787c478bd9Sstevel@tonic-gate  * async mode is in effect.
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate void
6817c478bd9Sstevel@tonic-gate do_sigcancel()
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
6867c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer == 0);
6877c478bd9Sstevel@tonic-gate 	self->ul_cancel_pending = 1;
6887c478bd9Sstevel@tonic-gate 	if (self->ul_cancel_async &&
6897c478bd9Sstevel@tonic-gate 	    !self->ul_cancel_disabled &&
6907c478bd9Sstevel@tonic-gate 	    !self->ul_cancelable)
6917c478bd9Sstevel@tonic-gate 		_pthread_exit(PTHREAD_CANCELED);
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate /*
695f841f6adSraf  * Set up the SIGCANCEL handler for threads cancellation,
696f841f6adSraf  * needed only when we have more than one thread,
697f841f6adSraf  * or the SIGAIOCANCEL handler for aio cancellation,
698f841f6adSraf  * called when aio is initialized, in __uaio_init().
6997c478bd9Sstevel@tonic-gate  */
7007c478bd9Sstevel@tonic-gate void
701f841f6adSraf setup_cancelsig(int sig)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
704*41efec22Sraf 	rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
7057c478bd9Sstevel@tonic-gate 	struct sigaction act;
7067c478bd9Sstevel@tonic-gate 
707f841f6adSraf 	ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
708*41efec22Sraf 	lrw_rdlock(rwlp);
709f841f6adSraf 	act = udp->siguaction[sig].sig_uaction;
710*41efec22Sraf 	lrw_unlock(rwlp);
7117c478bd9Sstevel@tonic-gate 	if (act.sa_sigaction == SIG_DFL ||
7127c478bd9Sstevel@tonic-gate 	    act.sa_sigaction == SIG_IGN)
7137c478bd9Sstevel@tonic-gate 		act.sa_flags = SA_SIGINFO;
7147c478bd9Sstevel@tonic-gate 	else {
7157c478bd9Sstevel@tonic-gate 		act.sa_flags |= SA_SIGINFO;
7167c478bd9Sstevel@tonic-gate 		act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 	act.sa_sigaction = udp->sigacthandler;
7197c478bd9Sstevel@tonic-gate 	act.sa_mask = maskset;
720f841f6adSraf 	(void) __sigaction(sig, &act, NULL);
7217c478bd9Sstevel@tonic-gate }
722