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
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/signalvar.h>
40 #include "thr_private.h"
43 static inline void build_siginfo(siginfo_t *info, int signo);
44 #ifndef SYSTEM_SCOPE_ONLY
45 static struct pthread *thr_sig_find(struct kse *curkse, int sig,
48 static inline void thr_sigframe_restore(struct pthread *thread,
49 struct pthread_sigframe *psf);
50 static inline void thr_sigframe_save(struct pthread *thread,
51 struct pthread_sigframe *psf);
53 #define SA_KILL 0x01 /* terminates process by default */
57 static int sigproptbl[NSIG] = {
60 SA_KILL, /* SIGQUIT */
62 SA_KILL, /* SIGTRAP */
63 SA_KILL, /* SIGABRT */
66 SA_KILL, /* SIGKILL */
68 SA_KILL, /* SIGSEGV */
70 SA_KILL, /* SIGPIPE */
71 SA_KILL, /* SIGALRM */
72 SA_KILL, /* SIGTERM */
74 SA_STOP, /* SIGSTOP */
75 SA_STOP, /* SIGTSTP */
76 SA_CONT, /* SIGCONT */
78 SA_STOP, /* SIGTTIN */
79 SA_STOP, /* SIGTTOU */
81 SA_KILL, /* SIGXCPU */
82 SA_KILL, /* SIGXFSZ */
83 SA_KILL, /* SIGVTALRM */
84 SA_KILL, /* SIGPROF */
87 SA_KILL, /* SIGUSR1 */
91 /* #define DEBUG_SIGNAL */
93 #define DBG_MSG stdout_debug
99 * Signal setup and delivery.
101 * 1) Delivering signals to threads in the same KSE.
102 * These signals are sent by upcall events and are set in the
103 * km_sigscaught field of the KSE mailbox. Since these signals
104 * are received while operating on the KSE stack, they can be
105 * delivered either by using signalcontext() to add a stack frame
106 * to the target thread's stack, or by adding them in the thread's
107 * pending set and having the thread run them down after it
108 * 2) Delivering signals to threads in other KSEs/KSEGs.
109 * 3) Delivering signals to threads in critical regions.
110 * 4) Delivering signals to threads after they change their signal masks.
112 * Methods of delivering signals.
114 * 1) Add a signal frame to the thread's saved context.
115 * 2) Add the signal to the thread structure, mark the thread as
116 * having signals to handle, and let the thread run them down
117 * after it resumes from the KSE scheduler.
119 * Problem with 1). You can't do this to a running thread or a
120 * thread in a critical region.
122 * Problem with 2). You can't do this to a thread that doesn't
123 * yield in some way (explicitly enters the scheduler). A thread
124 * blocked in the kernel or a CPU hungry thread will not see the
125 * signal without entering the scheduler.
127 * The solution is to use both 1) and 2) to deliver signals:
129 * o Thread in critical region - use 2). When the thread
130 * leaves the critical region it will check to see if it
131 * has pending signals and run them down.
133 * o Thread enters scheduler explicitly - use 2). The thread
134 * can check for pending signals after it returns from the
137 * o Thread is running and not current thread - use 2). When the
138 * thread hits a condition specified by one of the other bullets,
139 * the signal will be delivered.
141 * o Thread is running and is current thread (e.g., the thread
142 * has just changed its signal mask and now sees that it has
143 * pending signals) - just run down the pending signals.
145 * o Thread is swapped out due to quantum expiration - use 1)
147 * o Thread is blocked in kernel - kse_thr_wakeup() and then
152 * Rules for selecting threads for signals received:
154 * 1) If the signal is a sychronous signal, it is delivered to
155 * the generating (current thread). If the thread has the
156 * signal masked, it is added to the threads pending signal
157 * set until the thread unmasks it.
159 * 2) A thread in sigwait() where the signal is in the thread's
162 * 3) A thread in sigsuspend() where the signal is not in the
163 * thread's suspended signal mask.
165 * 4) Any thread (first found/easiest to deliver) that has the
169 #ifndef SYSTEM_SCOPE_ONLY
172 sig_daemon(void *arg /* Unused */)
179 struct pthread *curthread = _get_curthread();
181 DBG_MSG("signal daemon started(%p)\n", curthread);
183 curthread->name = strdup("signal thread");
184 crit = _kse_critical_enter();
185 curkse = _get_curkse();
188 * Daemon thread is a bound thread and we must be created with
193 __sys_sigprocmask(SIG_SETMASK, &set, NULL);
195 __sys_sigpending(&set);
199 KSE_LOCK_ACQUIRE(curkse, &_thread_signal_lock);
200 _thr_proc_sigpending = set;
201 KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
202 for (i = 1; i <= _SIG_MAXSIG; i++) {
203 if (SIGISMEMBER(set, i) != 0)
204 _thr_sig_dispatch(curkse, i,
205 NULL /* no siginfo */);
209 curkse->k_kcb->kcb_kmbx.km_flags =
210 KMF_NOUPCALL | KMF_NOCOMPLETED | KMF_WAITSIGEVENT;
212 curkse->k_kcb->kcb_kmbx.km_flags = 0;
213 set = curkse->k_kcb->kcb_kmbx.km_sigscaught;
219 /* Utility function to create signal daemon thread */
221 _thr_start_sig_daemon(void)
224 sigset_t sigset, oldset;
227 pthread_sigmask(SIG_SETMASK, &sigset, &oldset);
228 pthread_attr_init(&attr);
229 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
230 attr->flags |= THR_SIGNAL_THREAD;
231 /* sigmask will be inherited */
232 if (pthread_create(&_thr_sig_daemon, &attr, sig_daemon, NULL))
233 PANIC("can not create signal daemon thread!\n");
234 pthread_attr_destroy(&attr);
235 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
240 * This signal handler only delivers asynchronous signals.
241 * This must be called with upcalls disabled and without
245 _thr_sig_dispatch(struct kse *curkse, int sig, siginfo_t *info)
247 struct kse_mailbox *kmbx;
248 struct pthread *thread;
250 DBG_MSG(">>> _thr_sig_dispatch(%d)\n", sig);
252 /* Check if the signal requires a dump of thread information: */
253 if (_thr_dump_enabled() && (sig == SIGINFO)) {
254 /* Dump thread information to file: */
258 while ((thread = thr_sig_find(curkse, sig, info)) != NULL) {
260 * Setup the target thread to receive the signal:
262 DBG_MSG("Got signal %d, selecting thread %p\n", sig, thread);
263 KSE_SCHED_LOCK(curkse, thread->kseg);
264 if ((thread->state == PS_DEAD) ||
265 (thread->state == PS_DEADLOCK) ||
266 THR_IS_EXITING(thread) || THR_IS_SUSPENDED(thread)) {
267 KSE_SCHED_UNLOCK(curkse, thread->kseg);
268 _thr_ref_delete(NULL, thread);
269 } else if (SIGISMEMBER(thread->sigmask, sig)) {
270 KSE_SCHED_UNLOCK(curkse, thread->kseg);
271 _thr_ref_delete(NULL, thread);
273 kmbx = _thr_sig_add(thread, sig, info);
274 KSE_SCHED_UNLOCK(curkse, thread->kseg);
275 _thr_ref_delete(NULL, thread);
281 DBG_MSG("<<< _thr_sig_dispatch\n");
284 #endif /* ! SYSTEM_SCOPE_ONLY */
290 if (sig > 0 && sig < NSIG)
291 return (sigproptbl[_SIG_IDX(sig)]);
295 typedef void (*ohandler)(int sig, int code,
296 struct sigcontext *scp, char *addr, __sighandler_t *catcher);
299 _thr_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp)
301 struct pthread_sigframe psf;
302 __siginfohandler_t *sigfunc;
303 struct pthread *curthread;
305 struct sigaction act;
306 int sa_flags, err_save;
310 DBG_MSG(">>> _thr_sig_handler(%d)\n", sig);
312 curthread = _get_curthread();
313 if (curthread == NULL)
314 PANIC("No current thread.\n");
315 if (!(curthread->attr.flags & PTHREAD_SCOPE_SYSTEM))
316 PANIC("Thread is not system scope.\n");
317 if (curthread->flags & THR_FLAGS_EXITING) {
322 curkse = _get_curkse();
324 * If thread is in critical region or if thread is on
325 * the way of state transition, then latch signal into buffer.
327 if (_kse_in_critical() || THR_IN_CRITICAL(curthread) ||
328 curthread->state != PS_RUNNING) {
329 DBG_MSG(">>> _thr_sig_handler(%d) in critical\n", sig);
330 curthread->siginfo[sig-1] = *info;
331 curthread->check_pending = 1;
332 curkse->k_sigseqno++;
333 SIGADDSET(curthread->sigpend, sig);
335 * If the kse is on the way to idle itself, but
336 * we have signal ready, we should prevent it
337 * to sleep, kernel will latch the wakeup request,
338 * so kse_release will return from kernel immediately.
340 if (KSE_IS_IDLE(curkse))
341 kse_wakeup(&curkse->k_kcb->kcb_kmbx);
346 /* Check if the signal requires a dump of thread information: */
347 if (_thr_dump_enabled() && (sig == SIGINFO)) {
348 /* Dump thread information to file: */
352 /* Check the threads previous state: */
353 curthread->critical_count++;
354 if (curthread->sigbackout != NULL)
355 curthread->sigbackout((void *)curthread);
356 curthread->critical_count--;
357 thr_sigframe_save(curthread, &psf);
358 THR_ASSERT(!(curthread->sigbackout), "sigbackout was not cleared.");
360 _kse_critical_enter();
361 /* Get a fresh copy of signal mask */
362 __sys_sigprocmask(SIG_BLOCK, NULL, &curthread->sigmask);
363 KSE_LOCK_ACQUIRE(curkse, &_thread_signal_lock);
364 sigfunc = _thread_sigact[sig - 1].sa_sigaction;
365 sa_flags = _thread_sigact[sig - 1].sa_flags;
366 if (sa_flags & SA_RESETHAND) {
367 act.sa_handler = SIG_DFL;
368 act.sa_flags = SA_RESTART;
369 SIGEMPTYSET(act.sa_mask);
370 __sys_sigaction(sig, &act, NULL);
371 __sys_sigaction(sig, NULL, &_thread_sigact[sig - 1]);
373 KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
374 _kse_critical_leave(&curthread->tcb->tcb_tmbx);
376 /* Now invoke real handler */
377 if (((__sighandler_t *)sigfunc != SIG_DFL) &&
378 ((__sighandler_t *)sigfunc != SIG_IGN) &&
379 (sigfunc != (__siginfohandler_t *)_thr_sig_handler)) {
380 if ((sa_flags & SA_SIGINFO) != 0 || info == NULL)
381 (*(sigfunc))(sig, info, ucp);
383 ((ohandler)(*sigfunc))(
384 sig, info->si_code, (struct sigcontext *)ucp,
385 info->si_addr, (__sighandler_t *)sigfunc);
388 if ((__sighandler_t *)sigfunc == SIG_DFL) {
389 if (sigprop(sig) & SA_KILL) {
390 if (_kse_isthreaded())
391 kse_thr_interrupt(NULL,
392 KSE_INTR_SIGEXIT, sig);
397 else if (sigprop(sig) & SA_STOP)
398 kse_thr_interrupt(NULL, KSE_INTR_JOBSTOP, sig);
402 _kse_critical_enter();
403 curthread->sigmask = ucp->uc_sigmask;
404 SIG_CANTMASK(curthread->sigmask);
405 _kse_critical_leave(&curthread->tcb->tcb_tmbx);
407 thr_sigframe_restore(curthread, &psf);
409 DBG_MSG("<<< _thr_sig_handler(%d)\n", sig);
414 struct sighandle_info {
415 __siginfohandler_t *sigfunc;
422 static void handle_signal(struct pthread *curthread,
423 struct sighandle_info *shi);
424 static void handle_signal_altstack(struct pthread *curthread,
425 struct sighandle_info *shi);
427 /* Must be called with signal lock and schedule lock held in order */
429 thr_sig_invoke_handler(struct pthread *curthread, int sig, siginfo_t *info,
432 __siginfohandler_t *sigfunc;
436 struct sigaction act;
438 struct sighandle_info shi;
441 * Invoke the signal handler without going through the scheduler:
443 DBG_MSG("Got signal %d, calling handler for current thread %p\n",
446 if (!_kse_in_critical())
447 PANIC("thr_sig_invoke_handler without in critical\n");
448 curkse = curthread->kse;
450 * Check that a custom handler is installed and if
451 * the signal is not blocked:
453 sigfunc = _thread_sigact[sig - 1].sa_sigaction;
454 sa_flags = _thread_sigact[sig - 1].sa_flags;
455 sigmask = curthread->sigmask;
456 SIGSETOR(curthread->sigmask, _thread_sigact[sig - 1].sa_mask);
457 if (!(sa_flags & (SA_NODEFER | SA_RESETHAND)))
458 SIGADDSET(curthread->sigmask, sig);
459 if ((sig != SIGILL) && (sa_flags & SA_RESETHAND)) {
460 act.sa_handler = SIG_DFL;
461 act.sa_flags = SA_RESTART;
462 SIGEMPTYSET(act.sa_mask);
463 __sys_sigaction(sig, &act, NULL);
464 __sys_sigaction(sig, NULL, &_thread_sigact[sig - 1]);
466 KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
467 KSE_SCHED_UNLOCK(curkse, curkse->k_kseg);
469 * We are processing buffered signals, synchronize working
470 * signal mask into kernel.
472 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
473 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
474 onstack = _thr_sigonstack(&sigfunc);
475 ucp->uc_stack = curthread->sigstk;
476 ucp->uc_stack.ss_flags = (curthread->sigstk.ss_flags & SS_DISABLE)
477 ? SS_DISABLE : ((onstack) ? SS_ONSTACK : 0);
478 if (curthread->oldsigmask) {
479 ucp->uc_sigmask = *(curthread->oldsigmask);
480 curthread->oldsigmask = NULL;
482 ucp->uc_sigmask = sigmask;
483 shi.sigfunc = sigfunc;
485 shi.sa_flags = sa_flags;
488 if ((curthread->sigstk.ss_flags & SS_DISABLE) == 0) {
489 /* Deliver signal on alternative stack */
490 if (sa_flags & SA_ONSTACK && !onstack)
491 handle_signal_altstack(curthread, &shi);
493 handle_signal(curthread, &shi);
495 handle_signal(curthread, &shi);
498 _kse_critical_enter();
499 /* Don't trust after critical leave/enter */
500 curkse = curthread->kse;
503 * Restore the thread's signal mask.
505 curthread->sigmask = ucp->uc_sigmask;
506 SIG_CANTMASK(curthread->sigmask);
507 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
508 __sys_sigprocmask(SIG_SETMASK, &ucp->uc_sigmask, NULL);
509 KSE_SCHED_LOCK(curkse, curkse->k_kseg);
510 KSE_LOCK_ACQUIRE(curkse, &_thread_signal_lock);
512 DBG_MSG("Got signal %d, handler returned %p\n", sig, curthread);
516 handle_signal(struct pthread *curthread, struct sighandle_info *shi)
518 _kse_critical_leave(&curthread->tcb->tcb_tmbx);
520 /* Check if the signal requires a dump of thread information: */
521 if (_thr_dump_enabled() && (shi->sig == SIGINFO)) {
522 /* Dump thread information to file: */
526 if (((__sighandler_t *)shi->sigfunc != SIG_DFL) &&
527 ((__sighandler_t *)shi->sigfunc != SIG_IGN)) {
528 if ((shi->sa_flags & SA_SIGINFO) != 0 || shi->info == NULL)
529 (*(shi->sigfunc))(shi->sig, shi->info, shi->ucp);
531 ((ohandler)(*shi->sigfunc))(
532 shi->sig, shi->info->si_code,
533 (struct sigcontext *)shi->ucp,
535 (__sighandler_t *)shi->sigfunc);
538 if ((__sighandler_t *)shi->sigfunc == SIG_DFL) {
539 if (sigprop(shi->sig) & SA_KILL) {
540 if (_kse_isthreaded())
541 kse_thr_interrupt(NULL,
542 KSE_INTR_SIGEXIT, shi->sig);
544 kill(getpid(), shi->sig);
547 else if (sigprop(shi->sig) & SA_STOP)
548 kse_thr_interrupt(NULL, KSE_INTR_JOBSTOP,
556 handle_signal_wrapper(struct pthread *curthread, ucontext_t *ret_uc,
557 struct sighandle_info *shi)
559 shi->ucp->uc_stack.ss_flags = SS_ONSTACK;
560 handle_signal(curthread, shi);
561 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
564 /* Work around for ia64, THR_SETCONTEXT does not work */
565 _kse_critical_enter();
566 curthread->tcb->tcb_tmbx.tm_context = *ret_uc;
567 _thread_switch(curthread->kse->k_kcb, curthread->tcb, 1);
573 * Jump to stack set by sigaltstack before invoking signal handler
576 handle_signal_altstack(struct pthread *curthread, struct sighandle_info *shi)
579 ucontext_t uc1, *uc2;
581 THR_ASSERT(_kse_in_critical(), "Not in critical");
584 THR_GETCONTEXT(&uc1);
588 * We are still in critical region, it is safe to operate thread
591 uc2 = &curthread->tcb->tcb_tmbx.tm_context;
592 uc2->uc_stack = curthread->sigstk;
593 makecontext(uc2, (void (*)(void))handle_signal_wrapper,
594 3, curthread, &uc1, shi);
595 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
598 _thread_switch(curthread->kse->k_kcb, curthread->tcb, 1);
599 /* THR_SETCONTEXT(uc2); */
605 _thr_getprocsig(int sig, siginfo_t *siginfo)
611 DBG_MSG(">>> _thr_getprocsig\n");
613 crit = _kse_critical_enter();
614 curkse = _get_curkse();
615 KSE_LOCK_ACQUIRE(curkse, &_thread_signal_lock);
616 ret = _thr_getprocsig_unlocked(sig, siginfo);
617 KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
618 _kse_critical_leave(crit);
620 DBG_MSG("<<< _thr_getprocsig\n");
625 _thr_getprocsig_unlocked(int sig, siginfo_t *siginfo)
630 /* try to retrieve signal from kernel */
632 SIGADDSET(sigset, sig);
635 SIGDELSET(_thr_proc_sigpending, sig);
636 if (__sys_sigtimedwait(&sigset, siginfo, &ts) > 0)
641 #ifndef SYSTEM_SCOPE_ONLY
643 * Find a thread that can handle the signal. This must be called
644 * with upcalls disabled.
647 thr_sig_find(struct kse *curkse, int sig, siginfo_t *info)
649 struct kse_mailbox *kmbx = NULL;
650 struct pthread *pthread;
651 struct pthread *suspended_thread, *signaled_thread;
652 __siginfohandler_t *sigfunc;
655 DBG_MSG("Looking for thread to handle signal %d\n", sig);
658 * Enter a loop to look for threads that have the signal
659 * unmasked. POSIX specifies that a thread in a sigwait
660 * will get the signal over any other threads. Second
661 * preference will be threads in in a sigsuspend. Third
662 * preference will be the current thread. If none of the
663 * above, then the signal is delivered to the first thread
664 * that is found. Note that if a custom handler is not
665 * installed, the signal only affects threads in sigwait.
667 suspended_thread = NULL;
668 signaled_thread = NULL;
670 KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock);
671 TAILQ_FOREACH(pthread, &_thread_list, tle) {
672 if (pthread == _thr_sig_daemon)
674 /* Signal delivering to bound thread is done by kernel */
675 if (pthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
677 /* Take the scheduling lock. */
678 KSE_SCHED_LOCK(curkse, pthread->kseg);
679 if ((pthread->state == PS_DEAD) ||
680 (pthread->state == PS_DEADLOCK) ||
681 THR_IS_EXITING(pthread) ||
682 THR_IS_SUSPENDED(pthread)) {
683 ; /* Skip this thread. */
684 } else if (pthread->state == PS_SIGWAIT &&
685 SIGISMEMBER(*(pthread->data.sigwait->waitset), sig)) {
687 * retrieve signal from kernel, if it is job control
688 * signal, and sigaction is SIG_DFL, then we will
689 * be stopped in kernel, we hold lock here, but that
690 * does not matter, because that's job control, and
691 * whole process should be stopped.
693 if (_thr_getprocsig(sig, &si)) {
694 DBG_MSG("Waking thread %p in sigwait"
695 " with signal %d\n", pthread, sig);
696 /* where to put siginfo ? */
697 *(pthread->data.sigwait->siginfo) = si;
698 kmbx = _thr_setrunnable_unlocked(pthread);
700 KSE_SCHED_UNLOCK(curkse, pthread->kseg);
702 * POSIX doesn't doesn't specify which thread
703 * will get the signal if there are multiple
704 * waiters, so we give it to the first thread
707 * Do not attempt to deliver this signal
708 * to other threads and do not add the signal
709 * to the process pending set.
711 KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
714 if (suspended_thread != NULL)
715 _thr_ref_delete(NULL, suspended_thread);
716 if (signaled_thread != NULL)
717 _thr_ref_delete(NULL, signaled_thread);
719 } else if (!SIGISMEMBER(pthread->sigmask, sig)) {
721 * If debugger is running, we don't quick exit,
722 * and give it a chance to check the signal.
724 if (_libkse_debug == 0) {
725 sigfunc = _thread_sigact[sig - 1].sa_sigaction;
726 if ((__sighandler_t *)sigfunc == SIG_DFL) {
727 if (sigprop(sig) & SA_KILL) {
728 kse_thr_interrupt(NULL,
729 KSE_INTR_SIGEXIT, sig);
734 if (pthread->state == PS_SIGSUSPEND) {
735 if (suspended_thread == NULL) {
736 suspended_thread = pthread;
737 suspended_thread->refcount++;
739 } else if (signaled_thread == NULL) {
740 signaled_thread = pthread;
741 signaled_thread->refcount++;
744 KSE_SCHED_UNLOCK(curkse, pthread->kseg);
746 KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
748 if (suspended_thread != NULL) {
749 pthread = suspended_thread;
751 _thr_ref_delete(NULL, signaled_thread);
752 } else if (signaled_thread) {
753 pthread = signaled_thread;
759 #endif /* ! SYSTEM_SCOPE_ONLY */
762 build_siginfo(siginfo_t *info, int signo)
764 bzero(info, sizeof(*info));
765 info->si_signo = signo;
766 info->si_pid = _thr_pid;
770 * This is called by a thread when it has pending signals to deliver.
771 * It should only be called from the context of the thread.
774 _thr_sig_rundown(struct pthread *curthread, ucontext_t *ucp)
776 struct pthread_sigframe psf;
785 DBG_MSG(">>> thr_sig_rundown (%p)\n", curthread);
787 /* Check the threads previous state: */
788 curthread->critical_count++;
789 if (curthread->sigbackout != NULL)
790 curthread->sigbackout((void *)curthread);
791 curthread->critical_count--;
793 THR_ASSERT(!(curthread->sigbackout), "sigbackout was not cleared.");
794 THR_ASSERT((curthread->state == PS_RUNNING), "state is not PS_RUNNING");
796 thr_sigframe_save(curthread, &psf);
798 * Lower the priority before calling the handler in case
799 * it never returns (longjmps back):
801 crit = _kse_critical_enter();
802 curkse = curthread->kse;
803 KSE_SCHED_LOCK(curkse, curkse->k_kseg);
804 KSE_LOCK_ACQUIRE(curkse, &_thread_signal_lock);
805 curthread->active_priority &= ~THR_SIGNAL_PRIORITY;
809 * For bound thread, we mask all signals and get a fresh
810 * copy of signal mask from kernel
812 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
813 __sys_sigprocmask(SIG_SETMASK, &sigmask,
814 &curthread->sigmask);
816 for (i = 1; i <= _SIG_MAXSIG; i++) {
817 if (SIGISMEMBER(curthread->sigmask, i))
819 if (SIGISMEMBER(curthread->sigpend, i)) {
820 SIGDELSET(curthread->sigpend, i);
821 siginfo = curthread->siginfo[i-1];
824 if (!(curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
825 && SIGISMEMBER(_thr_proc_sigpending, i)) {
826 if (_thr_getprocsig_unlocked(i, &siginfo))
830 if (i <= _SIG_MAXSIG)
831 thr_sig_invoke_handler(curthread, i, &siginfo, ucp);
833 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
834 __sys_sigprocmask(SIG_SETMASK,
835 &curthread->sigmask, NULL);
841 /* Don't trust after signal handling */
842 curkse = curthread->kse;
843 KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
844 KSE_SCHED_UNLOCK(curkse, curkse->k_kseg);
845 _kse_critical_leave(&curthread->tcb->tcb_tmbx);
846 /* repost masked signal to kernel, it hardly happens in real world */
847 if ((curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) &&
848 !SIGISEMPTY(curthread->sigpend)) { /* dirty read */
849 __sys_sigprocmask(SIG_SETMASK, &sigmask, &curthread->sigmask);
850 for (i = 1; i <= _SIG_MAXSIG; ++i) {
851 if (SIGISMEMBER(curthread->sigpend, i)) {
852 SIGDELSET(curthread->sigpend, i);
853 if (!_kse_isthreaded())
857 &curthread->tcb->tcb_tmbx,
862 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
864 DBG_MSG("<<< thr_sig_rundown (%p)\n", curthread);
866 thr_sigframe_restore(curthread, &psf);
871 * This checks pending signals for the current thread. It should be
872 * called whenever a thread changes its signal mask. Note that this
873 * is called from a thread (using its stack).
875 * XXX - We might want to just check to see if there are pending
876 * signals for the thread here, but enter the UTS scheduler
877 * to actually install the signal handler(s).
880 _thr_sig_check_pending(struct pthread *curthread)
887 * If the thread is in critical region, delay processing signals.
888 * If the thread state is not PS_RUNNING, it might be switching
889 * into UTS and but a THR_LOCK_RELEASE saw check_pending, and it
890 * goes here, in the case we delay processing signals, lets UTS
891 * process complicated things, normally UTS will call _thr_sig_add
892 * to resume the thread, so we needn't repeat doing it here.
894 if (THR_IN_CRITICAL(curthread) || curthread->state != PS_RUNNING)
902 curthread->check_pending = 0;
903 _thr_sig_rundown(curthread, &uc);
909 * Perform thread specific actions in response to a signal.
910 * This function is only called if there is a handler installed
911 * for the signal, and if the target thread has the signal
914 * This must be called with the thread's scheduling lock held.
917 _thr_sig_add(struct pthread *pthread, int sig, siginfo_t *info)
921 struct kse_mailbox *kmbx = NULL;
922 struct pthread *curthread = _get_curthread();
924 int suppress_handler = 0;
926 __sighandler_t *sigfunc;
928 DBG_MSG(">>> _thr_sig_add %p (%d)\n", pthread, sig);
930 curkse = _get_curkse();
931 restart = _thread_sigact[sig - 1].sa_flags & SA_RESTART;
932 sigfunc = _thread_sigact[sig - 1].sa_handler;
933 fromproc = (curthread == _thr_sig_daemon);
935 if (pthread->state == PS_DEAD || pthread->state == PS_DEADLOCK ||
936 pthread->state == PS_STATE_MAX)
937 return (NULL); /* return false */
939 if ((pthread->attr.flags & PTHREAD_SCOPE_SYSTEM) &&
940 (curthread != pthread)) {
941 PANIC("Please use _thr_send_sig for bound thread");
945 if (pthread->state != PS_SIGWAIT &&
946 SIGISMEMBER(pthread->sigmask, sig)) {
947 /* signal is masked, just add signal to thread. */
949 SIGADDSET(pthread->sigpend, sig);
951 build_siginfo(&pthread->siginfo[sig-1], sig);
952 else if (info != &pthread->siginfo[sig-1])
953 memcpy(&pthread->siginfo[sig-1], info,
956 if (!_thr_getprocsig(sig, &pthread->siginfo[sig-1]))
958 SIGADDSET(pthread->sigpend, sig);
962 /* if process signal not exists, just return */
964 if (!_thr_getprocsig(sig, &siginfo))
969 if (pthread->state != PS_SIGWAIT && sigfunc == SIG_DFL &&
970 (sigprop(sig) & SA_KILL)) {
971 kse_thr_interrupt(NULL, KSE_INTR_SIGEXIT, sig);
976 * Process according to thread state:
978 switch (pthread->state) {
982 return (NULL); /* XXX return false */
986 * You can't call a signal handler for threads in these
989 suppress_handler = 1;
992 if ((pthread->flags & THR_FLAGS_IN_RUNQ)) {
993 THR_RUNQ_REMOVE(pthread);
994 pthread->active_priority |= THR_SIGNAL_PRIORITY;
995 THR_RUNQ_INSERT_TAIL(pthread);
997 /* Possible not in RUNQ and has curframe ? */
998 pthread->active_priority |= THR_SIGNAL_PRIORITY;
1002 * States which cannot be interrupted but still require the
1003 * signal handler to run:
1011 * Unmasked signals always cause sleep to terminate
1012 * early regardless of SA_RESTART:
1014 pthread->interrupted = 1;
1021 pthread->interrupted = 1;
1026 build_siginfo(&pthread->siginfo[sig-1], sig);
1027 else if (info != &pthread->siginfo[sig-1])
1028 memcpy(&pthread->siginfo[sig-1], info,
1031 * The signal handler is not called for threads in
1034 suppress_handler = 1;
1035 /* Wake up the thread if the signal is not blocked. */
1036 if (SIGISMEMBER(*(pthread->data.sigwait->waitset), sig)) {
1037 /* Return the signal number: */
1038 *(pthread->data.sigwait->siginfo) = pthread->siginfo[sig-1];
1039 /* Make the thread runnable: */
1040 kmbx = _thr_setrunnable_unlocked(pthread);
1042 /* Increment the pending signal count. */
1043 SIGADDSET(pthread->sigpend, sig);
1044 if (!SIGISMEMBER(pthread->sigmask, sig)) {
1045 if (sigfunc == SIG_DFL &&
1046 sigprop(sig) & SA_KILL) {
1047 kse_thr_interrupt(NULL,
1052 pthread->check_pending = 1;
1053 pthread->interrupted = 1;
1054 kmbx = _thr_setrunnable_unlocked(pthread);
1060 SIGADDSET(pthread->sigpend, sig);
1062 build_siginfo(&pthread->siginfo[sig-1], sig);
1063 else if (info != &pthread->siginfo[sig-1])
1064 memcpy(&pthread->siginfo[sig-1], info, sizeof(*info));
1065 pthread->check_pending = 1;
1066 if (!(pthread->attr.flags & PTHREAD_SCOPE_SYSTEM) &&
1067 (pthread->blocked != 0) && !THR_IN_CRITICAL(pthread))
1068 kse_thr_interrupt(&pthread->tcb->tcb_tmbx,
1069 restart ? KSE_INTR_RESTART : KSE_INTR_INTERRUPT, 0);
1070 if (suppress_handler == 0) {
1072 * Setup a signal frame and save the current threads
1075 if (pthread->state != PS_RUNNING) {
1076 if (pthread->flags & THR_FLAGS_IN_RUNQ)
1077 THR_RUNQ_REMOVE(pthread);
1078 pthread->active_priority |= THR_SIGNAL_PRIORITY;
1079 kmbx = _thr_setrunnable_unlocked(pthread);
1087 * Send a signal to a specific thread (ala pthread_kill):
1090 _thr_sig_send(struct pthread *pthread, int sig)
1092 struct pthread *curthread = _get_curthread();
1093 struct kse_mailbox *kmbx;
1095 if (pthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
1096 kse_thr_interrupt(&pthread->tcb->tcb_tmbx, KSE_INTR_SENDSIG, sig);
1100 /* Lock the scheduling queue of the target thread. */
1101 THR_SCHED_LOCK(curthread, pthread);
1102 if (_thread_sigact[sig - 1].sa_handler != SIG_IGN) {
1103 kmbx = _thr_sig_add(pthread, sig, NULL);
1104 /* Add a preemption point. */
1105 if (kmbx == NULL && (curthread->kseg == pthread->kseg) &&
1106 (pthread->active_priority > curthread->active_priority))
1107 curthread->critical_yield = 1;
1108 THR_SCHED_UNLOCK(curthread, pthread);
1112 * If thread sent signal to itself, check signals now.
1113 * It is not really needed, _kse_critical_leave should
1114 * have already checked signals.
1116 if (pthread == curthread && curthread->check_pending)
1117 _thr_sig_check_pending(curthread);
1120 THR_SCHED_UNLOCK(curthread, pthread);
1125 thr_sigframe_restore(struct pthread *curthread, struct pthread_sigframe *psf)
1127 kse_critical_t crit;
1130 THR_THREAD_LOCK(curthread, curthread);
1131 curthread->cancelflags = psf->psf_cancelflags;
1132 crit = _kse_critical_enter();
1133 curkse = curthread->kse;
1134 KSE_SCHED_LOCK(curkse, curthread->kseg);
1135 curthread->flags = psf->psf_flags;
1136 curthread->interrupted = psf->psf_interrupted;
1137 curthread->timeout = psf->psf_timeout;
1138 curthread->data = psf->psf_wait_data;
1139 curthread->wakeup_time = psf->psf_wakeup_time;
1140 curthread->continuation = psf->psf_continuation;
1141 KSE_SCHED_UNLOCK(curkse, curthread->kseg);
1142 _kse_critical_leave(crit);
1143 THR_THREAD_UNLOCK(curthread, curthread);
1147 thr_sigframe_save(struct pthread *curthread, struct pthread_sigframe *psf)
1149 kse_critical_t crit;
1152 THR_THREAD_LOCK(curthread, curthread);
1153 psf->psf_cancelflags = curthread->cancelflags;
1154 crit = _kse_critical_enter();
1155 curkse = curthread->kse;
1156 KSE_SCHED_LOCK(curkse, curthread->kseg);
1157 /* This has to initialize all members of the sigframe. */
1158 psf->psf_flags = (curthread->flags & (THR_FLAGS_PRIVATE | THR_FLAGS_EXITING));
1159 psf->psf_interrupted = curthread->interrupted;
1160 psf->psf_timeout = curthread->timeout;
1161 psf->psf_wait_data = curthread->data;
1162 psf->psf_wakeup_time = curthread->wakeup_time;
1163 psf->psf_continuation = curthread->continuation;
1164 KSE_SCHED_UNLOCK(curkse, curthread->kseg);
1165 _kse_critical_leave(crit);
1166 THR_THREAD_UNLOCK(curthread, curthread);
1170 _thr_signal_init(void)
1172 struct sigaction act;
1173 __siginfohandler_t *sigfunc;
1178 __sys_sigprocmask(SIG_SETMASK, &sigset, &_thr_initial->sigmask);
1179 /* Enter a loop to get the existing signal status: */
1180 for (i = 1; i <= _SIG_MAXSIG; i++) {
1181 /* Get the signal handler details: */
1182 if (__sys_sigaction(i, NULL, &_thread_sigact[i - 1]) != 0) {
1184 * Abort this process if signal
1185 * initialisation fails:
1187 PANIC("Cannot read signal handler info");
1189 /* Intall wrapper if handler was set */
1190 sigfunc = _thread_sigact[i - 1].sa_sigaction;
1191 if (((__sighandler_t *)sigfunc) != SIG_DFL &&
1192 ((__sighandler_t *)sigfunc) != SIG_IGN) {
1193 act = _thread_sigact[i - 1];
1194 act.sa_flags |= SA_SIGINFO;
1196 (__siginfohandler_t *)_thr_sig_handler;
1197 __sys_sigaction(i, &act, NULL);
1200 if (_thr_dump_enabled()) {
1202 * Install the signal handler for SIGINFO. It isn't
1203 * really needed, but it is nice to have for debugging
1206 _thread_sigact[SIGINFO - 1].sa_flags = SA_SIGINFO | SA_RESTART;
1207 SIGEMPTYSET(act.sa_mask);
1208 act.sa_flags = SA_SIGINFO | SA_RESTART;
1209 act.sa_sigaction = (__siginfohandler_t *)&_thr_sig_handler;
1210 if (__sys_sigaction(SIGINFO, &act, NULL) != 0) {
1211 __sys_sigprocmask(SIG_SETMASK, &_thr_initial->sigmask,
1214 * Abort this process if signal initialisation fails:
1216 PANIC("Cannot initialize signal handler");
1219 __sys_sigprocmask(SIG_SETMASK, &_thr_initial->sigmask, NULL);
1220 __sys_sigaltstack(NULL, &_thr_initial->sigstk);
1224 _thr_signal_deinit(void)
1227 struct pthread *curthread = _get_curthread();
1229 /* Clear process pending signals. */
1230 sigemptyset(&_thr_proc_sigpending);
1232 /* Enter a loop to get the existing signal status: */
1233 for (i = 1; i <= _SIG_MAXSIG; i++) {
1234 /* Check for signals which cannot be trapped: */
1235 if (i == SIGKILL || i == SIGSTOP) {
1238 /* Set the signal handler details: */
1239 else if (__sys_sigaction(i, &_thread_sigact[i - 1],
1242 * Abort this process if signal
1243 * initialisation fails:
1245 PANIC("Cannot set signal handler info");
1248 __sys_sigaltstack(&curthread->sigstk, NULL);