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*11426SRoger.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 430Sstevel@tonic-gate const sigset_t maskset = {MASKSET0, MASKSET1, 0, 0}; /* maskable signals */ 440Sstevel@tonic-gate 450Sstevel@tonic-gate /* 460Sstevel@tonic-gate * Return true if the valid signal bits in both sets are the same. 470Sstevel@tonic-gate */ 480Sstevel@tonic-gate int 490Sstevel@tonic-gate sigequalset(const sigset_t *s1, const sigset_t *s2) 500Sstevel@tonic-gate { 510Sstevel@tonic-gate /* 520Sstevel@tonic-gate * We only test valid signal bits, not rubbish following MAXSIG 530Sstevel@tonic-gate * (for speed). Algorithm: 540Sstevel@tonic-gate * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0 550Sstevel@tonic-gate */ 560Sstevel@tonic-gate return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) | 570Sstevel@tonic-gate ((s1->__sigbits[1] ^ s2->__sigbits[1]) & FILLSET1))); 580Sstevel@tonic-gate } 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * Common code for calling the user-specified signal handler. 620Sstevel@tonic-gate */ 630Sstevel@tonic-gate void 640Sstevel@tonic-gate call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp) 650Sstevel@tonic-gate { 660Sstevel@tonic-gate ulwp_t *self = curthread; 670Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 680Sstevel@tonic-gate struct sigaction uact; 690Sstevel@tonic-gate volatile struct sigaction *sap; 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * If we are taking a signal while parked or about to be parked 730Sstevel@tonic-gate * on __lwp_park() then remove ourself from the sleep queue so 740Sstevel@tonic-gate * that we can grab locks. The code in mutex_lock_queue() and 750Sstevel@tonic-gate * cond_wait_common() will detect this and deal with it when 760Sstevel@tonic-gate * __lwp_park() returns. 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate unsleep_self(); 790Sstevel@tonic-gate set_parking_flag(self, 0); 800Sstevel@tonic-gate 810Sstevel@tonic-gate if (__td_event_report(self, TD_CATCHSIG, udp)) { 820Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_CATCHSIG; 830Sstevel@tonic-gate self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig; 840Sstevel@tonic-gate tdb_event(TD_CATCHSIG, udp); 850Sstevel@tonic-gate } 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * Get a self-consistent set of flags, handler, and mask 890Sstevel@tonic-gate * while holding the sig's sig_lock for the least possible time. 900Sstevel@tonic-gate * We must acquire the sig's sig_lock because some thread running 910Sstevel@tonic-gate * in sigaction() might be establishing a new signal handler. 924570Sraf * The code in sigaction() acquires the writer lock; here 934570Sraf * we acquire the readers lock to ehance concurrency in the 944570Sraf * face of heavy signal traffic, such as generated by java. 950Sstevel@tonic-gate * 960Sstevel@tonic-gate * Locking exceptions: 970Sstevel@tonic-gate * No locking for a child of vfork(). 980Sstevel@tonic-gate * If the signal is SIGPROF with an si_code of PROF_SIG, 990Sstevel@tonic-gate * then we assume that this signal was generated by 1000Sstevel@tonic-gate * setitimer(ITIMER_REALPROF) set up by the dbx collector. 1010Sstevel@tonic-gate * If the signal is SIGEMT with an si_code of EMT_CPCOVF, 1020Sstevel@tonic-gate * then we assume that the signal was generated by 1030Sstevel@tonic-gate * a hardware performance counter overflow. 1040Sstevel@tonic-gate * In these cases, assume that we need no locking. It is the 1050Sstevel@tonic-gate * monitoring program's responsibility to ensure correctness. 1060Sstevel@tonic-gate */ 1070Sstevel@tonic-gate sap = &udp->siguaction[sig].sig_uaction; 1080Sstevel@tonic-gate if (self->ul_vfork || 1090Sstevel@tonic-gate (sip != NULL && 1100Sstevel@tonic-gate ((sig == SIGPROF && sip->si_code == PROF_SIG) || 1110Sstevel@tonic-gate (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) { 1120Sstevel@tonic-gate /* we wish this assignment could be atomic */ 1136515Sraf (void) memcpy(&uact, (void *)sap, sizeof (uact)); 1140Sstevel@tonic-gate } else { 1154570Sraf rwlock_t *rwlp = &udp->siguaction[sig].sig_lock; 1164570Sraf lrw_rdlock(rwlp); 1176515Sraf (void) memcpy(&uact, (void *)sap, sizeof (uact)); 1185891Sraf if ((sig == SIGCANCEL || sig == SIGAIOCANCEL) && 1195891Sraf (sap->sa_flags & SA_RESETHAND)) 1200Sstevel@tonic-gate sap->sa_sigaction = SIG_DFL; 1214570Sraf lrw_unlock(rwlp); 1220Sstevel@tonic-gate } 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate /* 1250Sstevel@tonic-gate * Set the proper signal mask and call the user's signal handler. 1260Sstevel@tonic-gate * (We overrode the user-requested signal mask with maskset 1270Sstevel@tonic-gate * so we currently have all blockable signals blocked.) 1280Sstevel@tonic-gate * 1290Sstevel@tonic-gate * We would like to ASSERT() that the signal is not a member of the 1300Sstevel@tonic-gate * signal mask at the previous level (ucp->uc_sigmask) or the specified 1310Sstevel@tonic-gate * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but 1320Sstevel@tonic-gate * /proc can override this via PCSSIG, so we don't bother. 1330Sstevel@tonic-gate * 1340Sstevel@tonic-gate * We would also like to ASSERT() that the signal mask at the previous 1350Sstevel@tonic-gate * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()), 1360Sstevel@tonic-gate * but /proc can change the thread's signal mask via PCSHOLD, so we 1370Sstevel@tonic-gate * don't bother with that either. 1380Sstevel@tonic-gate */ 1390Sstevel@tonic-gate ASSERT(ucp->uc_flags & UC_SIGMASK); 1400Sstevel@tonic-gate if (self->ul_sigsuspend) { 1410Sstevel@tonic-gate ucp->uc_sigmask = self->ul_sigmask; 1420Sstevel@tonic-gate self->ul_sigsuspend = 0; 1430Sstevel@tonic-gate /* the sigsuspend() or pollsys() signal mask */ 1440Sstevel@tonic-gate sigorset(&uact.sa_mask, &self->ul_tmpmask); 1450Sstevel@tonic-gate } else { 1460Sstevel@tonic-gate /* the signal mask at the previous level */ 1470Sstevel@tonic-gate sigorset(&uact.sa_mask, &ucp->uc_sigmask); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate if (!(uact.sa_flags & SA_NODEFER)) /* add current signal */ 1506515Sraf (void) sigaddset(&uact.sa_mask, sig); 1510Sstevel@tonic-gate self->ul_sigmask = uact.sa_mask; 1520Sstevel@tonic-gate self->ul_siglink = ucp; 1530Sstevel@tonic-gate (void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate /* 1560Sstevel@tonic-gate * If this thread has been sent SIGCANCEL from the kernel 1570Sstevel@tonic-gate * or from pthread_cancel(), it is being asked to exit. 1580Sstevel@tonic-gate * The kernel may send SIGCANCEL without a siginfo struct. 1590Sstevel@tonic-gate * If the SIGCANCEL is process-directed (from kill() or 1600Sstevel@tonic-gate * sigqueue()), treat it as an ordinary signal. 1610Sstevel@tonic-gate */ 1620Sstevel@tonic-gate if (sig == SIGCANCEL) { 1630Sstevel@tonic-gate if (sip == NULL || SI_FROMKERNEL(sip) || 1640Sstevel@tonic-gate sip->si_code == SI_LWP) { 1650Sstevel@tonic-gate do_sigcancel(); 1660Sstevel@tonic-gate goto out; 1670Sstevel@tonic-gate } 1682248Sraf /* SIGCANCEL is ignored by default */ 1692248Sraf if (uact.sa_sigaction == SIG_DFL || 1702248Sraf uact.sa_sigaction == SIG_IGN) 1712248Sraf goto out; 1722248Sraf } 1732248Sraf 1742248Sraf /* 1752248Sraf * If this thread has been sent SIGAIOCANCEL (SIGLWP) and 1762248Sraf * we are an aio worker thread, cancel the aio request. 1772248Sraf */ 1782248Sraf if (sig == SIGAIOCANCEL) { 1796812Sraf aio_worker_t *aiowp = pthread_getspecific(_aio_key); 1802248Sraf 1812248Sraf if (sip != NULL && sip->si_code == SI_LWP && aiowp != NULL) 1826812Sraf siglongjmp(aiowp->work_jmp_buf, 1); 1832248Sraf /* SIGLWP is ignored by default */ 1840Sstevel@tonic-gate if (uact.sa_sigaction == SIG_DFL || 1850Sstevel@tonic-gate uact.sa_sigaction == SIG_IGN) 1860Sstevel@tonic-gate goto out; 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate if (!(uact.sa_flags & SA_SIGINFO)) 1900Sstevel@tonic-gate sip = NULL; 1910Sstevel@tonic-gate __sighndlr(sig, sip, ucp, uact.sa_sigaction); 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate #if defined(sparc) || defined(__sparc) 1940Sstevel@tonic-gate /* 1950Sstevel@tonic-gate * If this is a floating point exception and the queue 1960Sstevel@tonic-gate * is non-empty, pop the top entry from the queue. This 1970Sstevel@tonic-gate * is to maintain expected behavior. 1980Sstevel@tonic-gate */ 1990Sstevel@tonic-gate if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) { 2000Sstevel@tonic-gate fpregset_t *fp = &ucp->uc_mcontext.fpregs; 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate if (--fp->fpu_qcnt > 0) { 2030Sstevel@tonic-gate unsigned char i; 2040Sstevel@tonic-gate struct fq *fqp; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate fqp = fp->fpu_q; 2070Sstevel@tonic-gate for (i = 0; i < fp->fpu_qcnt; i++) 2080Sstevel@tonic-gate fqp[i] = fqp[i+1]; 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate #endif /* sparc */ 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate out: 2146812Sraf (void) setcontext(ucp); 2156812Sraf thr_panic("call_user_handler(): setcontext() returned"); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate /* 2190Sstevel@tonic-gate * take_deferred_signal() is called when ul_critical and ul_sigdefer become 2200Sstevel@tonic-gate * zero and a deferred signal has been recorded on the current thread. 2210Sstevel@tonic-gate * We are out of the critical region and are ready to take a signal. 2220Sstevel@tonic-gate * The kernel has all signals blocked on this lwp, but our value of 2230Sstevel@tonic-gate * ul_sigmask is the correct signal mask for the previous context. 2244806Sraf * 2254806Sraf * We call __sigresend() to atomically restore the signal mask and 2264806Sraf * cause the signal to be sent again with the remembered siginfo. 2274806Sraf * We will not return successfully from __sigresend() until the 2284806Sraf * application's signal handler has been run via sigacthandler(). 2290Sstevel@tonic-gate */ 2300Sstevel@tonic-gate void 2310Sstevel@tonic-gate take_deferred_signal(int sig) 2320Sstevel@tonic-gate { 2334806Sraf extern int __sigresend(int, siginfo_t *, sigset_t *); 2340Sstevel@tonic-gate ulwp_t *self = curthread; 2355010Sraf siguaction_t *suap = &self->ul_uberdata->siguaction[sig]; 2360Sstevel@tonic-gate siginfo_t *sip; 2374806Sraf int error; 2380Sstevel@tonic-gate 2394806Sraf ASSERT((self->ul_critical | self->ul_sigdefer | self->ul_cursig) == 0); 2404806Sraf 2415010Sraf /* 2425010Sraf * If the signal handler was established with SA_RESETHAND, 2435010Sraf * the kernel has reset the handler to SIG_DFL, so we have 2445010Sraf * to reestablish the handler now so that it will be entered 2455010Sraf * again when we call __sigresend(), below. 2465837Sraf * 2475837Sraf * Logically, we should acquire and release the signal's 2485837Sraf * sig_lock around this operation to protect the integrity 2495837Sraf * of the signal action while we copy it, as is done below 2505837Sraf * in _libc_sigaction(). However, we may be on a user-level 2515837Sraf * sleep queue at this point and lrw_wrlock(&suap->sig_lock) 2525837Sraf * might attempt to sleep on a different sleep queue and 2535837Sraf * that would corrupt the entire sleep queue mechanism. 2545837Sraf * 2555837Sraf * If we are on a sleep queue we will remove ourself from 2565837Sraf * it in call_user_handler(), called from sigacthandler(), 2575837Sraf * before entering the application's signal handler. 2585837Sraf * In the meantime, we must not acquire any locks. 2595010Sraf */ 2605010Sraf if (suap->sig_uaction.sa_flags & SA_RESETHAND) { 2615010Sraf struct sigaction tact = suap->sig_uaction; 2625010Sraf tact.sa_flags &= ~SA_NODEFER; 2635010Sraf tact.sa_sigaction = self->ul_uberdata->sigacthandler; 2645010Sraf tact.sa_mask = maskset; 2655010Sraf (void) __sigaction(sig, &tact, NULL); 2665010Sraf } 2675010Sraf 2680Sstevel@tonic-gate if (self->ul_siginfo.si_signo == 0) 2690Sstevel@tonic-gate sip = NULL; 2704806Sraf else 2714806Sraf sip = &self->ul_siginfo; 2724806Sraf 2734806Sraf /* EAGAIN can happen only for a pending SIGSTOP signal */ 2744806Sraf while ((error = __sigresend(sig, sip, &self->ul_sigmask)) == EAGAIN) 2754806Sraf continue; 2764806Sraf if (error) 2774806Sraf thr_panic("take_deferred_signal(): __sigresend() failed"); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate void 2810Sstevel@tonic-gate sigacthandler(int sig, siginfo_t *sip, void *uvp) 2820Sstevel@tonic-gate { 2830Sstevel@tonic-gate ucontext_t *ucp = uvp; 2840Sstevel@tonic-gate ulwp_t *self = curthread; 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate /* 2870Sstevel@tonic-gate * Do this in case we took a signal while in a cancelable system call. 2880Sstevel@tonic-gate * It does no harm if we were not in such a system call. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate self->ul_sp = 0; 2910Sstevel@tonic-gate if (sig != SIGCANCEL) 2920Sstevel@tonic-gate self->ul_cancel_async = self->ul_save_async; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* 295*11426SRoger.Faulkner@Sun.COM * If this thread has performed a longjmp() from a signal handler 296*11426SRoger.Faulkner@Sun.COM * back to main level some time in the past, it has left the kernel 297*11426SRoger.Faulkner@Sun.COM * thinking that it is still in the signal context. We repair this 298*11426SRoger.Faulkner@Sun.COM * possible damage by setting ucp->uc_link to NULL if we know that 299*11426SRoger.Faulkner@Sun.COM * we are actually executing at main level (self->ul_siglink == NULL). 300*11426SRoger.Faulkner@Sun.COM * See the code for setjmp()/longjmp() for more details. 301*11426SRoger.Faulkner@Sun.COM */ 302*11426SRoger.Faulkner@Sun.COM if (self->ul_siglink == NULL) 303*11426SRoger.Faulkner@Sun.COM ucp->uc_link = NULL; 304*11426SRoger.Faulkner@Sun.COM 305*11426SRoger.Faulkner@Sun.COM /* 3060Sstevel@tonic-gate * If we are not in a critical region and are 3070Sstevel@tonic-gate * not deferring signals, take the signal now. 3080Sstevel@tonic-gate */ 3090Sstevel@tonic-gate if ((self->ul_critical + self->ul_sigdefer) == 0) { 3100Sstevel@tonic-gate call_user_handler(sig, sip, ucp); 3115961Srh87107 /* 3125961Srh87107 * On the surface, the following call seems redundant 3135961Srh87107 * because call_user_handler() cannot return. However, 3145961Srh87107 * we don't want to return from here because the compiler 3155961Srh87107 * might recycle our frame. We want to keep it on the 3165961Srh87107 * stack to assist debuggers such as pstack in identifying 3175961Srh87107 * signal frames. The call to thr_panic() serves to prevent 3185961Srh87107 * tail-call optimisation here. 3195961Srh87107 */ 3205961Srh87107 thr_panic("sigacthandler(): call_user_handler() returned"); 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate /* 3240Sstevel@tonic-gate * We are in a critical region or we are deferring signals. When 3250Sstevel@tonic-gate * we emerge from the region we will call take_deferred_signal(). 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate ASSERT(self->ul_cursig == 0); 3280Sstevel@tonic-gate self->ul_cursig = (char)sig; 3290Sstevel@tonic-gate if (sip != NULL) 3306515Sraf (void) memcpy(&self->ul_siginfo, 3311111Sraf sip, sizeof (siginfo_t)); 3320Sstevel@tonic-gate else 3330Sstevel@tonic-gate self->ul_siginfo.si_signo = 0; 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * Make sure that if we return to a call to __lwp_park() 3370Sstevel@tonic-gate * or ___lwp_cond_wait() that it returns right away 3380Sstevel@tonic-gate * (giving us a spurious wakeup but not a deadlock). 3390Sstevel@tonic-gate */ 3400Sstevel@tonic-gate set_parking_flag(self, 0); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate /* 3430Sstevel@tonic-gate * Return to the previous context with all signals blocked. 3440Sstevel@tonic-gate * We will restore the signal mask in take_deferred_signal(). 3450Sstevel@tonic-gate * Note that we are calling the system call trap here, not 3466812Sraf * the setcontext() wrapper. We don't want to change the 3470Sstevel@tonic-gate * thread's ul_sigmask by this operation. 3480Sstevel@tonic-gate */ 3490Sstevel@tonic-gate ucp->uc_sigmask = maskset; 3506515Sraf (void) __setcontext(ucp); 3510Sstevel@tonic-gate thr_panic("sigacthandler(): __setcontext() returned"); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3546812Sraf #pragma weak _sigaction = sigaction 3550Sstevel@tonic-gate int 3566812Sraf sigaction(int sig, const struct sigaction *nact, struct sigaction *oact) 3570Sstevel@tonic-gate { 3580Sstevel@tonic-gate ulwp_t *self = curthread; 3590Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 3600Sstevel@tonic-gate struct sigaction oaction; 3610Sstevel@tonic-gate struct sigaction tact; 3620Sstevel@tonic-gate struct sigaction *tactp = NULL; 3630Sstevel@tonic-gate int rv; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate if (sig <= 0 || sig >= NSIG) { 3660Sstevel@tonic-gate errno = EINVAL; 3670Sstevel@tonic-gate return (-1); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate if (!self->ul_vfork) 3714570Sraf lrw_wrlock(&udp->siguaction[sig].sig_lock); 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate oaction = udp->siguaction[sig].sig_uaction; 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate if (nact != NULL) { 3760Sstevel@tonic-gate tact = *nact; /* make a copy so we can modify it */ 3770Sstevel@tonic-gate tactp = &tact; 3780Sstevel@tonic-gate delete_reserved_signals(&tact.sa_mask); 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate #if !defined(_LP64) 3810Sstevel@tonic-gate tact.sa_resv[0] = tact.sa_resv[1] = 0; /* cleanliness */ 3820Sstevel@tonic-gate #endif 3830Sstevel@tonic-gate /* 3840Sstevel@tonic-gate * To be compatible with the behavior of SunOS 4.x: 3850Sstevel@tonic-gate * If the new signal handler is SIG_IGN or SIG_DFL, do 3860Sstevel@tonic-gate * not change the signal's entry in the siguaction array. 3870Sstevel@tonic-gate * This allows a child of vfork(2) to set signal handlers 3880Sstevel@tonic-gate * to SIG_IGN or SIG_DFL without affecting the parent. 3890Sstevel@tonic-gate * 3900Sstevel@tonic-gate * This also covers a race condition with some thread 3910Sstevel@tonic-gate * setting the signal action to SIG_DFL or SIG_IGN 3920Sstevel@tonic-gate * when the thread has also received and deferred 3930Sstevel@tonic-gate * that signal. When the thread takes the deferred 3940Sstevel@tonic-gate * signal, even though it has set the action to SIG_DFL 3950Sstevel@tonic-gate * or SIG_IGN, it will execute the old signal handler 3960Sstevel@tonic-gate * anyway. This is an inherent signaling race condition 3970Sstevel@tonic-gate * and is not a bug. 3980Sstevel@tonic-gate * 3990Sstevel@tonic-gate * A child of vfork() is not allowed to change signal 4000Sstevel@tonic-gate * handlers to anything other than SIG_DFL or SIG_IGN. 4010Sstevel@tonic-gate */ 4020Sstevel@tonic-gate if (self->ul_vfork) { 4030Sstevel@tonic-gate if (tact.sa_sigaction != SIG_IGN) 4040Sstevel@tonic-gate tact.sa_sigaction = SIG_DFL; 4052248Sraf } else if (sig == SIGCANCEL || sig == SIGAIOCANCEL) { 4060Sstevel@tonic-gate /* 4072248Sraf * Always catch these signals. 4082248Sraf * We need SIGCANCEL for pthread_cancel() to work. 4092248Sraf * We need SIGAIOCANCEL for aio_cancel() to work. 4100Sstevel@tonic-gate */ 4110Sstevel@tonic-gate udp->siguaction[sig].sig_uaction = tact; 4120Sstevel@tonic-gate if (tact.sa_sigaction == SIG_DFL || 4130Sstevel@tonic-gate tact.sa_sigaction == SIG_IGN) 4140Sstevel@tonic-gate tact.sa_flags = SA_SIGINFO; 4150Sstevel@tonic-gate else { 4160Sstevel@tonic-gate tact.sa_flags |= SA_SIGINFO; 4175891Sraf tact.sa_flags &= 4185891Sraf ~(SA_NODEFER | SA_RESETHAND | SA_RESTART); 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate tact.sa_sigaction = udp->sigacthandler; 4210Sstevel@tonic-gate tact.sa_mask = maskset; 4220Sstevel@tonic-gate } else if (tact.sa_sigaction != SIG_DFL && 4230Sstevel@tonic-gate tact.sa_sigaction != SIG_IGN) { 4240Sstevel@tonic-gate udp->siguaction[sig].sig_uaction = tact; 4250Sstevel@tonic-gate tact.sa_flags &= ~SA_NODEFER; 4260Sstevel@tonic-gate tact.sa_sigaction = udp->sigacthandler; 4270Sstevel@tonic-gate tact.sa_mask = maskset; 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate if ((rv = __sigaction(sig, tactp, oact)) != 0) 4320Sstevel@tonic-gate udp->siguaction[sig].sig_uaction = oaction; 4330Sstevel@tonic-gate else if (oact != NULL && 4340Sstevel@tonic-gate oact->sa_sigaction != SIG_DFL && 4350Sstevel@tonic-gate oact->sa_sigaction != SIG_IGN) 4360Sstevel@tonic-gate *oact = oaction; 4370Sstevel@tonic-gate 4382248Sraf /* 4392248Sraf * We detect setting the disposition of SIGIO just to set the 4402248Sraf * _sigio_enabled flag for the asynchronous i/o (aio) code. 4412248Sraf */ 4422248Sraf if (sig == SIGIO && rv == 0 && tactp != NULL) { 4432248Sraf _sigio_enabled = 4442248Sraf (tactp->sa_handler != SIG_DFL && 4452248Sraf tactp->sa_handler != SIG_IGN); 4462248Sraf } 4472248Sraf 4480Sstevel@tonic-gate if (!self->ul_vfork) 4494570Sraf lrw_unlock(&udp->siguaction[sig].sig_lock); 4500Sstevel@tonic-gate return (rv); 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4536515Sraf /* 4546515Sraf * This is a private interface for the linux brand interface. 4556515Sraf */ 4562712Snn35248 void 4572712Snn35248 setsigacthandler(void (*nsigacthandler)(int, siginfo_t *, void *), 4582712Snn35248 void (**osigacthandler)(int, siginfo_t *, void *)) 4592712Snn35248 { 4602712Snn35248 ulwp_t *self = curthread; 4612712Snn35248 uberdata_t *udp = self->ul_uberdata; 4622712Snn35248 4632712Snn35248 if (osigacthandler != NULL) 4642712Snn35248 *osigacthandler = udp->sigacthandler; 4652712Snn35248 4662712Snn35248 udp->sigacthandler = nsigacthandler; 4672712Snn35248 } 4682712Snn35248 4690Sstevel@tonic-gate /* 4700Sstevel@tonic-gate * Tell the kernel to block all signals. 4710Sstevel@tonic-gate * Use the schedctl interface, or failing that, use __lwp_sigmask(). 4720Sstevel@tonic-gate * This action can be rescinded only by making a system call that 4730Sstevel@tonic-gate * sets the signal mask: 4740Sstevel@tonic-gate * __lwp_sigmask(), __sigprocmask(), __setcontext(), 4750Sstevel@tonic-gate * __sigsuspend() or __pollsys(). 4760Sstevel@tonic-gate * In particular, this action cannot be reversed by assigning 4770Sstevel@tonic-gate * scp->sc_sigblock = 0. That would be a way to lose signals. 4780Sstevel@tonic-gate * See the definition of restore_signals(self). 4790Sstevel@tonic-gate */ 4800Sstevel@tonic-gate void 4810Sstevel@tonic-gate block_all_signals(ulwp_t *self) 4820Sstevel@tonic-gate { 4830Sstevel@tonic-gate volatile sc_shared_t *scp; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate enter_critical(self); 4860Sstevel@tonic-gate if ((scp = self->ul_schedctl) != NULL || 4870Sstevel@tonic-gate (scp = setup_schedctl()) != NULL) 4880Sstevel@tonic-gate scp->sc_sigblock = 1; 4890Sstevel@tonic-gate else 4900Sstevel@tonic-gate (void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL); 4910Sstevel@tonic-gate exit_critical(self); 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4942712Snn35248 /* 4956515Sraf * setcontext() has code that forcibly restores the curthread 4962712Snn35248 * pointer in a context passed to the setcontext(2) syscall. 4972712Snn35248 * 4982712Snn35248 * Certain processes may need to disable this feature, so these routines 4992712Snn35248 * provide the mechanism to do so. 5002712Snn35248 * 5012712Snn35248 * (As an example, branded 32-bit x86 processes may use %gs for their own 5022712Snn35248 * purposes, so they need to be able to specify a %gs value to be restored 5032712Snn35248 * on return from a signal handler via the passed ucontext_t.) 5042712Snn35248 */ 5052712Snn35248 static int setcontext_enforcement = 1; 5062712Snn35248 5072712Snn35248 void 5082712Snn35248 set_setcontext_enforcement(int on) 5092712Snn35248 { 5102712Snn35248 setcontext_enforcement = on; 5112712Snn35248 } 5122712Snn35248 5136812Sraf #pragma weak _setcontext = setcontext 5140Sstevel@tonic-gate int 5156812Sraf setcontext(const ucontext_t *ucp) 5160Sstevel@tonic-gate { 5170Sstevel@tonic-gate ulwp_t *self = curthread; 5180Sstevel@tonic-gate int ret; 5190Sstevel@tonic-gate ucontext_t uc; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate /* 5220Sstevel@tonic-gate * Returning from the main context (uc_link == NULL) causes 5230Sstevel@tonic-gate * the thread to exit. See setcontext(2) and makecontext(3C). 5240Sstevel@tonic-gate */ 5250Sstevel@tonic-gate if (ucp == NULL) 5266812Sraf thr_exit(NULL); 5276515Sraf (void) memcpy(&uc, ucp, sizeof (uc)); 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate /* 5300Sstevel@tonic-gate * Restore previous signal mask and context link. 5310Sstevel@tonic-gate */ 5320Sstevel@tonic-gate if (uc.uc_flags & UC_SIGMASK) { 5330Sstevel@tonic-gate block_all_signals(self); 5340Sstevel@tonic-gate delete_reserved_signals(&uc.uc_sigmask); 5350Sstevel@tonic-gate self->ul_sigmask = uc.uc_sigmask; 5360Sstevel@tonic-gate if (self->ul_cursig) { 5370Sstevel@tonic-gate /* 5380Sstevel@tonic-gate * We have a deferred signal present. 5390Sstevel@tonic-gate * The signal mask will be set when the 5400Sstevel@tonic-gate * signal is taken in take_deferred_signal(). 5410Sstevel@tonic-gate */ 5420Sstevel@tonic-gate ASSERT(self->ul_critical + self->ul_sigdefer != 0); 5430Sstevel@tonic-gate uc.uc_flags &= ~UC_SIGMASK; 5440Sstevel@tonic-gate } 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate self->ul_siglink = uc.uc_link; 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate /* 5490Sstevel@tonic-gate * We don't know where this context structure has been. 5500Sstevel@tonic-gate * Preserve the curthread pointer, at least. 5512712Snn35248 * 5522712Snn35248 * Allow this feature to be disabled if a particular process 5532712Snn35248 * requests it. 5540Sstevel@tonic-gate */ 5552712Snn35248 if (setcontext_enforcement) { 5560Sstevel@tonic-gate #if defined(__sparc) 5572712Snn35248 uc.uc_mcontext.gregs[REG_G7] = (greg_t)self; 5580Sstevel@tonic-gate #elif defined(__amd64) 5593446Smrj uc.uc_mcontext.gregs[REG_FS] = (greg_t)0; /* null for fsbase */ 5600Sstevel@tonic-gate #elif defined(__i386) 5613446Smrj uc.uc_mcontext.gregs[GS] = (greg_t)LWPGS_SEL; 5620Sstevel@tonic-gate #else 5630Sstevel@tonic-gate #error "none of __sparc, __amd64, __i386 defined" 5640Sstevel@tonic-gate #endif 5652712Snn35248 } 5662712Snn35248 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * Make sure that if we return to a call to __lwp_park() 5690Sstevel@tonic-gate * or ___lwp_cond_wait() that it returns right away 5700Sstevel@tonic-gate * (giving us a spurious wakeup but not a deadlock). 5710Sstevel@tonic-gate */ 5720Sstevel@tonic-gate set_parking_flag(self, 0); 5730Sstevel@tonic-gate self->ul_sp = 0; 5746515Sraf ret = __setcontext(&uc); 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate /* 5770Sstevel@tonic-gate * It is OK for setcontext() to return if the user has not specified 5780Sstevel@tonic-gate * UC_CPU. 5790Sstevel@tonic-gate */ 5800Sstevel@tonic-gate if (uc.uc_flags & UC_CPU) 5810Sstevel@tonic-gate thr_panic("setcontext(): __setcontext() returned"); 5820Sstevel@tonic-gate return (ret); 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate 5856812Sraf #pragma weak _thr_sigsetmask = thr_sigsetmask 5860Sstevel@tonic-gate int 5876812Sraf thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset) 5880Sstevel@tonic-gate { 5890Sstevel@tonic-gate ulwp_t *self = curthread; 5900Sstevel@tonic-gate sigset_t saveset; 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate if (set == NULL) { 5930Sstevel@tonic-gate enter_critical(self); 5940Sstevel@tonic-gate if (oset != NULL) 5950Sstevel@tonic-gate *oset = self->ul_sigmask; 5960Sstevel@tonic-gate exit_critical(self); 5970Sstevel@tonic-gate } else { 5980Sstevel@tonic-gate switch (how) { 5990Sstevel@tonic-gate case SIG_BLOCK: 6000Sstevel@tonic-gate case SIG_UNBLOCK: 6010Sstevel@tonic-gate case SIG_SETMASK: 6020Sstevel@tonic-gate break; 6030Sstevel@tonic-gate default: 6040Sstevel@tonic-gate return (EINVAL); 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate /* 6080Sstevel@tonic-gate * The assignments to self->ul_sigmask must be protected from 6090Sstevel@tonic-gate * signals. The nuances of this code are subtle. Be careful. 6100Sstevel@tonic-gate */ 6110Sstevel@tonic-gate block_all_signals(self); 6120Sstevel@tonic-gate if (oset != NULL) 6130Sstevel@tonic-gate saveset = self->ul_sigmask; 6140Sstevel@tonic-gate switch (how) { 6150Sstevel@tonic-gate case SIG_BLOCK: 6160Sstevel@tonic-gate self->ul_sigmask.__sigbits[0] |= set->__sigbits[0]; 6170Sstevel@tonic-gate self->ul_sigmask.__sigbits[1] |= set->__sigbits[1]; 6180Sstevel@tonic-gate break; 6190Sstevel@tonic-gate case SIG_UNBLOCK: 6200Sstevel@tonic-gate self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0]; 6210Sstevel@tonic-gate self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1]; 6220Sstevel@tonic-gate break; 6230Sstevel@tonic-gate case SIG_SETMASK: 6240Sstevel@tonic-gate self->ul_sigmask.__sigbits[0] = set->__sigbits[0]; 6250Sstevel@tonic-gate self->ul_sigmask.__sigbits[1] = set->__sigbits[1]; 6260Sstevel@tonic-gate break; 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate delete_reserved_signals(&self->ul_sigmask); 6290Sstevel@tonic-gate if (oset != NULL) 6300Sstevel@tonic-gate *oset = saveset; 6310Sstevel@tonic-gate restore_signals(self); 6320Sstevel@tonic-gate } 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate return (0); 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6376812Sraf #pragma weak _pthread_sigmask = pthread_sigmask 6380Sstevel@tonic-gate int 6396812Sraf pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) 6406812Sraf { 6416812Sraf return (thr_sigsetmask(how, set, oset)); 6426812Sraf } 6436812Sraf 6446812Sraf #pragma weak _sigprocmask = sigprocmask 6456812Sraf int 6466812Sraf sigprocmask(int how, const sigset_t *set, sigset_t *oset) 6470Sstevel@tonic-gate { 6480Sstevel@tonic-gate int error; 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate /* 6510Sstevel@tonic-gate * Guard against children of vfork(). 6520Sstevel@tonic-gate */ 6530Sstevel@tonic-gate if (curthread->ul_vfork) 6540Sstevel@tonic-gate return (__lwp_sigmask(how, set, oset)); 6550Sstevel@tonic-gate 6566812Sraf if ((error = thr_sigsetmask(how, set, oset)) != 0) { 6570Sstevel@tonic-gate errno = error; 6580Sstevel@tonic-gate return (-1); 6590Sstevel@tonic-gate } 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate return (0); 6620Sstevel@tonic-gate } 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate /* 6650Sstevel@tonic-gate * Called at library initialization to set up signal handling. 6664570Sraf * All we really do is initialize the sig_lock rwlocks. 6670Sstevel@tonic-gate * All signal handlers are either SIG_DFL or SIG_IGN on exec(). 6680Sstevel@tonic-gate * However, if any signal handlers were established on alternate 6690Sstevel@tonic-gate * link maps before the primary link map has been initialized, 6700Sstevel@tonic-gate * then inform the kernel of the new sigacthandler. 6710Sstevel@tonic-gate */ 6720Sstevel@tonic-gate void 6730Sstevel@tonic-gate signal_init() 6740Sstevel@tonic-gate { 6750Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 6760Sstevel@tonic-gate struct sigaction *sap; 6770Sstevel@tonic-gate struct sigaction act; 6784570Sraf rwlock_t *rwlp; 6790Sstevel@tonic-gate int sig; 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate for (sig = 0; sig < NSIG; sig++) { 6824570Sraf rwlp = &udp->siguaction[sig].sig_lock; 6834570Sraf rwlp->rwlock_magic = RWL_MAGIC; 6844570Sraf rwlp->mutex.mutex_flag = LOCK_INITED; 6854570Sraf rwlp->mutex.mutex_magic = MUTEX_MAGIC; 6860Sstevel@tonic-gate sap = &udp->siguaction[sig].sig_uaction; 6870Sstevel@tonic-gate if (sap->sa_sigaction != SIG_DFL && 6880Sstevel@tonic-gate sap->sa_sigaction != SIG_IGN && 6890Sstevel@tonic-gate __sigaction(sig, NULL, &act) == 0 && 6900Sstevel@tonic-gate act.sa_sigaction != SIG_DFL && 6910Sstevel@tonic-gate act.sa_sigaction != SIG_IGN) { 6920Sstevel@tonic-gate act = *sap; 6930Sstevel@tonic-gate act.sa_flags &= ~SA_NODEFER; 6940Sstevel@tonic-gate act.sa_sigaction = udp->sigacthandler; 6950Sstevel@tonic-gate act.sa_mask = maskset; 6960Sstevel@tonic-gate (void) __sigaction(sig, &act, NULL); 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate } 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate /* 7020Sstevel@tonic-gate * Common code for cancelling self in _sigcancel() and pthread_cancel(). 7035891Sraf * First record the fact that a cancellation is pending. 7045891Sraf * Then, if cancellation is disabled or if we are holding unprotected 7055891Sraf * libc locks, just return to defer the cancellation. 7065891Sraf * Then, if we are at a cancellation point (ul_cancelable) just 7075891Sraf * return and let _canceloff() do the exit. 7085891Sraf * Else exit immediately if async mode is in effect. 7090Sstevel@tonic-gate */ 7100Sstevel@tonic-gate void 7115891Sraf do_sigcancel(void) 7120Sstevel@tonic-gate { 7130Sstevel@tonic-gate ulwp_t *self = curthread; 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate ASSERT(self->ul_critical == 0); 7160Sstevel@tonic-gate ASSERT(self->ul_sigdefer == 0); 7170Sstevel@tonic-gate self->ul_cancel_pending = 1; 7180Sstevel@tonic-gate if (self->ul_cancel_async && 7190Sstevel@tonic-gate !self->ul_cancel_disabled && 7205891Sraf self->ul_libc_locks == 0 && 7210Sstevel@tonic-gate !self->ul_cancelable) 7226812Sraf pthread_exit(PTHREAD_CANCELED); 7235891Sraf set_cancel_pending_flag(self, 0); 7240Sstevel@tonic-gate } 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate /* 7272248Sraf * Set up the SIGCANCEL handler for threads cancellation, 7282248Sraf * needed only when we have more than one thread, 7292248Sraf * or the SIGAIOCANCEL handler for aio cancellation, 7302248Sraf * called when aio is initialized, in __uaio_init(). 7310Sstevel@tonic-gate */ 7320Sstevel@tonic-gate void 7332248Sraf setup_cancelsig(int sig) 7340Sstevel@tonic-gate { 7350Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 7364570Sraf rwlock_t *rwlp = &udp->siguaction[sig].sig_lock; 7370Sstevel@tonic-gate struct sigaction act; 7380Sstevel@tonic-gate 7392248Sraf ASSERT(sig == SIGCANCEL || sig == SIGAIOCANCEL); 7404570Sraf lrw_rdlock(rwlp); 7412248Sraf act = udp->siguaction[sig].sig_uaction; 7424570Sraf lrw_unlock(rwlp); 7430Sstevel@tonic-gate if (act.sa_sigaction == SIG_DFL || 7440Sstevel@tonic-gate act.sa_sigaction == SIG_IGN) 7450Sstevel@tonic-gate act.sa_flags = SA_SIGINFO; 7460Sstevel@tonic-gate else { 7470Sstevel@tonic-gate act.sa_flags |= SA_SIGINFO; 7485891Sraf act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND | SA_RESTART); 7490Sstevel@tonic-gate } 7500Sstevel@tonic-gate act.sa_sigaction = udp->sigacthandler; 7510Sstevel@tonic-gate act.sa_mask = maskset; 7522248Sraf (void) __sigaction(sig, &act, NULL); 7530Sstevel@tonic-gate } 754