sigaction.c revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c
3853N/A/*
3853N/A * CDDL HEADER START
3853N/A *
3853N/A * The contents of this file are subject to the terms of the
3853N/A * Common Development and Distribution License (the "License").
3853N/A * You may not use this file except in compliance with the License.
3853N/A *
3853N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
3853N/A * or http://www.opensolaris.org/os/licensing.
3853N/A * See the License for the specific language governing permissions
3853N/A * and limitations under the License.
3853N/A *
3853N/A * When distributing Covered Code, include this CDDL HEADER in each
3853N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
3853N/A * If applicable, add the following below this CDDL HEADER, with the
3853N/A * fields enclosed by brackets "[]" replaced with your own identifying
3853N/A * information: Portions Copyright [yyyy] [name of copyright owner]
3853N/A *
3853N/A * CDDL HEADER END
3853N/A */
3853N/A
3853N/A/*
3853N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3853N/A * Use is subject to license terms.
4107N/A */
3853N/A
3853N/A#pragma ident "%Z%%M% %I% %E% SMI"
3853N/A
3853N/A#include "lint.h"
3853N/A#include "thr_uberdata.h"
3853N/A#include "asyncio.h"
3853N/A#include <signal.h>
3853N/A#include <siginfo.h>
4107N/A#include <ucontext.h>
4107N/A#include <sys/systm.h>
3853N/A
3853N/Aconst sigset_t maskset = {MASKSET0, MASKSET1, 0, 0}; /* maskable signals */
3940N/A
3853N/A/*
3853N/A * Return true if the valid signal bits in both sets are the same.
3853N/A */
3853N/Aint
3853N/Asigequalset(const sigset_t *s1, const sigset_t *s2)
3853N/A{
3853N/A /*
3853N/A * We only test valid signal bits, not rubbish following MAXSIG
3940N/A * (for speed). Algorithm:
3853N/A * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
3853N/A */
3853N/A return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) |
3853N/A ((s1->__sigbits[1] ^ s2->__sigbits[1]) & FILLSET1)));
3853N/A}
3853N/A
3853N/A/*
3853N/A * Common code for calling the user-specified signal handler.
3853N/A */
3853N/Avoid
3853N/Acall_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp)
3853N/A{
3853N/A ulwp_t *self = curthread;
3853N/A uberdata_t *udp = self->ul_uberdata;
3853N/A struct sigaction uact;
3853N/A volatile struct sigaction *sap;
3853N/A
3853N/A /*
3853N/A * If we are taking a signal while parked or about to be parked
3853N/A * on __lwp_park() then remove ourself from the sleep queue so
3853N/A * that we can grab locks. The code in mutex_lock_queue() and
3853N/A * cond_wait_common() will detect this and deal with it when
3853N/A * __lwp_park() returns.
3853N/A */
3853N/A unsleep_self();
3853N/A set_parking_flag(self, 0);
3853N/A
3853N/A if (__td_event_report(self, TD_CATCHSIG, udp)) {
3853N/A self->ul_td_evbuf.eventnum = TD_CATCHSIG;
3853N/A self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig;
3853N/A tdb_event(TD_CATCHSIG, udp);
3853N/A }
3853N/A
3853N/A /*
3853N/A * Get a self-consistent set of flags, handler, and mask
3853N/A * while holding the sig's sig_lock for the least possible time.
3853N/A * We must acquire the sig's sig_lock because some thread running
3853N/A * in sigaction() might be establishing a new signal handler.
3853N/A *
3853N/A * Locking exceptions:
3853N/A * No locking for a child of vfork().
3853N/A * If the signal is SIGPROF with an si_code of PROF_SIG,
3853N/A * then we assume that this signal was generated by
3853N/A * setitimer(ITIMER_REALPROF) set up by the dbx collector.
3853N/A * If the signal is SIGEMT with an si_code of EMT_CPCOVF,
3853N/A * then we assume that the signal was generated by
3853N/A * a hardware performance counter overflow.
3853N/A * In these cases, assume that we need no locking. It is the
3853N/A * monitoring program's responsibility to ensure correctness.
3853N/A */
3853N/A sap = &udp->siguaction[sig].sig_uaction;
3853N/A if (self->ul_vfork ||
3853N/A (sip != NULL &&
3853N/A ((sig == SIGPROF && sip->si_code == PROF_SIG) ||
3853N/A (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) {
3853N/A /* we wish this assignment could be atomic */
3853N/A (void) _private_memcpy(&uact, (void *)sap, sizeof (uact));
3853N/A } else {
3853N/A mutex_t *mp = &udp->siguaction[sig].sig_lock;
3853N/A lmutex_lock(mp);
3853N/A (void) _private_memcpy(&uact, (void *)sap, sizeof (uact));
3853N/A if (sig == SIGCANCEL && (sap->sa_flags & SA_RESETHAND))
3853N/A sap->sa_sigaction = SIG_DFL;
3853N/A lmutex_unlock(mp);
3853N/A }
3853N/A
3853N/A /*
3853N/A * Set the proper signal mask and call the user's signal handler.
3853N/A * (We overrode the user-requested signal mask with maskset
3853N/A * so we currently have all blockable signals blocked.)
3853N/A *
3853N/A * We would like to ASSERT() that the signal is not a member of the
3853N/A * signal mask at the previous level (ucp->uc_sigmask) or the specified
3853N/A * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
3853N/A * /proc can override this via PCSSIG, so we don't bother.
3853N/A *
3853N/A * We would also like to ASSERT() that the signal mask at the previous
3853N/A * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
3853N/A * but /proc can change the thread's signal mask via PCSHOLD, so we
3853N/A * don't bother with that either.
3853N/A */
3853N/A ASSERT(ucp->uc_flags & UC_SIGMASK);
3853N/A if (self->ul_sigsuspend) {
3853N/A ucp->uc_sigmask = self->ul_sigmask;
3853N/A self->ul_sigsuspend = 0;
3853N/A /* the sigsuspend() or pollsys() signal mask */
3853N/A sigorset(&uact.sa_mask, &self->ul_tmpmask);
3853N/A } else {
3853N/A /* the signal mask at the previous level */
3853N/A sigorset(&uact.sa_mask, &ucp->uc_sigmask);
3853N/A }
3853N/A if (!(uact.sa_flags & SA_NODEFER)) /* add current signal */
3853N/A (void) _private_sigaddset(&uact.sa_mask, sig);
3853N/A self->ul_sigmask = uact.sa_mask;
3853N/A self->ul_siglink = ucp;
3853N/A (void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL);
3853N/A
3853N/A /*
3853N/A * If this thread has been sent SIGCANCEL from the kernel
3853N/A * or from pthread_cancel(), it is being asked to exit.
3853N/A * The kernel may send SIGCANCEL without a siginfo struct.
3853N/A * If the SIGCANCEL is process-directed (from kill() or
3853N/A * sigqueue()), treat it as an ordinary signal.
3853N/A */
3853N/A if (sig == SIGCANCEL) {
3853N/A if (sip == NULL || SI_FROMKERNEL(sip) ||
3853N/A sip->si_code == SI_LWP) {
3853N/A do_sigcancel();
3853N/A goto out;
3853N/A }
3853N/A /* SIGCANCEL is ignored by default */
3853N/A if (uact.sa_sigaction == SIG_DFL ||
3853N/A uact.sa_sigaction == SIG_IGN)
3853N/A goto out;
3853N/A }
3853N/A
3853N/A /*
3853N/A * If this thread has been sent SIGAIOCANCEL (SIGLWP) and
3853N/A * we are an aio worker thread, cancel the aio request.
3853N/A */
3853N/A if (sig == SIGAIOCANCEL) {
3853N/A aio_worker_t *aiowp = _pthread_getspecific(_aio_key);
3853N/A
3853N/A if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL)
4107N/A _siglongjmp(aiowp->work_jmp_buf, 1);
3853N/A /* SIGLWP is ignored by default */
3853N/A if (uact.sa_sigaction == SIG_DFL ||
3853N/A uact.sa_sigaction == SIG_IGN)
3853N/A goto out;
3853N/A }
3853N/A
4107N/A if (!(uact.sa_flags & SA_SIGINFO))
3853N/A sip = NULL;
4107N/A __sighndlr(sig, sip, ucp, uact.sa_sigaction);
3853N/A
3853N/A#if defined(sparc) || defined(__sparc)
3853N/A /*
4107N/A * If this is a floating point exception and the queue
4107N/A * is non-empty, pop the top entry from the queue. This
4107N/A * is to maintain expected behavior.
4107N/A */
4107N/A if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) {
4107N/A fpregset_t *fp = &ucp->uc_mcontext.fpregs;
4107N/A
4107N/A if (--fp->fpu_qcnt > 0) {
4107N/A unsigned char i;
4107N/A struct fq *fqp;
4107N/A
4107N/A fqp = fp->fpu_q;
4107N/A for (i = 0; i < fp->fpu_qcnt; i++)
4107N/A fqp[i] = fqp[i+1];
4107N/A }
4107N/A }
4107N/A#endif /* sparc */
3853N/A
3853N/Aout:
3853N/A (void) _private_setcontext(ucp);
3853N/A thr_panic("call_user_handler(): _setcontext() returned");
3853N/A}
3853N/A
3853N/A/*
3853N/A * take_deferred_signal() is called when ul_critical and ul_sigdefer become
3853N/A * zero and a deferred signal has been recorded on the current thread.
3853N/A * We are out of the critical region and are ready to take a signal.
3853N/A * The kernel has all signals blocked on this lwp, but our value of
3853N/A * ul_sigmask is the correct signal mask for the previous context.
3853N/A */
3853N/Avoid
3853N/Atake_deferred_signal(int sig)
3853N/A{
3853N/A ulwp_t *self = curthread;
3853N/A siginfo_t siginfo;
3853N/A siginfo_t *sip;
3853N/A ucontext_t uc;
3853N/A volatile int returning;
3853N/A
3853N/A ASSERT(self->ul_critical == 0);
3853N/A ASSERT(self->ul_sigdefer == 0);
3853N/A ASSERT(self->ul_cursig == 0);
3853N/A
3853N/A returning = 0;
3853N/A uc.uc_flags = UC_ALL;
3853N/A /*
3853N/A * We call _private_getcontext (a libc-private synonym for
3853N/A * _getcontext) rather than _getcontext because we need to
3853N/A * avoid the dynamic linker and link auditing problems here.
3853N/A */
3853N/A (void) _private_getcontext(&uc);
3853N/A /*
3853N/A * If the application signal handler calls setcontext() on
3853N/A * the ucontext we give it, it returns here, then we return.
3853N/A */
3853N/A if (returning)
3853N/A return;
3853N/A returning = 1;
3853N/A ASSERT(sigequalset(&uc.uc_sigmask, &maskset));
3853N/A if (self->ul_siginfo.si_signo == 0)
3853N/A sip = NULL;
3853N/A else {
3853N/A (void) _private_memcpy(&siginfo,
3940N/A &self->ul_siginfo, sizeof (siginfo));
3940N/A sip = &siginfo;
3853N/A }
3853N/A uc.uc_sigmask = self->ul_sigmask;
3940N/A call_user_handler(sig, sip, &uc);
3940N/A}
3940N/A
3940N/Avoid
3940N/Asigacthandler(int sig, siginfo_t *sip, void *uvp)
3940N/A{
3940N/A ucontext_t *ucp = uvp;
3853N/A ulwp_t *self = curthread;
3853N/A
3853N/A /*
3853N/A * Do this in case we took a signal while in a cancelable system call.
3853N/A * It does no harm if we were not in such a system call.
3853N/A */
3853N/A self->ul_sp = 0;
3853N/A if (sig != SIGCANCEL)
3853N/A self->ul_cancel_async = self->ul_save_async;
3853N/A
3853N/A /*
3853N/A * If we are not in a critical region and are
3853N/A * not deferring signals, take the signal now.
3853N/A */
3853N/A if ((self->ul_critical + self->ul_sigdefer) == 0) {
3853N/A call_user_handler(sig, sip, ucp);
3853N/A return; /* call_user_handler() cannot return */
3853N/A }
3853N/A
3853N/A /*
3853N/A * We are in a critical region or we are deferring signals. When
3853N/A * we emerge from the region we will call take_deferred_signal().
3853N/A */
3853N/A ASSERT(self->ul_cursig == 0);
3853N/A self->ul_cursig = (char)sig;
3853N/A if (sip != NULL)
3853N/A (void) _private_memcpy(&self->ul_siginfo,
3853N/A sip, sizeof (siginfo_t));
3853N/A else
3853N/A self->ul_siginfo.si_signo = 0;
3853N/A
3853N/A /*
3853N/A * Make sure that if we return to a call to __lwp_park()
3853N/A * or ___lwp_cond_wait() that it returns right away
3853N/A * (giving us a spurious wakeup but not a deadlock).
3853N/A */
3853N/A set_parking_flag(self, 0);
3853N/A
3853N/A /*
4107N/A * Return to the previous context with all signals blocked.
4107N/A * We will restore the signal mask in take_deferred_signal().
4107N/A * Note that we are calling the system call trap here, not
4107N/A * the _setcontext() wrapper. We don't want to change the
4107N/A * thread's ul_sigmask by this operation.
4107N/A */
4107N/A ucp->uc_sigmask = maskset;
4107N/A (void) __setcontext_syscall(ucp);
4107N/A thr_panic("sigacthandler(): __setcontext() returned");
4107N/A}
4107N/A
4107N/A#pragma weak sigaction = _libc_sigaction
4107N/A#pragma weak _sigaction = _libc_sigaction
4107N/Aint
4107N/A_libc_sigaction(int sig, const struct sigaction *nact, struct sigaction *oact)
4107N/A{
4107N/A ulwp_t *self = curthread;
3853N/A uberdata_t *udp = self->ul_uberdata;
struct sigaction oaction;
struct sigaction tact;
struct sigaction *tactp = NULL;
int rv;
if (sig <= 0 || sig >= NSIG) {
errno = EINVAL;
return (-1);
}
if (!self->ul_vfork)
lmutex_lock(&udp->siguaction[sig].sig_lock);
oaction = udp->siguaction[sig].sig_uaction;
if (nact != NULL) {
tact = *nact; /* make a copy so we can modify it */
tactp = &tact;
delete_reserved_signals(&tact.sa_mask);
#if !defined(_LP64)
tact.sa_resv[0] = tact.sa_resv[1] = 0; /* cleanliness */
#endif
/*
* To be compatible with the behavior of SunOS 4.x:
* If the new signal handler is SIG_IGN or SIG_DFL, do
* not change the signal's entry in the siguaction array.
* This allows a child of vfork(2) to set signal handlers
* to SIG_IGN or SIG_DFL without affecting the parent.
*
* This also covers a race condition with some thread
* setting the signal action to SIG_DFL or SIG_IGN
* when the thread has also received and deferred
* that signal. When the thread takes the deferred
* signal, even though it has set the action to SIG_DFL
* or SIG_IGN, it will execute the old signal handler
* anyway. This is an inherent signaling race condition
* and is not a bug.
*
* A child of vfork() is not allowed to change signal
* handlers to anything other than SIG_DFL or SIG_IGN.
*/
if (self->ul_vfork) {
if (tact.sa_sigaction != SIG_IGN)
tact.sa_sigaction = SIG_DFL;
} else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) {
/*
* Always catch these signals.
* We need SIGCANCEL for pthread_cancel() to work.
* We need SIGAIOCANCEL for aio_cancel() to work.
*/
udp->siguaction[sig].sig_uaction = tact;
if (tact.sa_sigaction == SIG_DFL ||
tact.sa_sigaction == SIG_IGN)
tact.sa_flags = SA_SIGINFO;
else {
tact.sa_flags |= SA_SIGINFO;
tact.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
}
tact.sa_sigaction = udp->sigacthandler;
tact.sa_mask = maskset;
} else if (tact.sa_sigaction != SIG_DFL &&
tact.sa_sigaction != SIG_IGN) {
udp->siguaction[sig].sig_uaction = tact;
tact.sa_flags &= ~SA_NODEFER;
tact.sa_sigaction = udp->sigacthandler;
tact.sa_mask = maskset;
}
}
if ((rv = __sigaction(sig, tactp, oact)) != 0)
udp->siguaction[sig].sig_uaction = oaction;
else if (oact != NULL &&
oact->sa_sigaction != SIG_DFL &&
oact->sa_sigaction != SIG_IGN)
*oact = oaction;
/*
* We detect setting the disposition of SIGIO just to set the
* _sigio_enabled flag for the asynchronous i/o (aio) code.
*/
if (sig == SIGIO && rv == 0 && tactp != NULL) {
_sigio_enabled =
(tactp->sa_handler != SIG_DFL &&
tactp->sa_handler != SIG_IGN);
}
if (!self->ul_vfork)
lmutex_unlock(&udp->siguaction[sig].sig_lock);
return (rv);
}
void
setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *),
void (**osigacthandler)(int, siginfo_t *, void *))
{
ulwp_t *self = curthread;
uberdata_t *udp = self->ul_uberdata;
if (osigacthandler != NULL)
*osigacthandler = udp->sigacthandler;
udp->sigacthandler = nsigacthandler;
}
/*
* Calling set_parking_flag(curthread, 1) informs the kernel that we are
* calling __lwp_park or ___lwp_cond_wait(). If we take a signal in
* the unprotected (from signals) interval before reaching the kernel,
* sigacthandler() will call set_parking_flag(curthread, 0) to inform
* the kernel to return immediately from these system calls, giving us
* a spurious wakeup but not a deadlock.
*/
void
set_parking_flag(ulwp_t *self, int park)
{
volatile sc_shared_t *scp;
enter_critical(self);
if ((scp = self->ul_schedctl) != NULL ||
(scp = setup_schedctl()) != NULL)
scp->sc_park = park;
else if (park == 0) /* schedctl failed, do it the long way */
__lwp_unpark(self->ul_lwpid);
exit_critical(self);
}
/*
* Tell the kernel to block all signals.
* Use the schedctl interface, or failing that, use __lwp_sigmask().
* This action can be rescinded only by making a system call that
* sets the signal mask:
* __lwp_sigmask(), __sigprocmask(), __setcontext(),
* __sigsuspend() or __pollsys().
* In particular, this action cannot be reversed by assigning
* scp->sc_sigblock = 0. That would be a way to lose signals.
* See the definition of restore_signals(self).
*/
void
block_all_signals(ulwp_t *self)
{
volatile sc_shared_t *scp;
enter_critical(self);
if ((scp = self->ul_schedctl) != NULL ||
(scp = setup_schedctl()) != NULL)
scp->sc_sigblock = 1;
else
(void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL);
exit_critical(self);
}
/*
* _private_setcontext has code that forcibly restores the curthread
* pointer in a context passed to the setcontext(2) syscall.
*
* Certain processes may need to disable this feature, so these routines
* provide the mechanism to do so.
*
* (As an example, branded 32-bit x86 processes may use %gs for their own
* purposes, so they need to be able to specify a %gs value to be restored
* on return from a signal handler via the passed ucontext_t.)
*/
static int setcontext_enforcement = 1;
void
set_setcontext_enforcement(int on)
{
setcontext_enforcement = on;
}
#pragma weak setcontext = _private_setcontext
#pragma weak _setcontext = _private_setcontext
int
_private_setcontext(const ucontext_t *ucp)
{
ulwp_t *self = curthread;
int ret;
ucontext_t uc;
/*
* Returning from the main context (uc_link == NULL) causes
* the thread to exit. See setcontext(2) and makecontext(3C).
*/
if (ucp == NULL)
_thr_exit(NULL);
(void) _private_memcpy(&uc, ucp, sizeof (uc));
/*
* Restore previous signal mask and context link.
*/
if (uc.uc_flags & UC_SIGMASK) {
block_all_signals(self);
delete_reserved_signals(&uc.uc_sigmask);
self->ul_sigmask = uc.uc_sigmask;
if (self->ul_cursig) {
/*
* We have a deferred signal present.
* The signal mask will be set when the
* signal is taken in take_deferred_signal().
*/
ASSERT(self->ul_critical + self->ul_sigdefer != 0);
uc.uc_flags &= ~UC_SIGMASK;
}
}
self->ul_siglink = uc.uc_link;
/*
* We don't know where this context structure has been.
* Preserve the curthread pointer, at least.
*
* Allow this feature to be disabled if a particular process
* requests it.
*/
if (setcontext_enforcement) {
#if defined(__sparc)
uc.uc_mcontext.gregs[REG_G7] = (greg_t)self;
#elif defined(__amd64)
uc.uc_mcontext.gregs[REG_FS] = (greg_t)self->ul_gs;
#elif defined(__i386)
uc.uc_mcontext.gregs[GS] = (greg_t)self->ul_gs;
#else
#error "none of __sparc, __amd64, __i386 defined"
#endif
}
/*
* Make sure that if we return to a call to __lwp_park()
* or ___lwp_cond_wait() that it returns right away
* (giving us a spurious wakeup but not a deadlock).
*/
set_parking_flag(self, 0);
self->ul_sp = 0;
ret = __setcontext_syscall(&uc);
/*
* It is OK for setcontext() to return if the user has not specified
* UC_CPU.
*/
if (uc.uc_flags & UC_CPU)
thr_panic("setcontext(): __setcontext() returned");
return (ret);
}
#pragma weak thr_sigsetmask = _thr_sigsetmask
#pragma weak pthread_sigmask = _thr_sigsetmask
#pragma weak _pthread_sigmask = _thr_sigsetmask
int
_thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
{
ulwp_t *self = curthread;
sigset_t saveset;
if (set == NULL) {
enter_critical(self);
if (oset != NULL)
*oset = self->ul_sigmask;
exit_critical(self);
} else {
switch (how) {
case SIG_BLOCK:
case SIG_UNBLOCK:
case SIG_SETMASK:
break;
default:
return (EINVAL);
}
/*
* The assignments to self->ul_sigmask must be protected from
* signals. The nuances of this code are subtle. Be careful.
*/
block_all_signals(self);
if (oset != NULL)
saveset = self->ul_sigmask;
switch (how) {
case SIG_BLOCK:
self->ul_sigmask.__sigbits[0] |= set->__sigbits[0];
self->ul_sigmask.__sigbits[1] |= set->__sigbits[1];
break;
case SIG_UNBLOCK:
self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0];
self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1];
break;
case SIG_SETMASK:
self->ul_sigmask.__sigbits[0] = set->__sigbits[0];
self->ul_sigmask.__sigbits[1] = set->__sigbits[1];
break;
}
delete_reserved_signals(&self->ul_sigmask);
if (oset != NULL)
*oset = saveset;
restore_signals(self);
}
return (0);
}
#pragma weak sigprocmask = _sigprocmask
int
_sigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
int error;
/*
* Guard against children of vfork().
*/
if (curthread->ul_vfork)
return (__lwp_sigmask(how, set, oset));
if ((error = _thr_sigsetmask(how, set, oset)) != 0) {
errno = error;
return (-1);
}
return (0);
}
/*
* Called at library initialization to set up signal handling.
* All we really do is initialize the sig_lock mutexes.
* All signal handlers are either SIG_DFL or SIG_IGN on exec().
* However, if any signal handlers were established on alternate
* link maps before the primary link map has been initialized,
* then inform the kernel of the new sigacthandler.
*/
void
signal_init()
{
uberdata_t *udp = curthread->ul_uberdata;
struct sigaction *sap;
struct sigaction act;
int sig;
for (sig = 0; sig < NSIG; sig++) {
udp->siguaction[sig].sig_lock.mutex_magic = MUTEX_MAGIC;
sap = &udp->siguaction[sig].sig_uaction;
if (sap->sa_sigaction != SIG_DFL &&
sap->sa_sigaction != SIG_IGN &&
__sigaction(sig, NULL, &act) == 0 &&
act.sa_sigaction != SIG_DFL &&
act.sa_sigaction != SIG_IGN) {
act = *sap;
act.sa_flags &= ~SA_NODEFER;
act.sa_sigaction = udp->sigacthandler;
act.sa_mask = maskset;
(void) __sigaction(sig, &act, NULL);
}
}
}
/*
* Common code for cancelling self in _sigcancel() and pthread_cancel().
* If the thread is at a cancellation point (ul_cancelable) then just
* return and let _canceloff() do the exit, else exit immediately if
* async mode is in effect.
*/
void
do_sigcancel()
{
ulwp_t *self = curthread;
ASSERT(self->ul_critical == 0);
ASSERT(self->ul_sigdefer == 0);
self->ul_cancel_pending = 1;
if (self->ul_cancel_async &&
!self->ul_cancel_disabled &&
!self->ul_cancelable)
_pthread_exit(PTHREAD_CANCELED);
}
/*
* Set up the SIGCANCEL handler for threads cancellation,
* needed only when we have more than one thread,
* or the SIGAIOCANCEL handler for aio cancellation,
* called when aio is initialized, in __uaio_init().
*/
void
setup_cancelsig(int sig)
{
uberdata_t *udp = curthread->ul_uberdata;
mutex_t *mp = &udp->siguaction[sig].sig_lock;
struct sigaction act;
ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL);
lmutex_lock(mp);
act = udp->siguaction[sig].sig_uaction;
lmutex_unlock(mp);
if (act.sa_sigaction == SIG_DFL ||
act.sa_sigaction == SIG_IGN)
act.sa_flags = SA_SIGINFO;
else {
act.sa_flags |= SA_SIGINFO;
act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND);
}
act.sa_sigaction = udp->sigacthandler;
act.sa_mask = maskset;
(void) __sigaction(sig, &act, NULL);
}