xref: /onnv-gate/usr/src/lib/libc/port/threads/sigaction.c (revision 6812:febeba71273d)
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 /*
235837Sraf  * Copyright 2008 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"
30*6812Sraf #include <sys/feature_tests.h>
31*6812Sraf /*
32*6812Sraf  * setcontext() really can return, if UC_CPU is not specified.
33*6812Sraf  * Make the compiler shut up about it.
34*6812Sraf  */
35*6812Sraf #if defined(__NORETURN)
36*6812Sraf #undef	__NORETURN
37*6812Sraf #endif
38*6812Sraf #define	__NORETURN
390Sstevel@tonic-gate #include "thr_uberdata.h"
402248Sraf #include "asyncio.h"
410Sstevel@tonic-gate #include <signal.h>
420Sstevel@tonic-gate #include <siginfo.h>
430Sstevel@tonic-gate #include <sys/systm.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate const sigset_t maskset = {MASKSET0, MASKSET1, 0, 0};	/* maskable signals */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Return true if the valid signal bits in both sets are the same.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate int
510Sstevel@tonic-gate sigequalset(const sigset_t *s1, const sigset_t *s2)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	/*
540Sstevel@tonic-gate 	 * We only test valid signal bits, not rubbish following MAXSIG
550Sstevel@tonic-gate 	 * (for speed).  Algorithm:
560Sstevel@tonic-gate 	 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
570Sstevel@tonic-gate 	 */
580Sstevel@tonic-gate 	return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) |
590Sstevel@tonic-gate 	    ((s1->__sigbits[1] ^ s2->__sigbits[1]) & FILLSET1)));
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * Common code for calling the user-specified signal handler.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate void
660Sstevel@tonic-gate call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	ulwp_t *self = curthread;
690Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
700Sstevel@tonic-gate 	struct sigaction uact;
710Sstevel@tonic-gate 	volatile struct sigaction *sap;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	/*
740Sstevel@tonic-gate 	 * If we are taking a signal while parked or about to be parked
750Sstevel@tonic-gate 	 * on __lwp_park() then remove ourself from the sleep queue so
760Sstevel@tonic-gate 	 * that we can grab locks.  The code in mutex_lock_queue() and
770Sstevel@tonic-gate 	 * cond_wait_common() will detect this and deal with it when
780Sstevel@tonic-gate 	 * __lwp_park() returns.
790Sstevel@tonic-gate 	 */
800Sstevel@tonic-gate 	unsleep_self();
810Sstevel@tonic-gate 	set_parking_flag(self, 0);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if (__td_event_report(self, TD_CATCHSIG, udp)) {
840Sstevel@tonic-gate 		self->ul_td_evbuf.eventnum = TD_CATCHSIG;
850Sstevel@tonic-gate 		self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig;
860Sstevel@tonic-gate 		tdb_event(TD_CATCHSIG, udp);
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	/*
900Sstevel@tonic-gate 	 * Get a self-consistent set of flags, handler, and mask
910Sstevel@tonic-gate 	 * while holding the sig's sig_lock for the least possible time.
920Sstevel@tonic-gate 	 * We must acquire the sig's sig_lock because some thread running
930Sstevel@tonic-gate 	 * in sigaction() might be establishing a new signal handler.
944570Sraf 	 * The code in sigaction() acquires the writer lock; here
954570Sraf 	 * we acquire the readers lock to ehance concurrency in the
964570Sraf 	 * face of heavy signal traffic, such as generated by java.
970Sstevel@tonic-gate 	 *
980Sstevel@tonic-gate 	 * Locking exceptions:
990Sstevel@tonic-gate 	 * No locking for a child of vfork().
1000Sstevel@tonic-gate 	 * If the signal is SIGPROF with an si_code of PROF_SIG,
1010Sstevel@tonic-gate 	 * then we assume that this signal was generated by
1020Sstevel@tonic-gate 	 * setitimer(ITIMER_REALPROF) set up by the dbx collector.
1030Sstevel@tonic-gate 	 * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
1040Sstevel@tonic-gate 	 * then we assume that the signal was generated by
1050Sstevel@tonic-gate 	 * a hardware performance counter overflow.
1060Sstevel@tonic-gate 	 * In these cases, assume that we need no locking.  It is the
1070Sstevel@tonic-gate 	 * monitoring program's responsibility to ensure correctness.
1080Sstevel@tonic-gate 	 */
1090Sstevel@tonic-gate 	sap = &udp->siguaction[sig].sig_uaction;
1100Sstevel@tonic-gate 	if (self->ul_vfork ||
1110Sstevel@tonic-gate 	    (sip != NULL &&
1120Sstevel@tonic-gate 	    ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
1130Sstevel@tonic-gate 	    (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
1140Sstevel@tonic-gate 		/* we wish this assignment could be atomic */
1156515Sraf 		(void) memcpy(&uact, (void *)sap, sizeof (uact));
1160Sstevel@tonic-gate 	} else {
1174570Sraf 		rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
1184570Sraf 		lrw_rdlock(rwlp);
1196515Sraf 		(void) memcpy(&uact, (void *)sap, sizeof (uact));
1205891Sraf 		if ((sig == SIGCANCEL || sig == SIGAIOCANCEL) &&
1215891Sraf 		    (sap->sa_flags & SA_RESETHAND))
1220Sstevel@tonic-gate 			sap->sa_sigaction = SIG_DFL;
1234570Sraf 		lrw_unlock(rwlp);
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	/*
1270Sstevel@tonic-gate 	 * Set the proper signal mask and call the user's signal handler.
1280Sstevel@tonic-gate 	 * (We overrode the user-requested signal mask with maskset
1290Sstevel@tonic-gate 	 * so we currently have all blockable signals blocked.)
1300Sstevel@tonic-gate 	 *
1310Sstevel@tonic-gate 	 * We would like to ASSERT() that the signal is not a member of the
1320Sstevel@tonic-gate 	 * signal mask at the previous level (ucp->uc_sigmask) or the specified
1330Sstevel@tonic-gate 	 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
1340Sstevel@tonic-gate 	 * /proc can override this via PCSSIG, so we don't bother.
1350Sstevel@tonic-gate 	 *
1360Sstevel@tonic-gate 	 * We would also like to ASSERT() that the signal mask at the previous
1370Sstevel@tonic-gate 	 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
1380Sstevel@tonic-gate 	 * but /proc can change the thread's signal mask via PCSHOLD, so we
1390Sstevel@tonic-gate 	 * don't bother with that either.
1400Sstevel@tonic-gate 	 */
1410Sstevel@tonic-gate 	ASSERT(ucp->uc_flags & UC_SIGMASK);
1420Sstevel@tonic-gate 	if (self->ul_sigsuspend) {
1430Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
1440Sstevel@tonic-gate 		self->ul_sigsuspend = 0;
1450Sstevel@tonic-gate 		/* the sigsuspend() or pollsys() signal mask */
1460Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &self->ul_tmpmask);
1470Sstevel@tonic-gate 	} else {
1480Sstevel@tonic-gate 		/* the signal mask at the previous level */
1490Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &ucp->uc_sigmask);
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_NODEFER))	/* add current signal */
1526515Sraf 		(void) sigaddset(&uact.sa_mask, sig);
1530Sstevel@tonic-gate 	self->ul_sigmask = uact.sa_mask;
1540Sstevel@tonic-gate 	self->ul_siglink = ucp;
1550Sstevel@tonic-gate 	(void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	/*
1580Sstevel@tonic-gate 	 * If this thread has been sent SIGCANCEL from the kernel
1590Sstevel@tonic-gate 	 * or from pthread_cancel(), it is being asked to exit.
1600Sstevel@tonic-gate 	 * The kernel may send SIGCANCEL without a siginfo struct.
1610Sstevel@tonic-gate 	 * If the SIGCANCEL is process-directed (from kill() or
1620Sstevel@tonic-gate 	 * sigqueue()), treat it as an ordinary signal.
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 	if (sig == SIGCANCEL) {
1650Sstevel@tonic-gate 		if (sip == NULL || SI_FROMKERNEL(sip) ||
1660Sstevel@tonic-gate 		    sip->si_code == SI_LWP) {
1670Sstevel@tonic-gate 			do_sigcancel();
1680Sstevel@tonic-gate 			goto out;
1690Sstevel@tonic-gate 		}
1702248Sraf 		/* SIGCANCEL is ignored by default */
1712248Sraf 		if (uact.sa_sigaction == SIG_DFL ||
1722248Sraf 		    uact.sa_sigaction == SIG_IGN)
1732248Sraf 			goto out;
1742248Sraf 	}
1752248Sraf 
1762248Sraf 	/*
1772248Sraf 	 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
1782248Sraf 	 * we are an aio worker thread, cancel the aio request.
1792248Sraf 	 */
1802248Sraf 	if (sig == SIGAIOCANCEL) {
181*6812Sraf 		aio_worker_t *aiowp = pthread_getspecific(_aio_key);
1822248Sraf 
1832248Sraf 		if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
184*6812Sraf 			siglongjmp(aiowp->work_jmp_buf, 1);
1852248Sraf 		/* SIGLWP is ignored by default */
1860Sstevel@tonic-gate 		if (uact.sa_sigaction == SIG_DFL ||
1870Sstevel@tonic-gate 		    uact.sa_sigaction == SIG_IGN)
1880Sstevel@tonic-gate 			goto out;
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_SIGINFO))
1920Sstevel@tonic-gate 		sip = NULL;
1930Sstevel@tonic-gate 	__sighndlr(sig, sip, ucp, uact.sa_sigaction);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate #if defined(sparc) || defined(__sparc)
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * If this is a floating point exception and the queue
1980Sstevel@tonic-gate 	 * is non-empty, pop the top entry from the queue.  This
1990Sstevel@tonic-gate 	 * is to maintain expected behavior.
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
2020Sstevel@tonic-gate 		fpregset_t *fp = &ucp->uc_mcontext.fpregs;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		if (--fp->fpu_qcnt > 0) {
2050Sstevel@tonic-gate 			unsigned char i;
2060Sstevel@tonic-gate 			struct fq *fqp;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 			fqp = fp->fpu_q;
2090Sstevel@tonic-gate 			for (i = 0; i < fp->fpu_qcnt; i++)
2100Sstevel@tonic-gate 				fqp[i] = fqp[i+1];
2110Sstevel@tonic-gate 		}
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate #endif	/* sparc */
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate out:
216*6812Sraf 	(void) setcontext(ucp);
217*6812Sraf 	thr_panic("call_user_handler(): setcontext() returned");
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * take_deferred_signal() is called when ul_critical and ul_sigdefer become
2220Sstevel@tonic-gate  * zero and a deferred signal has been recorded on the current thread.
2230Sstevel@tonic-gate  * We are out of the critical region and are ready to take a signal.
2240Sstevel@tonic-gate  * The kernel has all signals blocked on this lwp, but our value of
2250Sstevel@tonic-gate  * ul_sigmask is the correct signal mask for the previous context.
2264806Sraf  *
2274806Sraf  * We call __sigresend() to atomically restore the signal mask and
2284806Sraf  * cause the signal to be sent again with the remembered siginfo.
2294806Sraf  * We will not return successfully from __sigresend() until the
2304806Sraf  * application's signal handler has been run via sigacthandler().
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate void
2330Sstevel@tonic-gate take_deferred_signal(int sig)
2340Sstevel@tonic-gate {
2354806Sraf 	extern int __sigresend(int, siginfo_t *, sigset_t *);
2360Sstevel@tonic-gate 	ulwp_t *self = curthread;
2375010Sraf 	siguaction_t *suap = &self->ul_uberdata->siguaction[sig];
2380Sstevel@tonic-gate 	siginfo_t *sip;
2394806Sraf 	int error;
2400Sstevel@tonic-gate 
2414806Sraf 	ASSERT((self->ul_critical | self->ul_sigdefer | self->ul_cursig) == 0);
2424806Sraf 
2435010Sraf 	/*
2445010Sraf 	 * If the signal handler was established with SA_RESETHAND,
2455010Sraf 	 * the kernel has reset the handler to SIG_DFL, so we have
2465010Sraf 	 * to reestablish the handler now so that it will be entered
2475010Sraf 	 * again when we call __sigresend(), below.
2485837Sraf 	 *
2495837Sraf 	 * Logically, we should acquire and release the signal's
2505837Sraf 	 * sig_lock around this operation to protect the integrity
2515837Sraf 	 * of the signal action while we copy it, as is done below
2525837Sraf 	 * in _libc_sigaction().  However, we may be on a user-level
2535837Sraf 	 * sleep queue at this point and lrw_wrlock(&suap->sig_lock)
2545837Sraf 	 * might attempt to sleep on a different sleep queue and
2555837Sraf 	 * that would corrupt the entire sleep queue mechanism.
2565837Sraf 	 *
2575837Sraf 	 * If we are on a sleep queue we will remove ourself from
2585837Sraf 	 * it in call_user_handler(), called from sigacthandler(),
2595837Sraf 	 * before entering the application's signal handler.
2605837Sraf 	 * In the meantime, we must not acquire any locks.
2615010Sraf 	 */
2625010Sraf 	if (suap->sig_uaction.sa_flags & SA_RESETHAND) {
2635010Sraf 		struct sigaction tact = suap->sig_uaction;
2645010Sraf 		tact.sa_flags &= ~SA_NODEFER;
2655010Sraf 		tact.sa_sigaction = self->ul_uberdata->sigacthandler;
2665010Sraf 		tact.sa_mask = maskset;
2675010Sraf 		(void) __sigaction(sig, &tact, NULL);
2685010Sraf 	}
2695010Sraf 
2700Sstevel@tonic-gate 	if (self->ul_siginfo.si_signo == 0)
2710Sstevel@tonic-gate 		sip = NULL;
2724806Sraf 	else
2734806Sraf 		sip = &self->ul_siginfo;
2744806Sraf 
2754806Sraf 	/* EAGAIN can happen only for a pending SIGSTOP signal */
2764806Sraf 	while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN)
2774806Sraf 		continue;
2784806Sraf 	if (error)
2794806Sraf 		thr_panic("take_deferred_signal(): __sigresend() failed");
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate void
2830Sstevel@tonic-gate sigacthandler(int sig, siginfo_t *sip, void *uvp)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	ucontext_t *ucp = uvp;
2860Sstevel@tonic-gate 	ulwp_t *self = curthread;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/*
2890Sstevel@tonic-gate 	 * Do this in case we took a signal while in a cancelable system call.
2900Sstevel@tonic-gate 	 * It does no harm if we were not in such a system call.
2910Sstevel@tonic-gate 	 */
2920Sstevel@tonic-gate 	self->ul_sp = 0;
2930Sstevel@tonic-gate 	if (sig != SIGCANCEL)
2940Sstevel@tonic-gate 		self->ul_cancel_async = self->ul_save_async;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	/*
2970Sstevel@tonic-gate 	 * If we are not in a critical region and are
2980Sstevel@tonic-gate 	 * not deferring signals, take the signal now.
2990Sstevel@tonic-gate 	 */
3000Sstevel@tonic-gate 	if ((self->ul_critical + self->ul_sigdefer) == 0) {
3010Sstevel@tonic-gate 		call_user_handler(sig, sip, ucp);
3025961Srh87107 		/*
3035961Srh87107 		 * On the surface, the following call seems redundant
3045961Srh87107 		 * because call_user_handler() cannot return. However,
3055961Srh87107 		 * we don't want to return from here because the compiler
3065961Srh87107 		 * might recycle our frame. We want to keep it on the
3075961Srh87107 		 * stack to assist debuggers such as pstack in identifying
3085961Srh87107 		 * signal frames. The call to thr_panic() serves to prevent
3095961Srh87107 		 * tail-call optimisation here.
3105961Srh87107 		 */
3115961Srh87107 		thr_panic("sigacthandler(): call_user_handler() returned");
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	/*
3150Sstevel@tonic-gate 	 * We are in a critical region or we are deferring signals.  When
3160Sstevel@tonic-gate 	 * we emerge from the region we will call take_deferred_signal().
3170Sstevel@tonic-gate 	 */
3180Sstevel@tonic-gate 	ASSERT(self->ul_cursig == 0);
3190Sstevel@tonic-gate 	self->ul_cursig = (char)sig;
3200Sstevel@tonic-gate 	if (sip != NULL)
3216515Sraf 		(void) memcpy(&self->ul_siginfo,
3221111Sraf 		    sip, sizeof (siginfo_t));
3230Sstevel@tonic-gate 	else
3240Sstevel@tonic-gate 		self->ul_siginfo.si_signo = 0;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/*
3270Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
3280Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
3290Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
3300Sstevel@tonic-gate 	 */
3310Sstevel@tonic-gate 	set_parking_flag(self, 0);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/*
3340Sstevel@tonic-gate 	 * Return to the previous context with all signals blocked.
3350Sstevel@tonic-gate 	 * We will restore the signal mask in take_deferred_signal().
3360Sstevel@tonic-gate 	 * Note that we are calling the system call trap here, not
337*6812Sraf 	 * the setcontext() wrapper.  We don't want to change the
3380Sstevel@tonic-gate 	 * thread's ul_sigmask by this operation.
3390Sstevel@tonic-gate 	 */
3400Sstevel@tonic-gate 	ucp->uc_sigmask = maskset;
3416515Sraf 	(void) __setcontext(ucp);
3420Sstevel@tonic-gate 	thr_panic("sigacthandler(): __setcontext() returned");
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
345*6812Sraf #pragma weak _sigaction = sigaction
3460Sstevel@tonic-gate int
347*6812Sraf sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	ulwp_t *self = curthread;
3500Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
3510Sstevel@tonic-gate 	struct sigaction oaction;
3520Sstevel@tonic-gate 	struct sigaction tact;
3530Sstevel@tonic-gate 	struct sigaction *tactp = NULL;
3540Sstevel@tonic-gate 	int rv;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	if (sig <= 0 || sig >= NSIG) {
3570Sstevel@tonic-gate 		errno = EINVAL;
3580Sstevel@tonic-gate 		return (-1);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if (!self->ul_vfork)
3624570Sraf 		lrw_wrlock(&udp->siguaction[sig].sig_lock);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	oaction = udp->siguaction[sig].sig_uaction;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if (nact != NULL) {
3670Sstevel@tonic-gate 		tact = *nact;	/* make a copy so we can modify it */
3680Sstevel@tonic-gate 		tactp = &tact;
3690Sstevel@tonic-gate 		delete_reserved_signals(&tact.sa_mask);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate #if !defined(_LP64)
3720Sstevel@tonic-gate 		tact.sa_resv[0] = tact.sa_resv[1] = 0;	/* cleanliness */
3730Sstevel@tonic-gate #endif
3740Sstevel@tonic-gate 		/*
3750Sstevel@tonic-gate 		 * To be compatible with the behavior of SunOS 4.x:
3760Sstevel@tonic-gate 		 * If the new signal handler is SIG_IGN or SIG_DFL, do
3770Sstevel@tonic-gate 		 * not change the signal's entry in the siguaction array.
3780Sstevel@tonic-gate 		 * This allows a child of vfork(2) to set signal handlers
3790Sstevel@tonic-gate 		 * to SIG_IGN or SIG_DFL without affecting the parent.
3800Sstevel@tonic-gate 		 *
3810Sstevel@tonic-gate 		 * This also covers a race condition with some thread
3820Sstevel@tonic-gate 		 * setting the signal action to SIG_DFL or SIG_IGN
3830Sstevel@tonic-gate 		 * when the thread has also received and deferred
3840Sstevel@tonic-gate 		 * that signal.  When the thread takes the deferred
3850Sstevel@tonic-gate 		 * signal, even though it has set the action to SIG_DFL
3860Sstevel@tonic-gate 		 * or SIG_IGN, it will execute the old signal handler
3870Sstevel@tonic-gate 		 * anyway.  This is an inherent signaling race condition
3880Sstevel@tonic-gate 		 * and is not a bug.
3890Sstevel@tonic-gate 		 *
3900Sstevel@tonic-gate 		 * A child of vfork() is not allowed to change signal
3910Sstevel@tonic-gate 		 * handlers to anything other than SIG_DFL or SIG_IGN.
3920Sstevel@tonic-gate 		 */
3930Sstevel@tonic-gate 		if (self->ul_vfork) {
3940Sstevel@tonic-gate 			if (tact.sa_sigaction != SIG_IGN)
3950Sstevel@tonic-gate 				tact.sa_sigaction = SIG_DFL;
3962248Sraf 		} else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
3970Sstevel@tonic-gate 			/*
3982248Sraf 			 * Always catch these signals.
3992248Sraf 			 * We need SIGCANCEL for pthread_cancel() to work.
4002248Sraf 			 * We need SIGAIOCANCEL for aio_cancel() to work.
4010Sstevel@tonic-gate 			 */
4020Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
4030Sstevel@tonic-gate 			if (tact.sa_sigaction == SIG_DFL ||
4040Sstevel@tonic-gate 			    tact.sa_sigaction == SIG_IGN)
4050Sstevel@tonic-gate 				tact.sa_flags = SA_SIGINFO;
4060Sstevel@tonic-gate 			else {
4070Sstevel@tonic-gate 				tact.sa_flags |= SA_SIGINFO;
4085891Sraf 				tact.sa_flags &=
4095891Sraf 				    ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
4100Sstevel@tonic-gate 			}
4110Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
4120Sstevel@tonic-gate 			tact.sa_mask = maskset;
4130Sstevel@tonic-gate 		} else if (tact.sa_sigaction != SIG_DFL &&
4140Sstevel@tonic-gate 		    tact.sa_sigaction != SIG_IGN) {
4150Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
4160Sstevel@tonic-gate 			tact.sa_flags &= ~SA_NODEFER;
4170Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
4180Sstevel@tonic-gate 			tact.sa_mask = maskset;
4190Sstevel@tonic-gate 		}
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if ((rv = __sigaction(sig, tactp, oact)) != 0)
4230Sstevel@tonic-gate 		udp->siguaction[sig].sig_uaction = oaction;
4240Sstevel@tonic-gate 	else if (oact != NULL &&
4250Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_DFL &&
4260Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_IGN)
4270Sstevel@tonic-gate 		*oact = oaction;
4280Sstevel@tonic-gate 
4292248Sraf 	/*
4302248Sraf 	 * We detect setting the disposition of SIGIO just to set the
4312248Sraf 	 * _sigio_enabled flag for the asynchronous i/o (aio) code.
4322248Sraf 	 */
4332248Sraf 	if (sig == SIGIO && rv == 0 && tactp != NULL) {
4342248Sraf 		_sigio_enabled =
4352248Sraf 		    (tactp->sa_handler != SIG_DFL &&
4362248Sraf 		    tactp->sa_handler != SIG_IGN);
4372248Sraf 	}
4382248Sraf 
4390Sstevel@tonic-gate 	if (!self->ul_vfork)
4404570Sraf 		lrw_unlock(&udp->siguaction[sig].sig_lock);
4410Sstevel@tonic-gate 	return (rv);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4446515Sraf /*
4456515Sraf  * This is a private interface for the linux brand interface.
4466515Sraf  */
4472712Snn35248 void
4482712Snn35248 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
4492712Snn35248     void (**osigacthandler)(int, siginfo_t *, void *))
4502712Snn35248 {
4512712Snn35248 	ulwp_t *self = curthread;
4522712Snn35248 	uberdata_t *udp = self->ul_uberdata;
4532712Snn35248 
4542712Snn35248 	if (osigacthandler != NULL)
4552712Snn35248 		*osigacthandler = udp->sigacthandler;
4562712Snn35248 
4572712Snn35248 	udp->sigacthandler = nsigacthandler;
4582712Snn35248 }
4592712Snn35248 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * Tell the kernel to block all signals.
4620Sstevel@tonic-gate  * Use the schedctl interface, or failing that, use __lwp_sigmask().
4630Sstevel@tonic-gate  * This action can be rescinded only by making a system call that
4640Sstevel@tonic-gate  * sets the signal mask:
4650Sstevel@tonic-gate  *	__lwp_sigmask(), __sigprocmask(), __setcontext(),
4660Sstevel@tonic-gate  *	__sigsuspend() or __pollsys().
4670Sstevel@tonic-gate  * In particular, this action cannot be reversed by assigning
4680Sstevel@tonic-gate  * scp->sc_sigblock = 0.  That would be a way to lose signals.
4690Sstevel@tonic-gate  * See the definition of restore_signals(self).
4700Sstevel@tonic-gate  */
4710Sstevel@tonic-gate void
4720Sstevel@tonic-gate block_all_signals(ulwp_t *self)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	volatile sc_shared_t *scp;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	enter_critical(self);
4770Sstevel@tonic-gate 	if ((scp = self->ul_schedctl) != NULL ||
4780Sstevel@tonic-gate 	    (scp = setup_schedctl()) != NULL)
4790Sstevel@tonic-gate 		scp->sc_sigblock = 1;
4800Sstevel@tonic-gate 	else
4810Sstevel@tonic-gate 		(void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL);
4820Sstevel@tonic-gate 	exit_critical(self);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4852712Snn35248 /*
4866515Sraf  * setcontext() has code that forcibly restores the curthread
4872712Snn35248  * pointer in a context passed to the setcontext(2) syscall.
4882712Snn35248  *
4892712Snn35248  * Certain processes may need to disable this feature, so these routines
4902712Snn35248  * provide the mechanism to do so.
4912712Snn35248  *
4922712Snn35248  * (As an example, branded 32-bit x86 processes may use %gs for their own
4932712Snn35248  * purposes, so they need to be able to specify a %gs value to be restored
4942712Snn35248  * on return from a signal handler via the passed ucontext_t.)
4952712Snn35248  */
4962712Snn35248 static int setcontext_enforcement = 1;
4972712Snn35248 
4982712Snn35248 void
4992712Snn35248 set_setcontext_enforcement(int on)
5002712Snn35248 {
5012712Snn35248 	setcontext_enforcement = on;
5022712Snn35248 }
5032712Snn35248 
504*6812Sraf #pragma weak _setcontext = setcontext
5050Sstevel@tonic-gate int
506*6812Sraf setcontext(const ucontext_t *ucp)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	ulwp_t *self = curthread;
5090Sstevel@tonic-gate 	int ret;
5100Sstevel@tonic-gate 	ucontext_t uc;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * Returning from the main context (uc_link == NULL) causes
5140Sstevel@tonic-gate 	 * the thread to exit.  See setcontext(2) and makecontext(3C).
5150Sstevel@tonic-gate 	 */
5160Sstevel@tonic-gate 	if (ucp == NULL)
517*6812Sraf 		thr_exit(NULL);
5186515Sraf 	(void) memcpy(&uc, ucp, sizeof (uc));
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/*
5210Sstevel@tonic-gate 	 * Restore previous signal mask and context link.
5220Sstevel@tonic-gate 	 */
5230Sstevel@tonic-gate 	if (uc.uc_flags & UC_SIGMASK) {
5240Sstevel@tonic-gate 		block_all_signals(self);
5250Sstevel@tonic-gate 		delete_reserved_signals(&uc.uc_sigmask);
5260Sstevel@tonic-gate 		self->ul_sigmask = uc.uc_sigmask;
5270Sstevel@tonic-gate 		if (self->ul_cursig) {
5280Sstevel@tonic-gate 			/*
5290Sstevel@tonic-gate 			 * We have a deferred signal present.
5300Sstevel@tonic-gate 			 * The signal mask will be set when the
5310Sstevel@tonic-gate 			 * signal is taken in take_deferred_signal().
5320Sstevel@tonic-gate 			 */
5330Sstevel@tonic-gate 			ASSERT(self->ul_critical + self->ul_sigdefer != 0);
5340Sstevel@tonic-gate 			uc.uc_flags &= ~UC_SIGMASK;
5350Sstevel@tonic-gate 		}
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 	self->ul_siglink = uc.uc_link;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/*
5400Sstevel@tonic-gate 	 * We don't know where this context structure has been.
5410Sstevel@tonic-gate 	 * Preserve the curthread pointer, at least.
5422712Snn35248 	 *
5432712Snn35248 	 * Allow this feature to be disabled if a particular process
5442712Snn35248 	 * requests it.
5450Sstevel@tonic-gate 	 */
5462712Snn35248 	if (setcontext_enforcement) {
5470Sstevel@tonic-gate #if defined(__sparc)
5482712Snn35248 		uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
5490Sstevel@tonic-gate #elif defined(__amd64)
5503446Smrj 		uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
5510Sstevel@tonic-gate #elif defined(__i386)
5523446Smrj 		uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
5530Sstevel@tonic-gate #else
5540Sstevel@tonic-gate #error "none of __sparc, __amd64, __i386 defined"
5550Sstevel@tonic-gate #endif
5562712Snn35248 	}
5572712Snn35248 
5580Sstevel@tonic-gate 	/*
5590Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
5600Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
5610Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
5620Sstevel@tonic-gate 	 */
5630Sstevel@tonic-gate 	set_parking_flag(self, 0);
5640Sstevel@tonic-gate 	self->ul_sp = 0;
5656515Sraf 	ret = __setcontext(&uc);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/*
5680Sstevel@tonic-gate 	 * It is OK for setcontext() to return if the user has not specified
5690Sstevel@tonic-gate 	 * UC_CPU.
5700Sstevel@tonic-gate 	 */
5710Sstevel@tonic-gate 	if (uc.uc_flags & UC_CPU)
5720Sstevel@tonic-gate 		thr_panic("setcontext(): __setcontext() returned");
5730Sstevel@tonic-gate 	return (ret);
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
576*6812Sraf #pragma weak _thr_sigsetmask = thr_sigsetmask
5770Sstevel@tonic-gate int
578*6812Sraf thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	ulwp_t *self = curthread;
5810Sstevel@tonic-gate 	sigset_t saveset;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if (set == NULL) {
5840Sstevel@tonic-gate 		enter_critical(self);
5850Sstevel@tonic-gate 		if (oset != NULL)
5860Sstevel@tonic-gate 			*oset = self->ul_sigmask;
5870Sstevel@tonic-gate 		exit_critical(self);
5880Sstevel@tonic-gate 	} else {
5890Sstevel@tonic-gate 		switch (how) {
5900Sstevel@tonic-gate 		case SIG_BLOCK:
5910Sstevel@tonic-gate 		case SIG_UNBLOCK:
5920Sstevel@tonic-gate 		case SIG_SETMASK:
5930Sstevel@tonic-gate 			break;
5940Sstevel@tonic-gate 		default:
5950Sstevel@tonic-gate 			return (EINVAL);
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 		/*
5990Sstevel@tonic-gate 		 * The assignments to self->ul_sigmask must be protected from
6000Sstevel@tonic-gate 		 * signals.  The nuances of this code are subtle.  Be careful.
6010Sstevel@tonic-gate 		 */
6020Sstevel@tonic-gate 		block_all_signals(self);
6030Sstevel@tonic-gate 		if (oset != NULL)
6040Sstevel@tonic-gate 			saveset = self->ul_sigmask;
6050Sstevel@tonic-gate 		switch (how) {
6060Sstevel@tonic-gate 		case SIG_BLOCK:
6070Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
6080Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
6090Sstevel@tonic-gate 			break;
6100Sstevel@tonic-gate 		case SIG_UNBLOCK:
6110Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
6120Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
6130Sstevel@tonic-gate 			break;
6140Sstevel@tonic-gate 		case SIG_SETMASK:
6150Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
6160Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
6170Sstevel@tonic-gate 			break;
6180Sstevel@tonic-gate 		}
6190Sstevel@tonic-gate 		delete_reserved_signals(&self->ul_sigmask);
6200Sstevel@tonic-gate 		if (oset != NULL)
6210Sstevel@tonic-gate 			*oset = saveset;
6220Sstevel@tonic-gate 		restore_signals(self);
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	return (0);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
628*6812Sraf #pragma weak _pthread_sigmask = pthread_sigmask
6290Sstevel@tonic-gate int
630*6812Sraf pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
631*6812Sraf {
632*6812Sraf 	return (thr_sigsetmask(how, set, oset));
633*6812Sraf }
634*6812Sraf 
635*6812Sraf #pragma weak _sigprocmask = sigprocmask
636*6812Sraf int
637*6812Sraf sigprocmask(int how, const sigset_t *set, sigset_t *oset)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	int error;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/*
6420Sstevel@tonic-gate 	 * Guard against children of vfork().
6430Sstevel@tonic-gate 	 */
6440Sstevel@tonic-gate 	if (curthread->ul_vfork)
6450Sstevel@tonic-gate 		return (__lwp_sigmask(how, set, oset));
6460Sstevel@tonic-gate 
647*6812Sraf 	if ((error = thr_sigsetmask(how, set, oset)) != 0) {
6480Sstevel@tonic-gate 		errno = error;
6490Sstevel@tonic-gate 		return (-1);
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	return (0);
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate /*
6560Sstevel@tonic-gate  * Called at library initialization to set up signal handling.
6574570Sraf  * All we really do is initialize the sig_lock rwlocks.
6580Sstevel@tonic-gate  * All signal handlers are either SIG_DFL or SIG_IGN on exec().
6590Sstevel@tonic-gate  * However, if any signal handlers were established on alternate
6600Sstevel@tonic-gate  * link maps before the primary link map has been initialized,
6610Sstevel@tonic-gate  * then inform the kernel of the new sigacthandler.
6620Sstevel@tonic-gate  */
6630Sstevel@tonic-gate void
6640Sstevel@tonic-gate signal_init()
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
6670Sstevel@tonic-gate 	struct sigaction *sap;
6680Sstevel@tonic-gate 	struct sigaction act;
6694570Sraf 	rwlock_t *rwlp;
6700Sstevel@tonic-gate 	int sig;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	for (sig = 0; sig < NSIG; sig++) {
6734570Sraf 		rwlp = &udp->siguaction[sig].sig_lock;
6744570Sraf 		rwlp->rwlock_magic = RWL_MAGIC;
6754570Sraf 		rwlp->mutex.mutex_flag = LOCK_INITED;
6764570Sraf 		rwlp->mutex.mutex_magic = MUTEX_MAGIC;
6770Sstevel@tonic-gate 		sap = &udp->siguaction[sig].sig_uaction;
6780Sstevel@tonic-gate 		if (sap->sa_sigaction != SIG_DFL &&
6790Sstevel@tonic-gate 		    sap->sa_sigaction != SIG_IGN &&
6800Sstevel@tonic-gate 		    __sigaction(sig, NULL, &act) == 0 &&
6810Sstevel@tonic-gate 		    act.sa_sigaction != SIG_DFL &&
6820Sstevel@tonic-gate 		    act.sa_sigaction != SIG_IGN) {
6830Sstevel@tonic-gate 			act = *sap;
6840Sstevel@tonic-gate 			act.sa_flags &= ~SA_NODEFER;
6850Sstevel@tonic-gate 			act.sa_sigaction = udp->sigacthandler;
6860Sstevel@tonic-gate 			act.sa_mask = maskset;
6870Sstevel@tonic-gate 			(void) __sigaction(sig, &act, NULL);
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate  * Common code for cancelling self in _sigcancel() and pthread_cancel().
6945891Sraf  * First record the fact that a cancellation is pending.
6955891Sraf  * Then, if cancellation is disabled or if we are holding unprotected
6965891Sraf  * libc locks, just return to defer the cancellation.
6975891Sraf  * Then, if we are at a cancellation point (ul_cancelable) just
6985891Sraf  * return and let _canceloff() do the exit.
6995891Sraf  * Else exit immediately if async mode is in effect.
7000Sstevel@tonic-gate  */
7010Sstevel@tonic-gate void
7025891Sraf do_sigcancel(void)
7030Sstevel@tonic-gate {
7040Sstevel@tonic-gate 	ulwp_t *self = curthread;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
7070Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer == 0);
7080Sstevel@tonic-gate 	self->ul_cancel_pending = 1;
7090Sstevel@tonic-gate 	if (self->ul_cancel_async &&
7100Sstevel@tonic-gate 	    !self->ul_cancel_disabled &&
7115891Sraf 	    self->ul_libc_locks == 0 &&
7120Sstevel@tonic-gate 	    !self->ul_cancelable)
713*6812Sraf 		pthread_exit(PTHREAD_CANCELED);
7145891Sraf 	set_cancel_pending_flag(self, 0);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate /*
7182248Sraf  * Set up the SIGCANCEL handler for threads cancellation,
7192248Sraf  * needed only when we have more than one thread,
7202248Sraf  * or the SIGAIOCANCEL handler for aio cancellation,
7212248Sraf  * called when aio is initialized, in __uaio_init().
7220Sstevel@tonic-gate  */
7230Sstevel@tonic-gate void
7242248Sraf setup_cancelsig(int sig)
7250Sstevel@tonic-gate {
7260Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
7274570Sraf 	rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
7280Sstevel@tonic-gate 	struct sigaction act;
7290Sstevel@tonic-gate 
7302248Sraf 	ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
7314570Sraf 	lrw_rdlock(rwlp);
7322248Sraf 	act = udp->siguaction[sig].sig_uaction;
7334570Sraf 	lrw_unlock(rwlp);
7340Sstevel@tonic-gate 	if (act.sa_sigaction == SIG_DFL ||
7350Sstevel@tonic-gate 	    act.sa_sigaction == SIG_IGN)
7360Sstevel@tonic-gate 		act.sa_flags = SA_SIGINFO;
7370Sstevel@tonic-gate 	else {
7380Sstevel@tonic-gate 		act.sa_flags |= SA_SIGINFO;
7395891Sraf 		act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 	act.sa_sigaction = udp->sigacthandler;
7420Sstevel@tonic-gate 	act.sa_mask = maskset;
7432248Sraf 	(void) __sigaction(sig, &act, NULL);
7440Sstevel@tonic-gate }
745