/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "lint.h"
#include <sys/feature_tests.h>
/*
* setcontext() really can return, if UC_CPU is not specified.
* Make the compiler shut up about it.
*/
#if defined(__NORETURN)
#endif
#define __NORETURN
#include "thr_uberdata.h"
#include "asyncio.h"
#include <signal.h>
#include <siginfo.h>
/* maskable signals */
/*
* Return true if the valid signal bits in both sets are the same.
*/
int
{
/*
* We only test valid signal bits, not rubbish following MAXSIG
* (for speed). Algorithm:
* if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0
*/
/* see lib/libc/inc/thr_uberdata.h for why this must be true */
#else
#error "fix me: MAXSIG out of bounds"
#endif
}
/*
* Common code for calling the user-specified signal handler.
*/
void
{
/*
* If we are taking a signal while parked or about to be parked
* on __lwp_park() then remove ourself from the sleep queue so
* that we can grab locks. The code in mutex_lock_queue() and
* cond_wait_common() will detect this and deal with it when
* __lwp_park() returns.
*/
unsleep_self();
set_parking_flag(self, 0);
}
/*
* Get a self-consistent set of flags, handler, and mask
* while holding the sig's sig_lock for the least possible time.
* We must acquire the sig's sig_lock because some thread running
* in sigaction() might be establishing a new signal handler.
* The code in sigaction() acquires the writer lock; here
* we acquire the readers lock to ehance concurrency in the
* face of heavy signal traffic, such as generated by java.
*
* Locking exceptions:
* No locking for a child of vfork().
* If the signal is SIGPROF with an si_code of PROF_SIG,
* then we assume that this signal was generated by
* setitimer(ITIMER_REALPROF) set up by the dbx collector.
* If the signal is SIGEMT with an si_code of EMT_CPCOVF,
* then we assume that the signal was generated by
* a hardware performance counter overflow.
* In these cases, assume that we need no locking. It is the
* monitoring program's responsibility to ensure correctness.
*/
/* we wish this assignment could be atomic */
} else {
}
/*
* Set the proper signal mask and call the user's signal handler.
* (We overrode the user-requested signal mask with maskset
* so we currently have all blockable signals blocked.)
*
* We would like to ASSERT() that the signal is not a member of the
* signal mask at the previous level (ucp->uc_sigmask) or the specified
* signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but
* /proc can override this via PCSSIG, so we don't bother.
*
* We would also like to ASSERT() that the signal mask at the previous
* level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()),
* but /proc can change the thread's signal mask via PCSHOLD, so we
* don't bother with that either.
*/
if (self->ul_sigsuspend) {
self->ul_sigsuspend = 0;
/* the sigsuspend() or pollsys() signal mask */
} else {
/* the signal mask at the previous level */
}
/*
* If this thread has been sent SIGCANCEL from the kernel
* or from pthread_cancel(), it is being asked to exit.
* The kernel may send SIGCANCEL without a siginfo struct.
* If the SIGCANCEL is process-directed (from kill() or
* sigqueue()), treat it as an ordinary signal.
*/
do_sigcancel();
goto out;
}
/* SIGCANCEL is ignored by default */
goto out;
}
/*
* If this thread has been sent SIGAIOCANCEL (SIGLWP) and
* we are an aio worker thread, cancel the aio request.
*/
if (sig == SIGAIOCANCEL) {
/* SIGLWP is ignored by default */
goto out;
}
/*
* If this is a floating point exception and the queue
* is non-empty, pop the top entry from the queue. This
* is to maintain expected behavior.
*/
unsigned char i;
}
}
#endif /* sparc */
out:
(void) setcontext(ucp);
thr_panic("call_user_handler(): setcontext() returned");
}
/*
* take_deferred_signal() is called when ul_critical and ul_sigdefer become
* zero and a deferred signal has been recorded on the current thread.
* We are out of the critical region and are ready to take a signal.
* The kernel has all signals blocked on this lwp, but our value of
* ul_sigmask is the correct signal mask for the previous context.
*
* We call __sigresend() to atomically restore the signal mask and
* cause the signal to be sent again with the remembered siginfo.
* We will not return successfully from __sigresend() until the
* application's signal handler has been run via sigacthandler().
*/
void
{
int error;
/*
* If the signal handler was established with SA_RESETHAND,
* the kernel has reset the handler to SIG_DFL, so we have
* to reestablish the handler now so that it will be entered
* again when we call __sigresend(), below.
*
* Logically, we should acquire and release the signal's
* sig_lock around this operation to protect the integrity
* of the signal action while we copy it, as is done below
* in _libc_sigaction(). However, we may be on a user-level
* sleep queue at this point and lrw_wrlock(&suap->sig_lock)
* might attempt to sleep on a different sleep queue and
* that would corrupt the entire sleep queue mechanism.
*
* If we are on a sleep queue we will remove ourself from
* it in call_user_handler(), called from sigacthandler(),
* before entering the application's signal handler.
* In the meantime, we must not acquire any locks.
*/
}
else
/* EAGAIN can happen only for a pending SIGSTOP signal */
continue;
if (error)
thr_panic("take_deferred_signal(): __sigresend() failed");
}
void
{
/*
* Do this in case we took a signal while in a cancelable system call.
* It does no harm if we were not in such a system call.
*/
/*
* If this thread has performed a longjmp() from a signal handler
* back to main level some time in the past, it has left the kernel
* thinking that it is still in the signal context. We repair this
* possible damage by setting ucp->uc_link to NULL if we know that
* we are actually executing at main level (self->ul_siglink == NULL).
* See the code for setjmp()/longjmp() for more details.
*/
/*
* If we are not in a critical region and are
* not deferring signals, take the signal now.
*/
/*
* On the surface, the following call seems redundant
* because call_user_handler() cannot return. However,
* we don't want to return from here because the compiler
* might recycle our frame. We want to keep it on the
* stack to assist debuggers such as pstack in identifying
* signal frames. The call to thr_panic() serves to prevent
* tail-call optimisation here.
*/
thr_panic("sigacthandler(): call_user_handler() returned");
}
/*
* We are in a critical region or we are deferring signals. When
* we emerge from the region we will call take_deferred_signal().
*/
else
/*
* 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);
/*
* Return to the previous context with all signals blocked.
* We will restore the signal mask in take_deferred_signal().
* Note that we are calling the system call trap here, not
* the setcontext() wrapper. We don't want to change the
* thread's ul_sigmask by this operation.
*/
(void) __setcontext(ucp);
thr_panic("sigacthandler(): __setcontext() returned");
}
int
{
int rv;
return (-1);
}
#if !defined(_LP64)
#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.
*/
/*
* Always catch these signals.
* We need SIGCANCEL for pthread_cancel() to work.
* We need SIGAIOCANCEL for aio_cancel() to work.
*/
else {
}
}
}
/*
* We detect setting the disposition of SIGIO just to set the
* _sigio_enabled flag for the asynchronous i/o (aio) code.
*/
}
return (rv);
}
/*
* This is a private interface for the linux brand interface.
*/
void
void (**osigacthandler)(int, siginfo_t *, void *))
{
if (osigacthandler != NULL)
}
/*
* 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
{
else
}
/*
* 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.)
*/
void
{
}
int
{
int ret;
/*
* Returning from the main context (uc_link == NULL) causes
* the thread to exit. See setcontext(2) and makecontext(3C).
*/
/*
* Restore previous signal mask and context link.
*/
/*
* We have a deferred signal present.
* The signal mask will be set when the
* signal is taken in take_deferred_signal().
*/
}
}
/*
* 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)
#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);
/*
* It is OK for setcontext() to return if the user has not specified
* UC_CPU.
*/
thr_panic("setcontext(): __setcontext() returned");
return (ret);
}
int
{
} 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.
*/
switch (how) {
case SIG_BLOCK:
break;
case SIG_UNBLOCK:
break;
case SIG_SETMASK:
break;
}
}
return (0);
}
int
{
}
int
{
int error;
/*
* Guard against children of vfork().
*/
return (-1);
}
return (0);
}
/*
* Called at library initialization to set up signal handling.
* All we really do is initialize the sig_lock rwlocks.
* 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
{
int sig;
}
}
}
/*
* Common code for cancelling self in _sigcancel() and pthread_cancel().
* First record the fact that a cancellation is pending.
* Then, if cancellation is disabled or if we are holding unprotected
* libc locks, just return to defer the cancellation.
* Then, if we are at a cancellation point (ul_cancelable) just
* return and let _canceloff() do the exit.
* Else exit immediately if async mode is in effect.
*/
void
do_sigcancel(void)
{
if (self->ul_cancel_async &&
!self->ul_cancel_disabled &&
self->ul_libc_locks == 0 &&
}
/*
* 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
{
else {
}
}