xref: /onnv-gate/usr/src/lib/libc/port/threads/sigaction.c (revision 11913:283e725df792)
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 /*
2311426SRoger.Faulkner@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include "lint.h"
286812Sraf #include <sys/feature_tests.h>
296812Sraf /*
306812Sraf  * setcontext() really can return, if UC_CPU is not specified.
316812Sraf  * Make the compiler shut up about it.
326812Sraf  */
336812Sraf #if defined(__NORETURN)
346812Sraf #undef	__NORETURN
356812Sraf #endif
366812Sraf #define	__NORETURN
370Sstevel@tonic-gate #include "thr_uberdata.h"
382248Sraf #include "asyncio.h"
390Sstevel@tonic-gate #include <signal.h>
400Sstevel@tonic-gate #include <siginfo.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate 
43*11913SRoger.Faulkner@Sun.COM /* maskable signals */
44*11913SRoger.Faulkner@Sun.COM const sigset_t maskset = {MASKSET0, MASKSET1, MASKSET2, MASKSET3};
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * Return true if the valid signal bits in both sets are the same.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate int
sigequalset(const sigset_t * s1,const sigset_t * s2)500Sstevel@tonic-gate sigequalset(const sigset_t *s1, const sigset_t *s2)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	/*
530Sstevel@tonic-gate 	 * We only test valid signal bits, not rubbish following MAXSIG
540Sstevel@tonic-gate 	 * (for speed).  Algorithm:
550Sstevel@tonic-gate 	 * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
560Sstevel@tonic-gate 	 */
57*11913SRoger.Faulkner@Sun.COM /* see lib/libc/inc/thr_uberdata.h for why this must be true */
58*11913SRoger.Faulkner@Sun.COM #if (MAXSIG > (2 * 32) && MAXSIG <= (3 * 32))
590Sstevel@tonic-gate 	return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) |
60*11913SRoger.Faulkner@Sun.COM 	    (s1->__sigbits[1] ^ s2->__sigbits[1]) |
61*11913SRoger.Faulkner@Sun.COM 	    ((s1->__sigbits[2] ^ s2->__sigbits[2]) & FILLSET2)));
62*11913SRoger.Faulkner@Sun.COM #else
63*11913SRoger.Faulkner@Sun.COM #error "fix me: MAXSIG out of bounds"
64*11913SRoger.Faulkner@Sun.COM #endif
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * Common code for calling the user-specified signal handler.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate void
call_user_handler(int sig,siginfo_t * sip,ucontext_t * ucp)710Sstevel@tonic-gate call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	ulwp_t *self = curthread;
740Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
750Sstevel@tonic-gate 	struct sigaction uact;
760Sstevel@tonic-gate 	volatile struct sigaction *sap;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	/*
790Sstevel@tonic-gate 	 * If we are taking a signal while parked or about to be parked
800Sstevel@tonic-gate 	 * on __lwp_park() then remove ourself from the sleep queue so
810Sstevel@tonic-gate 	 * that we can grab locks.  The code in mutex_lock_queue() and
820Sstevel@tonic-gate 	 * cond_wait_common() will detect this and deal with it when
830Sstevel@tonic-gate 	 * __lwp_park() returns.
840Sstevel@tonic-gate 	 */
850Sstevel@tonic-gate 	unsleep_self();
860Sstevel@tonic-gate 	set_parking_flag(self, 0);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (__td_event_report(self, TD_CATCHSIG, udp)) {
890Sstevel@tonic-gate 		self->ul_td_evbuf.eventnum = TD_CATCHSIG;
900Sstevel@tonic-gate 		self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig;
910Sstevel@tonic-gate 		tdb_event(TD_CATCHSIG, udp);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/*
950Sstevel@tonic-gate 	 * Get a self-consistent set of flags, handler, and mask
960Sstevel@tonic-gate 	 * while holding the sig's sig_lock for the least possible time.
970Sstevel@tonic-gate 	 * We must acquire the sig's sig_lock because some thread running
980Sstevel@tonic-gate 	 * in sigaction() might be establishing a new signal handler.
994570Sraf 	 * The code in sigaction() acquires the writer lock; here
1004570Sraf 	 * we acquire the readers lock to ehance concurrency in the
1014570Sraf 	 * face of heavy signal traffic, such as generated by java.
1020Sstevel@tonic-gate 	 *
1030Sstevel@tonic-gate 	 * Locking exceptions:
1040Sstevel@tonic-gate 	 * No locking for a child of vfork().
1050Sstevel@tonic-gate 	 * If the signal is SIGPROF with an si_code of PROF_SIG,
1060Sstevel@tonic-gate 	 * then we assume that this signal was generated by
1070Sstevel@tonic-gate 	 * setitimer(ITIMER_REALPROF) set up by the dbx collector.
1080Sstevel@tonic-gate 	 * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
1090Sstevel@tonic-gate 	 * then we assume that the signal was generated by
1100Sstevel@tonic-gate 	 * a hardware performance counter overflow.
1110Sstevel@tonic-gate 	 * In these cases, assume that we need no locking.  It is the
1120Sstevel@tonic-gate 	 * monitoring program's responsibility to ensure correctness.
1130Sstevel@tonic-gate 	 */
1140Sstevel@tonic-gate 	sap = &udp->siguaction[sig].sig_uaction;
1150Sstevel@tonic-gate 	if (self->ul_vfork ||
1160Sstevel@tonic-gate 	    (sip != NULL &&
1170Sstevel@tonic-gate 	    ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
1180Sstevel@tonic-gate 	    (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
1190Sstevel@tonic-gate 		/* we wish this assignment could be atomic */
1206515Sraf 		(void) memcpy(&uact, (void *)sap, sizeof (uact));
1210Sstevel@tonic-gate 	} else {
1224570Sraf 		rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
1234570Sraf 		lrw_rdlock(rwlp);
1246515Sraf 		(void) memcpy(&uact, (void *)sap, sizeof (uact));
1255891Sraf 		if ((sig == SIGCANCEL || sig == SIGAIOCANCEL) &&
1265891Sraf 		    (sap->sa_flags & SA_RESETHAND))
1270Sstevel@tonic-gate 			sap->sa_sigaction = SIG_DFL;
1284570Sraf 		lrw_unlock(rwlp);
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	/*
1320Sstevel@tonic-gate 	 * Set the proper signal mask and call the user's signal handler.
1330Sstevel@tonic-gate 	 * (We overrode the user-requested signal mask with maskset
1340Sstevel@tonic-gate 	 * so we currently have all blockable signals blocked.)
1350Sstevel@tonic-gate 	 *
1360Sstevel@tonic-gate 	 * We would like to ASSERT() that the signal is not a member of the
1370Sstevel@tonic-gate 	 * signal mask at the previous level (ucp->uc_sigmask) or the specified
1380Sstevel@tonic-gate 	 * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
1390Sstevel@tonic-gate 	 * /proc can override this via PCSSIG, so we don't bother.
1400Sstevel@tonic-gate 	 *
1410Sstevel@tonic-gate 	 * We would also like to ASSERT() that the signal mask at the previous
1420Sstevel@tonic-gate 	 * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
1430Sstevel@tonic-gate 	 * but /proc can change the thread's signal mask via PCSHOLD, so we
1440Sstevel@tonic-gate 	 * don't bother with that either.
1450Sstevel@tonic-gate 	 */
1460Sstevel@tonic-gate 	ASSERT(ucp->uc_flags & UC_SIGMASK);
1470Sstevel@tonic-gate 	if (self->ul_sigsuspend) {
1480Sstevel@tonic-gate 		ucp->uc_sigmask = self->ul_sigmask;
1490Sstevel@tonic-gate 		self->ul_sigsuspend = 0;
1500Sstevel@tonic-gate 		/* the sigsuspend() or pollsys() signal mask */
1510Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &self->ul_tmpmask);
1520Sstevel@tonic-gate 	} else {
1530Sstevel@tonic-gate 		/* the signal mask at the previous level */
1540Sstevel@tonic-gate 		sigorset(&uact.sa_mask, &ucp->uc_sigmask);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_NODEFER))	/* add current signal */
1576515Sraf 		(void) sigaddset(&uact.sa_mask, sig);
1580Sstevel@tonic-gate 	self->ul_sigmask = uact.sa_mask;
1590Sstevel@tonic-gate 	self->ul_siglink = ucp;
160*11913SRoger.Faulkner@Sun.COM 	(void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/*
1630Sstevel@tonic-gate 	 * If this thread has been sent SIGCANCEL from the kernel
1640Sstevel@tonic-gate 	 * or from pthread_cancel(), it is being asked to exit.
1650Sstevel@tonic-gate 	 * The kernel may send SIGCANCEL without a siginfo struct.
1660Sstevel@tonic-gate 	 * If the SIGCANCEL is process-directed (from kill() or
1670Sstevel@tonic-gate 	 * sigqueue()), treat it as an ordinary signal.
1680Sstevel@tonic-gate 	 */
1690Sstevel@tonic-gate 	if (sig == SIGCANCEL) {
1700Sstevel@tonic-gate 		if (sip == NULL || SI_FROMKERNEL(sip) ||
1710Sstevel@tonic-gate 		    sip->si_code == SI_LWP) {
1720Sstevel@tonic-gate 			do_sigcancel();
1730Sstevel@tonic-gate 			goto out;
1740Sstevel@tonic-gate 		}
1752248Sraf 		/* SIGCANCEL is ignored by default */
1762248Sraf 		if (uact.sa_sigaction == SIG_DFL ||
1772248Sraf 		    uact.sa_sigaction == SIG_IGN)
1782248Sraf 			goto out;
1792248Sraf 	}
1802248Sraf 
1812248Sraf 	/*
1822248Sraf 	 * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
1832248Sraf 	 * we are an aio worker thread, cancel the aio request.
1842248Sraf 	 */
1852248Sraf 	if (sig == SIGAIOCANCEL) {
1866812Sraf 		aio_worker_t *aiowp = pthread_getspecific(_aio_key);
1872248Sraf 
1882248Sraf 		if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
1896812Sraf 			siglongjmp(aiowp->work_jmp_buf, 1);
1902248Sraf 		/* SIGLWP is ignored by default */
1910Sstevel@tonic-gate 		if (uact.sa_sigaction == SIG_DFL ||
1920Sstevel@tonic-gate 		    uact.sa_sigaction == SIG_IGN)
1930Sstevel@tonic-gate 			goto out;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if (!(uact.sa_flags & SA_SIGINFO))
1970Sstevel@tonic-gate 		sip = NULL;
1980Sstevel@tonic-gate 	__sighndlr(sig, sip, ucp, uact.sa_sigaction);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate #if defined(sparc) || defined(__sparc)
2010Sstevel@tonic-gate 	/*
2020Sstevel@tonic-gate 	 * If this is a floating point exception and the queue
2030Sstevel@tonic-gate 	 * is non-empty, pop the top entry from the queue.  This
2040Sstevel@tonic-gate 	 * is to maintain expected behavior.
2050Sstevel@tonic-gate 	 */
2060Sstevel@tonic-gate 	if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
2070Sstevel@tonic-gate 		fpregset_t *fp = &ucp->uc_mcontext.fpregs;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		if (--fp->fpu_qcnt > 0) {
2100Sstevel@tonic-gate 			unsigned char i;
2110Sstevel@tonic-gate 			struct fq *fqp;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 			fqp = fp->fpu_q;
2140Sstevel@tonic-gate 			for (i = 0; i < fp->fpu_qcnt; i++)
2150Sstevel@tonic-gate 				fqp[i] = fqp[i+1];
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate #endif	/* sparc */
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate out:
2216812Sraf 	(void) setcontext(ucp);
2226812Sraf 	thr_panic("call_user_handler(): setcontext() returned");
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * take_deferred_signal() is called when ul_critical and ul_sigdefer become
2270Sstevel@tonic-gate  * zero and a deferred signal has been recorded on the current thread.
2280Sstevel@tonic-gate  * We are out of the critical region and are ready to take a signal.
2290Sstevel@tonic-gate  * The kernel has all signals blocked on this lwp, but our value of
2300Sstevel@tonic-gate  * ul_sigmask is the correct signal mask for the previous context.
2314806Sraf  *
2324806Sraf  * We call __sigresend() to atomically restore the signal mask and
2334806Sraf  * cause the signal to be sent again with the remembered siginfo.
2344806Sraf  * We will not return successfully from __sigresend() until the
2354806Sraf  * application's signal handler has been run via sigacthandler().
2360Sstevel@tonic-gate  */
2370Sstevel@tonic-gate void
take_deferred_signal(int sig)2380Sstevel@tonic-gate take_deferred_signal(int sig)
2390Sstevel@tonic-gate {
2404806Sraf 	extern int __sigresend(int, siginfo_t *, sigset_t *);
2410Sstevel@tonic-gate 	ulwp_t *self = curthread;
2425010Sraf 	siguaction_t *suap = &self->ul_uberdata->siguaction[sig];
2430Sstevel@tonic-gate 	siginfo_t *sip;
2444806Sraf 	int error;
2450Sstevel@tonic-gate 
2464806Sraf 	ASSERT((self->ul_critical | self->ul_sigdefer | self->ul_cursig) == 0);
2474806Sraf 
2485010Sraf 	/*
2495010Sraf 	 * If the signal handler was established with SA_RESETHAND,
2505010Sraf 	 * the kernel has reset the handler to SIG_DFL, so we have
2515010Sraf 	 * to reestablish the handler now so that it will be entered
2525010Sraf 	 * again when we call __sigresend(), below.
2535837Sraf 	 *
2545837Sraf 	 * Logically, we should acquire and release the signal's
2555837Sraf 	 * sig_lock around this operation to protect the integrity
2565837Sraf 	 * of the signal action while we copy it, as is done below
2575837Sraf 	 * in _libc_sigaction().  However, we may be on a user-level
2585837Sraf 	 * sleep queue at this point and lrw_wrlock(&suap->sig_lock)
2595837Sraf 	 * might attempt to sleep on a different sleep queue and
2605837Sraf 	 * that would corrupt the entire sleep queue mechanism.
2615837Sraf 	 *
2625837Sraf 	 * If we are on a sleep queue we will remove ourself from
2635837Sraf 	 * it in call_user_handler(), called from sigacthandler(),
2645837Sraf 	 * before entering the application's signal handler.
2655837Sraf 	 * In the meantime, we must not acquire any locks.
2665010Sraf 	 */
2675010Sraf 	if (suap->sig_uaction.sa_flags & SA_RESETHAND) {
2685010Sraf 		struct sigaction tact = suap->sig_uaction;
2695010Sraf 		tact.sa_flags &= ~SA_NODEFER;
2705010Sraf 		tact.sa_sigaction = self->ul_uberdata->sigacthandler;
2715010Sraf 		tact.sa_mask = maskset;
2725010Sraf 		(void) __sigaction(sig, &tact, NULL);
2735010Sraf 	}
2745010Sraf 
2750Sstevel@tonic-gate 	if (self->ul_siginfo.si_signo == 0)
2760Sstevel@tonic-gate 		sip = NULL;
2774806Sraf 	else
2784806Sraf 		sip = &self->ul_siginfo;
2794806Sraf 
2804806Sraf 	/* EAGAIN can happen only for a pending SIGSTOP signal */
2814806Sraf 	while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN)
2824806Sraf 		continue;
2834806Sraf 	if (error)
2844806Sraf 		thr_panic("take_deferred_signal(): __sigresend() failed");
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate void
sigacthandler(int sig,siginfo_t * sip,void * uvp)2880Sstevel@tonic-gate sigacthandler(int sig, siginfo_t *sip, void *uvp)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	ucontext_t *ucp = uvp;
2910Sstevel@tonic-gate 	ulwp_t *self = curthread;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * Do this in case we took a signal while in a cancelable system call.
2950Sstevel@tonic-gate 	 * It does no harm if we were not in such a system call.
2960Sstevel@tonic-gate 	 */
2970Sstevel@tonic-gate 	self->ul_sp = 0;
2980Sstevel@tonic-gate 	if (sig != SIGCANCEL)
2990Sstevel@tonic-gate 		self->ul_cancel_async = self->ul_save_async;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	/*
30211426SRoger.Faulkner@Sun.COM 	 * If this thread has performed a longjmp() from a signal handler
30311426SRoger.Faulkner@Sun.COM 	 * back to main level some time in the past, it has left the kernel
30411426SRoger.Faulkner@Sun.COM 	 * thinking that it is still in the signal context.  We repair this
30511426SRoger.Faulkner@Sun.COM 	 * possible damage by setting ucp->uc_link to NULL if we know that
30611426SRoger.Faulkner@Sun.COM 	 * we are actually executing at main level (self->ul_siglink == NULL).
30711426SRoger.Faulkner@Sun.COM 	 * See the code for setjmp()/longjmp() for more details.
30811426SRoger.Faulkner@Sun.COM 	 */
30911426SRoger.Faulkner@Sun.COM 	if (self->ul_siglink == NULL)
31011426SRoger.Faulkner@Sun.COM 		ucp->uc_link = NULL;
31111426SRoger.Faulkner@Sun.COM 
31211426SRoger.Faulkner@Sun.COM 	/*
3130Sstevel@tonic-gate 	 * If we are not in a critical region and are
3140Sstevel@tonic-gate 	 * not deferring signals, take the signal now.
3150Sstevel@tonic-gate 	 */
3160Sstevel@tonic-gate 	if ((self->ul_critical + self->ul_sigdefer) == 0) {
3170Sstevel@tonic-gate 		call_user_handler(sig, sip, ucp);
3185961Srh87107 		/*
3195961Srh87107 		 * On the surface, the following call seems redundant
3205961Srh87107 		 * because call_user_handler() cannot return. However,
3215961Srh87107 		 * we don't want to return from here because the compiler
3225961Srh87107 		 * might recycle our frame. We want to keep it on the
3235961Srh87107 		 * stack to assist debuggers such as pstack in identifying
3245961Srh87107 		 * signal frames. The call to thr_panic() serves to prevent
3255961Srh87107 		 * tail-call optimisation here.
3265961Srh87107 		 */
3275961Srh87107 		thr_panic("sigacthandler(): call_user_handler() returned");
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * We are in a critical region or we are deferring signals.  When
3320Sstevel@tonic-gate 	 * we emerge from the region we will call take_deferred_signal().
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	ASSERT(self->ul_cursig == 0);
3350Sstevel@tonic-gate 	self->ul_cursig = (char)sig;
3360Sstevel@tonic-gate 	if (sip != NULL)
3376515Sraf 		(void) memcpy(&self->ul_siginfo,
3381111Sraf 		    sip, sizeof (siginfo_t));
3390Sstevel@tonic-gate 	else
3400Sstevel@tonic-gate 		self->ul_siginfo.si_signo = 0;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	/*
3430Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
3440Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
3450Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	set_parking_flag(self, 0);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	/*
3500Sstevel@tonic-gate 	 * Return to the previous context with all signals blocked.
3510Sstevel@tonic-gate 	 * We will restore the signal mask in take_deferred_signal().
3520Sstevel@tonic-gate 	 * Note that we are calling the system call trap here, not
3536812Sraf 	 * the setcontext() wrapper.  We don't want to change the
3540Sstevel@tonic-gate 	 * thread's ul_sigmask by this operation.
3550Sstevel@tonic-gate 	 */
3560Sstevel@tonic-gate 	ucp->uc_sigmask = maskset;
3576515Sraf 	(void) __setcontext(ucp);
3580Sstevel@tonic-gate 	thr_panic("sigacthandler(): __setcontext() returned");
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3616812Sraf #pragma weak _sigaction = sigaction
3620Sstevel@tonic-gate int
sigaction(int sig,const struct sigaction * nact,struct sigaction * oact)3636812Sraf sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate 	ulwp_t *self = curthread;
3660Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
3670Sstevel@tonic-gate 	struct sigaction oaction;
3680Sstevel@tonic-gate 	struct sigaction tact;
3690Sstevel@tonic-gate 	struct sigaction *tactp = NULL;
3700Sstevel@tonic-gate 	int rv;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	if (sig <= 0 || sig >= NSIG) {
3730Sstevel@tonic-gate 		errno = EINVAL;
3740Sstevel@tonic-gate 		return (-1);
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	if (!self->ul_vfork)
3784570Sraf 		lrw_wrlock(&udp->siguaction[sig].sig_lock);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	oaction = udp->siguaction[sig].sig_uaction;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (nact != NULL) {
3830Sstevel@tonic-gate 		tact = *nact;	/* make a copy so we can modify it */
3840Sstevel@tonic-gate 		tactp = &tact;
3850Sstevel@tonic-gate 		delete_reserved_signals(&tact.sa_mask);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate #if !defined(_LP64)
3880Sstevel@tonic-gate 		tact.sa_resv[0] = tact.sa_resv[1] = 0;	/* cleanliness */
3890Sstevel@tonic-gate #endif
3900Sstevel@tonic-gate 		/*
3910Sstevel@tonic-gate 		 * To be compatible with the behavior of SunOS 4.x:
3920Sstevel@tonic-gate 		 * If the new signal handler is SIG_IGN or SIG_DFL, do
3930Sstevel@tonic-gate 		 * not change the signal's entry in the siguaction array.
3940Sstevel@tonic-gate 		 * This allows a child of vfork(2) to set signal handlers
3950Sstevel@tonic-gate 		 * to SIG_IGN or SIG_DFL without affecting the parent.
3960Sstevel@tonic-gate 		 *
3970Sstevel@tonic-gate 		 * This also covers a race condition with some thread
3980Sstevel@tonic-gate 		 * setting the signal action to SIG_DFL or SIG_IGN
3990Sstevel@tonic-gate 		 * when the thread has also received and deferred
4000Sstevel@tonic-gate 		 * that signal.  When the thread takes the deferred
4010Sstevel@tonic-gate 		 * signal, even though it has set the action to SIG_DFL
4020Sstevel@tonic-gate 		 * or SIG_IGN, it will execute the old signal handler
4030Sstevel@tonic-gate 		 * anyway.  This is an inherent signaling race condition
4040Sstevel@tonic-gate 		 * and is not a bug.
4050Sstevel@tonic-gate 		 *
4060Sstevel@tonic-gate 		 * A child of vfork() is not allowed to change signal
4070Sstevel@tonic-gate 		 * handlers to anything other than SIG_DFL or SIG_IGN.
4080Sstevel@tonic-gate 		 */
4090Sstevel@tonic-gate 		if (self->ul_vfork) {
4100Sstevel@tonic-gate 			if (tact.sa_sigaction != SIG_IGN)
4110Sstevel@tonic-gate 				tact.sa_sigaction = SIG_DFL;
4122248Sraf 		} else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
4130Sstevel@tonic-gate 			/*
4142248Sraf 			 * Always catch these signals.
4152248Sraf 			 * We need SIGCANCEL for pthread_cancel() to work.
4162248Sraf 			 * We need SIGAIOCANCEL for aio_cancel() to work.
4170Sstevel@tonic-gate 			 */
4180Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
4190Sstevel@tonic-gate 			if (tact.sa_sigaction == SIG_DFL ||
4200Sstevel@tonic-gate 			    tact.sa_sigaction == SIG_IGN)
4210Sstevel@tonic-gate 				tact.sa_flags = SA_SIGINFO;
4220Sstevel@tonic-gate 			else {
4230Sstevel@tonic-gate 				tact.sa_flags |= SA_SIGINFO;
4245891Sraf 				tact.sa_flags &=
4255891Sraf 				    ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
4260Sstevel@tonic-gate 			}
4270Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
4280Sstevel@tonic-gate 			tact.sa_mask = maskset;
4290Sstevel@tonic-gate 		} else if (tact.sa_sigaction != SIG_DFL &&
4300Sstevel@tonic-gate 		    tact.sa_sigaction != SIG_IGN) {
4310Sstevel@tonic-gate 			udp->siguaction[sig].sig_uaction = tact;
4320Sstevel@tonic-gate 			tact.sa_flags &= ~SA_NODEFER;
4330Sstevel@tonic-gate 			tact.sa_sigaction = udp->sigacthandler;
4340Sstevel@tonic-gate 			tact.sa_mask = maskset;
4350Sstevel@tonic-gate 		}
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	if ((rv = __sigaction(sig, tactp, oact)) != 0)
4390Sstevel@tonic-gate 		udp->siguaction[sig].sig_uaction = oaction;
4400Sstevel@tonic-gate 	else if (oact != NULL &&
4410Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_DFL &&
4420Sstevel@tonic-gate 	    oact->sa_sigaction != SIG_IGN)
4430Sstevel@tonic-gate 		*oact = oaction;
4440Sstevel@tonic-gate 
4452248Sraf 	/*
4462248Sraf 	 * We detect setting the disposition of SIGIO just to set the
4472248Sraf 	 * _sigio_enabled flag for the asynchronous i/o (aio) code.
4482248Sraf 	 */
4492248Sraf 	if (sig == SIGIO && rv == 0 && tactp != NULL) {
4502248Sraf 		_sigio_enabled =
4512248Sraf 		    (tactp->sa_handler != SIG_DFL &&
4522248Sraf 		    tactp->sa_handler != SIG_IGN);
4532248Sraf 	}
4542248Sraf 
4550Sstevel@tonic-gate 	if (!self->ul_vfork)
4564570Sraf 		lrw_unlock(&udp->siguaction[sig].sig_lock);
4570Sstevel@tonic-gate 	return (rv);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4606515Sraf /*
4616515Sraf  * This is a private interface for the linux brand interface.
4626515Sraf  */
4632712Snn35248 void
setsigacthandler(void (* nsigacthandler)(int,siginfo_t *,void *),void (** osigacthandler)(int,siginfo_t *,void *))4642712Snn35248 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
4652712Snn35248     void (**osigacthandler)(int, siginfo_t *, void *))
4662712Snn35248 {
4672712Snn35248 	ulwp_t *self = curthread;
4682712Snn35248 	uberdata_t *udp = self->ul_uberdata;
4692712Snn35248 
4702712Snn35248 	if (osigacthandler != NULL)
4712712Snn35248 		*osigacthandler = udp->sigacthandler;
4722712Snn35248 
4732712Snn35248 	udp->sigacthandler = nsigacthandler;
4742712Snn35248 }
4752712Snn35248 
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate  * Tell the kernel to block all signals.
4780Sstevel@tonic-gate  * Use the schedctl interface, or failing that, use __lwp_sigmask().
4790Sstevel@tonic-gate  * This action can be rescinded only by making a system call that
4800Sstevel@tonic-gate  * sets the signal mask:
4810Sstevel@tonic-gate  *	__lwp_sigmask(), __sigprocmask(), __setcontext(),
4820Sstevel@tonic-gate  *	__sigsuspend() or __pollsys().
4830Sstevel@tonic-gate  * In particular, this action cannot be reversed by assigning
4840Sstevel@tonic-gate  * scp->sc_sigblock = 0.  That would be a way to lose signals.
4850Sstevel@tonic-gate  * See the definition of restore_signals(self).
4860Sstevel@tonic-gate  */
4870Sstevel@tonic-gate void
block_all_signals(ulwp_t * self)4880Sstevel@tonic-gate block_all_signals(ulwp_t *self)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	volatile sc_shared_t *scp;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	enter_critical(self);
4930Sstevel@tonic-gate 	if ((scp = self->ul_schedctl) != NULL ||
4940Sstevel@tonic-gate 	    (scp = setup_schedctl()) != NULL)
4950Sstevel@tonic-gate 		scp->sc_sigblock = 1;
4960Sstevel@tonic-gate 	else
497*11913SRoger.Faulkner@Sun.COM 		(void) __lwp_sigmask(SIG_SETMASK, &maskset);
4980Sstevel@tonic-gate 	exit_critical(self);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5012712Snn35248 /*
5026515Sraf  * setcontext() has code that forcibly restores the curthread
5032712Snn35248  * pointer in a context passed to the setcontext(2) syscall.
5042712Snn35248  *
5052712Snn35248  * Certain processes may need to disable this feature, so these routines
5062712Snn35248  * provide the mechanism to do so.
5072712Snn35248  *
5082712Snn35248  * (As an example, branded 32-bit x86 processes may use %gs for their own
5092712Snn35248  * purposes, so they need to be able to specify a %gs value to be restored
5102712Snn35248  * on return from a signal handler via the passed ucontext_t.)
5112712Snn35248  */
5122712Snn35248 static int setcontext_enforcement = 1;
5132712Snn35248 
5142712Snn35248 void
set_setcontext_enforcement(int on)5152712Snn35248 set_setcontext_enforcement(int on)
5162712Snn35248 {
5172712Snn35248 	setcontext_enforcement = on;
5182712Snn35248 }
5192712Snn35248 
5206812Sraf #pragma weak _setcontext = setcontext
5210Sstevel@tonic-gate int
setcontext(const ucontext_t * ucp)5226812Sraf setcontext(const ucontext_t *ucp)
5230Sstevel@tonic-gate {
5240Sstevel@tonic-gate 	ulwp_t *self = curthread;
5250Sstevel@tonic-gate 	int ret;
5260Sstevel@tonic-gate 	ucontext_t uc;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	/*
5290Sstevel@tonic-gate 	 * Returning from the main context (uc_link == NULL) causes
5300Sstevel@tonic-gate 	 * the thread to exit.  See setcontext(2) and makecontext(3C).
5310Sstevel@tonic-gate 	 */
5320Sstevel@tonic-gate 	if (ucp == NULL)
5336812Sraf 		thr_exit(NULL);
5346515Sraf 	(void) memcpy(&uc, ucp, sizeof (uc));
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	/*
5370Sstevel@tonic-gate 	 * Restore previous signal mask and context link.
5380Sstevel@tonic-gate 	 */
5390Sstevel@tonic-gate 	if (uc.uc_flags & UC_SIGMASK) {
5400Sstevel@tonic-gate 		block_all_signals(self);
5410Sstevel@tonic-gate 		delete_reserved_signals(&uc.uc_sigmask);
5420Sstevel@tonic-gate 		self->ul_sigmask = uc.uc_sigmask;
5430Sstevel@tonic-gate 		if (self->ul_cursig) {
5440Sstevel@tonic-gate 			/*
5450Sstevel@tonic-gate 			 * We have a deferred signal present.
5460Sstevel@tonic-gate 			 * The signal mask will be set when the
5470Sstevel@tonic-gate 			 * signal is taken in take_deferred_signal().
5480Sstevel@tonic-gate 			 */
5490Sstevel@tonic-gate 			ASSERT(self->ul_critical + self->ul_sigdefer != 0);
5500Sstevel@tonic-gate 			uc.uc_flags &= ~UC_SIGMASK;
5510Sstevel@tonic-gate 		}
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate 	self->ul_siglink = uc.uc_link;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	/*
5560Sstevel@tonic-gate 	 * We don't know where this context structure has been.
5570Sstevel@tonic-gate 	 * Preserve the curthread pointer, at least.
5582712Snn35248 	 *
5592712Snn35248 	 * Allow this feature to be disabled if a particular process
5602712Snn35248 	 * requests it.
5610Sstevel@tonic-gate 	 */
5622712Snn35248 	if (setcontext_enforcement) {
5630Sstevel@tonic-gate #if defined(__sparc)
5642712Snn35248 		uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
5650Sstevel@tonic-gate #elif defined(__amd64)
5663446Smrj 		uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */
5670Sstevel@tonic-gate #elif defined(__i386)
5683446Smrj 		uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL;
5690Sstevel@tonic-gate #else
5700Sstevel@tonic-gate #error "none of __sparc, __amd64, __i386 defined"
5710Sstevel@tonic-gate #endif
5722712Snn35248 	}
5732712Snn35248 
5740Sstevel@tonic-gate 	/*
5750Sstevel@tonic-gate 	 * Make sure that if we return to a call to __lwp_park()
5760Sstevel@tonic-gate 	 * or ___lwp_cond_wait() that it returns right away
5770Sstevel@tonic-gate 	 * (giving us a spurious wakeup but not a deadlock).
5780Sstevel@tonic-gate 	 */
5790Sstevel@tonic-gate 	set_parking_flag(self, 0);
5800Sstevel@tonic-gate 	self->ul_sp = 0;
5816515Sraf 	ret = __setcontext(&uc);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/*
5840Sstevel@tonic-gate 	 * It is OK for setcontext() to return if the user has not specified
5850Sstevel@tonic-gate 	 * UC_CPU.
5860Sstevel@tonic-gate 	 */
5870Sstevel@tonic-gate 	if (uc.uc_flags & UC_CPU)
5880Sstevel@tonic-gate 		thr_panic("setcontext(): __setcontext() returned");
5890Sstevel@tonic-gate 	return (ret);
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate 
5926812Sraf #pragma weak _thr_sigsetmask = thr_sigsetmask
5930Sstevel@tonic-gate int
thr_sigsetmask(int how,const sigset_t * set,sigset_t * oset)5946812Sraf thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	ulwp_t *self = curthread;
5970Sstevel@tonic-gate 	sigset_t saveset;
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	if (set == NULL) {
6000Sstevel@tonic-gate 		enter_critical(self);
6010Sstevel@tonic-gate 		if (oset != NULL)
6020Sstevel@tonic-gate 			*oset = self->ul_sigmask;
6030Sstevel@tonic-gate 		exit_critical(self);
6040Sstevel@tonic-gate 	} else {
6050Sstevel@tonic-gate 		switch (how) {
6060Sstevel@tonic-gate 		case SIG_BLOCK:
6070Sstevel@tonic-gate 		case SIG_UNBLOCK:
6080Sstevel@tonic-gate 		case SIG_SETMASK:
6090Sstevel@tonic-gate 			break;
6100Sstevel@tonic-gate 		default:
6110Sstevel@tonic-gate 			return (EINVAL);
6120Sstevel@tonic-gate 		}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 		/*
6150Sstevel@tonic-gate 		 * The assignments to self->ul_sigmask must be protected from
6160Sstevel@tonic-gate 		 * signals.  The nuances of this code are subtle.  Be careful.
6170Sstevel@tonic-gate 		 */
6180Sstevel@tonic-gate 		block_all_signals(self);
6190Sstevel@tonic-gate 		if (oset != NULL)
6200Sstevel@tonic-gate 			saveset = self->ul_sigmask;
6210Sstevel@tonic-gate 		switch (how) {
6220Sstevel@tonic-gate 		case SIG_BLOCK:
6230Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
6240Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
625*11913SRoger.Faulkner@Sun.COM 			self->ul_sigmask.__sigbits[2] |= set->__sigbits[2];
626*11913SRoger.Faulkner@Sun.COM 			self->ul_sigmask.__sigbits[3] |= set->__sigbits[3];
6270Sstevel@tonic-gate 			break;
6280Sstevel@tonic-gate 		case SIG_UNBLOCK:
6290Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
6300Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
631*11913SRoger.Faulkner@Sun.COM 			self->ul_sigmask.__sigbits[2] &= ~set->__sigbits[2];
632*11913SRoger.Faulkner@Sun.COM 			self->ul_sigmask.__sigbits[3] &= ~set->__sigbits[3];
6330Sstevel@tonic-gate 			break;
6340Sstevel@tonic-gate 		case SIG_SETMASK:
6350Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
6360Sstevel@tonic-gate 			self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
637*11913SRoger.Faulkner@Sun.COM 			self->ul_sigmask.__sigbits[2] = set->__sigbits[2];
638*11913SRoger.Faulkner@Sun.COM 			self->ul_sigmask.__sigbits[3] = set->__sigbits[3];
6390Sstevel@tonic-gate 			break;
6400Sstevel@tonic-gate 		}
6410Sstevel@tonic-gate 		delete_reserved_signals(&self->ul_sigmask);
6420Sstevel@tonic-gate 		if (oset != NULL)
6430Sstevel@tonic-gate 			*oset = saveset;
6440Sstevel@tonic-gate 		restore_signals(self);
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	return (0);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6506812Sraf #pragma weak _pthread_sigmask = pthread_sigmask
6510Sstevel@tonic-gate int
pthread_sigmask(int how,const sigset_t * set,sigset_t * oset)6526812Sraf pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
6536812Sraf {
6546812Sraf 	return (thr_sigsetmask(how, set, oset));
6556812Sraf }
6566812Sraf 
6576812Sraf #pragma weak _sigprocmask = sigprocmask
6586812Sraf int
sigprocmask(int how,const sigset_t * set,sigset_t * oset)6596812Sraf sigprocmask(int how, const sigset_t *set, sigset_t *oset)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	int error;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	/*
6640Sstevel@tonic-gate 	 * Guard against children of vfork().
6650Sstevel@tonic-gate 	 */
6660Sstevel@tonic-gate 	if (curthread->ul_vfork)
667*11913SRoger.Faulkner@Sun.COM 		return (__sigprocmask(how, set, oset));
6680Sstevel@tonic-gate 
6696812Sraf 	if ((error = thr_sigsetmask(how, set, oset)) != 0) {
6700Sstevel@tonic-gate 		errno = error;
6710Sstevel@tonic-gate 		return (-1);
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	return (0);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate /*
6780Sstevel@tonic-gate  * Called at library initialization to set up signal handling.
6794570Sraf  * All we really do is initialize the sig_lock rwlocks.
6800Sstevel@tonic-gate  * All signal handlers are either SIG_DFL or SIG_IGN on exec().
6810Sstevel@tonic-gate  * However, if any signal handlers were established on alternate
6820Sstevel@tonic-gate  * link maps before the primary link map has been initialized,
6830Sstevel@tonic-gate  * then inform the kernel of the new sigacthandler.
6840Sstevel@tonic-gate  */
6850Sstevel@tonic-gate void
signal_init()6860Sstevel@tonic-gate signal_init()
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
6890Sstevel@tonic-gate 	struct sigaction *sap;
6900Sstevel@tonic-gate 	struct sigaction act;
6914570Sraf 	rwlock_t *rwlp;
6920Sstevel@tonic-gate 	int sig;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	for (sig = 0; sig < NSIG; sig++) {
6954570Sraf 		rwlp = &udp->siguaction[sig].sig_lock;
6964570Sraf 		rwlp->rwlock_magic = RWL_MAGIC;
6974570Sraf 		rwlp->mutex.mutex_flag = LOCK_INITED;
6984570Sraf 		rwlp->mutex.mutex_magic = MUTEX_MAGIC;
6990Sstevel@tonic-gate 		sap = &udp->siguaction[sig].sig_uaction;
7000Sstevel@tonic-gate 		if (sap->sa_sigaction != SIG_DFL &&
7010Sstevel@tonic-gate 		    sap->sa_sigaction != SIG_IGN &&
7020Sstevel@tonic-gate 		    __sigaction(sig, NULL, &act) == 0 &&
7030Sstevel@tonic-gate 		    act.sa_sigaction != SIG_DFL &&
7040Sstevel@tonic-gate 		    act.sa_sigaction != SIG_IGN) {
7050Sstevel@tonic-gate 			act = *sap;
7060Sstevel@tonic-gate 			act.sa_flags &= ~SA_NODEFER;
7070Sstevel@tonic-gate 			act.sa_sigaction = udp->sigacthandler;
7080Sstevel@tonic-gate 			act.sa_mask = maskset;
7090Sstevel@tonic-gate 			(void) __sigaction(sig, &act, NULL);
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate /*
7150Sstevel@tonic-gate  * Common code for cancelling self in _sigcancel() and pthread_cancel().
7165891Sraf  * First record the fact that a cancellation is pending.
7175891Sraf  * Then, if cancellation is disabled or if we are holding unprotected
7185891Sraf  * libc locks, just return to defer the cancellation.
7195891Sraf  * Then, if we are at a cancellation point (ul_cancelable) just
7205891Sraf  * return and let _canceloff() do the exit.
7215891Sraf  * Else exit immediately if async mode is in effect.
7220Sstevel@tonic-gate  */
7230Sstevel@tonic-gate void
do_sigcancel(void)7245891Sraf do_sigcancel(void)
7250Sstevel@tonic-gate {
7260Sstevel@tonic-gate 	ulwp_t *self = curthread;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
7290Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer == 0);
7300Sstevel@tonic-gate 	self->ul_cancel_pending = 1;
7310Sstevel@tonic-gate 	if (self->ul_cancel_async &&
7320Sstevel@tonic-gate 	    !self->ul_cancel_disabled &&
7335891Sraf 	    self->ul_libc_locks == 0 &&
7340Sstevel@tonic-gate 	    !self->ul_cancelable)
7356812Sraf 		pthread_exit(PTHREAD_CANCELED);
7365891Sraf 	set_cancel_pending_flag(self, 0);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate /*
7402248Sraf  * Set up the SIGCANCEL handler for threads cancellation,
7412248Sraf  * needed only when we have more than one thread,
7422248Sraf  * or the SIGAIOCANCEL handler for aio cancellation,
7432248Sraf  * called when aio is initialized, in __uaio_init().
7440Sstevel@tonic-gate  */
7450Sstevel@tonic-gate void
setup_cancelsig(int sig)7462248Sraf setup_cancelsig(int sig)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
7494570Sraf 	rwlock_t *rwlp = &udp->siguaction[sig].sig_lock;
7500Sstevel@tonic-gate 	struct sigaction act;
7510Sstevel@tonic-gate 
7522248Sraf 	ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
7534570Sraf 	lrw_rdlock(rwlp);
7542248Sraf 	act = udp->siguaction[sig].sig_uaction;
7554570Sraf 	lrw_unlock(rwlp);
7560Sstevel@tonic-gate 	if (act.sa_sigaction == SIG_DFL ||
7570Sstevel@tonic-gate 	    act.sa_sigaction == SIG_IGN)
7580Sstevel@tonic-gate 		act.sa_flags = SA_SIGINFO;
7590Sstevel@tonic-gate 	else {
7600Sstevel@tonic-gate 		act.sa_flags |= SA_SIGINFO;
7615891Sraf 		act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND | SA_RESTART);
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 	act.sa_sigaction = udp->sigacthandler;
7640Sstevel@tonic-gate 	act.sa_mask = maskset;
7652248Sraf 	(void) __sigaction(sig, &act, NULL);
7660Sstevel@tonic-gate }
767