2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include "namespace.h"
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/signalvar.h>
42 #include "un-namespace.h"
43 #include "thr_private.h"
46 static inline void build_siginfo(siginfo_t *info, int signo);
47 #ifndef SYSTEM_SCOPE_ONLY
48 static struct pthread *thr_sig_find(struct kse *curkse, int sig,
51 static inline void thr_sigframe_restore(struct pthread *thread,
52 struct pthread_sigframe *psf);
53 static inline void thr_sigframe_save(struct pthread *thread,
54 struct pthread_sigframe *psf);
56 #define SA_KILL 0x01 /* terminates process by default */
60 static int sigproptbl[NSIG] = {
63 SA_KILL, /* SIGQUIT */
65 SA_KILL, /* SIGTRAP */
66 SA_KILL, /* SIGABRT */
69 SA_KILL, /* SIGKILL */
71 SA_KILL, /* SIGSEGV */
73 SA_KILL, /* SIGPIPE */
74 SA_KILL, /* SIGALRM */
75 SA_KILL, /* SIGTERM */
77 SA_STOP, /* SIGSTOP */
78 SA_STOP, /* SIGTSTP */
79 SA_CONT, /* SIGCONT */
81 SA_STOP, /* SIGTTIN */
82 SA_STOP, /* SIGTTOU */
84 SA_KILL, /* SIGXCPU */
85 SA_KILL, /* SIGXFSZ */
86 SA_KILL, /* SIGVTALRM */
87 SA_KILL, /* SIGPROF */
90 SA_KILL, /* SIGUSR1 */
94 /* #define DEBUG_SIGNAL */
96 #define DBG_MSG stdout_debug
102 * Signal setup and delivery.
104 * 1) Delivering signals to threads in the same KSE.
105 * These signals are sent by upcall events and are set in the
106 * km_sigscaught field of the KSE mailbox. Since these signals
107 * are received while operating on the KSE stack, they can be
108 * delivered either by using signalcontext() to add a stack frame
109 * to the target thread's stack, or by adding them in the thread's
110 * pending set and having the thread run them down after it
111 * 2) Delivering signals to threads in other KSEs/KSEGs.
112 * 3) Delivering signals to threads in critical regions.
113 * 4) Delivering signals to threads after they change their signal masks.
115 * Methods of delivering signals.
117 * 1) Add a signal frame to the thread's saved context.
118 * 2) Add the signal to the thread structure, mark the thread as
119 * having signals to handle, and let the thread run them down
120 * after it resumes from the KSE scheduler.
122 * Problem with 1). You can't do this to a running thread or a
123 * thread in a critical region.
125 * Problem with 2). You can't do this to a thread that doesn't
126 * yield in some way (explicitly enters the scheduler). A thread
127 * blocked in the kernel or a CPU hungry thread will not see the
128 * signal without entering the scheduler.
130 * The solution is to use both 1) and 2) to deliver signals:
132 * o Thread in critical region - use 2). When the thread
133 * leaves the critical region it will check to see if it
134 * has pending signals and run them down.
136 * o Thread enters scheduler explicitly - use 2). The thread
137 * can check for pending signals after it returns from the
140 * o Thread is running and not current thread - use 2). When the
141 * thread hits a condition specified by one of the other bullets,
142 * the signal will be delivered.
144 * o Thread is running and is current thread (e.g., the thread
145 * has just changed its signal mask and now sees that it has
146 * pending signals) - just run down the pending signals.
148 * o Thread is swapped out due to quantum expiration - use 1)
150 * o Thread is blocked in kernel - kse_thr_wakeup() and then
155 * Rules for selecting threads for signals received:
157 * 1) If the signal is a sychronous signal, it is delivered to
158 * the generating (current thread). If the thread has the
159 * signal masked, it is added to the threads pending signal
160 * set until the thread unmasks it.
162 * 2) A thread in sigwait() where the signal is in the thread's
165 * 3) A thread in sigsuspend() where the signal is not in the
166 * thread's suspended signal mask.
168 * 4) Any thread (first found/easiest to deliver) that has the
172 #ifndef SYSTEM_SCOPE_ONLY
175 sig_daemon(void *arg __unused)
182 struct pthread *curthread = _get_curthread();
184 DBG_MSG("signal daemon started(%p)\n", curthread);
186 curthread->name = strdup("signal thread");
187 crit = _kse_critical_enter();
188 curkse = _get_curkse();
191 * Daemon thread is a bound thread and we must be created with
196 __sys_sigprocmask(SIG_SETMASK, &set, NULL);
198 __sys_sigpending(&set);
202 KSE_LOCK_ACQUIRE(curkse, &_thread_signal_lock);
203 _thr_proc_sigpending = set;
204 KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
205 for (i = 1; i <= _SIG_MAXSIG; i++) {
206 if (SIGISMEMBER(set, i) != 0)
207 _thr_sig_dispatch(curkse, i,
208 NULL /* no siginfo */);
212 curkse->k_kcb->kcb_kmbx.km_flags =
213 KMF_NOUPCALL | KMF_NOCOMPLETED | KMF_WAITSIGEVENT;
215 curkse->k_kcb->kcb_kmbx.km_flags = 0;
216 set = curkse->k_kcb->kcb_kmbx.km_sigscaught;
222 /* Utility function to create signal daemon thread */
224 _thr_start_sig_daemon(void)
227 sigset_t sigset, oldset;
230 _pthread_sigmask(SIG_SETMASK, &sigset, &oldset);
231 _pthread_attr_init(&attr);
232 _pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
233 attr->flags |= THR_SIGNAL_THREAD;
234 /* sigmask will be inherited */
235 if (_pthread_create(&_thr_sig_daemon, &attr, sig_daemon, NULL))
236 PANIC("can not create signal daemon thread!\n");
237 _pthread_attr_destroy(&attr);
238 _pthread_sigmask(SIG_SETMASK, &oldset, NULL);
243 * This signal handler only delivers asynchronous signals.
244 * This must be called with upcalls disabled and without
248 _thr_sig_dispatch(struct kse *curkse, int sig, siginfo_t *info)
250 struct kse_mailbox *kmbx;
251 struct pthread *thread;
253 DBG_MSG(">>> _thr_sig_dispatch(%d)\n", sig);
255 /* Check if the signal requires a dump of thread information: */
256 if (_thr_dump_enabled() && (sig == SIGINFO)) {
257 /* Dump thread information to file: */
261 while ((thread = thr_sig_find(curkse, sig, info)) != NULL) {
263 * Setup the target thread to receive the signal:
265 DBG_MSG("Got signal %d, selecting thread %p\n", sig, thread);
266 KSE_SCHED_LOCK(curkse, thread->kseg);
267 if ((thread->state == PS_DEAD) ||
268 (thread->state == PS_DEADLOCK) ||
269 THR_IS_EXITING(thread) || THR_IS_SUSPENDED(thread)) {
270 KSE_SCHED_UNLOCK(curkse, thread->kseg);
271 _thr_ref_delete(NULL, thread);
272 } else if (SIGISMEMBER(thread->sigmask, sig)) {
273 KSE_SCHED_UNLOCK(curkse, thread->kseg);
274 _thr_ref_delete(NULL, thread);
276 kmbx = _thr_sig_add(thread, sig, info);
277 KSE_SCHED_UNLOCK(curkse, thread->kseg);
278 _thr_ref_delete(NULL, thread);
284 DBG_MSG("<<< _thr_sig_dispatch\n");
287 #endif /* ! SYSTEM_SCOPE_ONLY */
293 if (sig > 0 && sig < NSIG)
294 return (sigproptbl[_SIG_IDX(sig)]);
298 typedef void (*ohandler)(int sig, int code,
299 struct sigcontext *scp, char *addr, __sighandler_t *catcher);
302 _thr_sig_handler(int sig, siginfo_t *info, void *ucp_arg)
304 struct pthread_sigframe psf;
305 __siginfohandler_t *sigfunc;
306 struct pthread *curthread;
309 struct sigaction act;
310 int sa_flags, err_save;
313 ucp = (ucontext_t *)ucp_arg;
315 DBG_MSG(">>> _thr_sig_handler(%d)\n", sig);
317 curthread = _get_curthread();
318 if (curthread == NULL)
319 PANIC("No current thread.\n");
320 if (!(curthread->attr.flags & PTHREAD_SCOPE_SYSTEM))
321 PANIC("Thread is not system scope.\n");
322 if (curthread->flags & THR_FLAGS_EXITING) {
327 curkse = _get_curkse();
329 * If thread is in critical region or if thread is on
330 * the way of state transition, then latch signal into buffer.
332 if (_kse_in_critical() || THR_IN_CRITICAL(curthread) ||
333 curthread->state != PS_RUNNING) {
334 DBG_MSG(">>> _thr_sig_handler(%d) in critical\n", sig);
335 curthread->siginfo[sig-1] = *info;
336 curthread->check_pending = 1;
337 curkse->k_sigseqno++;
338 SIGADDSET(curthread->sigpend, sig);
340 * If the kse is on the way to idle itself, but
341 * we have signal ready, we should prevent it
342 * to sleep, kernel will latch the wakeup request,
343 * so kse_release will return from kernel immediately.
345 if (KSE_IS_IDLE(curkse))
346 kse_wakeup(&curkse->k_kcb->kcb_kmbx);
351 /* Check if the signal requires a dump of thread information: */
352 if (_thr_dump_enabled() && (sig == SIGINFO)) {
353 /* Dump thread information to file: */
357 /* Check the threads previous state: */
358 curthread->critical_count++;
359 if (curthread->sigbackout != NULL)
360 curthread->sigbackout((void *)curthread);
361 curthread->critical_count--;
362 thr_sigframe_save(curthread, &psf);
363 THR_ASSERT(!(curthread->sigbackout), "sigbackout was not cleared.");
365 _kse_critical_enter();
366 /* Get a fresh copy of signal mask */
367 __sys_sigprocmask(SIG_BLOCK, NULL, &curthread->sigmask);
368 KSE_LOCK_ACQUIRE(curkse, &_thread_signal_lock);
369 sigfunc = _thread_sigact[sig - 1].sa_sigaction;
370 sa_flags = _thread_sigact[sig - 1].sa_flags;
371 if (sa_flags & SA_RESETHAND) {
372 act.sa_handler = SIG_DFL;
373 act.sa_flags = SA_RESTART;
374 SIGEMPTYSET(act.sa_mask);
375 __sys_sigaction(sig, &act, NULL);
376 __sys_sigaction(sig, NULL, &_thread_sigact[sig - 1]);
378 KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
379 _kse_critical_leave(&curthread->tcb->tcb_tmbx);
381 /* Now invoke real handler */
382 if (((__sighandler_t *)sigfunc != SIG_DFL) &&
383 ((__sighandler_t *)sigfunc != SIG_IGN) &&
384 (sigfunc != (__siginfohandler_t *)_thr_sig_handler)) {
385 if ((sa_flags & SA_SIGINFO) != 0 || info == NULL)
386 (*(sigfunc))(sig, info, ucp);
388 ((ohandler)(*sigfunc))(
389 sig, info->si_code, (struct sigcontext *)ucp,
390 info->si_addr, (__sighandler_t *)sigfunc);
393 if ((__sighandler_t *)sigfunc == SIG_DFL) {
394 if (sigprop(sig) & SA_KILL) {
395 if (_kse_isthreaded())
396 kse_thr_interrupt(NULL,
397 KSE_INTR_SIGEXIT, sig);
402 else if (sigprop(sig) & SA_STOP)
403 kse_thr_interrupt(NULL, KSE_INTR_JOBSTOP, sig);
407 _kse_critical_enter();
408 curthread->sigmask = ucp->uc_sigmask;
409 SIG_CANTMASK(curthread->sigmask);
410 _kse_critical_leave(&curthread->tcb->tcb_tmbx);
412 thr_sigframe_restore(curthread, &psf);
414 DBG_MSG("<<< _thr_sig_handler(%d)\n", sig);
419 struct sighandle_info {
420 __siginfohandler_t *sigfunc;
427 static void handle_signal(struct pthread *curthread,
428 struct sighandle_info *shi);
429 static void handle_signal_altstack(struct pthread *curthread,
430 struct sighandle_info *shi);
432 /* Must be called with signal lock and schedule lock held in order */
434 thr_sig_invoke_handler(struct pthread *curthread, int sig, siginfo_t *info,
437 __siginfohandler_t *sigfunc;
441 struct sigaction act;
443 struct sighandle_info shi;
446 * Invoke the signal handler without going through the scheduler:
448 DBG_MSG("Got signal %d, calling handler for current thread %p\n",
451 if (!_kse_in_critical())
452 PANIC("thr_sig_invoke_handler without in critical\n");
453 curkse = curthread->kse;
455 * Check that a custom handler is installed and if
456 * the signal is not blocked:
458 sigfunc = _thread_sigact[sig - 1].sa_sigaction;
459 sa_flags = _thread_sigact[sig - 1].sa_flags;
460 sigmask = curthread->sigmask;
461 SIGSETOR(curthread->sigmask, _thread_sigact[sig - 1].sa_mask);
462 if (!(sa_flags & (SA_NODEFER | SA_RESETHAND)))
463 SIGADDSET(curthread->sigmask, sig);
464 if ((sig != SIGILL) && (sa_flags & SA_RESETHAND)) {
465 act.sa_handler = SIG_DFL;
466 act.sa_flags = SA_RESTART;
467 SIGEMPTYSET(act.sa_mask);
468 __sys_sigaction(sig, &act, NULL);
469 __sys_sigaction(sig, NULL, &_thread_sigact[sig - 1]);
471 KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
472 KSE_SCHED_UNLOCK(curkse, curkse->k_kseg);
474 * We are processing buffered signals, synchronize working
475 * signal mask into kernel.
477 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
478 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
479 onstack = _thr_sigonstack(&sigfunc);
480 ucp->uc_stack = curthread->sigstk;
481 ucp->uc_stack.ss_flags = (curthread->sigstk.ss_flags & SS_DISABLE)
482 ? SS_DISABLE : ((onstack) ? SS_ONSTACK : 0);
483 if (curthread->oldsigmask) {
484 ucp->uc_sigmask = *(curthread->oldsigmask);
485 curthread->oldsigmask = NULL;
487 ucp->uc_sigmask = sigmask;
488 shi.sigfunc = sigfunc;
490 shi.sa_flags = sa_flags;
493 if ((curthread->sigstk.ss_flags & SS_DISABLE) == 0) {
494 /* Deliver signal on alternative stack */
495 if (sa_flags & SA_ONSTACK && !onstack)
496 handle_signal_altstack(curthread, &shi);
498 handle_signal(curthread, &shi);
500 handle_signal(curthread, &shi);
503 _kse_critical_enter();
504 /* Don't trust after critical leave/enter */
505 curkse = curthread->kse;
508 * Restore the thread's signal mask.
510 curthread->sigmask = ucp->uc_sigmask;
511 SIG_CANTMASK(curthread->sigmask);
512 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
513 __sys_sigprocmask(SIG_SETMASK, &ucp->uc_sigmask, NULL);
514 KSE_SCHED_LOCK(curkse, curkse->k_kseg);
515 KSE_LOCK_ACQUIRE(curkse, &_thread_signal_lock);
517 DBG_MSG("Got signal %d, handler returned %p\n", sig, curthread);
521 handle_signal(struct pthread *curthread, struct sighandle_info *shi)
523 _kse_critical_leave(&curthread->tcb->tcb_tmbx);
525 /* Check if the signal requires a dump of thread information: */
526 if (_thr_dump_enabled() && (shi->sig == SIGINFO)) {
527 /* Dump thread information to file: */
531 if (((__sighandler_t *)shi->sigfunc != SIG_DFL) &&
532 ((__sighandler_t *)shi->sigfunc != SIG_IGN)) {
533 if ((shi->sa_flags & SA_SIGINFO) != 0 || shi->info == NULL)
534 (*(shi->sigfunc))(shi->sig, shi->info, shi->ucp);
536 ((ohandler)(*shi->sigfunc))(
537 shi->sig, shi->info->si_code,
538 (struct sigcontext *)shi->ucp,
540 (__sighandler_t *)shi->sigfunc);
543 if ((__sighandler_t *)shi->sigfunc == SIG_DFL) {
544 if (sigprop(shi->sig) & SA_KILL) {
545 if (_kse_isthreaded())
546 kse_thr_interrupt(NULL,
547 KSE_INTR_SIGEXIT, shi->sig);
549 kill(getpid(), shi->sig);
552 else if (sigprop(shi->sig) & SA_STOP)
553 kse_thr_interrupt(NULL, KSE_INTR_JOBSTOP,
561 handle_signal_wrapper(struct pthread *curthread, ucontext_t *ret_uc,
562 struct sighandle_info *shi)
564 shi->ucp->uc_stack.ss_flags = SS_ONSTACK;
565 handle_signal(curthread, shi);
566 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
569 /* Work around for ia64, THR_SETCONTEXT does not work */
570 _kse_critical_enter();
571 curthread->tcb->tcb_tmbx.tm_context = *ret_uc;
572 _thread_switch(curthread->kse->k_kcb, curthread->tcb, 1);
578 * Jump to stack set by sigaltstack before invoking signal handler
581 handle_signal_altstack(struct pthread *curthread, struct sighandle_info *shi)
584 ucontext_t uc1, *uc2;
586 THR_ASSERT(_kse_in_critical(), "Not in critical");
589 THR_GETCONTEXT(&uc1);
593 * We are still in critical region, it is safe to operate thread
596 uc2 = &curthread->tcb->tcb_tmbx.tm_context;
597 uc2->uc_stack = curthread->sigstk;
598 makecontext(uc2, (void (*)(void))handle_signal_wrapper,
599 3, curthread, &uc1, shi);
600 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
603 _thread_switch(curthread->kse->k_kcb, curthread->tcb, 1);
604 /* THR_SETCONTEXT(uc2); */
610 _thr_getprocsig(int sig, siginfo_t *siginfo)
616 DBG_MSG(">>> _thr_getprocsig\n");
618 crit = _kse_critical_enter();
619 curkse = _get_curkse();
620 KSE_LOCK_ACQUIRE(curkse, &_thread_signal_lock);
621 ret = _thr_getprocsig_unlocked(sig, siginfo);
622 KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
623 _kse_critical_leave(crit);
625 DBG_MSG("<<< _thr_getprocsig\n");
630 _thr_getprocsig_unlocked(int sig, siginfo_t *siginfo)
635 /* try to retrieve signal from kernel */
637 SIGADDSET(sigset, sig);
640 SIGDELSET(_thr_proc_sigpending, sig);
641 if (__sys_sigtimedwait(&sigset, siginfo, &ts) > 0)
646 #ifndef SYSTEM_SCOPE_ONLY
648 * Find a thread that can handle the signal. This must be called
649 * with upcalls disabled.
652 thr_sig_find(struct kse *curkse, int sig, siginfo_t *info __unused)
654 struct kse_mailbox *kmbx = NULL;
655 struct pthread *pthread;
656 struct pthread *suspended_thread, *signaled_thread;
657 __siginfohandler_t *sigfunc;
660 DBG_MSG("Looking for thread to handle signal %d\n", sig);
663 * Enter a loop to look for threads that have the signal
664 * unmasked. POSIX specifies that a thread in a sigwait
665 * will get the signal over any other threads. Second
666 * preference will be threads in a sigsuspend. Third
667 * preference will be the current thread. If none of the
668 * above, then the signal is delivered to the first thread
669 * that is found. Note that if a custom handler is not
670 * installed, the signal only affects threads in sigwait.
672 suspended_thread = NULL;
673 signaled_thread = NULL;
675 KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock);
676 TAILQ_FOREACH(pthread, &_thread_list, tle) {
677 if (pthread == _thr_sig_daemon)
679 /* Signal delivering to bound thread is done by kernel */
680 if (pthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
682 /* Take the scheduling lock. */
683 KSE_SCHED_LOCK(curkse, pthread->kseg);
684 if ((pthread->state == PS_DEAD) ||
685 (pthread->state == PS_DEADLOCK) ||
686 THR_IS_EXITING(pthread) ||
687 THR_IS_SUSPENDED(pthread)) {
688 ; /* Skip this thread. */
689 } else if (pthread->state == PS_SIGWAIT &&
690 SIGISMEMBER(*(pthread->data.sigwait->waitset), sig)) {
692 * retrieve signal from kernel, if it is job control
693 * signal, and sigaction is SIG_DFL, then we will
694 * be stopped in kernel, we hold lock here, but that
695 * does not matter, because that's job control, and
696 * whole process should be stopped.
698 if (_thr_getprocsig(sig, &si)) {
699 DBG_MSG("Waking thread %p in sigwait"
700 " with signal %d\n", pthread, sig);
701 /* where to put siginfo ? */
702 *(pthread->data.sigwait->siginfo) = si;
703 kmbx = _thr_setrunnable_unlocked(pthread);
705 KSE_SCHED_UNLOCK(curkse, pthread->kseg);
707 * POSIX doesn't doesn't specify which thread
708 * will get the signal if there are multiple
709 * waiters, so we give it to the first thread
712 * Do not attempt to deliver this signal
713 * to other threads and do not add the signal
714 * to the process pending set.
716 KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
719 if (suspended_thread != NULL)
720 _thr_ref_delete(NULL, suspended_thread);
721 if (signaled_thread != NULL)
722 _thr_ref_delete(NULL, signaled_thread);
724 } else if (!SIGISMEMBER(pthread->sigmask, sig)) {
726 * If debugger is running, we don't quick exit,
727 * and give it a chance to check the signal.
729 if (_libkse_debug == 0) {
730 sigfunc = _thread_sigact[sig - 1].sa_sigaction;
731 if ((__sighandler_t *)sigfunc == SIG_DFL) {
732 if (sigprop(sig) & SA_KILL) {
733 kse_thr_interrupt(NULL,
734 KSE_INTR_SIGEXIT, sig);
739 if (pthread->state == PS_SIGSUSPEND) {
740 if (suspended_thread == NULL) {
741 suspended_thread = pthread;
742 suspended_thread->refcount++;
744 } else if (signaled_thread == NULL) {
745 signaled_thread = pthread;
746 signaled_thread->refcount++;
749 KSE_SCHED_UNLOCK(curkse, pthread->kseg);
751 KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
753 if (suspended_thread != NULL) {
754 pthread = suspended_thread;
756 _thr_ref_delete(NULL, signaled_thread);
757 } else if (signaled_thread) {
758 pthread = signaled_thread;
764 #endif /* ! SYSTEM_SCOPE_ONLY */
767 build_siginfo(siginfo_t *info, int signo)
769 bzero(info, sizeof(*info));
770 info->si_signo = signo;
771 info->si_pid = _thr_pid;
775 * This is called by a thread when it has pending signals to deliver.
776 * It should only be called from the context of the thread.
779 _thr_sig_rundown(struct pthread *curthread, ucontext_t *ucp)
781 struct pthread_sigframe psf;
790 DBG_MSG(">>> thr_sig_rundown (%p)\n", curthread);
792 /* Check the threads previous state: */
793 curthread->critical_count++;
794 if (curthread->sigbackout != NULL)
795 curthread->sigbackout((void *)curthread);
796 curthread->critical_count--;
798 THR_ASSERT(!(curthread->sigbackout), "sigbackout was not cleared.");
799 THR_ASSERT((curthread->state == PS_RUNNING), "state is not PS_RUNNING");
801 thr_sigframe_save(curthread, &psf);
803 * Lower the priority before calling the handler in case
804 * it never returns (longjmps back):
806 crit = _kse_critical_enter();
807 curkse = curthread->kse;
808 KSE_SCHED_LOCK(curkse, curkse->k_kseg);
809 KSE_LOCK_ACQUIRE(curkse, &_thread_signal_lock);
810 curthread->active_priority &= ~THR_SIGNAL_PRIORITY;
814 * For bound thread, we mask all signals and get a fresh
815 * copy of signal mask from kernel
817 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
818 __sys_sigprocmask(SIG_SETMASK, &sigmask,
819 &curthread->sigmask);
821 for (i = 1; i <= _SIG_MAXSIG; i++) {
822 if (SIGISMEMBER(curthread->sigmask, i))
824 if (SIGISMEMBER(curthread->sigpend, i)) {
825 SIGDELSET(curthread->sigpend, i);
826 siginfo = curthread->siginfo[i-1];
829 if (!(curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
830 && SIGISMEMBER(_thr_proc_sigpending, i)) {
831 if (_thr_getprocsig_unlocked(i, &siginfo))
835 if (i <= _SIG_MAXSIG)
836 thr_sig_invoke_handler(curthread, i, &siginfo, ucp);
838 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
839 __sys_sigprocmask(SIG_SETMASK,
840 &curthread->sigmask, NULL);
846 /* Don't trust after signal handling */
847 curkse = curthread->kse;
848 KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
849 KSE_SCHED_UNLOCK(curkse, curkse->k_kseg);
850 _kse_critical_leave(&curthread->tcb->tcb_tmbx);
851 /* repost masked signal to kernel, it hardly happens in real world */
852 if ((curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) &&
853 !SIGISEMPTY(curthread->sigpend)) { /* dirty read */
854 __sys_sigprocmask(SIG_SETMASK, &sigmask, &curthread->sigmask);
855 for (i = 1; i <= _SIG_MAXSIG; ++i) {
856 if (SIGISMEMBER(curthread->sigpend, i)) {
857 SIGDELSET(curthread->sigpend, i);
858 if (!_kse_isthreaded())
862 &curthread->tcb->tcb_tmbx,
867 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
869 DBG_MSG("<<< thr_sig_rundown (%p)\n", curthread);
871 thr_sigframe_restore(curthread, &psf);
876 * This checks pending signals for the current thread. It should be
877 * called whenever a thread changes its signal mask. Note that this
878 * is called from a thread (using its stack).
880 * XXX - We might want to just check to see if there are pending
881 * signals for the thread here, but enter the UTS scheduler
882 * to actually install the signal handler(s).
885 _thr_sig_check_pending(struct pthread *curthread)
892 * If the thread is in critical region, delay processing signals.
893 * If the thread state is not PS_RUNNING, it might be switching
894 * into UTS and but a THR_LOCK_RELEASE saw check_pending, and it
895 * goes here, in the case we delay processing signals, lets UTS
896 * process complicated things, normally UTS will call _thr_sig_add
897 * to resume the thread, so we needn't repeat doing it here.
899 if (THR_IN_CRITICAL(curthread) || curthread->state != PS_RUNNING)
907 curthread->check_pending = 0;
908 _thr_sig_rundown(curthread, &uc);
914 * Perform thread specific actions in response to a signal.
915 * This function is only called if there is a handler installed
916 * for the signal, and if the target thread has the signal
919 * This must be called with the thread's scheduling lock held.
922 _thr_sig_add(struct pthread *pthread, int sig, siginfo_t *info)
926 struct kse_mailbox *kmbx = NULL;
927 struct pthread *curthread = _get_curthread();
929 int suppress_handler = 0;
931 __sighandler_t *sigfunc;
933 DBG_MSG(">>> _thr_sig_add %p (%d)\n", pthread, sig);
935 curkse = _get_curkse();
936 restart = _thread_sigact[sig - 1].sa_flags & SA_RESTART;
937 sigfunc = _thread_sigact[sig - 1].sa_handler;
938 fromproc = (curthread == _thr_sig_daemon);
940 if (pthread->state == PS_DEAD || pthread->state == PS_DEADLOCK ||
941 pthread->state == PS_STATE_MAX)
942 return (NULL); /* return false */
944 if ((pthread->attr.flags & PTHREAD_SCOPE_SYSTEM) &&
945 (curthread != pthread)) {
946 PANIC("Please use _thr_send_sig for bound thread");
950 if (pthread->state != PS_SIGWAIT &&
951 SIGISMEMBER(pthread->sigmask, sig)) {
952 /* signal is masked, just add signal to thread. */
954 SIGADDSET(pthread->sigpend, sig);
956 build_siginfo(&pthread->siginfo[sig-1], sig);
957 else if (info != &pthread->siginfo[sig-1])
958 memcpy(&pthread->siginfo[sig-1], info,
961 if (!_thr_getprocsig(sig, &pthread->siginfo[sig-1]))
963 SIGADDSET(pthread->sigpend, sig);
967 /* if process signal not exists, just return */
969 if (!_thr_getprocsig(sig, &siginfo))
974 if (pthread->state != PS_SIGWAIT && sigfunc == SIG_DFL &&
975 (sigprop(sig) & SA_KILL)) {
976 kse_thr_interrupt(NULL, KSE_INTR_SIGEXIT, sig);
981 * Process according to thread state:
983 switch (pthread->state) {
987 return (NULL); /* XXX return false */
991 * You can't call a signal handler for threads in these
994 suppress_handler = 1;
997 if ((pthread->flags & THR_FLAGS_IN_RUNQ)) {
998 THR_RUNQ_REMOVE(pthread);
999 pthread->active_priority |= THR_SIGNAL_PRIORITY;
1000 THR_RUNQ_INSERT_TAIL(pthread);
1002 /* Possible not in RUNQ and has curframe ? */
1003 pthread->active_priority |= THR_SIGNAL_PRIORITY;
1007 * States which cannot be interrupted but still require the
1008 * signal handler to run:
1016 * Unmasked signals always cause sleep to terminate
1017 * early regardless of SA_RESTART:
1019 pthread->interrupted = 1;
1026 pthread->interrupted = 1;
1031 build_siginfo(&pthread->siginfo[sig-1], sig);
1032 else if (info != &pthread->siginfo[sig-1])
1033 memcpy(&pthread->siginfo[sig-1], info,
1036 * The signal handler is not called for threads in
1039 suppress_handler = 1;
1040 /* Wake up the thread if the signal is not blocked. */
1041 if (SIGISMEMBER(*(pthread->data.sigwait->waitset), sig)) {
1042 /* Return the signal number: */
1043 *(pthread->data.sigwait->siginfo) = pthread->siginfo[sig-1];
1044 /* Make the thread runnable: */
1045 kmbx = _thr_setrunnable_unlocked(pthread);
1047 /* Increment the pending signal count. */
1048 SIGADDSET(pthread->sigpend, sig);
1049 if (!SIGISMEMBER(pthread->sigmask, sig)) {
1050 if (sigfunc == SIG_DFL &&
1051 sigprop(sig) & SA_KILL) {
1052 kse_thr_interrupt(NULL,
1057 pthread->check_pending = 1;
1058 pthread->interrupted = 1;
1059 kmbx = _thr_setrunnable_unlocked(pthread);
1065 SIGADDSET(pthread->sigpend, sig);
1067 build_siginfo(&pthread->siginfo[sig-1], sig);
1068 else if (info != &pthread->siginfo[sig-1])
1069 memcpy(&pthread->siginfo[sig-1], info, sizeof(*info));
1070 pthread->check_pending = 1;
1071 if (!(pthread->attr.flags & PTHREAD_SCOPE_SYSTEM) &&
1072 (pthread->blocked != 0) && !THR_IN_CRITICAL(pthread))
1073 kse_thr_interrupt(&pthread->tcb->tcb_tmbx,
1074 restart ? KSE_INTR_RESTART : KSE_INTR_INTERRUPT, 0);
1075 if (suppress_handler == 0) {
1077 * Setup a signal frame and save the current threads
1080 if (pthread->state != PS_RUNNING) {
1081 if (pthread->flags & THR_FLAGS_IN_RUNQ)
1082 THR_RUNQ_REMOVE(pthread);
1083 pthread->active_priority |= THR_SIGNAL_PRIORITY;
1084 kmbx = _thr_setrunnable_unlocked(pthread);
1092 * Send a signal to a specific thread (ala pthread_kill):
1095 _thr_sig_send(struct pthread *pthread, int sig)
1097 struct pthread *curthread = _get_curthread();
1098 struct kse_mailbox *kmbx;
1100 if (pthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
1101 kse_thr_interrupt(&pthread->tcb->tcb_tmbx, KSE_INTR_SENDSIG, sig);
1105 /* Lock the scheduling queue of the target thread. */
1106 THR_SCHED_LOCK(curthread, pthread);
1107 if (_thread_sigact[sig - 1].sa_handler != SIG_IGN) {
1108 kmbx = _thr_sig_add(pthread, sig, NULL);
1109 /* Add a preemption point. */
1110 if (kmbx == NULL && (curthread->kseg == pthread->kseg) &&
1111 (pthread->active_priority > curthread->active_priority))
1112 curthread->critical_yield = 1;
1113 THR_SCHED_UNLOCK(curthread, pthread);
1117 * If thread sent signal to itself, check signals now.
1118 * It is not really needed, _kse_critical_leave should
1119 * have already checked signals.
1121 if (pthread == curthread && curthread->check_pending)
1122 _thr_sig_check_pending(curthread);
1125 THR_SCHED_UNLOCK(curthread, pthread);
1130 thr_sigframe_restore(struct pthread *curthread, struct pthread_sigframe *psf)
1132 kse_critical_t crit;
1135 THR_THREAD_LOCK(curthread, curthread);
1136 curthread->cancelflags = psf->psf_cancelflags;
1137 crit = _kse_critical_enter();
1138 curkse = curthread->kse;
1139 KSE_SCHED_LOCK(curkse, curthread->kseg);
1140 curthread->flags = psf->psf_flags;
1141 curthread->interrupted = psf->psf_interrupted;
1142 curthread->timeout = psf->psf_timeout;
1143 curthread->data = psf->psf_wait_data;
1144 curthread->wakeup_time = psf->psf_wakeup_time;
1145 curthread->continuation = psf->psf_continuation;
1146 KSE_SCHED_UNLOCK(curkse, curthread->kseg);
1147 _kse_critical_leave(crit);
1148 THR_THREAD_UNLOCK(curthread, curthread);
1152 thr_sigframe_save(struct pthread *curthread, struct pthread_sigframe *psf)
1154 kse_critical_t crit;
1157 THR_THREAD_LOCK(curthread, curthread);
1158 psf->psf_cancelflags = curthread->cancelflags;
1159 crit = _kse_critical_enter();
1160 curkse = curthread->kse;
1161 KSE_SCHED_LOCK(curkse, curthread->kseg);
1162 /* This has to initialize all members of the sigframe. */
1163 psf->psf_flags = (curthread->flags & (THR_FLAGS_PRIVATE | THR_FLAGS_EXITING));
1164 psf->psf_interrupted = curthread->interrupted;
1165 psf->psf_timeout = curthread->timeout;
1166 psf->psf_wait_data = curthread->data;
1167 psf->psf_wakeup_time = curthread->wakeup_time;
1168 psf->psf_continuation = curthread->continuation;
1169 KSE_SCHED_UNLOCK(curkse, curthread->kseg);
1170 _kse_critical_leave(crit);
1171 THR_THREAD_UNLOCK(curthread, curthread);
1175 _thr_signal_init(void)
1177 struct sigaction act;
1178 __siginfohandler_t *sigfunc;
1183 __sys_sigprocmask(SIG_SETMASK, &sigset, &_thr_initial->sigmask);
1184 /* Enter a loop to get the existing signal status: */
1185 for (i = 1; i <= _SIG_MAXSIG; i++) {
1186 /* Get the signal handler details: */
1187 if (__sys_sigaction(i, NULL, &_thread_sigact[i - 1]) != 0) {
1189 * Abort this process if signal
1190 * initialisation fails:
1192 PANIC("Cannot read signal handler info");
1194 /* Intall wrapper if handler was set */
1195 sigfunc = _thread_sigact[i - 1].sa_sigaction;
1196 if (((__sighandler_t *)sigfunc) != SIG_DFL &&
1197 ((__sighandler_t *)sigfunc) != SIG_IGN) {
1198 act = _thread_sigact[i - 1];
1199 act.sa_flags |= SA_SIGINFO;
1201 (__siginfohandler_t *)_thr_sig_handler;
1202 __sys_sigaction(i, &act, NULL);
1205 if (_thr_dump_enabled()) {
1207 * Install the signal handler for SIGINFO. It isn't
1208 * really needed, but it is nice to have for debugging
1211 _thread_sigact[SIGINFO - 1].sa_flags = SA_SIGINFO | SA_RESTART;
1212 SIGEMPTYSET(act.sa_mask);
1213 act.sa_flags = SA_SIGINFO | SA_RESTART;
1214 act.sa_sigaction = (__siginfohandler_t *)&_thr_sig_handler;
1215 if (__sys_sigaction(SIGINFO, &act, NULL) != 0) {
1216 __sys_sigprocmask(SIG_SETMASK, &_thr_initial->sigmask,
1219 * Abort this process if signal initialisation fails:
1221 PANIC("Cannot initialize signal handler");
1224 __sys_sigprocmask(SIG_SETMASK, &_thr_initial->sigmask, NULL);
1225 __sys_sigaltstack(NULL, &_thr_initial->sigstk);
1229 _thr_signal_deinit(void)
1232 struct pthread *curthread = _get_curthread();
1234 /* Clear process pending signals. */
1235 sigemptyset(&_thr_proc_sigpending);
1237 /* Enter a loop to get the existing signal status: */
1238 for (i = 1; i <= _SIG_MAXSIG; i++) {
1239 /* Check for signals which cannot be trapped: */
1240 if (i == SIGKILL || i == SIGSTOP) {
1243 /* Set the signal handler details: */
1244 else if (__sys_sigaction(i, &_thread_sigact[i - 1],
1247 * Abort this process if signal
1248 * initialisation fails:
1250 PANIC("Cannot set signal handler info");
1253 __sys_sigaltstack(&curthread->sigstk, NULL);