1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "lint.h"
30*7c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
31*7c478bd9Sstevel@tonic-gate #include <signal.h>
32*7c478bd9Sstevel@tonic-gate #include <siginfo.h>
33*7c478bd9Sstevel@tonic-gate #include <ucontext.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate const sigset_t maskset = {MASKSET0, MASKSET1, 0, 0};	/* maskable signals */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  * Return true if the valid signal bits in both sets are the same.
40*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate int
42*7c478bd9Sstevel@tonic-gate sigequalset(const sigset_t *s1, const sigset_t *s2)
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	/*
45*7c478bd9Sstevel@tonic-gate 	 * We only test valid signal bits, not rubbish following MAXSIG
46*7c478bd9Sstevel@tonic-gate 	 * (for speed).  Algorithm:
47*7c478bd9Sstevel@tonic-gate 	 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
48*7c478bd9Sstevel@tonic-gate 	 */
49*7c478bd9Sstevel@tonic-gate 	return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) |
50*7c478bd9Sstevel@tonic-gate 	    ((s1->__sigbits[1] ^ s2->__sigbits[1]) & FILLSET1)));
51*7c478bd9Sstevel@tonic-gate }
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate /*
54*7c478bd9Sstevel@tonic-gate  * Common code for calling the user-specified signal handler.
55*7c478bd9Sstevel@tonic-gate  */
56*7c478bd9Sstevel@tonic-gate void
57*7c478bd9Sstevel@tonic-gate call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
60*7c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
61*7c478bd9Sstevel@tonic-gate 	struct sigaction uact;
62*7c478bd9Sstevel@tonic-gate 	volatile struct sigaction *sap;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	/*
65*7c478bd9Sstevel@tonic-gate 	 * If we are taking a signal while parked or about to be parked
66*7c478bd9Sstevel@tonic-gate 	 * on __lwp_park() then remove ourself from the sleep queue so
67*7c478bd9Sstevel@tonic-gate 	 * that we can grab locks.  The code in mutex_lock_queue() and
68*7c478bd9Sstevel@tonic-gate 	 * cond_wait_common() will detect this and deal with it when
69*7c478bd9Sstevel@tonic-gate 	 * __lwp_park() returns.
70*7c478bd9Sstevel@tonic-gate 	 */
71*7c478bd9Sstevel@tonic-gate 	unsleep_self();
72*7c478bd9Sstevel@tonic-gate 	set_parking_flag(self, 0);
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	if (__td_event_report(self, TD_CATCHSIG, udp)) {
75*7c478bd9Sstevel@tonic-gate 		self->ul_td_evbuf.eventnum = TD_CATCHSIG;
76*7c478bd9Sstevel@tonic-gate 		self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig;
77*7c478bd9Sstevel@tonic-gate 		tdb_event(TD_CATCHSIG, udp);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	/*
81*7c478bd9Sstevel@tonic-gate 	 * Get a self-consistent set of flags, handler, and mask
82*7c478bd9Sstevel@tonic-gate 	 * while holding the sig's sig_lock for the least possible time.
83*7c478bd9Sstevel@tonic-gate 	 * We must acquire the sig's sig_lock because some thread running
84*7c478bd9Sstevel@tonic-gate 	 * in sigaction() might be establishing a new signal handler.
85*7c478bd9Sstevel@tonic-gate 	 *
86*7c478bd9Sstevel@tonic-gate 	 * Locking exceptions:
87*7c478bd9Sstevel@tonic-gate 	 * No locking for a child of vfork().
88*7c478bd9Sstevel@tonic-gate 	 * If the signal is SIGPROF with an si_code of PROF_SIG,
89*7c478bd9Sstevel@tonic-gate 	 * then we assume that this signal was generated by
90*7c478bd9Sstevel@tonic-gate 	 * setitimer(ITIMER_REALPROF) set up by the dbx collector.
91*7c478bd9Sstevel@tonic-gate 	 * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
92*7c478bd9Sstevel@tonic-gate 	 * then we assume that the signal was generated by
93*7c478bd9Sstevel@tonic-gate 	 * a hardware performance counter overflow.
94*7c478bd9Sstevel@tonic-gate 	 * In these cases, assume that we need no locking.  It is the
95*7c478bd9Sstevel@tonic-gate 	 * monitoring program's responsibility to ensure correctness.
96*7c478bd9Sstevel@tonic-gate 	 */
97*7c478bd9Sstevel@tonic-gate 	sap = &udp->siguaction[sig].sig_uaction;
98*7c478bd9Sstevel@tonic-gate 	if (self->ul_vfork ||
99*7c478bd9Sstevel@tonic-gate 	    (sip != NULL &&
100*7c478bd9Sstevel@tonic-gate 	    ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
101*7c478bd9Sstevel@tonic-gate 	    (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
102*7c478bd9Sstevel@tonic-gate 		/* we wish this assignment could be atomic */
103*7c478bd9Sstevel@tonic-gate 		uact = *sap;
104*7c478bd9Sstevel@tonic-gate 	} else {
105*7c478bd9Sstevel@tonic-gate 		mutex_t *mp = &udp->siguaction[sig].sig_lock;
106*7c478bd9Sstevel@tonic-gate 		lmutex_lock(mp);
107*7c478bd9Sstevel@tonic-gate 		uact = *sap;
108*7c478bd9Sstevel@tonic-gate 		if (sig == SIGCANCEL && (sap->sa_flags & SA_RESETHAND))
109*7c478bd9Sstevel@tonic-gate 			sap->sa_sigaction = SIG_DFL;
110*7c478bd9Sstevel@tonic-gate 		lmutex_unlock(mp);
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	/*
114*7c478bd9Sstevel@tonic-gate 	 * Set the proper signal mask and call the user's signal handler.
115*7c478bd9Sstevel@tonic-gate 	 * (We overrode the user-requested signal mask with maskset
116*7c478bd9Sstevel@tonic-gate 	 * so we currently have all blockable signals blocked.)
117*7c478bd9Sstevel@tonic-gate 	 *
118*7c478bd9Sstevel@tonic-gate 	 * We would like to ASSERT() that the signal is not a member of the
119*7c478bd9Sstevel@tonic-gate 	 * signal mask at the previous level (ucp->uc_sigmask) or the specified
120*7c478bd9Sstevel@tonic-gate 	 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
121*7c478bd9Sstevel@tonic-gate 	 * /proc can override this via PCSSIG, so we don't bother.
122*7c478bd9Sstevel@tonic-gate 	 *
123*7c478bd9Sstevel@tonic-gate 	 * We would also like to ASSERT() that the signal mask at the previous
124*7c478bd9Sstevel@tonic-gate 	 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
125*7c478bd9Sstevel@tonic-gate 	 * but /proc can change the thread's signal mask via PCSHOLD, so we
126*7c478bd9Sstevel@tonic-gate 	 * don't bother with that either.
127*7c478bd9Sstevel@tonic-gate 	 */
128*7c478bd9Sstevel@tonic-gate 	ASSERT(ucp->uc_flags & UC_SIGMASK);
129*7c478bd9Sstevel@tonic-gate 	if (self->ul_sigsuspend) {
130*7c478bd9Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
131*7c478bd9Sstevel@tonic-gate 		self->ul_sigsuspend = 0;
132*7c478bd9Sstevel@tonic-gate 		/* the sigsuspend() or pollsys() signal mask */
133*7c478bd9Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &self->ul_tmpmask);
134*7c478bd9Sstevel@tonic-gate 	} else {
135*7c478bd9Sstevel@tonic-gate 		/* the signal mask at the previous level */
136*7c478bd9Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &ucp->uc_sigmask);
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_NODEFER))	/* add current signal */
139*7c478bd9Sstevel@tonic-gate 		(void) _private_sigaddset(&uact.sa_mask, sig);
140*7c478bd9Sstevel@tonic-gate 	self->ul_sigmask = uact.sa_mask;
141*7c478bd9Sstevel@tonic-gate 	self->ul_siglink = ucp;
142*7c478bd9Sstevel@tonic-gate 	(void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL);
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	/*
145*7c478bd9Sstevel@tonic-gate 	 * If this thread has been sent SIGCANCEL from the kernel
146*7c478bd9Sstevel@tonic-gate 	 * or from pthread_cancel(), it is being asked to exit.
147*7c478bd9Sstevel@tonic-gate 	 * The kernel may send SIGCANCEL without a siginfo struct.
148*7c478bd9Sstevel@tonic-gate 	 * If the SIGCANCEL is process-directed (from kill() or
149*7c478bd9Sstevel@tonic-gate 	 * sigqueue()), treat it as an ordinary signal.
150*7c478bd9Sstevel@tonic-gate 	 */
151*7c478bd9Sstevel@tonic-gate 	if (sig == SIGCANCEL) {
152*7c478bd9Sstevel@tonic-gate 		if (sip == NULL || SI_FROMKERNEL(sip) ||
153*7c478bd9Sstevel@tonic-gate 		    sip->si_code == SI_LWP) {
154*7c478bd9Sstevel@tonic-gate 			do_sigcancel();
155*7c478bd9Sstevel@tonic-gate 			goto out;
156*7c478bd9Sstevel@tonic-gate 		}
157*7c478bd9Sstevel@tonic-gate 		if (uact.sa_sigaction == SIG_DFL ||
158*7c478bd9Sstevel@tonic-gate 		    uact.sa_sigaction == SIG_IGN)
159*7c478bd9Sstevel@tonic-gate 			goto out;
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_SIGINFO))
163*7c478bd9Sstevel@tonic-gate 		sip = NULL;
164*7c478bd9Sstevel@tonic-gate 	__sighndlr(sig, sip, ucp, uact.sa_sigaction);
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate #if defined(sparc) || defined(__sparc)
167*7c478bd9Sstevel@tonic-gate 	/*
168*7c478bd9Sstevel@tonic-gate 	 * If this is a floating point exception and the queue
169*7c478bd9Sstevel@tonic-gate 	 * is non-empty, pop the top entry from the queue.  This
170*7c478bd9Sstevel@tonic-gate 	 * is to maintain expected behavior.
171*7c478bd9Sstevel@tonic-gate 	 */
172*7c478bd9Sstevel@tonic-gate 	if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
173*7c478bd9Sstevel@tonic-gate 		fpregset_t *fp = &ucp->uc_mcontext.fpregs;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 		if (--fp->fpu_qcnt > 0) {
176*7c478bd9Sstevel@tonic-gate 			unsigned char i;
177*7c478bd9Sstevel@tonic-gate 			struct fq *fqp;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 			fqp = fp->fpu_q;
180*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < fp->fpu_qcnt; i++)
181*7c478bd9Sstevel@tonic-gate 				fqp[i] = fqp[i+1];
182*7c478bd9Sstevel@tonic-gate 		}
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate #endif	/* sparc */
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate out:
187*7c478bd9Sstevel@tonic-gate 	(void) _private_setcontext(ucp);
188*7c478bd9Sstevel@tonic-gate 	thr_panic("call_user_handler(): _setcontext() returned");
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate /*
192*7c478bd9Sstevel@tonic-gate  * take_deferred_signal() is called when ul_critical and ul_sigdefer become
193*7c478bd9Sstevel@tonic-gate  * zero and a deferred signal has been recorded on the current thread.
194*7c478bd9Sstevel@tonic-gate  * We are out of the critical region and are ready to take a signal.
195*7c478bd9Sstevel@tonic-gate  * The kernel has all signals blocked on this lwp, but our value of
196*7c478bd9Sstevel@tonic-gate  * ul_sigmask is the correct signal mask for the previous context.
197*7c478bd9Sstevel@tonic-gate  */
198*7c478bd9Sstevel@tonic-gate void
199*7c478bd9Sstevel@tonic-gate take_deferred_signal(int sig)
200*7c478bd9Sstevel@tonic-gate {
201*7c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
202*7c478bd9Sstevel@tonic-gate 	siginfo_t siginfo;
203*7c478bd9Sstevel@tonic-gate 	siginfo_t *sip;
204*7c478bd9Sstevel@tonic-gate 	ucontext_t uc;
205*7c478bd9Sstevel@tonic-gate 	volatile int returning;
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
208*7c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer == 0);
209*7c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_cursig == 0);
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	returning = 0;
212*7c478bd9Sstevel@tonic-gate 	uc.uc_flags = UC_ALL;
213*7c478bd9Sstevel@tonic-gate 	/*
214*7c478bd9Sstevel@tonic-gate 	 * We call _private_getcontext (a libc-private synonym for
215*7c478bd9Sstevel@tonic-gate 	 * _getcontext) rather than _getcontext because we need to
216*7c478bd9Sstevel@tonic-gate 	 * avoid the dynamic linker and link auditing problems here.
217*7c478bd9Sstevel@tonic-gate 	 */
218*7c478bd9Sstevel@tonic-gate 	(void) _private_getcontext(&uc);
219*7c478bd9Sstevel@tonic-gate 	/*
220*7c478bd9Sstevel@tonic-gate 	 * If the application signal handler calls setcontext() on
221*7c478bd9Sstevel@tonic-gate 	 * the ucontext we give it, it returns here, then we return.
222*7c478bd9Sstevel@tonic-gate 	 */
223*7c478bd9Sstevel@tonic-gate 	if (returning)
224*7c478bd9Sstevel@tonic-gate 		return;
225*7c478bd9Sstevel@tonic-gate 	returning = 1;
226*7c478bd9Sstevel@tonic-gate 	ASSERT(sigequalset(&uc.uc_sigmask, &maskset));
227*7c478bd9Sstevel@tonic-gate 	if (self->ul_siginfo.si_signo == 0)
228*7c478bd9Sstevel@tonic-gate 		sip = NULL;
229*7c478bd9Sstevel@tonic-gate 	else {
230*7c478bd9Sstevel@tonic-gate 		siginfo = self->ul_siginfo;
231*7c478bd9Sstevel@tonic-gate 		sip = &siginfo;
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 	uc.uc_sigmask = self->ul_sigmask;
234*7c478bd9Sstevel@tonic-gate 	call_user_handler(sig, sip, &uc);
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate void
238*7c478bd9Sstevel@tonic-gate sigacthandler(int sig, siginfo_t *sip, void *uvp)
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate 	ucontext_t *ucp = uvp;
241*7c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	/*
244*7c478bd9Sstevel@tonic-gate 	 * Do this in case we took a signal while in a cancelable system call.
245*7c478bd9Sstevel@tonic-gate 	 * It does no harm if we were not in such a system call.
246*7c478bd9Sstevel@tonic-gate 	 */
247*7c478bd9Sstevel@tonic-gate 	self->ul_sp = 0;
248*7c478bd9Sstevel@tonic-gate 	if (sig != SIGCANCEL)
249*7c478bd9Sstevel@tonic-gate 		self->ul_cancel_async = self->ul_save_async;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	/*
252*7c478bd9Sstevel@tonic-gate 	 * If we are not in a critical region and are
253*7c478bd9Sstevel@tonic-gate 	 * not deferring signals, take the signal now.
254*7c478bd9Sstevel@tonic-gate 	 */
255*7c478bd9Sstevel@tonic-gate 	if ((self->ul_critical + self->ul_sigdefer) == 0) {
256*7c478bd9Sstevel@tonic-gate 		call_user_handler(sig, sip, ucp);
257*7c478bd9Sstevel@tonic-gate 		return;	/* call_user_handler() cannot return */
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	/*
261*7c478bd9Sstevel@tonic-gate 	 * We are in a critical region or we are deferring signals.  When
262*7c478bd9Sstevel@tonic-gate 	 * we emerge from the region we will call take_deferred_signal().
263*7c478bd9Sstevel@tonic-gate 	 */
264*7c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_cursig == 0);
265*7c478bd9Sstevel@tonic-gate 	self->ul_cursig = (char)sig;
266*7c478bd9Sstevel@tonic-gate 	if (sip != NULL)
267*7c478bd9Sstevel@tonic-gate 		self->ul_siginfo = *sip;
268*7c478bd9Sstevel@tonic-gate 	else
269*7c478bd9Sstevel@tonic-gate 		self->ul_siginfo.si_signo = 0;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	/*
272*7c478bd9Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
273*7c478bd9Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
274*7c478bd9Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
275*7c478bd9Sstevel@tonic-gate 	 */
276*7c478bd9Sstevel@tonic-gate 	set_parking_flag(self, 0);
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	/*
279*7c478bd9Sstevel@tonic-gate 	 * Return to the previous context with all signals blocked.
280*7c478bd9Sstevel@tonic-gate 	 * We will restore the signal mask in take_deferred_signal().
281*7c478bd9Sstevel@tonic-gate 	 * Note that we are calling the system call trap here, not
282*7c478bd9Sstevel@tonic-gate 	 * the _setcontext() wrapper.  We don't want to change the
283*7c478bd9Sstevel@tonic-gate 	 * thread's ul_sigmask by this operation.
284*7c478bd9Sstevel@tonic-gate 	 */
285*7c478bd9Sstevel@tonic-gate 	ucp->uc_sigmask = maskset;
286*7c478bd9Sstevel@tonic-gate 	(void) __setcontext_syscall(ucp);
287*7c478bd9Sstevel@tonic-gate 	thr_panic("sigacthandler(): __setcontext() returned");
288*7c478bd9Sstevel@tonic-gate }
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate #pragma weak sigaction = _sigaction
291*7c478bd9Sstevel@tonic-gate int
292*7c478bd9Sstevel@tonic-gate _sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
293*7c478bd9Sstevel@tonic-gate {
294*7c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
295*7c478bd9Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
296*7c478bd9Sstevel@tonic-gate 	struct sigaction oaction;
297*7c478bd9Sstevel@tonic-gate 	struct sigaction tact;
298*7c478bd9Sstevel@tonic-gate 	struct sigaction *tactp = NULL;
299*7c478bd9Sstevel@tonic-gate 	int rv;
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	if (sig <= 0 || sig >= NSIG) {
302*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
303*7c478bd9Sstevel@tonic-gate 		return (-1);
304*7c478bd9Sstevel@tonic-gate 	}
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	if (!self->ul_vfork)
307*7c478bd9Sstevel@tonic-gate 		lmutex_lock(&udp->siguaction[sig].sig_lock);
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	oaction = udp->siguaction[sig].sig_uaction;
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	if (nact != NULL) {
312*7c478bd9Sstevel@tonic-gate 		tact = *nact;	/* make a copy so we can modify it */
313*7c478bd9Sstevel@tonic-gate 		tactp = &tact;
314*7c478bd9Sstevel@tonic-gate 		delete_reserved_signals(&tact.sa_mask);
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate #if !defined(_LP64)
317*7c478bd9Sstevel@tonic-gate 		tact.sa_resv[0] = tact.sa_resv[1] = 0;	/* cleanliness */
318*7c478bd9Sstevel@tonic-gate #endif
319*7c478bd9Sstevel@tonic-gate 		/*
320*7c478bd9Sstevel@tonic-gate 		 * To be compatible with the behavior of SunOS 4.x:
321*7c478bd9Sstevel@tonic-gate 		 * If the new signal handler is SIG_IGN or SIG_DFL, do
322*7c478bd9Sstevel@tonic-gate 		 * not change the signal's entry in the siguaction array.
323*7c478bd9Sstevel@tonic-gate 		 * This allows a child of vfork(2) to set signal handlers
324*7c478bd9Sstevel@tonic-gate 		 * to SIG_IGN or SIG_DFL without affecting the parent.
325*7c478bd9Sstevel@tonic-gate 		 *
326*7c478bd9Sstevel@tonic-gate 		 * This also covers a race condition with some thread
327*7c478bd9Sstevel@tonic-gate 		 * setting the signal action to SIG_DFL or SIG_IGN
328*7c478bd9Sstevel@tonic-gate 		 * when the thread has also received and deferred
329*7c478bd9Sstevel@tonic-gate 		 * that signal.  When the thread takes the deferred
330*7c478bd9Sstevel@tonic-gate 		 * signal, even though it has set the action to SIG_DFL
331*7c478bd9Sstevel@tonic-gate 		 * or SIG_IGN, it will execute the old signal handler
332*7c478bd9Sstevel@tonic-gate 		 * anyway.  This is an inherent signaling race condition
333*7c478bd9Sstevel@tonic-gate 		 * and is not a bug.
334*7c478bd9Sstevel@tonic-gate 		 *
335*7c478bd9Sstevel@tonic-gate 		 * A child of vfork() is not allowed to change signal
336*7c478bd9Sstevel@tonic-gate 		 * handlers to anything other than SIG_DFL or SIG_IGN.
337*7c478bd9Sstevel@tonic-gate 		 */
338*7c478bd9Sstevel@tonic-gate 		if (self->ul_vfork) {
339*7c478bd9Sstevel@tonic-gate 			if (tact.sa_sigaction != SIG_IGN)
340*7c478bd9Sstevel@tonic-gate 				tact.sa_sigaction = SIG_DFL;
341*7c478bd9Sstevel@tonic-gate 		} else if (sig == SIGCANCEL) {
342*7c478bd9Sstevel@tonic-gate 			/*
343*7c478bd9Sstevel@tonic-gate 			 * Always catch SIGCANCEL.
344*7c478bd9Sstevel@tonic-gate 			 * We need it for pthread_cancel() to work.
345*7c478bd9Sstevel@tonic-gate 			 */
346*7c478bd9Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
347*7c478bd9Sstevel@tonic-gate 			if (tact.sa_sigaction == SIG_DFL ||
348*7c478bd9Sstevel@tonic-gate 			    tact.sa_sigaction == SIG_IGN)
349*7c478bd9Sstevel@tonic-gate 				tact.sa_flags = SA_SIGINFO;
350*7c478bd9Sstevel@tonic-gate 			else {
351*7c478bd9Sstevel@tonic-gate 				tact.sa_flags |= SA_SIGINFO;
352*7c478bd9Sstevel@tonic-gate 				tact.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
353*7c478bd9Sstevel@tonic-gate 			}
354*7c478bd9Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
355*7c478bd9Sstevel@tonic-gate 			tact.sa_mask = maskset;
356*7c478bd9Sstevel@tonic-gate 		} else if (tact.sa_sigaction != SIG_DFL &&
357*7c478bd9Sstevel@tonic-gate 		    tact.sa_sigaction != SIG_IGN) {
358*7c478bd9Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
359*7c478bd9Sstevel@tonic-gate 			tact.sa_flags &= ~SA_NODEFER;
360*7c478bd9Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
361*7c478bd9Sstevel@tonic-gate 			tact.sa_mask = maskset;
362*7c478bd9Sstevel@tonic-gate 		}
363*7c478bd9Sstevel@tonic-gate 	}
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	if ((rv = __sigaction(sig, tactp, oact)) != 0)
366*7c478bd9Sstevel@tonic-gate 		udp->siguaction[sig].sig_uaction = oaction;
367*7c478bd9Sstevel@tonic-gate 	else if (oact != NULL &&
368*7c478bd9Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_DFL &&
369*7c478bd9Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_IGN)
370*7c478bd9Sstevel@tonic-gate 		*oact = oaction;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	if (!self->ul_vfork)
373*7c478bd9Sstevel@tonic-gate 		lmutex_unlock(&udp->siguaction[sig].sig_lock);
374*7c478bd9Sstevel@tonic-gate 	return (rv);
375*7c478bd9Sstevel@tonic-gate }
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate /*
378*7c478bd9Sstevel@tonic-gate  * Calling set_parking_flag(curthread, 1) informs the kernel that we are
379*7c478bd9Sstevel@tonic-gate  * calling __lwp_park or ___lwp_cond_wait().  If we take a signal in
380*7c478bd9Sstevel@tonic-gate  * the unprotected (from signals) interval before reaching the kernel,
381*7c478bd9Sstevel@tonic-gate  * sigacthandler() will call set_parking_flag(curthread, 0) to inform
382*7c478bd9Sstevel@tonic-gate  * the kernel to return immediately from these system calls, giving us
383*7c478bd9Sstevel@tonic-gate  * a spurious wakeup but not a deadlock.
384*7c478bd9Sstevel@tonic-gate  */
385*7c478bd9Sstevel@tonic-gate void
386*7c478bd9Sstevel@tonic-gate set_parking_flag(ulwp_t *self, int park)
387*7c478bd9Sstevel@tonic-gate {
388*7c478bd9Sstevel@tonic-gate 	volatile sc_shared_t *scp;
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	enter_critical(self);
391*7c478bd9Sstevel@tonic-gate 	if ((scp = self->ul_schedctl) != NULL ||
392*7c478bd9Sstevel@tonic-gate 	    (scp = setup_schedctl()) != NULL)
393*7c478bd9Sstevel@tonic-gate 		scp->sc_park = park;
394*7c478bd9Sstevel@tonic-gate 	else if (park == 0)	/* schedctl failed, do it the long way */
395*7c478bd9Sstevel@tonic-gate 		__lwp_unpark(self->ul_lwpid);
396*7c478bd9Sstevel@tonic-gate 	exit_critical(self);
397*7c478bd9Sstevel@tonic-gate }
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate /*
400*7c478bd9Sstevel@tonic-gate  * Tell the kernel to block all signals.
401*7c478bd9Sstevel@tonic-gate  * Use the schedctl interface, or failing that, use __lwp_sigmask().
402*7c478bd9Sstevel@tonic-gate  * This action can be rescinded only by making a system call that
403*7c478bd9Sstevel@tonic-gate  * sets the signal mask:
404*7c478bd9Sstevel@tonic-gate  *	__lwp_sigmask(), __sigprocmask(), __setcontext(),
405*7c478bd9Sstevel@tonic-gate  *	__sigsuspend() or __pollsys().
406*7c478bd9Sstevel@tonic-gate  * In particular, this action cannot be reversed by assigning
407*7c478bd9Sstevel@tonic-gate  * scp->sc_sigblock = 0.  That would be a way to lose signals.
408*7c478bd9Sstevel@tonic-gate  * See the definition of restore_signals(self).
409*7c478bd9Sstevel@tonic-gate  */
410*7c478bd9Sstevel@tonic-gate void
411*7c478bd9Sstevel@tonic-gate block_all_signals(ulwp_t *self)
412*7c478bd9Sstevel@tonic-gate {
413*7c478bd9Sstevel@tonic-gate 	volatile sc_shared_t *scp;
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	enter_critical(self);
416*7c478bd9Sstevel@tonic-gate 	if ((scp = self->ul_schedctl) != NULL ||
417*7c478bd9Sstevel@tonic-gate 	    (scp = setup_schedctl()) != NULL)
418*7c478bd9Sstevel@tonic-gate 		scp->sc_sigblock = 1;
419*7c478bd9Sstevel@tonic-gate 	else
420*7c478bd9Sstevel@tonic-gate 		(void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL);
421*7c478bd9Sstevel@tonic-gate 	exit_critical(self);
422*7c478bd9Sstevel@tonic-gate }
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate #pragma weak setcontext = _private_setcontext
425*7c478bd9Sstevel@tonic-gate #pragma weak _setcontext = _private_setcontext
426*7c478bd9Sstevel@tonic-gate int
427*7c478bd9Sstevel@tonic-gate _private_setcontext(const ucontext_t *ucp)
428*7c478bd9Sstevel@tonic-gate {
429*7c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
430*7c478bd9Sstevel@tonic-gate 	int ret;
431*7c478bd9Sstevel@tonic-gate 	ucontext_t uc;
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	/*
434*7c478bd9Sstevel@tonic-gate 	 * Returning from the main context (uc_link == NULL) causes
435*7c478bd9Sstevel@tonic-gate 	 * the thread to exit.  See setcontext(2) and makecontext(3C).
436*7c478bd9Sstevel@tonic-gate 	 */
437*7c478bd9Sstevel@tonic-gate 	if (ucp == NULL)
438*7c478bd9Sstevel@tonic-gate 		_thr_exit(NULL);
439*7c478bd9Sstevel@tonic-gate 	uc = *ucp;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	/*
442*7c478bd9Sstevel@tonic-gate 	 * Restore previous signal mask and context link.
443*7c478bd9Sstevel@tonic-gate 	 */
444*7c478bd9Sstevel@tonic-gate 	if (uc.uc_flags & UC_SIGMASK) {
445*7c478bd9Sstevel@tonic-gate 		block_all_signals(self);
446*7c478bd9Sstevel@tonic-gate 		delete_reserved_signals(&uc.uc_sigmask);
447*7c478bd9Sstevel@tonic-gate 		self->ul_sigmask = uc.uc_sigmask;
448*7c478bd9Sstevel@tonic-gate 		if (self->ul_cursig) {
449*7c478bd9Sstevel@tonic-gate 			/*
450*7c478bd9Sstevel@tonic-gate 			 * We have a deferred signal present.
451*7c478bd9Sstevel@tonic-gate 			 * The signal mask will be set when the
452*7c478bd9Sstevel@tonic-gate 			 * signal is taken in take_deferred_signal().
453*7c478bd9Sstevel@tonic-gate 			 */
454*7c478bd9Sstevel@tonic-gate 			ASSERT(self->ul_critical + self->ul_sigdefer != 0);
455*7c478bd9Sstevel@tonic-gate 			uc.uc_flags &= ~UC_SIGMASK;
456*7c478bd9Sstevel@tonic-gate 		}
457*7c478bd9Sstevel@tonic-gate 	}
458*7c478bd9Sstevel@tonic-gate 	self->ul_siglink = uc.uc_link;
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	/*
461*7c478bd9Sstevel@tonic-gate 	 * We don't know where this context structure has been.
462*7c478bd9Sstevel@tonic-gate 	 * Preserve the curthread pointer, at least.
463*7c478bd9Sstevel@tonic-gate 	 */
464*7c478bd9Sstevel@tonic-gate #if defined(__sparc)
465*7c478bd9Sstevel@tonic-gate 	uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
466*7c478bd9Sstevel@tonic-gate #elif defined(__amd64)
467*7c478bd9Sstevel@tonic-gate 	uc.uc_mcontext.gregs[REG_FS] = (greg_t)self->ul_gs;
468*7c478bd9Sstevel@tonic-gate #elif defined(__i386)
469*7c478bd9Sstevel@tonic-gate 	uc.uc_mcontext.gregs[GS] = (greg_t)self->ul_gs;
470*7c478bd9Sstevel@tonic-gate #else
471*7c478bd9Sstevel@tonic-gate #error "none of __sparc, __amd64, __i386 defined"
472*7c478bd9Sstevel@tonic-gate #endif
473*7c478bd9Sstevel@tonic-gate 	/*
474*7c478bd9Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
475*7c478bd9Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
476*7c478bd9Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
477*7c478bd9Sstevel@tonic-gate 	 */
478*7c478bd9Sstevel@tonic-gate 	set_parking_flag(self, 0);
479*7c478bd9Sstevel@tonic-gate 	self->ul_sp = 0;
480*7c478bd9Sstevel@tonic-gate 	ret = __setcontext_syscall(&uc);
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 	/*
483*7c478bd9Sstevel@tonic-gate 	 * It is OK for setcontext() to return if the user has not specified
484*7c478bd9Sstevel@tonic-gate 	 * UC_CPU.
485*7c478bd9Sstevel@tonic-gate 	 */
486*7c478bd9Sstevel@tonic-gate 	if (uc.uc_flags & UC_CPU)
487*7c478bd9Sstevel@tonic-gate 		thr_panic("setcontext(): __setcontext() returned");
488*7c478bd9Sstevel@tonic-gate 	return (ret);
489*7c478bd9Sstevel@tonic-gate }
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate #pragma weak thr_sigsetmask = _thr_sigsetmask
492*7c478bd9Sstevel@tonic-gate #pragma weak pthread_sigmask = _thr_sigsetmask
493*7c478bd9Sstevel@tonic-gate #pragma weak _pthread_sigmask = _thr_sigsetmask
494*7c478bd9Sstevel@tonic-gate int
495*7c478bd9Sstevel@tonic-gate _thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
496*7c478bd9Sstevel@tonic-gate {
497*7c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
498*7c478bd9Sstevel@tonic-gate 	sigset_t saveset;
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 	if (set == NULL) {
501*7c478bd9Sstevel@tonic-gate 		enter_critical(self);
502*7c478bd9Sstevel@tonic-gate 		if (oset != NULL)
503*7c478bd9Sstevel@tonic-gate 			*oset = self->ul_sigmask;
504*7c478bd9Sstevel@tonic-gate 		exit_critical(self);
505*7c478bd9Sstevel@tonic-gate 	} else {
506*7c478bd9Sstevel@tonic-gate 		switch (how) {
507*7c478bd9Sstevel@tonic-gate 		case SIG_BLOCK:
508*7c478bd9Sstevel@tonic-gate 		case SIG_UNBLOCK:
509*7c478bd9Sstevel@tonic-gate 		case SIG_SETMASK:
510*7c478bd9Sstevel@tonic-gate 			break;
511*7c478bd9Sstevel@tonic-gate 		default:
512*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
513*7c478bd9Sstevel@tonic-gate 		}
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate 		/*
516*7c478bd9Sstevel@tonic-gate 		 * The assignments to self->ul_sigmask must be protected from
517*7c478bd9Sstevel@tonic-gate 		 * signals.  The nuances of this code are subtle.  Be careful.
518*7c478bd9Sstevel@tonic-gate 		 */
519*7c478bd9Sstevel@tonic-gate 		block_all_signals(self);
520*7c478bd9Sstevel@tonic-gate 		if (oset != NULL)
521*7c478bd9Sstevel@tonic-gate 			saveset = self->ul_sigmask;
522*7c478bd9Sstevel@tonic-gate 		switch (how) {
523*7c478bd9Sstevel@tonic-gate 		case SIG_BLOCK:
524*7c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
525*7c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
526*7c478bd9Sstevel@tonic-gate 			break;
527*7c478bd9Sstevel@tonic-gate 		case SIG_UNBLOCK:
528*7c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
529*7c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
530*7c478bd9Sstevel@tonic-gate 			break;
531*7c478bd9Sstevel@tonic-gate 		case SIG_SETMASK:
532*7c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
533*7c478bd9Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
534*7c478bd9Sstevel@tonic-gate 			break;
535*7c478bd9Sstevel@tonic-gate 		}
536*7c478bd9Sstevel@tonic-gate 		delete_reserved_signals(&self->ul_sigmask);
537*7c478bd9Sstevel@tonic-gate 		if (oset != NULL)
538*7c478bd9Sstevel@tonic-gate 			*oset = saveset;
539*7c478bd9Sstevel@tonic-gate 		restore_signals(self);
540*7c478bd9Sstevel@tonic-gate 	}
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 	return (0);
543*7c478bd9Sstevel@tonic-gate }
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate #pragma weak sigprocmask = _sigprocmask
546*7c478bd9Sstevel@tonic-gate int
547*7c478bd9Sstevel@tonic-gate _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
548*7c478bd9Sstevel@tonic-gate {
549*7c478bd9Sstevel@tonic-gate 	int error;
550*7c478bd9Sstevel@tonic-gate 
551*7c478bd9Sstevel@tonic-gate 	/*
552*7c478bd9Sstevel@tonic-gate 	 * Guard against children of vfork().
553*7c478bd9Sstevel@tonic-gate 	 */
554*7c478bd9Sstevel@tonic-gate 	if (curthread->ul_vfork)
555*7c478bd9Sstevel@tonic-gate 		return (__lwp_sigmask(how, set, oset));
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate 	if ((error = _thr_sigsetmask(how, set, oset)) != 0) {
558*7c478bd9Sstevel@tonic-gate 		errno = error;
559*7c478bd9Sstevel@tonic-gate 		return (-1);
560*7c478bd9Sstevel@tonic-gate 	}
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 	return (0);
563*7c478bd9Sstevel@tonic-gate }
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate /*
566*7c478bd9Sstevel@tonic-gate  * Called at library initialization to set up signal handling.
567*7c478bd9Sstevel@tonic-gate  * All we really do is initialize the sig_lock mutexes.
568*7c478bd9Sstevel@tonic-gate  * All signal handlers are either SIG_DFL or SIG_IGN on exec().
569*7c478bd9Sstevel@tonic-gate  * However, if any signal handlers were established on alternate
570*7c478bd9Sstevel@tonic-gate  * link maps before the primary link map has been initialized,
571*7c478bd9Sstevel@tonic-gate  * then inform the kernel of the new sigacthandler.
572*7c478bd9Sstevel@tonic-gate  */
573*7c478bd9Sstevel@tonic-gate void
574*7c478bd9Sstevel@tonic-gate signal_init()
575*7c478bd9Sstevel@tonic-gate {
576*7c478bd9Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
577*7c478bd9Sstevel@tonic-gate 	struct sigaction *sap;
578*7c478bd9Sstevel@tonic-gate 	struct sigaction act;
579*7c478bd9Sstevel@tonic-gate 	int sig;
580*7c478bd9Sstevel@tonic-gate 
581*7c478bd9Sstevel@tonic-gate 	for (sig = 0; sig < NSIG; sig++) {
582*7c478bd9Sstevel@tonic-gate 		udp->siguaction[sig].sig_lock.mutex_magic = MUTEX_MAGIC;
583*7c478bd9Sstevel@tonic-gate 		sap = &udp->siguaction[sig].sig_uaction;
584*7c478bd9Sstevel@tonic-gate 		if (sap->sa_sigaction != SIG_DFL &&
585*7c478bd9Sstevel@tonic-gate 		    sap->sa_sigaction != SIG_IGN &&
586*7c478bd9Sstevel@tonic-gate 		    __sigaction(sig, NULL, &act) == 0 &&
587*7c478bd9Sstevel@tonic-gate 		    act.sa_sigaction != SIG_DFL &&
588*7c478bd9Sstevel@tonic-gate 		    act.sa_sigaction != SIG_IGN) {
589*7c478bd9Sstevel@tonic-gate 			act = *sap;
590*7c478bd9Sstevel@tonic-gate 			act.sa_flags &= ~SA_NODEFER;
591*7c478bd9Sstevel@tonic-gate 			act.sa_sigaction = udp->sigacthandler;
592*7c478bd9Sstevel@tonic-gate 			act.sa_mask = maskset;
593*7c478bd9Sstevel@tonic-gate 			(void) __sigaction(sig, &act, NULL);
594*7c478bd9Sstevel@tonic-gate 		}
595*7c478bd9Sstevel@tonic-gate 	}
596*7c478bd9Sstevel@tonic-gate }
597*7c478bd9Sstevel@tonic-gate 
598*7c478bd9Sstevel@tonic-gate /*
599*7c478bd9Sstevel@tonic-gate  * Common code for cancelling self in _sigcancel() and pthread_cancel().
600*7c478bd9Sstevel@tonic-gate  * If the thread is at a cancellation point (ul_cancelable) then just
601*7c478bd9Sstevel@tonic-gate  * return and let _canceloff() do the exit, else exit immediately if
602*7c478bd9Sstevel@tonic-gate  * async mode is in effect.
603*7c478bd9Sstevel@tonic-gate  */
604*7c478bd9Sstevel@tonic-gate void
605*7c478bd9Sstevel@tonic-gate do_sigcancel()
606*7c478bd9Sstevel@tonic-gate {
607*7c478bd9Sstevel@tonic-gate 	ulwp_t *self = curthread;
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
610*7c478bd9Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer == 0);
611*7c478bd9Sstevel@tonic-gate 	self->ul_cancel_pending = 1;
612*7c478bd9Sstevel@tonic-gate 	if (self->ul_cancel_async &&
613*7c478bd9Sstevel@tonic-gate 	    !self->ul_cancel_disabled &&
614*7c478bd9Sstevel@tonic-gate 	    !self->ul_cancelable)
615*7c478bd9Sstevel@tonic-gate 		_pthread_exit(PTHREAD_CANCELED);
616*7c478bd9Sstevel@tonic-gate }
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate /*
619*7c478bd9Sstevel@tonic-gate  * Set up the SIGCANCEL handler for threads cancellation
620*7c478bd9Sstevel@tonic-gate  * (needed only when we have more than one thread).
621*7c478bd9Sstevel@tonic-gate  * We need no locks here because we are called from
622*7c478bd9Sstevel@tonic-gate  * finish_init() while still single-threaded.
623*7c478bd9Sstevel@tonic-gate  */
624*7c478bd9Sstevel@tonic-gate void
625*7c478bd9Sstevel@tonic-gate init_sigcancel()
626*7c478bd9Sstevel@tonic-gate {
627*7c478bd9Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
628*7c478bd9Sstevel@tonic-gate 	struct sigaction act;
629*7c478bd9Sstevel@tonic-gate 
630*7c478bd9Sstevel@tonic-gate 	act = udp->siguaction[SIGCANCEL].sig_uaction;
631*7c478bd9Sstevel@tonic-gate 	if (act.sa_sigaction == SIG_DFL ||
632*7c478bd9Sstevel@tonic-gate 	    act.sa_sigaction == SIG_IGN)
633*7c478bd9Sstevel@tonic-gate 		act.sa_flags = SA_SIGINFO;
634*7c478bd9Sstevel@tonic-gate 	else {
635*7c478bd9Sstevel@tonic-gate 		act.sa_flags |= SA_SIGINFO;
636*7c478bd9Sstevel@tonic-gate 		act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
637*7c478bd9Sstevel@tonic-gate 	}
638*7c478bd9Sstevel@tonic-gate 	act.sa_sigaction = udp->sigacthandler;
639*7c478bd9Sstevel@tonic-gate 	act.sa_mask = maskset;
640*7c478bd9Sstevel@tonic-gate 	(void) __sigaction(SIGCANCEL, &act, NULL);
641*7c478bd9Sstevel@tonic-gate }
642