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