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>
41 #include "pthread_private.h"
44 static void thread_sig_add(struct pthread *pthread, int sig, int has_args);
45 static void thread_sig_check_state(struct pthread *pthread, int sig);
46 static struct pthread *thread_sig_find(int sig);
47 static void thread_sig_handle_special(int sig);
48 static void thread_sigframe_add(struct pthread *thread, int sig,
50 static void thread_sigframe_save(struct pthread *thread,
51 struct pthread_signal_frame *psf);
52 static void thread_sig_invoke_handler(int sig, siginfo_t *info,
53 ucontext_t *ucp, int unblock);
55 /*#define DEBUG_SIGNAL*/
57 #define DBG_MSG stdout_debug
62 #if defined(_PTHREADS_INVARIANTS)
63 #define SIG_SET_ACTIVE() _sig_in_handler = 1
64 #define SIG_SET_INACTIVE() _sig_in_handler = 0
66 #define SIG_SET_ACTIVE()
67 #define SIG_SET_INACTIVE()
71 _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp)
73 struct pthread *curthread = _get_curthread();
74 struct pthread *pthread, *pthread_h;
75 int in_sched = _thread_kern_in_sched;
80 PANIC("Thread signal handler received null context");
81 DBG_MSG("Got signal %d, current thread %p\n", sig, curthread);
83 /* Check if an interval timer signal: */
84 if (sig == _SCHED_SIGNAL) {
85 /* Update the scheduling clock: */
86 gettimeofday((struct timeval *)&_sched_tod, NULL);
91 * The scheduler is already running; ignore this
96 * Check if the scheduler interrupt has come when
97 * the currently running thread has deferred thread
100 else if (curthread->sig_defer_count > 0)
101 curthread->yield_on_sig_undefer = 1;
103 /* Schedule the next thread: */
104 _thread_kern_sched(ucp);
107 * This point should not be reached, so abort the
110 PANIC("Returned to signal function from scheduler");
114 * Check if the kernel has been interrupted while the scheduler
115 * is accessing the scheduling queues or if there is a currently
116 * running thread that has deferred signals.
118 else if ((in_sched != 0) || (curthread->sig_defer_count > 0)) {
119 /* Cast the signal number to a character variable: */
123 * Write the signal number to the kernel pipe so that it will
124 * be ready to read when this signal handler returns.
126 if (_queue_signals != 0) {
129 wgot = __sys_write(_thread_kern_pipe[1], &c,
131 } while (wgot < 0 && errno == EINTR);
132 if (wgot < 0 && errno != EAGAIN) {
133 PANIC("Failed to queue signal");
135 DBG_MSG("Got signal %d, queueing to kernel pipe\n", sig);
137 if (_thread_sigq[sig - 1].blocked == 0) {
138 DBG_MSG("Got signal %d, adding to _thread_sigq\n", sig);
140 * Do not block this signal; it will be blocked
141 * when the pending signals are run down.
143 /* _thread_sigq[sig - 1].blocked = 1; */
146 * Queue the signal, saving siginfo and sigcontext
149 * XXX - Do we need to copy siginfo and ucp?
151 _thread_sigq[sig - 1].signo = sig;
153 memcpy(&_thread_sigq[sig - 1].siginfo, info,
155 memcpy(&_thread_sigq[sig - 1].uc, ucp, sizeof(*ucp));
157 /* Indicate that there are queued signals: */
158 _thread_sigq[sig - 1].pending = 1;
159 _sigq_check_reqd = 1;
161 /* These signals need special handling: */
162 else if (sig == SIGCHLD || sig == SIGTSTP ||
163 sig == SIGTTIN || sig == SIGTTOU) {
164 _thread_sigq[sig - 1].pending = 1;
165 _thread_sigq[sig - 1].signo = sig;
166 _sigq_check_reqd = 1;
169 DBG_MSG("Got signal %d, ignored.\n", sig);
172 * The signal handlers should have been installed so that they
173 * cannot be interrupted by other signals.
175 else if (_thread_sigq[sig - 1].blocked == 0) {
177 * The signal is not blocked; handle the signal.
179 * Ignore subsequent occurrences of this signal
180 * until the current signal is handled:
182 _thread_sigq[sig - 1].blocked = 1;
184 /* This signal will be handled; clear the pending flag: */
185 _thread_sigq[sig - 1].pending = 0;
188 * Save siginfo and sigcontext (ucontext).
190 * XXX - Do we need to copy siginfo and ucp?
192 _thread_sigq[sig - 1].signo = sig;
195 memcpy(&_thread_sigq[sig - 1].siginfo, info,
197 memcpy(&_thread_sigq[sig - 1].uc, ucp, sizeof(*ucp));
200 /* Handle special signals: */
201 thread_sig_handle_special(sig);
204 if ((pthread = thread_sig_find(sig)) == NULL)
205 DBG_MSG("No thread to handle signal %d\n", sig);
206 else if (pthread == curthread) {
207 /* Call the signal handler for the current thread: */
208 thread_sig_invoke_handler(sig, info, ucp, 2);
211 * Set the process signal mask in the context; it
212 * could have changed by the handler.
214 ucp->uc_sigmask = _process_sigmask;
217 * The signal mask was restored; check for any
220 sigset = curthread->sigpend;
221 SIGSETOR(sigset, _process_sigpending);
222 SIGSETNAND(sigset, curthread->sigmask);
223 if (SIGNOTEMPTY(sigset))
224 curthread->check_pending = 1;
226 /* Resume the interrupted thread: */
227 __sys_sigreturn(ucp);
229 DBG_MSG("Got signal %d, adding frame to thread %p\n",
232 /* Setup the target thread to receive the signal: */
233 thread_sig_add(pthread, sig, /*has_args*/ 1);
235 /* Take a peek at the next ready to run thread: */
236 pthread_h = PTHREAD_PRIOQ_FIRST();
237 DBG_MSG("Finished adding frame, head of prio list %p\n",
243 * Switch to a different context if the currently running
244 * thread takes a signal, or if another thread takes a
245 * signal and the currently running thread is not in a
248 if ((pthread_h != NULL) &&
249 (pthread_h->active_priority > curthread->active_priority)) {
250 /* Enter the kernel scheduler: */
251 _thread_kern_sched(ucp);
256 thread_sig_handle_special(sig);
262 thread_sig_invoke_handler(int sig, siginfo_t *info, ucontext_t *ucp,
265 struct pthread *curthread = _get_curthread();
266 void (*sigfunc)(int, siginfo_t *, void *);
268 sigset_t saved_sigmask;
270 /* Invoke the signal handler without going through the scheduler:
272 DBG_MSG("Got signal %d, calling handler for current thread %p\n",
275 /* Save the threads signal mask: */
276 saved_sigmask = curthread->sigmask;
277 saved_seqno = curthread->sigmask_seqno;
279 /* Setup the threads signal mask: */
280 SIGSETOR(curthread->sigmask, _thread_sigact[sig - 1].sa_mask);
281 sigaddset(&curthread->sigmask, sig);
285 * Unblock the signal and restore the process signal
286 * mask in case we don't return from the handler:
288 _thread_sigq[sig - 1].blocked = 0;
290 __sys_sigprocmask(SIG_SETMASK, &_process_sigmask,
295 * Check that a custom handler is installed and if
296 * the signal is not blocked:
298 sigfunc = _thread_sigact[sig - 1].sa_sigaction;
299 if (((__sighandler_t *)sigfunc != SIG_DFL) &&
300 ((__sighandler_t *)sigfunc != SIG_IGN)) {
301 if (((_thread_sigact[sig - 1].sa_flags & SA_SIGINFO) != 0) ||
303 (*(sigfunc))(sig, info, ucp);
305 (*(sigfunc))(sig, (void*)(intptr_t)info->si_code, ucp);
308 * Only restore the signal mask if it hasn't been changed by the
309 * application during invocation of the signal handler:
311 if (curthread->sigmask_seqno == saved_seqno)
312 curthread->sigmask = saved_sigmask;
316 * Find a thread that can handle the signal.
319 thread_sig_find(int sig)
321 struct pthread *curthread = _get_curthread();
322 int handler_installed;
323 struct pthread *pthread, *pthread_next;
324 struct pthread *suspended_thread, *signaled_thread;
326 DBG_MSG("Looking for thread to handle signal %d\n", sig);
327 /* Check if the signal requires a dump of thread information: */
328 if (sig == SIGINFO) {
329 /* Dump thread information to file: */
332 /* Unblock this signal to allow further dumps: */
333 _thread_sigq[sig - 1].blocked = 0;
335 /* Check if an interval timer signal: */
336 else if (sig == _SCHED_SIGNAL) {
338 * This shouldn't ever occur (should this panic?).
342 * Enter a loop to look for threads that have the signal
343 * unmasked. POSIX specifies that a thread in a sigwait
344 * will get the signal over any other threads. Second
345 * preference will be threads in a sigsuspend. Third
346 * preference will be the current thread. If none of the
347 * above, then the signal is delivered to the first thread
348 * that is found. Note that if a custom handler is not
349 * installed, the signal only affects threads in sigwait.
351 suspended_thread = NULL;
352 if ((curthread != &_thread_kern_thread) &&
353 !sigismember(&curthread->sigmask, sig))
354 signaled_thread = curthread;
356 signaled_thread = NULL;
357 if ((_thread_sigact[sig - 1].sa_handler == SIG_IGN) ||
358 (_thread_sigact[sig - 1].sa_handler == SIG_DFL))
359 handler_installed = 0;
361 handler_installed = 1;
363 for (pthread = TAILQ_FIRST(&_waitingq);
364 pthread != NULL; pthread = pthread_next) {
366 * Grab the next thread before possibly destroying
369 pthread_next = TAILQ_NEXT(pthread, pqe);
371 if ((pthread->state == PS_SIGWAIT) &&
372 sigismember(pthread->data.sigwait, sig)) {
373 /* Change the state of the thread to run: */
374 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
376 * A signal handler is not invoked for threads
377 * in sigwait. Clear the blocked and pending
380 _thread_sigq[sig - 1].blocked = 0;
381 _thread_sigq[sig - 1].pending = 0;
383 /* Return the signal number: */
384 pthread->signo = sig;
387 * POSIX doesn't doesn't specify which thread
388 * will get the signal if there are multiple
389 * waiters, so we give it to the first thread
392 * Do not attempt to deliver this signal
393 * to other threads and do not add the signal
394 * to the process pending set.
398 else if ((handler_installed != 0) &&
399 !sigismember(&pthread->sigmask, sig) &&
400 ((pthread->flags & PTHREAD_FLAGS_SUSPENDED) == 0)) {
401 if (pthread->state == PS_SIGSUSPEND) {
402 if (suspended_thread == NULL)
403 suspended_thread = pthread;
404 } else if (signaled_thread == NULL)
405 signaled_thread = pthread;
410 * Only perform wakeups and signal delivery if there is a
411 * custom handler installed:
413 if (handler_installed == 0) {
415 * There is no handler installed. Unblock the
416 * signal so that if a handler _is_ installed, any
417 * subsequent signals can be handled.
419 _thread_sigq[sig - 1].blocked = 0;
422 * If we didn't find a thread in the waiting queue,
423 * check the all threads queue:
425 if (suspended_thread == NULL &&
426 signaled_thread == NULL) {
428 * Enter a loop to look for other threads
429 * capable of receiving the signal:
431 TAILQ_FOREACH(pthread, &_thread_list, tle) {
432 if (!sigismember(&pthread->sigmask,
434 signaled_thread = pthread;
440 if (suspended_thread == NULL &&
441 signaled_thread == NULL)
443 * Add it to the set of signals pending
446 sigaddset(&_process_sigpending, sig);
449 * We only deliver the signal to one thread;
450 * give preference to the suspended thread:
452 if (suspended_thread != NULL)
453 pthread = suspended_thread;
455 pthread = signaled_thread;
461 /* Returns nothing. */
466 _thread_sig_check_pending(struct pthread *pthread)
472 * Check if there are pending signals for the running
473 * thread or process that aren't blocked:
475 sigset = pthread->sigpend;
476 SIGSETOR(sigset, _process_sigpending);
477 SIGSETNAND(sigset, pthread->sigmask);
478 if (SIGNOTEMPTY(sigset)) {
479 for (i = 1; i < NSIG; i++) {
480 if (sigismember(&sigset, i) != 0) {
481 if (sigismember(&pthread->sigpend, i) != 0)
482 thread_sig_add(pthread, i,
485 thread_sig_add(pthread, i,
487 sigdelset(&_process_sigpending, i);
495 * This can only be called from the kernel scheduler. It assumes that
496 * all thread contexts are saved and that a signal frame can safely be
497 * added to any user thread.
500 _thread_sig_handle_pending(void)
502 struct pthread *pthread;
505 PTHREAD_ASSERT(_thread_kern_in_sched != 0,
506 "_thread_sig_handle_pending called from outside kernel schedule");
508 * Check the array of pending signals:
510 for (i = 0; i < NSIG; i++) {
511 if (_thread_sigq[i].pending != 0) {
512 /* This signal is no longer pending. */
513 _thread_sigq[i].pending = 0;
515 sig = _thread_sigq[i].signo;
517 /* Some signals need special handling: */
518 thread_sig_handle_special(sig);
520 if (_thread_sigq[i].blocked == 0) {
522 * Block future signals until this one
525 _thread_sigq[i].blocked = 1;
527 if ((pthread = thread_sig_find(sig)) != NULL) {
529 * Setup the target thread to receive
532 thread_sig_add(pthread, sig,
541 thread_sig_handle_special(int sig)
543 struct pthread *pthread, *pthread_next;
549 * Go through the file list and set all files
550 * to non-blocking again in case the child
551 * set some of them to block. Sigh.
553 for (i = 0; i < _thread_dtablesize; i++) {
554 /* Check if this file is used: */
555 if (_thread_fd_table[i] != NULL) {
557 * Set the file descriptor to non-blocking:
559 __sys_fcntl(i, F_SETFL,
560 _thread_fd_getflags(i) | O_NONBLOCK);
564 * Enter a loop to wake up all threads waiting
565 * for a process to complete:
567 for (pthread = TAILQ_FIRST(&_waitingq);
568 pthread != NULL; pthread = pthread_next) {
570 * Grab the next thread before possibly
571 * destroying the link entry:
573 pthread_next = TAILQ_NEXT(pthread, pqe);
576 * If this thread is waiting for a child
577 * process to complete, wake it up:
579 if (pthread->state == PS_WAIT_WAIT) {
580 /* Make the thread runnable: */
581 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
583 /* Return the signal number: */
584 pthread->signo = sig;
590 * POSIX says that pending SIGCONT signals are
591 * discarded when one of these signals occurs.
597 * Enter a loop to discard pending SIGCONT
600 TAILQ_FOREACH(pthread, &_thread_list, tle) {
601 sigdelset(&pthread->sigpend, SIGCONT);
611 * Perform thread specific actions in response to a signal.
612 * This function is only called if there is a handler installed
613 * for the signal, and if the target thread has the signal
617 thread_sig_add(struct pthread *pthread, int sig, int has_args)
620 int suppress_handler = 0;
621 int thread_is_active = 0;
623 restart = _thread_sigact[sig - 1].sa_flags & SA_RESTART;
625 /* Make sure this signal isn't still in the pending set: */
626 sigdelset(&pthread->sigpend, sig);
629 * Process according to thread state:
631 switch (pthread->state) {
633 * States which do not change when a signal is trapped:
640 * You can't call a signal handler for threads in these
643 suppress_handler = 1;
647 * States which do not need any cleanup handling when signals
652 * Remove the thread from the queue before changing its
655 if ((pthread->flags & PTHREAD_FLAGS_IN_PRIOQ) != 0)
656 PTHREAD_PRIOQ_REMOVE(pthread);
659 * This thread is running; avoid placing it in
662 thread_is_active = 1;
669 /* Remove the thread from the workq and waitq: */
670 PTHREAD_WORKQ_REMOVE(pthread);
671 PTHREAD_WAITQ_REMOVE(pthread);
672 /* Make the thread runnable: */
673 PTHREAD_SET_STATE(pthread, PS_RUNNING);
677 /* The signal handler is not called for threads in SIGWAIT. */
678 suppress_handler = 1;
679 /* Wake up the thread if the signal is blocked. */
680 if (sigismember(pthread->data.sigwait, sig)) {
681 /* Change the state of the thread to run: */
682 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
684 /* Return the signal number: */
685 pthread->signo = sig;
687 /* Increment the pending signal count. */
688 sigaddset(&pthread->sigpend, sig);
692 * The wait state is a special case due to the handling of
696 if (sig == SIGCHLD) {
697 /* Change the state of the thread to run: */
698 PTHREAD_WAITQ_REMOVE(pthread);
699 PTHREAD_SET_STATE(pthread, PS_RUNNING);
701 /* Return the signal number: */
702 pthread->signo = sig;
706 * Mark the thread as interrupted only if the
707 * restart flag is not set on the signal action:
710 pthread->interrupted = 1;
711 PTHREAD_WAITQ_REMOVE(pthread);
712 PTHREAD_SET_STATE(pthread, PS_RUNNING);
717 * States which cannot be interrupted but still require the
718 * signal handler to run:
723 * Remove the thread from the wait queue. It will
724 * be added back to the wait queue once all signal
725 * handlers have been invoked.
727 PTHREAD_WAITQ_REMOVE(pthread);
732 * Remove the thread from the wait queue. It will
733 * be added back to the wait queue once all signal
734 * handlers have been invoked.
736 PTHREAD_WAITQ_REMOVE(pthread);
737 /* Make the thread runnable: */
738 PTHREAD_SET_STATE(pthread, PS_RUNNING);
742 * States which are interruptible but may need to be removed
743 * from queues before any signal handler is called.
745 * XXX - We may not need to handle this condition, but will
746 * mark it as a potential problem.
752 pthread->interrupted = 1;
754 * Remove the thread from the wait queue. Our
755 * signal handler hook will remove this thread
756 * from the fd or file queue before invoking
757 * the actual handler.
759 PTHREAD_WAITQ_REMOVE(pthread);
763 * States which are interruptible:
769 * Flag the operation as interrupted and
770 * set the state to running:
772 pthread->interrupted = 1;
773 PTHREAD_SET_STATE(pthread, PS_RUNNING);
775 PTHREAD_WORKQ_REMOVE(pthread);
776 PTHREAD_WAITQ_REMOVE(pthread);
783 * Unmasked signals always cause poll, select, and sleep
784 * to terminate early, regardless of SA_RESTART:
786 pthread->interrupted = 1;
787 /* Remove threads in poll and select from the workq: */
788 if ((pthread->flags & PTHREAD_FLAGS_IN_WORKQ) != 0)
789 PTHREAD_WORKQ_REMOVE(pthread);
790 PTHREAD_WAITQ_REMOVE(pthread);
791 PTHREAD_SET_STATE(pthread, PS_RUNNING);
795 PTHREAD_WAITQ_REMOVE(pthread);
796 PTHREAD_SET_STATE(pthread, PS_RUNNING);
800 if (suppress_handler == 0) {
801 /* Setup a signal frame and save the current threads state: */
802 thread_sigframe_add(pthread, sig, has_args);
805 * Signals are deferred until just before the threads
806 * signal handler is invoked:
808 pthread->sig_defer_count = 1;
810 /* Make sure the thread is runnable: */
811 if (pthread->state != PS_RUNNING)
812 PTHREAD_SET_STATE(pthread, PS_RUNNING);
814 * The thread should be removed from all scheduling
815 * queues at this point. Raise the priority and place
816 * the thread in the run queue. It is also possible
817 * for a signal to be sent to a suspended thread,
818 * mostly via pthread_kill(). If a thread is suspended,
819 * don't insert it into the priority queue; just set
820 * its state to suspended and it will run the signal
821 * handler when it is resumed.
823 pthread->active_priority |= PTHREAD_SIGNAL_PRIORITY;
824 if ((pthread->flags & PTHREAD_FLAGS_SUSPENDED) != 0)
825 PTHREAD_SET_STATE(pthread, PS_SUSPENDED);
826 else if (thread_is_active == 0)
827 PTHREAD_PRIOQ_INSERT_TAIL(pthread);
832 thread_sig_check_state(struct pthread *pthread, int sig)
835 * Process according to thread state:
837 switch (pthread->state) {
839 * States which do not change when a signal is trapped:
854 /* Wake up the thread if the signal is blocked. */
855 if (sigismember(pthread->data.sigwait, sig)) {
856 /* Change the state of the thread to run: */
857 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
859 /* Return the signal number: */
860 pthread->signo = sig;
862 /* Increment the pending signal count. */
863 sigaddset(&pthread->sigpend, sig);
867 * The wait state is a special case due to the handling of
871 if (sig == SIGCHLD) {
873 * Remove the thread from the wait queue and
876 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
878 /* Return the signal number: */
879 pthread->signo = sig;
888 * Remove the thread from the wait queue and make it
891 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
893 /* Flag the operation as interrupted: */
894 pthread->interrupted = 1;
898 * These states are additionally in the work queue:
906 * Remove the thread from the wait and work queues, and
909 PTHREAD_WORKQ_REMOVE(pthread);
910 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
912 /* Flag the operation as interrupted: */
913 pthread->interrupted = 1;
919 * Send a signal to a specific thread (ala pthread_kill):
922 _thread_sig_send(struct pthread *pthread, int sig)
924 struct pthread *curthread = _get_curthread();
926 /* Check for signals whose actions are SIG_DFL: */
927 if (_thread_sigact[sig - 1].sa_handler == SIG_DFL) {
929 * Check to see if a temporary signal handler is
930 * installed for sigwaiters:
932 if (_thread_dfl_count[sig] == 0)
934 * Deliver the signal to the process if a handler
939 * Assuming we're still running after the above kill(),
940 * make any necessary state changes to the thread:
942 thread_sig_check_state(pthread, sig);
945 * Check that the signal is not being ignored:
947 else if (_thread_sigact[sig - 1].sa_handler != SIG_IGN) {
948 if (pthread->state == PS_SIGWAIT &&
949 sigismember(pthread->data.sigwait, sig)) {
950 /* Change the state of the thread to run: */
951 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
953 /* Return the signal number: */
954 pthread->signo = sig;
955 } else if (sigismember(&pthread->sigmask, sig))
956 /* Add the signal to the pending set: */
957 sigaddset(&pthread->sigpend, sig);
958 else if (pthread == curthread)
959 /* Call the signal handler for the current thread: */
960 thread_sig_invoke_handler(sig, NULL, NULL, 0);
962 /* Protect the scheduling queues: */
963 _thread_kern_sig_defer();
965 * Perform any state changes due to signal
968 thread_sig_add(pthread, sig, /* has args */ 0);
969 /* Unprotect the scheduling queues: */
970 _thread_kern_sig_undefer();
976 * User thread signal handler wrapper.
978 * thread - current running thread
981 _thread_sig_wrapper(void)
983 struct pthread_signal_frame *psf;
984 struct pthread *thread = _get_curthread();
986 /* Get the current frame and state: */
987 psf = thread->curframe;
988 thread->curframe = NULL;
989 PTHREAD_ASSERT(psf != NULL, "Invalid signal frame in signal handler");
992 * We're coming from the kernel scheduler; clear the in
995 _thread_kern_in_sched = 0;
997 /* Check the threads previous state: */
998 if (psf->saved_state.psd_state != PS_RUNNING) {
1000 * Do a little cleanup handling for those threads in
1001 * queues before calling the signal handler. Signals
1002 * for these threads are temporarily blocked until
1003 * after cleanup handling.
1005 switch (psf->saved_state.psd_state) {
1008 _fd_lock_backout(thread);
1009 psf->saved_state.psd_state = PS_RUNNING;
1013 _cond_wait_backout(thread);
1014 psf->saved_state.psd_state = PS_RUNNING;
1018 _mutex_lock_backout(thread);
1019 psf->saved_state.psd_state = PS_RUNNING;
1028 * Lower the priority before calling the handler in case
1029 * it never returns (longjmps back):
1031 thread->active_priority &= ~PTHREAD_SIGNAL_PRIORITY;
1034 * Reenable interruptions without checking for the need to
1037 thread->sig_defer_count = 0;
1040 * Dispatch the signal via the custom signal handler:
1042 if (psf->sig_has_args == 0)
1043 thread_sig_invoke_handler(psf->signo, NULL, NULL, 1);
1045 thread_sig_invoke_handler(psf->signo, &psf->siginfo, &psf->uc,
1049 * Call the kernel scheduler to safely restore the frame and
1050 * schedule the next thread:
1052 _thread_kern_sched_frame(psf);
1056 thread_sigframe_add(struct pthread *thread, int sig, int has_args)
1058 struct pthread_signal_frame *psf = NULL;
1059 unsigned long stackp;
1061 /* Get the top of the threads stack: */
1062 stackp = GET_STACK_JB(thread->ctx.jb);
1064 #if !defined(__ia64__)
1066 * Leave a little space on the stack and round down to the
1067 * nearest aligned word:
1069 #if defined(__amd64__)
1070 stackp -= 128; /* Skip over 128 byte red-zone */
1072 stackp -= sizeof(double);
1073 #if defined(__amd64__)
1080 /* Allocate room on top of the stack for a new signal frame: */
1081 stackp -= sizeof(struct pthread_signal_frame);
1082 #if defined(__ia64__) || defined(__amd64__)
1086 psf = (struct pthread_signal_frame *) stackp;
1088 /* Save the current context in the signal frame: */
1089 thread_sigframe_save(thread, psf);
1091 /* Set handler specific information: */
1092 psf->sig_has_args = has_args;
1095 /* Copy the signal handler arguments to the signal frame: */
1096 memcpy(&psf->uc, &_thread_sigq[psf->signo - 1].uc,
1098 memcpy(&psf->siginfo, &_thread_sigq[psf->signo - 1].siginfo,
1099 sizeof(psf->siginfo));
1102 /* Setup the signal mask: */
1103 SIGSETOR(thread->sigmask, _thread_sigact[sig - 1].sa_mask);
1104 sigaddset(&thread->sigmask, sig);
1106 /* Set up the new frame: */
1107 thread->curframe = psf;
1108 thread->flags &= PTHREAD_FLAGS_PRIVATE | PTHREAD_FLAGS_TRACE |
1109 PTHREAD_FLAGS_IN_SYNCQ;
1111 * Set up the context:
1113 #if !defined(__ia64__)
1114 stackp -= sizeof(double);
1115 #if defined(__amd64__)
1119 _setjmp(thread->ctx.jb);
1120 #if !defined(__ia64__)
1121 SET_STACK_JB(thread->ctx.jb, stackp);
1123 UPD_STACK_JB(thread->ctx.jb, stackp - 16);
1125 SET_RETURN_ADDR_JB(thread->ctx.jb, _thread_sig_wrapper);
1129 _thread_sigframe_restore(struct pthread *thread,
1130 struct pthread_signal_frame *psf)
1132 memcpy(&thread->ctx, &psf->ctx, sizeof(thread->ctx));
1134 * Only restore the signal mask if it hasn't been changed
1135 * by the application during invocation of the signal handler:
1137 if (thread->sigmask_seqno == psf->saved_state.psd_sigmask_seqno)
1138 thread->sigmask = psf->saved_state.psd_sigmask;
1139 thread->curframe = psf->saved_state.psd_curframe;
1140 thread->wakeup_time = psf->saved_state.psd_wakeup_time;
1141 thread->data = psf->saved_state.psd_wait_data;
1142 thread->state = psf->saved_state.psd_state;
1143 thread->flags = psf->saved_state.psd_flags;
1144 thread->interrupted = psf->saved_state.psd_interrupted;
1145 thread->signo = psf->saved_state.psd_signo;
1146 thread->sig_defer_count = psf->saved_state.psd_sig_defer_count;
1150 thread_sigframe_save(struct pthread *thread, struct pthread_signal_frame *psf)
1152 memcpy(&psf->ctx, &thread->ctx, sizeof(thread->ctx));
1153 psf->saved_state.psd_sigmask = thread->sigmask;
1154 psf->saved_state.psd_curframe = thread->curframe;
1155 psf->saved_state.psd_wakeup_time = thread->wakeup_time;
1156 psf->saved_state.psd_wait_data = thread->data;
1157 psf->saved_state.psd_state = thread->state;
1158 psf->saved_state.psd_flags = thread->flags &
1159 (PTHREAD_FLAGS_PRIVATE | PTHREAD_FLAGS_TRACE);
1160 psf->saved_state.psd_interrupted = thread->interrupted;
1161 psf->saved_state.psd_sigmask_seqno = thread->sigmask_seqno;
1162 psf->saved_state.psd_signo = thread->signo;
1163 psf->saved_state.psd_sig_defer_count = thread->sig_defer_count;