xref: /onnv-gate/usr/src/lib/libc/port/threads/sigaction.c (revision 3446:5903aece022d)
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 /*
23*3446Smrj  * 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.
860Sstevel@tonic-gate 	 *
870Sstevel@tonic-gate 	 * Locking exceptions:
880Sstevel@tonic-gate 	 * No locking for a child of vfork().
890Sstevel@tonic-gate 	 * If the signal is SIGPROF with an si_code of PROF_SIG,
900Sstevel@tonic-gate 	 * then we assume that this signal was generated by
910Sstevel@tonic-gate 	 * setitimer(ITIMER_REALPROF) set up by the dbx collector.
920Sstevel@tonic-gate 	 * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
930Sstevel@tonic-gate 	 * then we assume that the signal was generated by
940Sstevel@tonic-gate 	 * a hardware performance counter overflow.
950Sstevel@tonic-gate 	 * In these cases, assume that we need no locking.  It is the
960Sstevel@tonic-gate 	 * monitoring program's responsibility to ensure correctness.
970Sstevel@tonic-gate 	 */
980Sstevel@tonic-gate 	sap = &udp->siguaction[sig].sig_uaction;
990Sstevel@tonic-gate 	if (self->ul_vfork ||
1000Sstevel@tonic-gate 	    (sip != NULL &&
1010Sstevel@tonic-gate 	    ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
1020Sstevel@tonic-gate 	    (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
1030Sstevel@tonic-gate 		/* we wish this assignment could be atomic */
1041111Sraf 		(void) _private_memcpy(&uact, (void *)sap, sizeof (uact));
1050Sstevel@tonic-gate 	} else {
1060Sstevel@tonic-gate 		mutex_t *mp = &udp->siguaction[sig].sig_lock;
1070Sstevel@tonic-gate 		lmutex_lock(mp);
1081111Sraf 		(void) _private_memcpy(&uact, (void *)sap, sizeof (uact));
1090Sstevel@tonic-gate 		if (sig == SIGCANCEL && (sap->sa_flags & SA_RESETHAND))
1100Sstevel@tonic-gate 			sap->sa_sigaction = SIG_DFL;
1110Sstevel@tonic-gate 		lmutex_unlock(mp);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	/*
1150Sstevel@tonic-gate 	 * Set the proper signal mask and call the user's signal handler.
1160Sstevel@tonic-gate 	 * (We overrode the user-requested signal mask with maskset
1170Sstevel@tonic-gate 	 * so we currently have all blockable signals blocked.)
1180Sstevel@tonic-gate 	 *
1190Sstevel@tonic-gate 	 * We would like to ASSERT() that the signal is not a member of the
1200Sstevel@tonic-gate 	 * signal mask at the previous level (ucp->uc_sigmask) or the specified
1210Sstevel@tonic-gate 	 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
1220Sstevel@tonic-gate 	 * /proc can override this via PCSSIG, so we don't bother.
1230Sstevel@tonic-gate 	 *
1240Sstevel@tonic-gate 	 * We would also like to ASSERT() that the signal mask at the previous
1250Sstevel@tonic-gate 	 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
1260Sstevel@tonic-gate 	 * but /proc can change the thread's signal mask via PCSHOLD, so we
1270Sstevel@tonic-gate 	 * don't bother with that either.
1280Sstevel@tonic-gate 	 */
1290Sstevel@tonic-gate 	ASSERT(ucp->uc_flags & UC_SIGMASK);
1300Sstevel@tonic-gate 	if (self->ul_sigsuspend) {
1310Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
1320Sstevel@tonic-gate 		self->ul_sigsuspend = 0;
1330Sstevel@tonic-gate 		/* the sigsuspend() or pollsys() signal mask */
1340Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &self->ul_tmpmask);
1350Sstevel@tonic-gate 	} else {
1360Sstevel@tonic-gate 		/* the signal mask at the previous level */
1370Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &ucp->uc_sigmask);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_NODEFER))	/* add current signal */
1400Sstevel@tonic-gate 		(void) _private_sigaddset(&uact.sa_mask, sig);
1410Sstevel@tonic-gate 	self->ul_sigmask = uact.sa_mask;
1420Sstevel@tonic-gate 	self->ul_siglink = ucp;
1430Sstevel@tonic-gate 	(void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * If this thread has been sent SIGCANCEL from the kernel
1470Sstevel@tonic-gate 	 * or from pthread_cancel(), it is being asked to exit.
1480Sstevel@tonic-gate 	 * The kernel may send SIGCANCEL without a siginfo struct.
1490Sstevel@tonic-gate 	 * If the SIGCANCEL is process-directed (from kill() or
1500Sstevel@tonic-gate 	 * sigqueue()), treat it as an ordinary signal.
1510Sstevel@tonic-gate 	 */
1520Sstevel@tonic-gate 	if (sig == SIGCANCEL) {
1530Sstevel@tonic-gate 		if (sip == NULL || SI_FROMKERNEL(sip) ||
1540Sstevel@tonic-gate 		    sip->si_code == SI_LWP) {
1550Sstevel@tonic-gate 			do_sigcancel();
1560Sstevel@tonic-gate 			goto out;
1570Sstevel@tonic-gate 		}
1582248Sraf 		/* SIGCANCEL is ignored by default */
1592248Sraf 		if (uact.sa_sigaction == SIG_DFL ||
1602248Sraf 		    uact.sa_sigaction == SIG_IGN)
1612248Sraf 			goto out;
1622248Sraf 	}
1632248Sraf 
1642248Sraf 	/*
1652248Sraf 	 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
1662248Sraf 	 * we are an aio worker thread, cancel the aio request.
1672248Sraf 	 */
1682248Sraf 	if (sig == SIGAIOCANCEL) {
1692248Sraf 		aio_worker_t *aiowp = _pthread_getspecific(_aio_key);
1702248Sraf 
1712248Sraf 		if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
1722248Sraf 			_siglongjmp(aiowp->work_jmp_buf, 1);
1732248Sraf 		/* SIGLWP is ignored by default */
1740Sstevel@tonic-gate 		if (uact.sa_sigaction == SIG_DFL ||
1750Sstevel@tonic-gate 		    uact.sa_sigaction == SIG_IGN)
1760Sstevel@tonic-gate 			goto out;
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_SIGINFO))
1800Sstevel@tonic-gate 		sip = NULL;
1810Sstevel@tonic-gate 	__sighndlr(sig, sip, ucp, uact.sa_sigaction);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate #if defined(sparc) || defined(__sparc)
1840Sstevel@tonic-gate 	/*
1850Sstevel@tonic-gate 	 * If this is a floating point exception and the queue
1860Sstevel@tonic-gate 	 * is non-empty, pop the top entry from the queue.  This
1870Sstevel@tonic-gate 	 * is to maintain expected behavior.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
1900Sstevel@tonic-gate 		fpregset_t *fp = &ucp->uc_mcontext.fpregs;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		if (--fp->fpu_qcnt > 0) {
1930Sstevel@tonic-gate 			unsigned char i;
1940Sstevel@tonic-gate 			struct fq *fqp;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 			fqp = fp->fpu_q;
1970Sstevel@tonic-gate 			for (i = 0; i < fp->fpu_qcnt; i++)
1980Sstevel@tonic-gate 				fqp[i] = fqp[i+1];
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate #endif	/* sparc */
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate out:
2040Sstevel@tonic-gate 	(void) _private_setcontext(ucp);
2050Sstevel@tonic-gate 	thr_panic("call_user_handler(): _setcontext() returned");
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  * take_deferred_signal() is called when ul_critical and ul_sigdefer become
2100Sstevel@tonic-gate  * zero and a deferred signal has been recorded on the current thread.
2110Sstevel@tonic-gate  * We are out of the critical region and are ready to take a signal.
2120Sstevel@tonic-gate  * The kernel has all signals blocked on this lwp, but our value of
2130Sstevel@tonic-gate  * ul_sigmask is the correct signal mask for the previous context.
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate void
2160Sstevel@tonic-gate take_deferred_signal(int sig)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	ulwp_t *self = curthread;
2190Sstevel@tonic-gate 	siginfo_t siginfo;
2200Sstevel@tonic-gate 	siginfo_t *sip;
2210Sstevel@tonic-gate 	ucontext_t uc;
2220Sstevel@tonic-gate 	volatile int returning;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
2250Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer == 0);
2260Sstevel@tonic-gate 	ASSERT(self->ul_cursig == 0);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	returning = 0;
2290Sstevel@tonic-gate 	uc.uc_flags = UC_ALL;
2300Sstevel@tonic-gate 	/*
2310Sstevel@tonic-gate 	 * We call _private_getcontext (a libc-private synonym for
2320Sstevel@tonic-gate 	 * _getcontext) rather than _getcontext because we need to
2330Sstevel@tonic-gate 	 * avoid the dynamic linker and link auditing problems here.
2340Sstevel@tonic-gate 	 */
2350Sstevel@tonic-gate 	(void) _private_getcontext(&uc);
2360Sstevel@tonic-gate 	/*
2370Sstevel@tonic-gate 	 * If the application signal handler calls setcontext() on
2380Sstevel@tonic-gate 	 * the ucontext we give it, it returns here, then we return.
2390Sstevel@tonic-gate 	 */
2400Sstevel@tonic-gate 	if (returning)
2410Sstevel@tonic-gate 		return;
2420Sstevel@tonic-gate 	returning = 1;
2430Sstevel@tonic-gate 	ASSERT(sigequalset(&uc.uc_sigmask, &maskset));
2440Sstevel@tonic-gate 	if (self->ul_siginfo.si_signo == 0)
2450Sstevel@tonic-gate 		sip = NULL;
2460Sstevel@tonic-gate 	else {
2471111Sraf 		(void) _private_memcpy(&siginfo,
2481111Sraf 		    &self->ul_siginfo, sizeof (siginfo));
2490Sstevel@tonic-gate 		sip = &siginfo;
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 	uc.uc_sigmask = self->ul_sigmask;
2520Sstevel@tonic-gate 	call_user_handler(sig, sip, &uc);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate void
2560Sstevel@tonic-gate sigacthandler(int sig, siginfo_t *sip, void *uvp)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	ucontext_t *ucp = uvp;
2590Sstevel@tonic-gate 	ulwp_t *self = curthread;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * Do this in case we took a signal while in a cancelable system call.
2630Sstevel@tonic-gate 	 * It does no harm if we were not in such a system call.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	self->ul_sp = 0;
2660Sstevel@tonic-gate 	if (sig != SIGCANCEL)
2670Sstevel@tonic-gate 		self->ul_cancel_async = self->ul_save_async;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/*
2700Sstevel@tonic-gate 	 * If we are not in a critical region and are
2710Sstevel@tonic-gate 	 * not deferring signals, take the signal now.
2720Sstevel@tonic-gate 	 */
2730Sstevel@tonic-gate 	if ((self->ul_critical + self->ul_sigdefer) == 0) {
2740Sstevel@tonic-gate 		call_user_handler(sig, sip, ucp);
2750Sstevel@tonic-gate 		return;	/* call_user_handler() cannot return */
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/*
2790Sstevel@tonic-gate 	 * We are in a critical region or we are deferring signals.  When
2800Sstevel@tonic-gate 	 * we emerge from the region we will call take_deferred_signal().
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	ASSERT(self->ul_cursig == 0);
2830Sstevel@tonic-gate 	self->ul_cursig = (char)sig;
2840Sstevel@tonic-gate 	if (sip != NULL)
2851111Sraf 		(void) _private_memcpy(&self->ul_siginfo,
2861111Sraf 		    sip, sizeof (siginfo_t));
2870Sstevel@tonic-gate 	else
2880Sstevel@tonic-gate 		self->ul_siginfo.si_signo = 0;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/*
2910Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
2920Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
2930Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
2940Sstevel@tonic-gate 	 */
2950Sstevel@tonic-gate 	set_parking_flag(self, 0);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * Return to the previous context with all signals blocked.
2990Sstevel@tonic-gate 	 * We will restore the signal mask in take_deferred_signal().
3000Sstevel@tonic-gate 	 * Note that we are calling the system call trap here, not
3010Sstevel@tonic-gate 	 * the _setcontext() wrapper.  We don't want to change the
3020Sstevel@tonic-gate 	 * thread's ul_sigmask by this operation.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 	ucp->uc_sigmask = maskset;
3050Sstevel@tonic-gate 	(void) __setcontext_syscall(ucp);
3060Sstevel@tonic-gate 	thr_panic("sigacthandler(): __setcontext() returned");
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3092712Snn35248 #pragma weak sigaction = _libc_sigaction
3102712Snn35248 #pragma weak _sigaction = _libc_sigaction
3110Sstevel@tonic-gate int
3122712Snn35248 _libc_sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	ulwp_t *self = curthread;
3150Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
3160Sstevel@tonic-gate 	struct sigaction oaction;
3170Sstevel@tonic-gate 	struct sigaction tact;
3180Sstevel@tonic-gate 	struct sigaction *tactp = NULL;
3190Sstevel@tonic-gate 	int rv;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	if (sig <= 0 || sig >= NSIG) {
3220Sstevel@tonic-gate 		errno = EINVAL;
3230Sstevel@tonic-gate 		return (-1);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (!self->ul_vfork)
3270Sstevel@tonic-gate 		lmutex_lock(&udp->siguaction[sig].sig_lock);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	oaction = udp->siguaction[sig].sig_uaction;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (nact != NULL) {
3320Sstevel@tonic-gate 		tact = *nact;	/* make a copy so we can modify it */
3330Sstevel@tonic-gate 		tactp = &tact;
3340Sstevel@tonic-gate 		delete_reserved_signals(&tact.sa_mask);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate #if !defined(_LP64)
3370Sstevel@tonic-gate 		tact.sa_resv[0] = tact.sa_resv[1] = 0;	/* cleanliness */
3380Sstevel@tonic-gate #endif
3390Sstevel@tonic-gate 		/*
3400Sstevel@tonic-gate 		 * To be compatible with the behavior of SunOS 4.x:
3410Sstevel@tonic-gate 		 * If the new signal handler is SIG_IGN or SIG_DFL, do
3420Sstevel@tonic-gate 		 * not change the signal's entry in the siguaction array.
3430Sstevel@tonic-gate 		 * This allows a child of vfork(2) to set signal handlers
3440Sstevel@tonic-gate 		 * to SIG_IGN or SIG_DFL without affecting the parent.
3450Sstevel@tonic-gate 		 *
3460Sstevel@tonic-gate 		 * This also covers a race condition with some thread
3470Sstevel@tonic-gate 		 * setting the signal action to SIG_DFL or SIG_IGN
3480Sstevel@tonic-gate 		 * when the thread has also received and deferred
3490Sstevel@tonic-gate 		 * that signal.  When the thread takes the deferred
3500Sstevel@tonic-gate 		 * signal, even though it has set the action to SIG_DFL
3510Sstevel@tonic-gate 		 * or SIG_IGN, it will execute the old signal handler
3520Sstevel@tonic-gate 		 * anyway.  This is an inherent signaling race condition
3530Sstevel@tonic-gate 		 * and is not a bug.
3540Sstevel@tonic-gate 		 *
3550Sstevel@tonic-gate 		 * A child of vfork() is not allowed to change signal
3560Sstevel@tonic-gate 		 * handlers to anything other than SIG_DFL or SIG_IGN.
3570Sstevel@tonic-gate 		 */
3580Sstevel@tonic-gate 		if (self->ul_vfork) {
3590Sstevel@tonic-gate 			if (tact.sa_sigaction != SIG_IGN)
3600Sstevel@tonic-gate 				tact.sa_sigaction = SIG_DFL;
3612248Sraf 		} else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
3620Sstevel@tonic-gate 			/*
3632248Sraf 			 * Always catch these signals.
3642248Sraf 			 * We need SIGCANCEL for pthread_cancel() to work.
3652248Sraf 			 * We need SIGAIOCANCEL for aio_cancel() to work.
3660Sstevel@tonic-gate 			 */
3670Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
3680Sstevel@tonic-gate 			if (tact.sa_sigaction == SIG_DFL ||
3690Sstevel@tonic-gate 			    tact.sa_sigaction == SIG_IGN)
3700Sstevel@tonic-gate 				tact.sa_flags = SA_SIGINFO;
3710Sstevel@tonic-gate 			else {
3720Sstevel@tonic-gate 				tact.sa_flags |= SA_SIGINFO;
3730Sstevel@tonic-gate 				tact.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
3740Sstevel@tonic-gate 			}
3750Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
3760Sstevel@tonic-gate 			tact.sa_mask = maskset;
3770Sstevel@tonic-gate 		} else if (tact.sa_sigaction != SIG_DFL &&
3780Sstevel@tonic-gate 		    tact.sa_sigaction != SIG_IGN) {
3790Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
3800Sstevel@tonic-gate 			tact.sa_flags &= ~SA_NODEFER;
3810Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
3820Sstevel@tonic-gate 			tact.sa_mask = maskset;
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	if ((rv = __sigaction(sig, tactp, oact)) != 0)
3870Sstevel@tonic-gate 		udp->siguaction[sig].sig_uaction = oaction;
3880Sstevel@tonic-gate 	else if (oact != NULL &&
3890Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_DFL &&
3900Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_IGN)
3910Sstevel@tonic-gate 		*oact = oaction;
3920Sstevel@tonic-gate 
3932248Sraf 	/*
3942248Sraf 	 * We detect setting the disposition of SIGIO just to set the
3952248Sraf 	 * _sigio_enabled flag for the asynchronous i/o (aio) code.
3962248Sraf 	 */
3972248Sraf 	if (sig == SIGIO && rv == 0 && tactp != NULL) {
3982248Sraf 		_sigio_enabled =
3992248Sraf 		    (tactp->sa_handler != SIG_DFL &&
4002248Sraf 		    tactp->sa_handler != SIG_IGN);
4012248Sraf 	}
4022248Sraf 
4030Sstevel@tonic-gate 	if (!self->ul_vfork)
4040Sstevel@tonic-gate 		lmutex_unlock(&udp->siguaction[sig].sig_lock);
4050Sstevel@tonic-gate 	return (rv);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4082712Snn35248 void
4092712Snn35248 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
4102712Snn35248     void (**osigacthandler)(int, siginfo_t *, void *))
4112712Snn35248 {
4122712Snn35248 	ulwp_t *self = curthread;
4132712Snn35248 	uberdata_t *udp = self->ul_uberdata;
4142712Snn35248 
4152712Snn35248 	if (osigacthandler != NULL)
4162712Snn35248 		*osigacthandler = udp->sigacthandler;
4172712Snn35248 
4182712Snn35248 	udp->sigacthandler = nsigacthandler;
4192712Snn35248 }
4202712Snn35248 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * Calling set_parking_flag(curthread, 1) informs the kernel that we are
4230Sstevel@tonic-gate  * calling __lwp_park or ___lwp_cond_wait().  If we take a signal in
4240Sstevel@tonic-gate  * the unprotected (from signals) interval before reaching the kernel,
4250Sstevel@tonic-gate  * sigacthandler() will call set_parking_flag(curthread, 0) to inform
4260Sstevel@tonic-gate  * the kernel to return immediately from these system calls, giving us
4270Sstevel@tonic-gate  * a spurious wakeup but not a deadlock.
4280Sstevel@tonic-gate  */
4290Sstevel@tonic-gate void
4300Sstevel@tonic-gate set_parking_flag(ulwp_t *self, int park)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	volatile sc_shared_t *scp;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	enter_critical(self);
4350Sstevel@tonic-gate 	if ((scp = self->ul_schedctl) != NULL ||
4360Sstevel@tonic-gate 	    (scp = setup_schedctl()) != NULL)
4370Sstevel@tonic-gate 		scp->sc_park = park;
4380Sstevel@tonic-gate 	else if (park == 0)	/* schedctl failed, do it the long way */
4390Sstevel@tonic-gate 		__lwp_unpark(self->ul_lwpid);
4400Sstevel@tonic-gate 	exit_critical(self);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * Tell the kernel to block all signals.
4450Sstevel@tonic-gate  * Use the schedctl interface, or failing that, use __lwp_sigmask().
4460Sstevel@tonic-gate  * This action can be rescinded only by making a system call that
4470Sstevel@tonic-gate  * sets the signal mask:
4480Sstevel@tonic-gate  *	__lwp_sigmask(), __sigprocmask(), __setcontext(),
4490Sstevel@tonic-gate  *	__sigsuspend() or __pollsys().
4500Sstevel@tonic-gate  * In particular, this action cannot be reversed by assigning
4510Sstevel@tonic-gate  * scp->sc_sigblock = 0.  That would be a way to lose signals.
4520Sstevel@tonic-gate  * See the definition of restore_signals(self).
4530Sstevel@tonic-gate  */
4540Sstevel@tonic-gate void
4550Sstevel@tonic-gate block_all_signals(ulwp_t *self)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	volatile sc_shared_t *scp;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	enter_critical(self);
4600Sstevel@tonic-gate 	if ((scp = self->ul_schedctl) != NULL ||
4610Sstevel@tonic-gate 	    (scp = setup_schedctl()) != NULL)
4620Sstevel@tonic-gate 		scp->sc_sigblock = 1;
4630Sstevel@tonic-gate 	else
4640Sstevel@tonic-gate 		(void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL);
4650Sstevel@tonic-gate 	exit_critical(self);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4682712Snn35248 /*
4692712Snn35248  * _private_setcontext has code that forcibly restores the curthread
4702712Snn35248  * pointer in a context passed to the setcontext(2) syscall.
4712712Snn35248  *
4722712Snn35248  * Certain processes may need to disable this feature, so these routines
4732712Snn35248  * provide the mechanism to do so.
4742712Snn35248  *
4752712Snn35248  * (As an example, branded 32-bit x86 processes may use %gs for their own
4762712Snn35248  * purposes, so they need to be able to specify a %gs value to be restored
4772712Snn35248  * on return from a signal handler via the passed ucontext_t.)
4782712Snn35248  */
4792712Snn35248 static int setcontext_enforcement = 1;
4802712Snn35248 
4812712Snn35248 void
4822712Snn35248 set_setcontext_enforcement(int on)
4832712Snn35248 {
4842712Snn35248 	setcontext_enforcement = on;
4852712Snn35248 }
4862712Snn35248 
4870Sstevel@tonic-gate #pragma weak setcontext = _private_setcontext
4880Sstevel@tonic-gate #pragma weak _setcontext = _private_setcontext
4890Sstevel@tonic-gate int
4900Sstevel@tonic-gate _private_setcontext(const ucontext_t *ucp)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	ulwp_t *self = curthread;
4930Sstevel@tonic-gate 	int ret;
4940Sstevel@tonic-gate 	ucontext_t uc;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	/*
4970Sstevel@tonic-gate 	 * Returning from the main context (uc_link == NULL) causes
4980Sstevel@tonic-gate 	 * the thread to exit.  See setcontext(2) and makecontext(3C).
4990Sstevel@tonic-gate 	 */
5000Sstevel@tonic-gate 	if (ucp == NULL)
5010Sstevel@tonic-gate 		_thr_exit(NULL);
5021111Sraf 	(void) _private_memcpy(&uc, ucp, sizeof (uc));
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	/*
5050Sstevel@tonic-gate 	 * Restore previous signal mask and context link.
5060Sstevel@tonic-gate 	 */
5070Sstevel@tonic-gate 	if (uc.uc_flags & UC_SIGMASK) {
5080Sstevel@tonic-gate 		block_all_signals(self);
5090Sstevel@tonic-gate 		delete_reserved_signals(&uc.uc_sigmask);
5100Sstevel@tonic-gate 		self->ul_sigmask = uc.uc_sigmask;
5110Sstevel@tonic-gate 		if (self->ul_cursig) {
5120Sstevel@tonic-gate 			/*
5130Sstevel@tonic-gate 			 * We have a deferred signal present.
5140Sstevel@tonic-gate 			 * The signal mask will be set when the
5150Sstevel@tonic-gate 			 * signal is taken in take_deferred_signal().
5160Sstevel@tonic-gate 			 */
5170Sstevel@tonic-gate 			ASSERT(self->ul_critical + self->ul_sigdefer != 0);
5180Sstevel@tonic-gate 			uc.uc_flags &= ~UC_SIGMASK;
5190Sstevel@tonic-gate 		}
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 	self->ul_siglink = uc.uc_link;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/*
5240Sstevel@tonic-gate 	 * We don't know where this context structure has been.
5250Sstevel@tonic-gate 	 * Preserve the curthread pointer, at least.
5262712Snn35248 	 *
5272712Snn35248 	 * Allow this feature to be disabled if a particular process
5282712Snn35248 	 * requests it.
5290Sstevel@tonic-gate 	 */
5302712Snn35248 	if (setcontext_enforcement) {
5310Sstevel@tonic-gate #if defined(__sparc)
5322712Snn35248 		uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
5330Sstevel@tonic-gate #elif defined(__amd64)
534*3446Smrj 		uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
5350Sstevel@tonic-gate #elif defined(__i386)
536*3446Smrj 		uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
5370Sstevel@tonic-gate #else
5380Sstevel@tonic-gate #error "none of __sparc, __amd64, __i386 defined"
5390Sstevel@tonic-gate #endif
5402712Snn35248 	}
5412712Snn35248 
5420Sstevel@tonic-gate 	/*
5430Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
5440Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
5450Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
5460Sstevel@tonic-gate 	 */
5470Sstevel@tonic-gate 	set_parking_flag(self, 0);
5480Sstevel@tonic-gate 	self->ul_sp = 0;
5490Sstevel@tonic-gate 	ret = __setcontext_syscall(&uc);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	/*
5520Sstevel@tonic-gate 	 * It is OK for setcontext() to return if the user has not specified
5530Sstevel@tonic-gate 	 * UC_CPU.
5540Sstevel@tonic-gate 	 */
5550Sstevel@tonic-gate 	if (uc.uc_flags & UC_CPU)
5560Sstevel@tonic-gate 		thr_panic("setcontext(): __setcontext() returned");
5570Sstevel@tonic-gate 	return (ret);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate #pragma weak thr_sigsetmask = _thr_sigsetmask
5610Sstevel@tonic-gate #pragma weak pthread_sigmask = _thr_sigsetmask
5620Sstevel@tonic-gate #pragma weak _pthread_sigmask = _thr_sigsetmask
5630Sstevel@tonic-gate int
5640Sstevel@tonic-gate _thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate 	ulwp_t *self = curthread;
5670Sstevel@tonic-gate 	sigset_t saveset;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	if (set == NULL) {
5700Sstevel@tonic-gate 		enter_critical(self);
5710Sstevel@tonic-gate 		if (oset != NULL)
5720Sstevel@tonic-gate 			*oset = self->ul_sigmask;
5730Sstevel@tonic-gate 		exit_critical(self);
5740Sstevel@tonic-gate 	} else {
5750Sstevel@tonic-gate 		switch (how) {
5760Sstevel@tonic-gate 		case SIG_BLOCK:
5770Sstevel@tonic-gate 		case SIG_UNBLOCK:
5780Sstevel@tonic-gate 		case SIG_SETMASK:
5790Sstevel@tonic-gate 			break;
5800Sstevel@tonic-gate 		default:
5810Sstevel@tonic-gate 			return (EINVAL);
5820Sstevel@tonic-gate 		}
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 		/*
5850Sstevel@tonic-gate 		 * The assignments to self->ul_sigmask must be protected from
5860Sstevel@tonic-gate 		 * signals.  The nuances of this code are subtle.  Be careful.
5870Sstevel@tonic-gate 		 */
5880Sstevel@tonic-gate 		block_all_signals(self);
5890Sstevel@tonic-gate 		if (oset != NULL)
5900Sstevel@tonic-gate 			saveset = self->ul_sigmask;
5910Sstevel@tonic-gate 		switch (how) {
5920Sstevel@tonic-gate 		case SIG_BLOCK:
5930Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
5940Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
5950Sstevel@tonic-gate 			break;
5960Sstevel@tonic-gate 		case SIG_UNBLOCK:
5970Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
5980Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
5990Sstevel@tonic-gate 			break;
6000Sstevel@tonic-gate 		case SIG_SETMASK:
6010Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
6020Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
6030Sstevel@tonic-gate 			break;
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 		delete_reserved_signals(&self->ul_sigmask);
6060Sstevel@tonic-gate 		if (oset != NULL)
6070Sstevel@tonic-gate 			*oset = saveset;
6080Sstevel@tonic-gate 		restore_signals(self);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	return (0);
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate #pragma weak sigprocmask = _sigprocmask
6150Sstevel@tonic-gate int
6160Sstevel@tonic-gate _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate 	int error;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * Guard against children of vfork().
6220Sstevel@tonic-gate 	 */
6230Sstevel@tonic-gate 	if (curthread->ul_vfork)
6240Sstevel@tonic-gate 		return (__lwp_sigmask(how, set, oset));
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	if ((error = _thr_sigsetmask(how, set, oset)) != 0) {
6270Sstevel@tonic-gate 		errno = error;
6280Sstevel@tonic-gate 		return (-1);
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	return (0);
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate  * Called at library initialization to set up signal handling.
6360Sstevel@tonic-gate  * All we really do is initialize the sig_lock mutexes.
6370Sstevel@tonic-gate  * All signal handlers are either SIG_DFL or SIG_IGN on exec().
6380Sstevel@tonic-gate  * However, if any signal handlers were established on alternate
6390Sstevel@tonic-gate  * link maps before the primary link map has been initialized,
6400Sstevel@tonic-gate  * then inform the kernel of the new sigacthandler.
6410Sstevel@tonic-gate  */
6420Sstevel@tonic-gate void
6430Sstevel@tonic-gate signal_init()
6440Sstevel@tonic-gate {
6450Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
6460Sstevel@tonic-gate 	struct sigaction *sap;
6470Sstevel@tonic-gate 	struct sigaction act;
6480Sstevel@tonic-gate 	int sig;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	for (sig = 0; sig < NSIG; sig++) {
6510Sstevel@tonic-gate 		udp->siguaction[sig].sig_lock.mutex_magic = MUTEX_MAGIC;
6520Sstevel@tonic-gate 		sap = &udp->siguaction[sig].sig_uaction;
6530Sstevel@tonic-gate 		if (sap->sa_sigaction != SIG_DFL &&
6540Sstevel@tonic-gate 		    sap->sa_sigaction != SIG_IGN &&
6550Sstevel@tonic-gate 		    __sigaction(sig, NULL, &act) == 0 &&
6560Sstevel@tonic-gate 		    act.sa_sigaction != SIG_DFL &&
6570Sstevel@tonic-gate 		    act.sa_sigaction != SIG_IGN) {
6580Sstevel@tonic-gate 			act = *sap;
6590Sstevel@tonic-gate 			act.sa_flags &= ~SA_NODEFER;
6600Sstevel@tonic-gate 			act.sa_sigaction = udp->sigacthandler;
6610Sstevel@tonic-gate 			act.sa_mask = maskset;
6620Sstevel@tonic-gate 			(void) __sigaction(sig, &act, NULL);
6630Sstevel@tonic-gate 		}
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate  * Common code for cancelling self in _sigcancel() and pthread_cancel().
6690Sstevel@tonic-gate  * If the thread is at a cancellation point (ul_cancelable) then just
6700Sstevel@tonic-gate  * return and let _canceloff() do the exit, else exit immediately if
6710Sstevel@tonic-gate  * async mode is in effect.
6720Sstevel@tonic-gate  */
6730Sstevel@tonic-gate void
6740Sstevel@tonic-gate do_sigcancel()
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate 	ulwp_t *self = curthread;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
6790Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer == 0);
6800Sstevel@tonic-gate 	self->ul_cancel_pending = 1;
6810Sstevel@tonic-gate 	if (self->ul_cancel_async &&
6820Sstevel@tonic-gate 	    !self->ul_cancel_disabled &&
6830Sstevel@tonic-gate 	    !self->ul_cancelable)
6840Sstevel@tonic-gate 		_pthread_exit(PTHREAD_CANCELED);
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate /*
6882248Sraf  * Set up the SIGCANCEL handler for threads cancellation,
6892248Sraf  * needed only when we have more than one thread,
6902248Sraf  * or the SIGAIOCANCEL handler for aio cancellation,
6912248Sraf  * called when aio is initialized, in __uaio_init().
6920Sstevel@tonic-gate  */
6930Sstevel@tonic-gate void
6942248Sraf setup_cancelsig(int sig)
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
6972248Sraf 	mutex_t *mp = &udp->siguaction[sig].sig_lock;
6980Sstevel@tonic-gate 	struct sigaction act;
6990Sstevel@tonic-gate 
7002248Sraf 	ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
7012248Sraf 	lmutex_lock(mp);
7022248Sraf 	act = udp->siguaction[sig].sig_uaction;
7032248Sraf 	lmutex_unlock(mp);
7040Sstevel@tonic-gate 	if (act.sa_sigaction == SIG_DFL ||
7050Sstevel@tonic-gate 	    act.sa_sigaction == SIG_IGN)
7060Sstevel@tonic-gate 		act.sa_flags = SA_SIGINFO;
7070Sstevel@tonic-gate 	else {
7080Sstevel@tonic-gate 		act.sa_flags |= SA_SIGINFO;
7090Sstevel@tonic-gate 		act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 	act.sa_sigaction = udp->sigacthandler;
7120Sstevel@tonic-gate 	act.sa_mask = maskset;
7132248Sraf 	(void) __sigaction(sig, &act, NULL);
7140Sstevel@tonic-gate }
715