xref: /onnv-gate/usr/src/lib/libc/port/threads/sigaction.c (revision 4570:f93b74ddbdd5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51885Sraf  * Common Development and Distribution License (the "License").
61885Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211111Sraf 
220Sstevel@tonic-gate /*
233446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "lint.h"
300Sstevel@tonic-gate #include "thr_uberdata.h"
312248Sraf #include "asyncio.h"
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include <siginfo.h>
340Sstevel@tonic-gate #include <ucontext.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate const sigset_t maskset = {MASKSET0, MASKSET1, 0, 0};	/* maskable signals */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * Return true if the valid signal bits in both sets are the same.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate int
430Sstevel@tonic-gate sigequalset(const sigset_t *s1, const sigset_t *s2)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	/*
460Sstevel@tonic-gate 	 * We only test valid signal bits, not rubbish following MAXSIG
470Sstevel@tonic-gate 	 * (for speed).  Algorithm:
480Sstevel@tonic-gate 	 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
490Sstevel@tonic-gate 	 */
500Sstevel@tonic-gate 	return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) |
510Sstevel@tonic-gate 	    ((s1->__sigbits[1] ^ s2->__sigbits[1]) & FILLSET1)));
520Sstevel@tonic-gate }
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * Common code for calling the user-specified signal handler.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate void
580Sstevel@tonic-gate call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	ulwp_t *self = curthread;
610Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
620Sstevel@tonic-gate 	struct sigaction uact;
630Sstevel@tonic-gate 	volatile struct sigaction *sap;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	/*
660Sstevel@tonic-gate 	 * If we are taking a signal while parked or about to be parked
670Sstevel@tonic-gate 	 * on __lwp_park() then remove ourself from the sleep queue so
680Sstevel@tonic-gate 	 * that we can grab locks.  The code in mutex_lock_queue() and
690Sstevel@tonic-gate 	 * cond_wait_common() will detect this and deal with it when
700Sstevel@tonic-gate 	 * __lwp_park() returns.
710Sstevel@tonic-gate 	 */
720Sstevel@tonic-gate 	unsleep_self();
730Sstevel@tonic-gate 	set_parking_flag(self, 0);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (__td_event_report(self, TD_CATCHSIG, udp)) {
760Sstevel@tonic-gate 		self->ul_td_evbuf.eventnum = TD_CATCHSIG;
770Sstevel@tonic-gate 		self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig;
780Sstevel@tonic-gate 		tdb_event(TD_CATCHSIG, udp);
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	/*
820Sstevel@tonic-gate 	 * Get a self-consistent set of flags, handler, and mask
830Sstevel@tonic-gate 	 * while holding the sig's sig_lock for the least possible time.
840Sstevel@tonic-gate 	 * We must acquire the sig's sig_lock because some thread running
850Sstevel@tonic-gate 	 * in sigaction() might be establishing a new signal handler.
86*4570Sraf 	 * The code in sigaction() acquires the writer lock; here
87*4570Sraf 	 * we acquire the readers lock to ehance concurrency in the
88*4570Sraf 	 * face of heavy signal traffic, such as generated by java.
890Sstevel@tonic-gate 	 *
900Sstevel@tonic-gate 	 * Locking exceptions:
910Sstevel@tonic-gate 	 * No locking for a child of vfork().
920Sstevel@tonic-gate 	 * If the signal is SIGPROF with an si_code of PROF_SIG,
930Sstevel@tonic-gate 	 * then we assume that this signal was generated by
940Sstevel@tonic-gate 	 * setitimer(ITIMER_REALPROF) set up by the dbx collector.
950Sstevel@tonic-gate 	 * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
960Sstevel@tonic-gate 	 * then we assume that the signal was generated by
970Sstevel@tonic-gate 	 * a hardware performance counter overflow.
980Sstevel@tonic-gate 	 * In these cases, assume that we need no locking.  It is the
990Sstevel@tonic-gate 	 * monitoring program's responsibility to ensure correctness.
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 	sap = &udp->siguaction[sig].sig_uaction;
1020Sstevel@tonic-gate 	if (self->ul_vfork ||
1030Sstevel@tonic-gate 	    (sip != NULL &&
1040Sstevel@tonic-gate 	    ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
1050Sstevel@tonic-gate 	    (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
1060Sstevel@tonic-gate 		/* we wish this assignment could be atomic */
1071111Sraf 		(void) _private_memcpy(&uact, (void *)sap, sizeof (uact));
1080Sstevel@tonic-gate 	} else {
109*4570Sraf 		rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
110*4570Sraf 		lrw_rdlock(rwlp);
1111111Sraf 		(void) _private_memcpy(&uact, (void *)sap, sizeof (uact));
1120Sstevel@tonic-gate 		if (sig == SIGCANCEL && (sap->sa_flags & SA_RESETHAND))
1130Sstevel@tonic-gate 			sap->sa_sigaction = SIG_DFL;
114*4570Sraf 		lrw_unlock(rwlp);
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/*
1180Sstevel@tonic-gate 	 * Set the proper signal mask and call the user's signal handler.
1190Sstevel@tonic-gate 	 * (We overrode the user-requested signal mask with maskset
1200Sstevel@tonic-gate 	 * so we currently have all blockable signals blocked.)
1210Sstevel@tonic-gate 	 *
1220Sstevel@tonic-gate 	 * We would like to ASSERT() that the signal is not a member of the
1230Sstevel@tonic-gate 	 * signal mask at the previous level (ucp->uc_sigmask) or the specified
1240Sstevel@tonic-gate 	 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
1250Sstevel@tonic-gate 	 * /proc can override this via PCSSIG, so we don't bother.
1260Sstevel@tonic-gate 	 *
1270Sstevel@tonic-gate 	 * We would also like to ASSERT() that the signal mask at the previous
1280Sstevel@tonic-gate 	 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
1290Sstevel@tonic-gate 	 * but /proc can change the thread's signal mask via PCSHOLD, so we
1300Sstevel@tonic-gate 	 * don't bother with that either.
1310Sstevel@tonic-gate 	 */
1320Sstevel@tonic-gate 	ASSERT(ucp->uc_flags & UC_SIGMASK);
1330Sstevel@tonic-gate 	if (self->ul_sigsuspend) {
1340Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
1350Sstevel@tonic-gate 		self->ul_sigsuspend = 0;
1360Sstevel@tonic-gate 		/* the sigsuspend() or pollsys() signal mask */
1370Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &self->ul_tmpmask);
1380Sstevel@tonic-gate 	} else {
1390Sstevel@tonic-gate 		/* the signal mask at the previous level */
1400Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &ucp->uc_sigmask);
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_NODEFER))	/* add current signal */
1430Sstevel@tonic-gate 		(void) _private_sigaddset(&uact.sa_mask, sig);
1440Sstevel@tonic-gate 	self->ul_sigmask = uact.sa_mask;
1450Sstevel@tonic-gate 	self->ul_siglink = ucp;
1460Sstevel@tonic-gate 	(void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/*
1490Sstevel@tonic-gate 	 * If this thread has been sent SIGCANCEL from the kernel
1500Sstevel@tonic-gate 	 * or from pthread_cancel(), it is being asked to exit.
1510Sstevel@tonic-gate 	 * The kernel may send SIGCANCEL without a siginfo struct.
1520Sstevel@tonic-gate 	 * If the SIGCANCEL is process-directed (from kill() or
1530Sstevel@tonic-gate 	 * sigqueue()), treat it as an ordinary signal.
1540Sstevel@tonic-gate 	 */
1550Sstevel@tonic-gate 	if (sig == SIGCANCEL) {
1560Sstevel@tonic-gate 		if (sip == NULL || SI_FROMKERNEL(sip) ||
1570Sstevel@tonic-gate 		    sip->si_code == SI_LWP) {
1580Sstevel@tonic-gate 			do_sigcancel();
1590Sstevel@tonic-gate 			goto out;
1600Sstevel@tonic-gate 		}
1612248Sraf 		/* SIGCANCEL is ignored by default */
1622248Sraf 		if (uact.sa_sigaction == SIG_DFL ||
1632248Sraf 		    uact.sa_sigaction == SIG_IGN)
1642248Sraf 			goto out;
1652248Sraf 	}
1662248Sraf 
1672248Sraf 	/*
1682248Sraf 	 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
1692248Sraf 	 * we are an aio worker thread, cancel the aio request.
1702248Sraf 	 */
1712248Sraf 	if (sig == SIGAIOCANCEL) {
1722248Sraf 		aio_worker_t *aiowp = _pthread_getspecific(_aio_key);
1732248Sraf 
1742248Sraf 		if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
1752248Sraf 			_siglongjmp(aiowp->work_jmp_buf, 1);
1762248Sraf 		/* SIGLWP is ignored by default */
1770Sstevel@tonic-gate 		if (uact.sa_sigaction == SIG_DFL ||
1780Sstevel@tonic-gate 		    uact.sa_sigaction == SIG_IGN)
1790Sstevel@tonic-gate 			goto out;
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_SIGINFO))
1830Sstevel@tonic-gate 		sip = NULL;
1840Sstevel@tonic-gate 	__sighndlr(sig, sip, ucp, uact.sa_sigaction);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate #if defined(sparc) || defined(__sparc)
1870Sstevel@tonic-gate 	/*
1880Sstevel@tonic-gate 	 * If this is a floating point exception and the queue
1890Sstevel@tonic-gate 	 * is non-empty, pop the top entry from the queue.  This
1900Sstevel@tonic-gate 	 * is to maintain expected behavior.
1910Sstevel@tonic-gate 	 */
1920Sstevel@tonic-gate 	if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
1930Sstevel@tonic-gate 		fpregset_t *fp = &ucp->uc_mcontext.fpregs;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		if (--fp->fpu_qcnt > 0) {
1960Sstevel@tonic-gate 			unsigned char i;
1970Sstevel@tonic-gate 			struct fq *fqp;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 			fqp = fp->fpu_q;
2000Sstevel@tonic-gate 			for (i = 0; i < fp->fpu_qcnt; i++)
2010Sstevel@tonic-gate 				fqp[i] = fqp[i+1];
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate #endif	/* sparc */
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate out:
2070Sstevel@tonic-gate 	(void) _private_setcontext(ucp);
2080Sstevel@tonic-gate 	thr_panic("call_user_handler(): _setcontext() returned");
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * take_deferred_signal() is called when ul_critical and ul_sigdefer become
2130Sstevel@tonic-gate  * zero and a deferred signal has been recorded on the current thread.
2140Sstevel@tonic-gate  * We are out of the critical region and are ready to take a signal.
2150Sstevel@tonic-gate  * The kernel has all signals blocked on this lwp, but our value of
2160Sstevel@tonic-gate  * ul_sigmask is the correct signal mask for the previous context.
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate void
2190Sstevel@tonic-gate take_deferred_signal(int sig)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate 	ulwp_t *self = curthread;
2220Sstevel@tonic-gate 	siginfo_t siginfo;
2230Sstevel@tonic-gate 	siginfo_t *sip;
2240Sstevel@tonic-gate 	ucontext_t uc;
2250Sstevel@tonic-gate 	volatile int returning;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
2280Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer == 0);
2290Sstevel@tonic-gate 	ASSERT(self->ul_cursig == 0);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	returning = 0;
2320Sstevel@tonic-gate 	uc.uc_flags = UC_ALL;
2330Sstevel@tonic-gate 	/*
2340Sstevel@tonic-gate 	 * We call _private_getcontext (a libc-private synonym for
2350Sstevel@tonic-gate 	 * _getcontext) rather than _getcontext because we need to
2360Sstevel@tonic-gate 	 * avoid the dynamic linker and link auditing problems here.
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 	(void) _private_getcontext(&uc);
2390Sstevel@tonic-gate 	/*
2400Sstevel@tonic-gate 	 * If the application signal handler calls setcontext() on
2410Sstevel@tonic-gate 	 * the ucontext we give it, it returns here, then we return.
2420Sstevel@tonic-gate 	 */
2430Sstevel@tonic-gate 	if (returning)
2440Sstevel@tonic-gate 		return;
2450Sstevel@tonic-gate 	returning = 1;
2460Sstevel@tonic-gate 	ASSERT(sigequalset(&uc.uc_sigmask, &maskset));
2470Sstevel@tonic-gate 	if (self->ul_siginfo.si_signo == 0)
2480Sstevel@tonic-gate 		sip = NULL;
2490Sstevel@tonic-gate 	else {
2501111Sraf 		(void) _private_memcpy(&siginfo,
2511111Sraf 		    &self->ul_siginfo, sizeof (siginfo));
2520Sstevel@tonic-gate 		sip = &siginfo;
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 	uc.uc_sigmask = self->ul_sigmask;
2550Sstevel@tonic-gate 	call_user_handler(sig, sip, &uc);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate void
2590Sstevel@tonic-gate sigacthandler(int sig, siginfo_t *sip, void *uvp)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	ucontext_t *ucp = uvp;
2620Sstevel@tonic-gate 	ulwp_t *self = curthread;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/*
2650Sstevel@tonic-gate 	 * Do this in case we took a signal while in a cancelable system call.
2660Sstevel@tonic-gate 	 * It does no harm if we were not in such a system call.
2670Sstevel@tonic-gate 	 */
2680Sstevel@tonic-gate 	self->ul_sp = 0;
2690Sstevel@tonic-gate 	if (sig != SIGCANCEL)
2700Sstevel@tonic-gate 		self->ul_cancel_async = self->ul_save_async;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * If we are not in a critical region and are
2740Sstevel@tonic-gate 	 * not deferring signals, take the signal now.
2750Sstevel@tonic-gate 	 */
2760Sstevel@tonic-gate 	if ((self->ul_critical + self->ul_sigdefer) == 0) {
2770Sstevel@tonic-gate 		call_user_handler(sig, sip, ucp);
2780Sstevel@tonic-gate 		return;	/* call_user_handler() cannot return */
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/*
2820Sstevel@tonic-gate 	 * We are in a critical region or we are deferring signals.  When
2830Sstevel@tonic-gate 	 * we emerge from the region we will call take_deferred_signal().
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 	ASSERT(self->ul_cursig == 0);
2860Sstevel@tonic-gate 	self->ul_cursig = (char)sig;
2870Sstevel@tonic-gate 	if (sip != NULL)
2881111Sraf 		(void) _private_memcpy(&self->ul_siginfo,
2891111Sraf 		    sip, sizeof (siginfo_t));
2900Sstevel@tonic-gate 	else
2910Sstevel@tonic-gate 		self->ul_siginfo.si_signo = 0;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
2950Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
2960Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
2970Sstevel@tonic-gate 	 */
2980Sstevel@tonic-gate 	set_parking_flag(self, 0);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/*
3010Sstevel@tonic-gate 	 * Return to the previous context with all signals blocked.
3020Sstevel@tonic-gate 	 * We will restore the signal mask in take_deferred_signal().
3030Sstevel@tonic-gate 	 * Note that we are calling the system call trap here, not
3040Sstevel@tonic-gate 	 * the _setcontext() wrapper.  We don't want to change the
3050Sstevel@tonic-gate 	 * thread's ul_sigmask by this operation.
3060Sstevel@tonic-gate 	 */
3070Sstevel@tonic-gate 	ucp->uc_sigmask = maskset;
3080Sstevel@tonic-gate 	(void) __setcontext_syscall(ucp);
3090Sstevel@tonic-gate 	thr_panic("sigacthandler(): __setcontext() returned");
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3122712Snn35248 #pragma weak sigaction = _libc_sigaction
3132712Snn35248 #pragma weak _sigaction = _libc_sigaction
3140Sstevel@tonic-gate int
3152712Snn35248 _libc_sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate 	ulwp_t *self = curthread;
3180Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
3190Sstevel@tonic-gate 	struct sigaction oaction;
3200Sstevel@tonic-gate 	struct sigaction tact;
3210Sstevel@tonic-gate 	struct sigaction *tactp = NULL;
3220Sstevel@tonic-gate 	int rv;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (sig <= 0 || sig >= NSIG) {
3250Sstevel@tonic-gate 		errno = EINVAL;
3260Sstevel@tonic-gate 		return (-1);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if (!self->ul_vfork)
330*4570Sraf 		lrw_wrlock(&udp->siguaction[sig].sig_lock);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	oaction = udp->siguaction[sig].sig_uaction;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	if (nact != NULL) {
3350Sstevel@tonic-gate 		tact = *nact;	/* make a copy so we can modify it */
3360Sstevel@tonic-gate 		tactp = &tact;
3370Sstevel@tonic-gate 		delete_reserved_signals(&tact.sa_mask);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate #if !defined(_LP64)
3400Sstevel@tonic-gate 		tact.sa_resv[0] = tact.sa_resv[1] = 0;	/* cleanliness */
3410Sstevel@tonic-gate #endif
3420Sstevel@tonic-gate 		/*
3430Sstevel@tonic-gate 		 * To be compatible with the behavior of SunOS 4.x:
3440Sstevel@tonic-gate 		 * If the new signal handler is SIG_IGN or SIG_DFL, do
3450Sstevel@tonic-gate 		 * not change the signal's entry in the siguaction array.
3460Sstevel@tonic-gate 		 * This allows a child of vfork(2) to set signal handlers
3470Sstevel@tonic-gate 		 * to SIG_IGN or SIG_DFL without affecting the parent.
3480Sstevel@tonic-gate 		 *
3490Sstevel@tonic-gate 		 * This also covers a race condition with some thread
3500Sstevel@tonic-gate 		 * setting the signal action to SIG_DFL or SIG_IGN
3510Sstevel@tonic-gate 		 * when the thread has also received and deferred
3520Sstevel@tonic-gate 		 * that signal.  When the thread takes the deferred
3530Sstevel@tonic-gate 		 * signal, even though it has set the action to SIG_DFL
3540Sstevel@tonic-gate 		 * or SIG_IGN, it will execute the old signal handler
3550Sstevel@tonic-gate 		 * anyway.  This is an inherent signaling race condition
3560Sstevel@tonic-gate 		 * and is not a bug.
3570Sstevel@tonic-gate 		 *
3580Sstevel@tonic-gate 		 * A child of vfork() is not allowed to change signal
3590Sstevel@tonic-gate 		 * handlers to anything other than SIG_DFL or SIG_IGN.
3600Sstevel@tonic-gate 		 */
3610Sstevel@tonic-gate 		if (self->ul_vfork) {
3620Sstevel@tonic-gate 			if (tact.sa_sigaction != SIG_IGN)
3630Sstevel@tonic-gate 				tact.sa_sigaction = SIG_DFL;
3642248Sraf 		} else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
3650Sstevel@tonic-gate 			/*
3662248Sraf 			 * Always catch these signals.
3672248Sraf 			 * We need SIGCANCEL for pthread_cancel() to work.
3682248Sraf 			 * We need SIGAIOCANCEL for aio_cancel() to work.
3690Sstevel@tonic-gate 			 */
3700Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
3710Sstevel@tonic-gate 			if (tact.sa_sigaction == SIG_DFL ||
3720Sstevel@tonic-gate 			    tact.sa_sigaction == SIG_IGN)
3730Sstevel@tonic-gate 				tact.sa_flags = SA_SIGINFO;
3740Sstevel@tonic-gate 			else {
3750Sstevel@tonic-gate 				tact.sa_flags |= SA_SIGINFO;
3760Sstevel@tonic-gate 				tact.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
3770Sstevel@tonic-gate 			}
3780Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
3790Sstevel@tonic-gate 			tact.sa_mask = maskset;
3800Sstevel@tonic-gate 		} else if (tact.sa_sigaction != SIG_DFL &&
3810Sstevel@tonic-gate 		    tact.sa_sigaction != SIG_IGN) {
3820Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
3830Sstevel@tonic-gate 			tact.sa_flags &= ~SA_NODEFER;
3840Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
3850Sstevel@tonic-gate 			tact.sa_mask = maskset;
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if ((rv = __sigaction(sig, tactp, oact)) != 0)
3900Sstevel@tonic-gate 		udp->siguaction[sig].sig_uaction = oaction;
3910Sstevel@tonic-gate 	else if (oact != NULL &&
3920Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_DFL &&
3930Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_IGN)
3940Sstevel@tonic-gate 		*oact = oaction;
3950Sstevel@tonic-gate 
3962248Sraf 	/*
3972248Sraf 	 * We detect setting the disposition of SIGIO just to set the
3982248Sraf 	 * _sigio_enabled flag for the asynchronous i/o (aio) code.
3992248Sraf 	 */
4002248Sraf 	if (sig == SIGIO && rv == 0 && tactp != NULL) {
4012248Sraf 		_sigio_enabled =
4022248Sraf 		    (tactp->sa_handler != SIG_DFL &&
4032248Sraf 		    tactp->sa_handler != SIG_IGN);
4042248Sraf 	}
4052248Sraf 
4060Sstevel@tonic-gate 	if (!self->ul_vfork)
407*4570Sraf 		lrw_unlock(&udp->siguaction[sig].sig_lock);
4080Sstevel@tonic-gate 	return (rv);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4112712Snn35248 void
4122712Snn35248 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
4132712Snn35248     void (**osigacthandler)(int, siginfo_t *, void *))
4142712Snn35248 {
4152712Snn35248 	ulwp_t *self = curthread;
4162712Snn35248 	uberdata_t *udp = self->ul_uberdata;
4172712Snn35248 
4182712Snn35248 	if (osigacthandler != NULL)
4192712Snn35248 		*osigacthandler = udp->sigacthandler;
4202712Snn35248 
4212712Snn35248 	udp->sigacthandler = nsigacthandler;
4222712Snn35248 }
4232712Snn35248 
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate  * Calling set_parking_flag(curthread, 1) informs the kernel that we are
4260Sstevel@tonic-gate  * calling __lwp_park or ___lwp_cond_wait().  If we take a signal in
4270Sstevel@tonic-gate  * the unprotected (from signals) interval before reaching the kernel,
4280Sstevel@tonic-gate  * sigacthandler() will call set_parking_flag(curthread, 0) to inform
4290Sstevel@tonic-gate  * the kernel to return immediately from these system calls, giving us
4300Sstevel@tonic-gate  * a spurious wakeup but not a deadlock.
4310Sstevel@tonic-gate  */
4320Sstevel@tonic-gate void
4330Sstevel@tonic-gate set_parking_flag(ulwp_t *self, int park)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate 	volatile sc_shared_t *scp;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	enter_critical(self);
4380Sstevel@tonic-gate 	if ((scp = self->ul_schedctl) != NULL ||
4390Sstevel@tonic-gate 	    (scp = setup_schedctl()) != NULL)
4400Sstevel@tonic-gate 		scp->sc_park = park;
4410Sstevel@tonic-gate 	else if (park == 0)	/* schedctl failed, do it the long way */
4420Sstevel@tonic-gate 		__lwp_unpark(self->ul_lwpid);
4430Sstevel@tonic-gate 	exit_critical(self);
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate  * Tell the kernel to block all signals.
4480Sstevel@tonic-gate  * Use the schedctl interface, or failing that, use __lwp_sigmask().
4490Sstevel@tonic-gate  * This action can be rescinded only by making a system call that
4500Sstevel@tonic-gate  * sets the signal mask:
4510Sstevel@tonic-gate  *	__lwp_sigmask(), __sigprocmask(), __setcontext(),
4520Sstevel@tonic-gate  *	__sigsuspend() or __pollsys().
4530Sstevel@tonic-gate  * In particular, this action cannot be reversed by assigning
4540Sstevel@tonic-gate  * scp->sc_sigblock = 0.  That would be a way to lose signals.
4550Sstevel@tonic-gate  * See the definition of restore_signals(self).
4560Sstevel@tonic-gate  */
4570Sstevel@tonic-gate void
4580Sstevel@tonic-gate block_all_signals(ulwp_t *self)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	volatile sc_shared_t *scp;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	enter_critical(self);
4630Sstevel@tonic-gate 	if ((scp = self->ul_schedctl) != NULL ||
4640Sstevel@tonic-gate 	    (scp = setup_schedctl()) != NULL)
4650Sstevel@tonic-gate 		scp->sc_sigblock = 1;
4660Sstevel@tonic-gate 	else
4670Sstevel@tonic-gate 		(void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL);
4680Sstevel@tonic-gate 	exit_critical(self);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4712712Snn35248 /*
4722712Snn35248  * _private_setcontext has code that forcibly restores the curthread
4732712Snn35248  * pointer in a context passed to the setcontext(2) syscall.
4742712Snn35248  *
4752712Snn35248  * Certain processes may need to disable this feature, so these routines
4762712Snn35248  * provide the mechanism to do so.
4772712Snn35248  *
4782712Snn35248  * (As an example, branded 32-bit x86 processes may use %gs for their own
4792712Snn35248  * purposes, so they need to be able to specify a %gs value to be restored
4802712Snn35248  * on return from a signal handler via the passed ucontext_t.)
4812712Snn35248  */
4822712Snn35248 static int setcontext_enforcement = 1;
4832712Snn35248 
4842712Snn35248 void
4852712Snn35248 set_setcontext_enforcement(int on)
4862712Snn35248 {
4872712Snn35248 	setcontext_enforcement = on;
4882712Snn35248 }
4892712Snn35248 
4900Sstevel@tonic-gate #pragma weak setcontext = _private_setcontext
4910Sstevel@tonic-gate #pragma weak _setcontext = _private_setcontext
4920Sstevel@tonic-gate int
4930Sstevel@tonic-gate _private_setcontext(const ucontext_t *ucp)
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	ulwp_t *self = curthread;
4960Sstevel@tonic-gate 	int ret;
4970Sstevel@tonic-gate 	ucontext_t uc;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/*
5000Sstevel@tonic-gate 	 * Returning from the main context (uc_link == NULL) causes
5010Sstevel@tonic-gate 	 * the thread to exit.  See setcontext(2) and makecontext(3C).
5020Sstevel@tonic-gate 	 */
5030Sstevel@tonic-gate 	if (ucp == NULL)
5040Sstevel@tonic-gate 		_thr_exit(NULL);
5051111Sraf 	(void) _private_memcpy(&uc, ucp, sizeof (uc));
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * Restore previous signal mask and context link.
5090Sstevel@tonic-gate 	 */
5100Sstevel@tonic-gate 	if (uc.uc_flags & UC_SIGMASK) {
5110Sstevel@tonic-gate 		block_all_signals(self);
5120Sstevel@tonic-gate 		delete_reserved_signals(&uc.uc_sigmask);
5130Sstevel@tonic-gate 		self->ul_sigmask = uc.uc_sigmask;
5140Sstevel@tonic-gate 		if (self->ul_cursig) {
5150Sstevel@tonic-gate 			/*
5160Sstevel@tonic-gate 			 * We have a deferred signal present.
5170Sstevel@tonic-gate 			 * The signal mask will be set when the
5180Sstevel@tonic-gate 			 * signal is taken in take_deferred_signal().
5190Sstevel@tonic-gate 			 */
5200Sstevel@tonic-gate 			ASSERT(self->ul_critical + self->ul_sigdefer != 0);
5210Sstevel@tonic-gate 			uc.uc_flags &= ~UC_SIGMASK;
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 	self->ul_siglink = uc.uc_link;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	/*
5270Sstevel@tonic-gate 	 * We don't know where this context structure has been.
5280Sstevel@tonic-gate 	 * Preserve the curthread pointer, at least.
5292712Snn35248 	 *
5302712Snn35248 	 * Allow this feature to be disabled if a particular process
5312712Snn35248 	 * requests it.
5320Sstevel@tonic-gate 	 */
5332712Snn35248 	if (setcontext_enforcement) {
5340Sstevel@tonic-gate #if defined(__sparc)
5352712Snn35248 		uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
5360Sstevel@tonic-gate #elif defined(__amd64)
5373446Smrj 		uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
5380Sstevel@tonic-gate #elif defined(__i386)
5393446Smrj 		uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
5400Sstevel@tonic-gate #else
5410Sstevel@tonic-gate #error "none of __sparc, __amd64, __i386 defined"
5420Sstevel@tonic-gate #endif
5432712Snn35248 	}
5442712Snn35248 
5450Sstevel@tonic-gate 	/*
5460Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
5470Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
5480Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
5490Sstevel@tonic-gate 	 */
5500Sstevel@tonic-gate 	set_parking_flag(self, 0);
5510Sstevel@tonic-gate 	self->ul_sp = 0;
5520Sstevel@tonic-gate 	ret = __setcontext_syscall(&uc);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	/*
5550Sstevel@tonic-gate 	 * It is OK for setcontext() to return if the user has not specified
5560Sstevel@tonic-gate 	 * UC_CPU.
5570Sstevel@tonic-gate 	 */
5580Sstevel@tonic-gate 	if (uc.uc_flags & UC_CPU)
5590Sstevel@tonic-gate 		thr_panic("setcontext(): __setcontext() returned");
5600Sstevel@tonic-gate 	return (ret);
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate #pragma weak thr_sigsetmask = _thr_sigsetmask
5640Sstevel@tonic-gate #pragma weak pthread_sigmask = _thr_sigsetmask
5650Sstevel@tonic-gate #pragma weak _pthread_sigmask = _thr_sigsetmask
5660Sstevel@tonic-gate int
5670Sstevel@tonic-gate _thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
5680Sstevel@tonic-gate {
5690Sstevel@tonic-gate 	ulwp_t *self = curthread;
5700Sstevel@tonic-gate 	sigset_t saveset;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if (set == NULL) {
5730Sstevel@tonic-gate 		enter_critical(self);
5740Sstevel@tonic-gate 		if (oset != NULL)
5750Sstevel@tonic-gate 			*oset = self->ul_sigmask;
5760Sstevel@tonic-gate 		exit_critical(self);
5770Sstevel@tonic-gate 	} else {
5780Sstevel@tonic-gate 		switch (how) {
5790Sstevel@tonic-gate 		case SIG_BLOCK:
5800Sstevel@tonic-gate 		case SIG_UNBLOCK:
5810Sstevel@tonic-gate 		case SIG_SETMASK:
5820Sstevel@tonic-gate 			break;
5830Sstevel@tonic-gate 		default:
5840Sstevel@tonic-gate 			return (EINVAL);
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 		/*
5880Sstevel@tonic-gate 		 * The assignments to self->ul_sigmask must be protected from
5890Sstevel@tonic-gate 		 * signals.  The nuances of this code are subtle.  Be careful.
5900Sstevel@tonic-gate 		 */
5910Sstevel@tonic-gate 		block_all_signals(self);
5920Sstevel@tonic-gate 		if (oset != NULL)
5930Sstevel@tonic-gate 			saveset = self->ul_sigmask;
5940Sstevel@tonic-gate 		switch (how) {
5950Sstevel@tonic-gate 		case SIG_BLOCK:
5960Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
5970Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
5980Sstevel@tonic-gate 			break;
5990Sstevel@tonic-gate 		case SIG_UNBLOCK:
6000Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
6010Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
6020Sstevel@tonic-gate 			break;
6030Sstevel@tonic-gate 		case SIG_SETMASK:
6040Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
6050Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
6060Sstevel@tonic-gate 			break;
6070Sstevel@tonic-gate 		}
6080Sstevel@tonic-gate 		delete_reserved_signals(&self->ul_sigmask);
6090Sstevel@tonic-gate 		if (oset != NULL)
6100Sstevel@tonic-gate 			*oset = saveset;
6110Sstevel@tonic-gate 		restore_signals(self);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	return (0);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate #pragma weak sigprocmask = _sigprocmask
6180Sstevel@tonic-gate int
6190Sstevel@tonic-gate _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	int error;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/*
6240Sstevel@tonic-gate 	 * Guard against children of vfork().
6250Sstevel@tonic-gate 	 */
6260Sstevel@tonic-gate 	if (curthread->ul_vfork)
6270Sstevel@tonic-gate 		return (__lwp_sigmask(how, set, oset));
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if ((error = _thr_sigsetmask(how, set, oset)) != 0) {
6300Sstevel@tonic-gate 		errno = error;
6310Sstevel@tonic-gate 		return (-1);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	return (0);
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate /*
6380Sstevel@tonic-gate  * Called at library initialization to set up signal handling.
639*4570Sraf  * All we really do is initialize the sig_lock rwlocks.
6400Sstevel@tonic-gate  * All signal handlers are either SIG_DFL or SIG_IGN on exec().
6410Sstevel@tonic-gate  * However, if any signal handlers were established on alternate
6420Sstevel@tonic-gate  * link maps before the primary link map has been initialized,
6430Sstevel@tonic-gate  * then inform the kernel of the new sigacthandler.
6440Sstevel@tonic-gate  */
6450Sstevel@tonic-gate void
6460Sstevel@tonic-gate signal_init()
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
6490Sstevel@tonic-gate 	struct sigaction *sap;
6500Sstevel@tonic-gate 	struct sigaction act;
651*4570Sraf 	rwlock_t *rwlp;
6520Sstevel@tonic-gate 	int sig;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	for (sig = 0; sig < NSIG; sig++) {
655*4570Sraf 		rwlp = &udp->siguaction[sig].sig_lock;
656*4570Sraf 		rwlp->rwlock_magic = RWL_MAGIC;
657*4570Sraf 		rwlp->mutex.mutex_flag = LOCK_INITED;
658*4570Sraf 		rwlp->mutex.mutex_magic = MUTEX_MAGIC;
6590Sstevel@tonic-gate 		sap = &udp->siguaction[sig].sig_uaction;
6600Sstevel@tonic-gate 		if (sap->sa_sigaction != SIG_DFL &&
6610Sstevel@tonic-gate 		    sap->sa_sigaction != SIG_IGN &&
6620Sstevel@tonic-gate 		    __sigaction(sig, NULL, &act) == 0 &&
6630Sstevel@tonic-gate 		    act.sa_sigaction != SIG_DFL &&
6640Sstevel@tonic-gate 		    act.sa_sigaction != SIG_IGN) {
6650Sstevel@tonic-gate 			act = *sap;
6660Sstevel@tonic-gate 			act.sa_flags &= ~SA_NODEFER;
6670Sstevel@tonic-gate 			act.sa_sigaction = udp->sigacthandler;
6680Sstevel@tonic-gate 			act.sa_mask = maskset;
6690Sstevel@tonic-gate 			(void) __sigaction(sig, &act, NULL);
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate /*
6750Sstevel@tonic-gate  * Common code for cancelling self in _sigcancel() and pthread_cancel().
6760Sstevel@tonic-gate  * If the thread is at a cancellation point (ul_cancelable) then just
6770Sstevel@tonic-gate  * return and let _canceloff() do the exit, else exit immediately if
6780Sstevel@tonic-gate  * async mode is in effect.
6790Sstevel@tonic-gate  */
6800Sstevel@tonic-gate void
6810Sstevel@tonic-gate do_sigcancel()
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 	ulwp_t *self = curthread;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
6860Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer == 0);
6870Sstevel@tonic-gate 	self->ul_cancel_pending = 1;
6880Sstevel@tonic-gate 	if (self->ul_cancel_async &&
6890Sstevel@tonic-gate 	    !self->ul_cancel_disabled &&
6900Sstevel@tonic-gate 	    !self->ul_cancelable)
6910Sstevel@tonic-gate 		_pthread_exit(PTHREAD_CANCELED);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate /*
6952248Sraf  * Set up the SIGCANCEL handler for threads cancellation,
6962248Sraf  * needed only when we have more than one thread,
6972248Sraf  * or the SIGAIOCANCEL handler for aio cancellation,
6982248Sraf  * called when aio is initialized, in __uaio_init().
6990Sstevel@tonic-gate  */
7000Sstevel@tonic-gate void
7012248Sraf setup_cancelsig(int sig)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
704*4570Sraf 	rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
7050Sstevel@tonic-gate 	struct sigaction act;
7060Sstevel@tonic-gate 
7072248Sraf 	ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
708*4570Sraf 	lrw_rdlock(rwlp);
7092248Sraf 	act = udp->siguaction[sig].sig_uaction;
710*4570Sraf 	lrw_unlock(rwlp);
7110Sstevel@tonic-gate 	if (act.sa_sigaction == SIG_DFL ||
7120Sstevel@tonic-gate 	    act.sa_sigaction == SIG_IGN)
7130Sstevel@tonic-gate 		act.sa_flags = SA_SIGINFO;
7140Sstevel@tonic-gate 	else {
7150Sstevel@tonic-gate 		act.sa_flags |= SA_SIGINFO;
7160Sstevel@tonic-gate 		act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 	act.sa_sigaction = udp->sigacthandler;
7190Sstevel@tonic-gate 	act.sa_mask = maskset;
7202248Sraf 	(void) __sigaction(sig, &act, NULL);
7210Sstevel@tonic-gate }
722