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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by John Birrell.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/lib/libc_r/uthread/uthread_sig.c,v 1.25.2.13 2002/10/22 14:44:03 fjoe Exp $
33 * $DragonFly: src/lib/libc_r/uthread/uthread_sig.c,v 1.5 2007/01/08 21:41:53 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/signalvar.h>
44 #include "pthread_private.h"
47 static void thread_sig_add(struct pthread *pthread, int sig, int has_args);
48 static void thread_sig_check_state(struct pthread *pthread, int sig);
49 static struct pthread *thread_sig_find(int sig);
50 static void thread_sig_handle_special(int sig);
51 static void thread_sigframe_add(struct pthread *thread, int sig,
53 static void thread_sigframe_save(struct pthread *thread,
54 struct pthread_signal_frame *psf);
55 static void thread_sig_invoke_handler(int sig, siginfo_t *info,
58 /*#define DEBUG_SIGNAL*/
60 #define DBG_MSG stdout_debug
65 #if defined(_PTHREADS_INVARIANTS)
66 #define SIG_SET_ACTIVE() _sig_in_handler = 1
67 #define SIG_SET_INACTIVE() _sig_in_handler = 0
69 #define SIG_SET_ACTIVE()
70 #define SIG_SET_INACTIVE()
74 _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp)
76 struct pthread *curthread = _get_curthread();
77 struct pthread *pthread, *pthread_h;
78 int in_sched = _thread_kern_in_sched;
82 PANIC("Thread signal handler received null context");
83 DBG_MSG("Got signal %d, current thread %p\n", sig, curthread);
85 /* Check if an interval timer signal: */
86 if (sig == _SCHED_SIGNAL) {
87 /* Update the scheduling clock: */
88 gettimeofday((struct timeval *)&_sched_tod, NULL);
93 * The scheduler is already running; ignore this
98 * Check if the scheduler interrupt has come when
99 * the currently running thread has deferred thread
102 else if (curthread->sig_defer_count > 0)
103 curthread->yield_on_sig_undefer = 1;
105 /* Schedule the next thread: */
106 _thread_kern_sched(ucp);
109 * This point should not be reached, so abort the
112 PANIC("Returned to signal function from scheduler");
116 * Check if the kernel has been interrupted while the scheduler
117 * is accessing the scheduling queues or if there is a currently
118 * running thread that has deferred signals.
120 else if ((in_sched != 0) || (curthread->sig_defer_count > 0)) {
121 /* Cast the signal number to a character variable: */
125 * Write the signal number to the kernel pipe so that it will
126 * be ready to read when this signal handler returns.
128 if (_queue_signals != 0) {
129 __sys_extpwrite(_thread_kern_pipe[1], &c, 1, O_FBLOCKING, -1);
130 DBG_MSG("Got signal %d, queueing to kernel pipe\n", sig);
132 if (_thread_sigq[sig - 1].blocked == 0) {
133 DBG_MSG("Got signal %d, adding to _thread_sigq\n", sig);
135 * Do not block this signal; it will be blocked
136 * when the pending signals are run down.
138 /* _thread_sigq[sig - 1].blocked = 1; */
141 * Queue the signal, saving siginfo and sigcontext
144 * XXX - Do we need to copy siginfo and ucp?
146 _thread_sigq[sig - 1].signo = sig;
148 memcpy(&_thread_sigq[sig - 1].siginfo, info,
150 memcpy(&_thread_sigq[sig - 1].uc, ucp, sizeof(*ucp));
152 /* Indicate that there are queued signals: */
153 _thread_sigq[sig - 1].pending = 1;
154 _sigq_check_reqd = 1;
156 /* These signals need special handling: */
157 else if (sig == SIGCHLD || sig == SIGTSTP ||
158 sig == SIGTTIN || sig == SIGTTOU) {
159 _thread_sigq[sig - 1].pending = 1;
160 _thread_sigq[sig - 1].signo = sig;
161 _sigq_check_reqd = 1;
164 DBG_MSG("Got signal %d, ignored.\n", sig);
167 * The signal handlers should have been installed so that they
168 * cannot be interrupted by other signals.
170 else if (_thread_sigq[sig - 1].blocked == 0) {
172 * The signal is not blocked; handle the signal.
174 * Ignore subsequent occurrences of this signal
175 * until the current signal is handled:
177 _thread_sigq[sig - 1].blocked = 1;
179 /* This signal will be handled; clear the pending flag: */
180 _thread_sigq[sig - 1].pending = 0;
183 * Save siginfo and sigcontext (ucontext).
185 * XXX - Do we need to copy siginfo and ucp?
187 _thread_sigq[sig - 1].signo = sig;
190 memcpy(&_thread_sigq[sig - 1].siginfo, info,
192 memcpy(&_thread_sigq[sig - 1].uc, ucp, sizeof(*ucp));
195 /* Handle special signals: */
196 thread_sig_handle_special(sig);
199 if ((pthread = thread_sig_find(sig)) == NULL)
200 DBG_MSG("No thread to handle signal %d\n", sig);
201 else if (pthread == curthread) {
203 * Unblock the signal and restore the process signal
204 * mask in case we don't return from the handler:
206 _thread_sigq[sig - 1].blocked = 0;
207 __sys_sigprocmask(SIG_SETMASK, &_process_sigmask, NULL);
209 /* Call the signal handler for the current thread: */
210 thread_sig_invoke_handler(sig, info, ucp);
213 * Set the process signal mask in the context; it
214 * could have changed by the handler.
216 ucp->uc_sigmask = _process_sigmask;
218 /* Resume the interrupted thread: */
221 DBG_MSG("Got signal %d, adding frame to thread %p\n",
224 /* Setup the target thread to receive the signal: */
225 thread_sig_add(pthread, sig, /*has_args*/ 1);
227 /* Take a peek at the next ready to run thread: */
228 pthread_h = PTHREAD_PRIOQ_FIRST();
229 DBG_MSG("Finished adding frame, head of prio list %p\n",
235 * Switch to a different context if the currently running
236 * thread takes a signal, or if another thread takes a
237 * signal and the currently running thread is not in a
240 if ((pthread_h != NULL) &&
241 (pthread_h->active_priority > curthread->active_priority)) {
242 /* Enter the kernel scheduler: */
243 _thread_kern_sched(ucp);
248 thread_sig_handle_special(sig);
254 thread_sig_invoke_handler(int sig, siginfo_t *info, ucontext_t *ucp)
256 struct pthread *curthread = _get_curthread();
257 void (*sigfunc)(int, siginfo_t *, void *);
259 sigset_t saved_sigmask;
261 /* Invoke the signal handler without going through the scheduler:
263 DBG_MSG("Got signal %d, calling handler for current thread %p\n",
266 /* Save the threads signal mask: */
267 saved_sigmask = curthread->sigmask;
268 saved_seqno = curthread->sigmask_seqno;
270 /* Setup the threads signal mask: */
271 SIGSETOR(curthread->sigmask, _thread_sigact[sig - 1].sa_mask);
272 sigaddset(&curthread->sigmask, sig);
275 * Check that a custom handler is installed and if
276 * the signal is not blocked:
278 sigfunc = _thread_sigact[sig - 1].sa_sigaction;
279 if (((__sighandler_t *)sigfunc != SIG_DFL) &&
280 ((__sighandler_t *)sigfunc != SIG_IGN)) {
281 if (((_thread_sigact[sig - 1].sa_flags & SA_SIGINFO) != 0) ||
283 (*(sigfunc))(sig, info, ucp);
285 (*(sigfunc))(sig, (siginfo_t *)info->si_code, ucp);
288 * Only restore the signal mask if it hasn't been changed by the
289 * application during invocation of the signal handler:
291 if (curthread->sigmask_seqno == saved_seqno)
292 curthread->sigmask = saved_sigmask;
296 * Find a thread that can handle the signal.
299 thread_sig_find(int sig)
301 struct pthread *curthread = _get_curthread();
302 int handler_installed;
303 struct pthread *pthread, *pthread_next;
304 struct pthread *suspended_thread, *signaled_thread;
306 DBG_MSG("Looking for thread to handle signal %d\n", sig);
307 /* Check if the signal requires a dump of thread information: */
308 if (sig == SIGINFO) {
309 /* Dump thread information to file: */
312 /* Unblock this signal to allow further dumps: */
313 _thread_sigq[sig - 1].blocked = 0;
315 /* Check if an interval timer signal: */
316 else if (sig == _SCHED_SIGNAL) {
318 * This shouldn't ever occur (should this panic?).
322 * Enter a loop to look for threads that have the signal
323 * unmasked. POSIX specifies that a thread in a sigwait
324 * will get the signal over any other threads. Second
325 * preference will be threads in in a sigsuspend. Third
326 * preference will be the current thread. If none of the
327 * above, then the signal is delivered to the first thread
328 * that is found. Note that if a custom handler is not
329 * installed, the signal only affects threads in sigwait.
331 suspended_thread = NULL;
332 if ((curthread != &_thread_kern_thread) &&
333 !sigismember(&curthread->sigmask, sig))
334 signaled_thread = curthread;
336 signaled_thread = NULL;
337 if ((_thread_sigact[sig - 1].sa_handler == SIG_IGN) ||
338 (_thread_sigact[sig - 1].sa_handler == SIG_DFL))
339 handler_installed = 0;
341 handler_installed = 1;
343 for (pthread = TAILQ_FIRST(&_waitingq);
344 pthread != NULL; pthread = pthread_next) {
346 * Grab the next thread before possibly destroying
349 pthread_next = TAILQ_NEXT(pthread, pqe);
351 if ((pthread->state == PS_SIGWAIT) &&
352 sigismember(pthread->data.sigwait, sig)) {
353 /* Change the state of the thread to run: */
354 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
356 * A signal handler is not invoked for threads
357 * in sigwait. Clear the blocked and pending
360 _thread_sigq[sig - 1].blocked = 0;
361 _thread_sigq[sig - 1].pending = 0;
363 /* Return the signal number: */
364 pthread->signo = sig;
367 * POSIX doesn't doesn't specify which thread
368 * will get the signal if there are multiple
369 * waiters, so we give it to the first thread
372 * Do not attempt to deliver this signal
373 * to other threads and do not add the signal
374 * to the process pending set.
378 else if ((handler_installed != 0) &&
379 !sigismember(&pthread->sigmask, sig) &&
380 ((pthread->flags & PTHREAD_FLAGS_SUSPENDED) == 0)) {
381 if (pthread->state == PS_SIGSUSPEND) {
382 if (suspended_thread == NULL)
383 suspended_thread = pthread;
384 } else if (signaled_thread == NULL)
385 signaled_thread = pthread;
390 * Only perform wakeups and signal delivery if there is a
391 * custom handler installed:
393 if (handler_installed == 0) {
395 * There is no handler installed. Unblock the
396 * signal so that if a handler _is_ installed, any
397 * subsequent signals can be handled.
399 _thread_sigq[sig - 1].blocked = 0;
402 * If we didn't find a thread in the waiting queue,
403 * check the all threads queue:
405 if (suspended_thread == NULL &&
406 signaled_thread == NULL) {
408 * Enter a loop to look for other threads
409 * capable of receiving the signal:
411 TAILQ_FOREACH(pthread, &_thread_list, tle) {
412 if (!sigismember(&pthread->sigmask,
414 signaled_thread = pthread;
420 if (suspended_thread == NULL &&
421 signaled_thread == NULL)
423 * Add it to the set of signals pending
426 sigaddset(&_process_sigpending, sig);
429 * We only deliver the signal to one thread;
430 * give preference to the suspended thread:
432 if (suspended_thread != NULL)
433 pthread = suspended_thread;
435 pthread = signaled_thread;
441 /* Returns nothing. */
446 _thread_sig_check_pending(struct pthread *pthread)
452 * Check if there are pending signals for the running
453 * thread or process that aren't blocked:
455 sigset = pthread->sigpend;
456 SIGSETOR(sigset, _process_sigpending);
457 SIGSETNAND(sigset, pthread->sigmask);
458 if (SIGNOTEMPTY(sigset)) {
459 for (i = 1; i < NSIG; i++) {
460 if (sigismember(&sigset, i) != 0) {
461 if (sigismember(&pthread->sigpend, i) != 0)
462 thread_sig_add(pthread, i,
465 thread_sig_add(pthread, i,
467 sigdelset(&_process_sigpending, i);
475 * This can only be called from the kernel scheduler. It assumes that
476 * all thread contexts are saved and that a signal frame can safely be
477 * added to any user thread.
480 _thread_sig_handle_pending(void)
482 struct pthread *pthread;
485 PTHREAD_ASSERT(_thread_kern_in_sched != 0,
486 "_thread_sig_handle_pending called from outside kernel schedule");
488 * Check the array of pending signals:
490 for (i = 0; i < NSIG; i++) {
491 if (_thread_sigq[i].pending != 0) {
492 /* This signal is no longer pending. */
493 _thread_sigq[i].pending = 0;
495 sig = _thread_sigq[i].signo;
497 /* Some signals need special handling: */
498 thread_sig_handle_special(sig);
500 if (_thread_sigq[i].blocked == 0) {
502 * Block future signals until this one
505 _thread_sigq[i].blocked = 1;
507 if ((pthread = thread_sig_find(sig)) != NULL) {
509 * Setup the target thread to receive
512 thread_sig_add(pthread, sig,
521 thread_sig_handle_special(int sig)
523 struct pthread *pthread, *pthread_next;
529 * Go through the file list and set all files
530 * to non-blocking again in case the child
531 * set some of them to block. Sigh.
533 for (i = 0; i < _thread_dtablesize; i++) {
534 /* Check if this file is used: */
535 if (_thread_fd_table[i] != NULL) {
537 * Set the file descriptor to non-blocking:
539 __sys_fcntl(i, F_SETFL,
540 _thread_fd_getflags(i) | O_NONBLOCK);
544 * Enter a loop to wake up all threads waiting
545 * for a process to complete:
547 for (pthread = TAILQ_FIRST(&_waitingq);
548 pthread != NULL; pthread = pthread_next) {
550 * Grab the next thread before possibly
551 * destroying the link entry:
553 pthread_next = TAILQ_NEXT(pthread, pqe);
556 * If this thread is waiting for a child
557 * process to complete, wake it up:
559 if (pthread->state == PS_WAIT_WAIT) {
560 /* Make the thread runnable: */
561 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
563 /* Return the signal number: */
564 pthread->signo = sig;
570 * POSIX says that pending SIGCONT signals are
571 * discarded when one of these signals occurs.
577 * Enter a loop to discard pending SIGCONT
580 TAILQ_FOREACH(pthread, &_thread_list, tle) {
581 sigdelset(&pthread->sigpend, SIGCONT);
591 * Perform thread specific actions in response to a signal.
592 * This function is only called if there is a handler installed
593 * for the signal, and if the target thread has the signal
597 thread_sig_add(struct pthread *pthread, int sig, int has_args)
600 int suppress_handler = 0;
601 int thread_is_active = 0;
603 restart = _thread_sigact[sig - 1].sa_flags & SA_RESTART;
605 /* Make sure this signal isn't still in the pending set: */
606 sigdelset(&pthread->sigpend, sig);
609 * Process according to thread state:
611 switch (pthread->state) {
613 * States which do not change when a signal is trapped:
620 * You can't call a signal handler for threads in these
623 suppress_handler = 1;
627 * States which do not need any cleanup handling when signals
632 * Remove the thread from the queue before changing its
635 if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0)
636 PTHREAD_PRIOQ_REMOVE(pthread);
639 * This thread is running; avoid placing it in
642 thread_is_active = 1;
649 /* Remove the thread from the workq and waitq: */
650 PTHREAD_WORKQ_REMOVE(pthread);
651 PTHREAD_WAITQ_REMOVE(pthread);
652 /* Make the thread runnable: */
653 PTHREAD_SET_STATE(pthread, PS_RUNNING);
657 /* The signal handler is not called for threads in SIGWAIT. */
658 suppress_handler = 1;
659 /* Wake up the thread if the signal is blocked. */
660 if (sigismember(pthread->data.sigwait, sig)) {
661 /* Change the state of the thread to run: */
662 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
664 /* Return the signal number: */
665 pthread->signo = sig;
667 /* Increment the pending signal count. */
668 sigaddset(&pthread->sigpend, sig);
672 * The wait state is a special case due to the handling of
676 if (sig == SIGCHLD) {
677 /* Change the state of the thread to run: */
678 PTHREAD_WAITQ_REMOVE(pthread);
679 PTHREAD_SET_STATE(pthread, PS_RUNNING);
681 /* Return the signal number: */
682 pthread->signo = sig;
686 * Mark the thread as interrupted only if the
687 * restart flag is not set on the signal action:
690 pthread->interrupted = 1;
691 PTHREAD_WAITQ_REMOVE(pthread);
692 PTHREAD_SET_STATE(pthread, PS_RUNNING);
697 * States which cannot be interrupted but still require the
698 * signal handler to run:
703 * Remove the thread from the wait queue. It will
704 * be added back to the wait queue once all signal
705 * handlers have been invoked.
707 PTHREAD_WAITQ_REMOVE(pthread);
712 * Remove the thread from the wait queue. It will
713 * be added back to the wait queue once all signal
714 * handlers have been invoked.
716 PTHREAD_WAITQ_REMOVE(pthread);
717 /* Make the thread runnable: */
718 PTHREAD_SET_STATE(pthread, PS_RUNNING);
722 * States which are interruptible but may need to be removed
723 * from queues before any signal handler is called.
725 * XXX - We may not need to handle this condition, but will
726 * mark it as a potential problem.
731 pthread->interrupted = 1;
733 * Remove the thread from the wait queue. Our
734 * signal handler hook will remove this thread
735 * from the fd or file queue before invoking
736 * the actual handler.
738 PTHREAD_WAITQ_REMOVE(pthread);
742 * States which are interruptible:
748 * Flag the operation as interrupted and
749 * set the state to running:
751 pthread->interrupted = 1;
752 PTHREAD_SET_STATE(pthread, PS_RUNNING);
754 PTHREAD_WORKQ_REMOVE(pthread);
755 PTHREAD_WAITQ_REMOVE(pthread);
762 * Unmasked signals always cause poll, select, and sleep
763 * to terminate early, regardless of SA_RESTART:
765 pthread->interrupted = 1;
766 /* Remove threads in poll and select from the workq: */
767 if ((pthread->flags & PTHREAD_FLAGS_IN_WORKQ) != 0)
768 PTHREAD_WORKQ_REMOVE(pthread);
769 PTHREAD_WAITQ_REMOVE(pthread);
770 PTHREAD_SET_STATE(pthread, PS_RUNNING);
774 PTHREAD_WAITQ_REMOVE(pthread);
775 PTHREAD_SET_STATE(pthread, PS_RUNNING);
779 if (suppress_handler == 0) {
780 /* Setup a signal frame and save the current threads state: */
781 thread_sigframe_add(pthread, sig, has_args);
784 * Signals are deferred until just before the threads
785 * signal handler is invoked:
787 pthread->sig_defer_count = 1;
789 /* Make sure the thread is runnable: */
790 if (pthread->state != PS_RUNNING)
791 PTHREAD_SET_STATE(pthread, PS_RUNNING);
793 * The thread should be removed from all scheduling
794 * queues at this point. Raise the priority and place
795 * the thread in the run queue. It is also possible
796 * for a signal to be sent to a suspended thread,
797 * mostly via pthread_kill(). If a thread is suspended,
798 * don't insert it into the priority queue; just set
799 * its state to suspended and it will run the signal
800 * handler when it is resumed.
802 pthread->active_priority |= PTHREAD_SIGNAL_PRIORITY;
803 if ((pthread->flags & PTHREAD_FLAGS_SUSPENDED) != 0)
804 PTHREAD_SET_STATE(pthread, PS_SUSPENDED);
805 else if (thread_is_active == 0)
806 PTHREAD_PRIOQ_INSERT_TAIL(pthread);
811 thread_sig_check_state(struct pthread *pthread, int sig)
814 * Process according to thread state:
816 switch (pthread->state) {
818 * States which do not change when a signal is trapped:
833 /* Wake up the thread if the signal is blocked. */
834 if (sigismember(pthread->data.sigwait, sig)) {
835 /* Change the state of the thread to run: */
836 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
838 /* Return the signal number: */
839 pthread->signo = sig;
841 /* Increment the pending signal count. */
842 sigaddset(&pthread->sigpend, sig);
846 * The wait state is a special case due to the handling of
850 if (sig == SIGCHLD) {
852 * Remove the thread from the wait queue and
855 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
857 /* Return the signal number: */
858 pthread->signo = sig;
867 * Remove the thread from the wait queue and make it
870 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
872 /* Flag the operation as interrupted: */
873 pthread->interrupted = 1;
877 * These states are additionally in the work queue:
884 * Remove the thread from the wait and work queues, and
887 PTHREAD_WORKQ_REMOVE(pthread);
888 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
890 /* Flag the operation as interrupted: */
891 pthread->interrupted = 1;
897 * Send a signal to a specific thread (ala pthread_kill):
900 _thread_sig_send(struct pthread *pthread, int sig)
902 struct pthread *curthread = _get_curthread();
904 /* Check for signals whose actions are SIG_DFL: */
905 if (_thread_sigact[sig - 1].sa_handler == SIG_DFL) {
907 * Check to see if a temporary signal handler is
908 * installed for sigwaiters:
910 if (_thread_dfl_count[sig] == 0)
912 * Deliver the signal to the process if a handler
917 * Assuming we're still running after the above kill(),
918 * make any necessary state changes to the thread:
920 thread_sig_check_state(pthread, sig);
923 * Check that the signal is not being ignored:
925 else if (_thread_sigact[sig - 1].sa_handler != SIG_IGN) {
926 if (pthread->state == PS_SIGWAIT &&
927 sigismember(pthread->data.sigwait, sig)) {
928 /* Change the state of the thread to run: */
929 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
931 /* Return the signal number: */
932 pthread->signo = sig;
933 } else if (sigismember(&pthread->sigmask, sig))
934 /* Add the signal to the pending set: */
935 sigaddset(&pthread->sigpend, sig);
936 else if (pthread == curthread)
937 /* Call the signal handler for the current thread: */
938 thread_sig_invoke_handler(sig, NULL, NULL);
940 /* Protect the scheduling queues: */
941 _thread_kern_sig_defer();
943 * Perform any state changes due to signal
946 thread_sig_add(pthread, sig, /* has args */ 0);
947 /* Unprotect the scheduling queues: */
948 _thread_kern_sig_undefer();
954 * User thread signal handler wrapper.
956 * thread - current running thread
959 _thread_sig_wrapper(void)
961 struct pthread_signal_frame *psf;
962 struct pthread *thread = _get_curthread();
964 /* Get the current frame and state: */
965 psf = thread->curframe;
966 thread->curframe = NULL;
967 PTHREAD_ASSERT(psf != NULL, "Invalid signal frame in signal handler");
970 * We're coming from the kernel scheduler; clear the in
973 _thread_kern_in_sched = 0;
975 /* Check the threads previous state: */
976 if (psf->saved_state.psd_state != PS_RUNNING) {
978 * Do a little cleanup handling for those threads in
979 * queues before calling the signal handler. Signals
980 * for these threads are temporarily blocked until
981 * after cleanup handling.
983 switch (psf->saved_state.psd_state) {
986 _fd_lock_backout(thread);
987 psf->saved_state.psd_state = PS_RUNNING;
991 _cond_wait_backout(thread);
992 psf->saved_state.psd_state = PS_RUNNING;
996 _mutex_lock_backout(thread);
997 psf->saved_state.psd_state = PS_RUNNING;
1005 /* Unblock the signal in case we don't return from the handler: */
1006 _thread_sigq[psf->signo - 1].blocked = 0;
1009 * Lower the priority before calling the handler in case
1010 * it never returns (longjmps back):
1012 thread->active_priority &= ~PTHREAD_SIGNAL_PRIORITY;
1015 * Reenable interruptions without checking for the need to
1018 thread->sig_defer_count = 0;
1021 * Dispatch the signal via the custom signal handler:
1023 if (psf->sig_has_args == 0)
1024 thread_sig_invoke_handler(psf->signo, NULL, NULL);
1026 thread_sig_invoke_handler(psf->signo, &psf->siginfo, &psf->uc);
1029 * Call the kernel scheduler to safely restore the frame and
1030 * schedule the next thread:
1032 _thread_kern_sched_frame(psf);
1036 thread_sigframe_add(struct pthread *thread, int sig, int has_args)
1038 struct pthread_signal_frame *psf = NULL;
1039 unsigned long stackp;
1041 /* Get the top of the threads stack: */
1042 stackp = GET_STACK_JB(thread->ctx.jb);
1045 * Leave a little space on the stack and round down to the
1046 * nearest aligned word:
1048 stackp -= sizeof(double);
1051 /* Allocate room on top of the stack for a new signal frame: */
1052 stackp -= sizeof(struct pthread_signal_frame);
1054 psf = (struct pthread_signal_frame *) stackp;
1056 /* Save the current context in the signal frame: */
1057 thread_sigframe_save(thread, psf);
1059 /* Set handler specific information: */
1060 psf->sig_has_args = has_args;
1063 /* Copy the signal handler arguments to the signal frame: */
1064 memcpy(&psf->uc, &_thread_sigq[psf->signo - 1].uc,
1066 memcpy(&psf->siginfo, &_thread_sigq[psf->signo - 1].siginfo,
1067 sizeof(psf->siginfo));
1070 /* Setup the signal mask: */
1071 SIGSETOR(thread->sigmask, _thread_sigact[sig - 1].sa_mask);
1072 sigaddset(&thread->sigmask, sig);
1074 /* Set up the new frame: */
1075 thread->curframe = psf;
1076 thread->flags &= PTHREAD_FLAGS_PRIVATE | PTHREAD_FLAGS_TRACE |
1077 PTHREAD_FLAGS_IN_SYNCQ;
1079 * Set up the context:
1081 stackp -= sizeof(double);
1082 _setjmp(thread->ctx.jb);
1083 SET_STACK_JB(thread->ctx.jb, stackp);
1084 SET_RETURN_ADDR_JB(thread->ctx.jb, _thread_sig_wrapper);
1088 _thread_sigframe_restore(struct pthread *thread,
1089 struct pthread_signal_frame *psf)
1091 memcpy(&thread->ctx, &psf->ctx, sizeof(thread->ctx));
1093 * Only restore the signal mask if it hasn't been changed
1094 * by the application during invocation of the signal handler:
1096 if (thread->sigmask_seqno == psf->saved_state.psd_sigmask_seqno)
1097 thread->sigmask = psf->saved_state.psd_sigmask;
1098 thread->curframe = psf->saved_state.psd_curframe;
1099 thread->wakeup_time = psf->saved_state.psd_wakeup_time;
1100 thread->data = psf->saved_state.psd_wait_data;
1101 thread->state = psf->saved_state.psd_state;
1102 thread->flags = psf->saved_state.psd_flags;
1103 thread->interrupted = psf->saved_state.psd_interrupted;
1104 thread->signo = psf->saved_state.psd_signo;
1105 thread->sig_defer_count = psf->saved_state.psd_sig_defer_count;
1109 thread_sigframe_save(struct pthread *thread, struct pthread_signal_frame *psf)
1111 memcpy(&psf->ctx, &thread->ctx, sizeof(thread->ctx));
1112 psf->saved_state.psd_sigmask = thread->sigmask;
1113 psf->saved_state.psd_curframe = thread->curframe;
1114 psf->saved_state.psd_wakeup_time = thread->wakeup_time;
1115 psf->saved_state.psd_wait_data = thread->data;
1116 psf->saved_state.psd_state = thread->state;
1117 psf->saved_state.psd_flags = thread->flags &
1118 (PTHREAD_FLAGS_PRIVATE | PTHREAD_FLAGS_TRACE);
1119 psf->saved_state.psd_interrupted = thread->interrupted;
1120 psf->saved_state.psd_sigmask_seqno = thread->sigmask_seqno;
1121 psf->saved_state.psd_signo = thread->signo;
1122 psf->saved_state.psd_sig_defer_count = thread->sig_defer_count;