]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libkse/thread/thr_sig.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libkse / thread / thr_sig.c
1 /*
2  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/signalvar.h>
34 #include <signal.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <pthread.h>
40 #include "thr_private.h"
41
42 /* Prototypes: */
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,
46                     siginfo_t *info);
47 #endif
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);
52
53 #define SA_KILL         0x01            /* terminates process by default */
54 #define SA_STOP         0x02
55 #define SA_CONT         0x04
56
57 static int sigproptbl[NSIG] = {
58         SA_KILL,        /* SIGHUP */
59         SA_KILL,        /* SIGINT */
60         SA_KILL,        /* SIGQUIT */
61         SA_KILL,        /* SIGILL */
62         SA_KILL,        /* SIGTRAP */
63         SA_KILL,        /* SIGABRT */
64         SA_KILL,        /* SIGEMT */
65         SA_KILL,        /* SIGFPE */
66         SA_KILL,        /* SIGKILL */
67         SA_KILL,        /* SIGBUS */
68         SA_KILL,        /* SIGSEGV */
69         SA_KILL,        /* SIGSYS */
70         SA_KILL,        /* SIGPIPE */
71         SA_KILL,        /* SIGALRM */
72         SA_KILL,        /* SIGTERM */
73         0,              /* SIGURG */
74         SA_STOP,        /* SIGSTOP */
75         SA_STOP,        /* SIGTSTP */
76         SA_CONT,        /* SIGCONT */
77         0,              /* SIGCHLD */
78         SA_STOP,        /* SIGTTIN */
79         SA_STOP,        /* SIGTTOU */
80         0,              /* SIGIO */
81         SA_KILL,        /* SIGXCPU */
82         SA_KILL,        /* SIGXFSZ */
83         SA_KILL,        /* SIGVTALRM */
84         SA_KILL,        /* SIGPROF */
85         0,              /* SIGWINCH  */
86         0,              /* SIGINFO */
87         SA_KILL,        /* SIGUSR1 */
88         SA_KILL         /* SIGUSR2 */
89 };
90
91 /* #define DEBUG_SIGNAL */
92 #ifdef DEBUG_SIGNAL
93 #define DBG_MSG         stdout_debug
94 #else
95 #define DBG_MSG(x...)
96 #endif
97
98 /*
99  * Signal setup and delivery.
100  *
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.
111  *
112  * Methods of delivering signals.
113  *
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.
118  *
119  * Problem with 1).  You can't do this to a running thread or a
120  * thread in a critical region.
121  *
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.
126  *
127  * The solution is to use both 1) and 2) to deliver signals:
128  *
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.
132  *
133  *   o Thread enters scheduler explicitly - use 2).  The thread
134  *     can check for pending signals after it returns from the
135  *     the scheduler.
136  *
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.
140  *
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.
144  *
145  *   o Thread is swapped out due to quantum expiration - use 1)
146  *
147  *   o Thread is blocked in kernel - kse_thr_wakeup() and then
148  *     use 1)
149  */
150
151 /*
152  * Rules for selecting threads for signals received:
153  *
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.
158  *
159  *   2) A thread in sigwait() where the signal is in the thread's
160  *      waitset.
161  *
162  *   3) A thread in sigsuspend() where the signal is not in the
163  *      thread's suspended signal mask.
164  *
165  *   4) Any thread (first found/easiest to deliver) that has the
166  *      signal unmasked.
167  */
168
169 #ifndef SYSTEM_SCOPE_ONLY
170
171 static void *
172 sig_daemon(void *arg /* Unused */)
173 {
174         int i;
175         kse_critical_t crit;
176         struct timespec ts;
177         sigset_t set;
178         struct kse *curkse;
179         struct pthread *curthread = _get_curthread();
180
181         DBG_MSG("signal daemon started(%p)\n", curthread);
182         
183         curthread->name = strdup("signal thread");
184         crit = _kse_critical_enter();
185         curkse = _get_curkse();
186
187         /*
188          * Daemon thread is a bound thread and we must be created with
189          * all signals masked
190          */
191 #if 0   
192         SIGFILLSET(set);
193         __sys_sigprocmask(SIG_SETMASK, &set, NULL);
194 #endif  
195         __sys_sigpending(&set);
196         ts.tv_sec = 0;
197         ts.tv_nsec = 0;
198         while (1) {
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 */);
206                 }
207                 ts.tv_sec = 30;
208                 ts.tv_nsec = 0;
209                 curkse->k_kcb->kcb_kmbx.km_flags =
210                     KMF_NOUPCALL | KMF_NOCOMPLETED | KMF_WAITSIGEVENT;
211                 kse_release(&ts);
212                 curkse->k_kcb->kcb_kmbx.km_flags = 0;
213                 set = curkse->k_kcb->kcb_kmbx.km_sigscaught;
214         }
215         return (0);
216 }
217
218
219 /* Utility function to create signal daemon thread */
220 int
221 _thr_start_sig_daemon(void)
222 {
223         pthread_attr_t attr;
224         sigset_t sigset, oldset;
225
226         SIGFILLSET(sigset);
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);
236         return (0);
237 }
238
239 /*
240  * This signal handler only delivers asynchronous signals.
241  * This must be called with upcalls disabled and without
242  * holding any locks.
243  */
244 void
245 _thr_sig_dispatch(struct kse *curkse, int sig, siginfo_t *info)
246 {
247         struct kse_mailbox *kmbx;
248         struct pthread *thread;
249
250         DBG_MSG(">>> _thr_sig_dispatch(%d)\n", sig);
251
252         /* Check if the signal requires a dump of thread information: */
253         if (_thr_dump_enabled() && (sig == SIGINFO)) {
254                 /* Dump thread information to file: */
255                 _thread_dump_info();
256         }
257
258         while ((thread = thr_sig_find(curkse, sig, info)) != NULL) {
259                 /*
260                  * Setup the target thread to receive the signal:
261                  */
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);
272                 } else {
273                         kmbx = _thr_sig_add(thread, sig, info);
274                         KSE_SCHED_UNLOCK(curkse, thread->kseg);
275                         _thr_ref_delete(NULL, thread);
276                         if (kmbx != NULL)
277                                 kse_wakeup(kmbx);
278                         break;
279                 }
280         }
281         DBG_MSG("<<< _thr_sig_dispatch\n");
282 }
283
284 #endif /* ! SYSTEM_SCOPE_ONLY */
285
286 static __inline int
287 sigprop(int sig)
288 {
289
290         if (sig > 0 && sig < NSIG)
291                 return (sigproptbl[_SIG_IDX(sig)]);
292         return (0);
293 }
294
295 typedef void (*ohandler)(int sig, int code,
296         struct sigcontext *scp, char *addr, __sighandler_t *catcher);
297
298 void
299 _thr_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp)
300 {
301         struct pthread_sigframe psf;
302         __siginfohandler_t *sigfunc;
303         struct pthread *curthread;
304         struct kse *curkse;
305         struct sigaction act;
306         int sa_flags, err_save;
307
308         err_save = errno;
309
310         DBG_MSG(">>> _thr_sig_handler(%d)\n", sig);
311
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) {
318                 errno = err_save;
319                 return;
320         }
321
322         curkse = _get_curkse();
323         /*
324          * If thread is in critical region or if thread is on
325          * the way of state transition, then latch signal into buffer.
326          */
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);
334                 /* 
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.
339                  */
340                 if (KSE_IS_IDLE(curkse))
341                         kse_wakeup(&curkse->k_kcb->kcb_kmbx);
342                 errno = err_save;
343                 return;
344         }
345
346         /* Check if the signal requires a dump of thread information: */
347         if (_thr_dump_enabled() && (sig == SIGINFO)) {
348                 /* Dump thread information to file: */
349                 _thread_dump_info();
350         }
351
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.");
359
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]);
372         }
373         KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
374         _kse_critical_leave(&curthread->tcb->tcb_tmbx);
375
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);
382                 else {
383                         ((ohandler)(*sigfunc))(
384                                 sig, info->si_code, (struct sigcontext *)ucp,
385                                 info->si_addr, (__sighandler_t *)sigfunc);
386                 }
387         } else {
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);
393                                 else
394                                         kill(getpid(), sig);
395                         }
396 #ifdef NOTYET
397                         else if (sigprop(sig) & SA_STOP)
398                                 kse_thr_interrupt(NULL, KSE_INTR_JOBSTOP, sig);
399 #endif
400                 }
401         }
402         _kse_critical_enter();
403         curthread->sigmask = ucp->uc_sigmask;
404         SIG_CANTMASK(curthread->sigmask);
405         _kse_critical_leave(&curthread->tcb->tcb_tmbx);
406
407         thr_sigframe_restore(curthread, &psf);
408
409         DBG_MSG("<<< _thr_sig_handler(%d)\n", sig);
410
411         errno = err_save;
412 }
413
414 struct sighandle_info {
415         __siginfohandler_t *sigfunc;
416         int sa_flags;
417         int sig;
418         siginfo_t *info;
419         ucontext_t *ucp;
420 };
421
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);
426
427 /* Must be called with signal lock and schedule lock held in order */
428 static void
429 thr_sig_invoke_handler(struct pthread *curthread, int sig, siginfo_t *info,
430     ucontext_t *ucp)
431 {
432         __siginfohandler_t *sigfunc;
433         sigset_t sigmask;
434         int sa_flags;
435         int onstack;
436         struct sigaction act;
437         struct kse *curkse;
438         struct sighandle_info shi;
439
440         /*
441          * Invoke the signal handler without going through the scheduler:
442          */
443         DBG_MSG("Got signal %d, calling handler for current thread %p\n",
444             sig, curthread);
445
446         if (!_kse_in_critical())
447                 PANIC("thr_sig_invoke_handler without in critical\n");
448         curkse = curthread->kse;
449         /*
450          * Check that a custom handler is installed and if
451          * the signal is not blocked:
452          */
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]);
465         }
466         KSE_LOCK_RELEASE(curkse, &_thread_signal_lock);
467         KSE_SCHED_UNLOCK(curkse, curkse->k_kseg);
468         /*
469          * We are processing buffered signals, synchronize working
470          * signal mask into kernel.
471          */
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;
481         } else
482                 ucp->uc_sigmask = sigmask;
483         shi.sigfunc = sigfunc;
484         shi.sig = sig;
485         shi.sa_flags = sa_flags;
486         shi.info = info;
487         shi.ucp = ucp;
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);
492                 else
493                         handle_signal(curthread, &shi);
494         } else {
495                 handle_signal(curthread, &shi);
496         }
497
498         _kse_critical_enter();
499         /* Don't trust after critical leave/enter */
500         curkse = curthread->kse;
501
502         /*
503          * Restore the thread's signal mask.
504          */
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);
511         
512         DBG_MSG("Got signal %d, handler returned %p\n", sig, curthread);
513 }
514
515 static void
516 handle_signal(struct pthread *curthread, struct sighandle_info *shi)
517 {
518         _kse_critical_leave(&curthread->tcb->tcb_tmbx);
519
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: */
523                 _thread_dump_info();
524         }
525
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);
530                 else {
531                         ((ohandler)(*shi->sigfunc))(
532                                 shi->sig, shi->info->si_code,
533                                 (struct sigcontext *)shi->ucp,
534                                 shi->info->si_addr,
535                                 (__sighandler_t *)shi->sigfunc);
536                 }
537         } else {
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);
543                                 else
544                                         kill(getpid(), shi->sig);
545                         }
546 #ifdef NOTYET
547                         else if (sigprop(shi->sig) & SA_STOP)
548                                 kse_thr_interrupt(NULL, KSE_INTR_JOBSTOP,
549                                         shi->sig);
550 #endif
551                 }
552         }
553 }
554
555 static void
556 handle_signal_wrapper(struct pthread *curthread, ucontext_t *ret_uc,
557         struct sighandle_info *shi)
558 {
559         shi->ucp->uc_stack.ss_flags = SS_ONSTACK;
560         handle_signal(curthread, shi);
561         if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
562                 setcontext(ret_uc);
563         else {
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);
568                 /* THR_SETCONTEXT */
569         }
570 }
571
572 /*
573  * Jump to stack set by sigaltstack before invoking signal handler
574  */
575 static void
576 handle_signal_altstack(struct pthread *curthread, struct sighandle_info *shi)
577 {
578         volatile int once;
579         ucontext_t uc1, *uc2;
580
581         THR_ASSERT(_kse_in_critical(), "Not in critical");
582
583         once = 0;
584         THR_GETCONTEXT(&uc1);
585         if (once == 0) {
586                 once = 1;
587                 /* XXX
588                  * We are still in critical region, it is safe to operate thread
589                  * context
590                  */
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)
596                         setcontext(uc2);
597                 else {
598                         _thread_switch(curthread->kse->k_kcb, curthread->tcb, 1);
599                         /* THR_SETCONTEXT(uc2); */
600                 }
601         }
602 }
603
604 int
605 _thr_getprocsig(int sig, siginfo_t *siginfo)
606 {
607         kse_critical_t crit;
608         struct kse *curkse;
609         int ret;
610
611         DBG_MSG(">>> _thr_getprocsig\n");
612
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);
619
620         DBG_MSG("<<< _thr_getprocsig\n");
621         return (ret);
622 }
623
624 int
625 _thr_getprocsig_unlocked(int sig, siginfo_t *siginfo)
626 {
627         sigset_t sigset;
628         struct timespec ts;
629
630         /* try to retrieve signal from kernel */
631         SIGEMPTYSET(sigset);
632         SIGADDSET(sigset, sig);
633         ts.tv_sec = 0;
634         ts.tv_nsec = 0;
635         SIGDELSET(_thr_proc_sigpending, sig);
636         if (__sys_sigtimedwait(&sigset, siginfo, &ts) > 0)
637                 return (sig);
638         return (0);
639 }
640
641 #ifndef SYSTEM_SCOPE_ONLY
642 /*
643  * Find a thread that can handle the signal.  This must be called
644  * with upcalls disabled.
645  */
646 struct pthread *
647 thr_sig_find(struct kse *curkse, int sig, siginfo_t *info)
648 {
649         struct kse_mailbox *kmbx = NULL;
650         struct pthread  *pthread;
651         struct pthread  *suspended_thread, *signaled_thread;
652         __siginfohandler_t *sigfunc;
653         siginfo_t si;
654
655         DBG_MSG("Looking for thread to handle signal %d\n", sig);
656
657         /*
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.
666          */
667         suspended_thread = NULL;
668         signaled_thread = NULL;
669
670         KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock);
671         TAILQ_FOREACH(pthread, &_thread_list, tle) {
672                 if (pthread == _thr_sig_daemon)
673                         continue;
674                 /* Signal delivering to bound thread is done by kernel */
675                 if (pthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
676                         continue;
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)) {
686                         /*
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.
692                          */
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);
699                         }
700                         KSE_SCHED_UNLOCK(curkse, pthread->kseg);
701                         /*
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
705                          * we find.
706                          *
707                          * Do not attempt to deliver this signal
708                          * to other threads and do not add the signal
709                          * to the process pending set.
710                          */
711                         KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
712                         if (kmbx != NULL)
713                                 kse_wakeup(kmbx);
714                         if (suspended_thread != NULL)
715                                 _thr_ref_delete(NULL, suspended_thread);
716                         if (signaled_thread != NULL)
717                                 _thr_ref_delete(NULL, signaled_thread);
718                         return (NULL);
719                 } else if (!SIGISMEMBER(pthread->sigmask, sig)) {
720                         /*
721                          * If debugger is running, we don't quick exit,
722                          * and give it a chance to check the signal.
723                          */  
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);
730                                                 /* Never reach */
731                                         }
732                                 }
733                         }
734                         if (pthread->state == PS_SIGSUSPEND) {
735                                 if (suspended_thread == NULL) {
736                                         suspended_thread = pthread;
737                                         suspended_thread->refcount++;
738                                 }
739                         } else if (signaled_thread == NULL) {
740                                 signaled_thread = pthread;
741                                 signaled_thread->refcount++;
742                         }
743                 }
744                 KSE_SCHED_UNLOCK(curkse, pthread->kseg);
745         }
746         KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
747
748         if (suspended_thread != NULL) {
749                 pthread = suspended_thread;
750                 if (signaled_thread)
751                         _thr_ref_delete(NULL, signaled_thread);
752         } else if (signaled_thread) {
753                 pthread = signaled_thread;
754         } else {
755                 pthread = NULL;
756         }
757         return (pthread);
758 }
759 #endif /* ! SYSTEM_SCOPE_ONLY */
760
761 static inline void
762 build_siginfo(siginfo_t *info, int signo)
763 {
764         bzero(info, sizeof(*info));
765         info->si_signo = signo;
766         info->si_pid = _thr_pid;
767 }
768
769 /*
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.
772  */
773 void
774 _thr_sig_rundown(struct pthread *curthread, ucontext_t *ucp)
775 {
776         struct pthread_sigframe psf;
777         siginfo_t siginfo;
778         int i, err_save;
779         kse_critical_t crit;
780         struct kse *curkse;
781         sigset_t sigmask;
782
783         err_save = errno;
784
785         DBG_MSG(">>> thr_sig_rundown (%p)\n", curthread);
786
787         /* Check the threads previous state: */
788         curthread->critical_count++;
789         if (curthread->sigbackout != NULL)
790                 curthread->sigbackout((void *)curthread);
791         curthread->critical_count--;
792
793         THR_ASSERT(!(curthread->sigbackout), "sigbackout was not cleared.");
794         THR_ASSERT((curthread->state == PS_RUNNING), "state is not PS_RUNNING");
795
796         thr_sigframe_save(curthread, &psf);
797         /*
798          * Lower the priority before calling the handler in case
799          * it never returns (longjmps back):
800          */
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;
806         SIGFILLSET(sigmask);
807         while (1) {
808                 /*
809                  * For bound thread, we mask all signals and get a fresh
810                  * copy of signal mask from kernel
811                  */
812                 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
813                         __sys_sigprocmask(SIG_SETMASK, &sigmask,
814                                  &curthread->sigmask);
815                 }
816                 for (i = 1; i <= _SIG_MAXSIG; i++) {
817                         if (SIGISMEMBER(curthread->sigmask, i))
818                                 continue;
819                         if (SIGISMEMBER(curthread->sigpend, i)) {
820                                 SIGDELSET(curthread->sigpend, i);
821                                 siginfo = curthread->siginfo[i-1];
822                                 break;
823                         }
824                         if (!(curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) 
825                             && SIGISMEMBER(_thr_proc_sigpending, i)) {
826                                 if (_thr_getprocsig_unlocked(i, &siginfo))
827                                         break;
828                         }
829                 }
830                 if (i <= _SIG_MAXSIG)
831                         thr_sig_invoke_handler(curthread, i, &siginfo, ucp);
832                 else {
833                         if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
834                                 __sys_sigprocmask(SIG_SETMASK,
835                                                  &curthread->sigmask, NULL);
836                         }
837                         break;
838                 }
839         }
840
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())
854                                         kill(getpid(), i);
855                                 else
856                                         kse_thr_interrupt(
857                                                 &curthread->tcb->tcb_tmbx,
858                                                 KSE_INTR_SENDSIG,
859                                                 i);
860                         }
861                 }
862                 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
863         }
864         DBG_MSG("<<< thr_sig_rundown (%p)\n", curthread);
865
866         thr_sigframe_restore(curthread, &psf);
867         errno = err_save;
868 }
869
870 /*
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).
874  *
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).
878  */
879 void
880 _thr_sig_check_pending(struct pthread *curthread)
881 {
882         ucontext_t uc;
883         volatile int once;
884         int errsave;
885
886         /*
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.
893          */
894         if (THR_IN_CRITICAL(curthread) || curthread->state != PS_RUNNING)
895                 return;
896
897         errsave = errno;
898         once = 0;
899         THR_GETCONTEXT(&uc);
900         if (once == 0) {
901                 once = 1;
902                 curthread->check_pending = 0;
903                 _thr_sig_rundown(curthread, &uc);
904         }
905         errno = errsave;
906 }
907
908 /*
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
912  * unmasked.
913  *
914  * This must be called with the thread's scheduling lock held.
915  */
916 struct kse_mailbox *
917 _thr_sig_add(struct pthread *pthread, int sig, siginfo_t *info)
918 {
919         siginfo_t siginfo;
920         struct kse *curkse;
921         struct kse_mailbox *kmbx = NULL;
922         struct pthread *curthread = _get_curthread();
923         int     restart;
924         int     suppress_handler = 0;
925         int     fromproc = 0;
926         __sighandler_t *sigfunc;
927
928         DBG_MSG(">>> _thr_sig_add %p (%d)\n", pthread, sig);
929
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);
934
935         if (pthread->state == PS_DEAD || pthread->state == PS_DEADLOCK ||
936             pthread->state == PS_STATE_MAX)
937                 return (NULL); /* return false */
938
939         if ((pthread->attr.flags & PTHREAD_SCOPE_SYSTEM) &&
940             (curthread != pthread)) {
941                 PANIC("Please use _thr_send_sig for bound thread");
942                 return (NULL);
943         }
944
945         if (pthread->state != PS_SIGWAIT &&
946             SIGISMEMBER(pthread->sigmask, sig)) {
947                 /* signal is masked, just add signal to thread. */
948                 if (!fromproc) {
949                         SIGADDSET(pthread->sigpend, sig);
950                         if (info == NULL)
951                                 build_siginfo(&pthread->siginfo[sig-1], sig);
952                         else if (info != &pthread->siginfo[sig-1])
953                                 memcpy(&pthread->siginfo[sig-1], info,
954                                          sizeof(*info));
955                 } else {
956                         if (!_thr_getprocsig(sig, &pthread->siginfo[sig-1]))
957                                 return (NULL);
958                         SIGADDSET(pthread->sigpend, sig);
959                 }
960         }
961         else {
962                 /* if process signal not exists, just return */
963                 if (fromproc) {
964                         if (!_thr_getprocsig(sig, &siginfo))
965                                 return (NULL);
966                         info = &siginfo;
967                 }
968
969                 if (pthread->state != PS_SIGWAIT && sigfunc == SIG_DFL &&
970                     (sigprop(sig) & SA_KILL)) {
971                         kse_thr_interrupt(NULL, KSE_INTR_SIGEXIT, sig);
972                         /* Never reach */
973                 }
974
975                 /*
976                  * Process according to thread state:
977                  */
978                 switch (pthread->state) {
979                 case PS_DEAD:
980                 case PS_DEADLOCK:
981                 case PS_STATE_MAX:
982                         return (NULL);  /* XXX return false */
983                 case PS_LOCKWAIT:
984                 case PS_SUSPENDED:
985                         /*
986                          * You can't call a signal handler for threads in these
987                          * states.
988                          */
989                         suppress_handler = 1;
990                         break;
991                 case PS_RUNNING:
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);
996                         } else {
997                                 /* Possible not in RUNQ and has curframe ? */
998                                 pthread->active_priority |= THR_SIGNAL_PRIORITY;
999                         }
1000                         break;
1001                 /*
1002                  * States which cannot be interrupted but still require the
1003                  * signal handler to run:
1004                  */
1005                 case PS_COND_WAIT:
1006                 case PS_MUTEX_WAIT:
1007                         break;
1008
1009                 case PS_SLEEP_WAIT:
1010                         /*
1011                          * Unmasked signals always cause sleep to terminate
1012                          * early regardless of SA_RESTART:
1013                          */
1014                         pthread->interrupted = 1;
1015                         break;
1016
1017                 case PS_JOIN:
1018                         break;
1019
1020                 case PS_SIGSUSPEND:
1021                         pthread->interrupted = 1;
1022                         break;
1023
1024                 case PS_SIGWAIT:
1025                         if (info == NULL)
1026                                 build_siginfo(&pthread->siginfo[sig-1], sig);
1027                         else if (info != &pthread->siginfo[sig-1])
1028                                 memcpy(&pthread->siginfo[sig-1], info,
1029                                         sizeof(*info));
1030                         /*
1031                          * The signal handler is not called for threads in
1032                          * SIGWAIT.
1033                          */
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);
1041                         } else {
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,
1048                                                          KSE_INTR_SIGEXIT,
1049                                                          sig);
1050                                                 /* Never reach */
1051                                         }
1052                                         pthread->check_pending = 1;
1053                                         pthread->interrupted = 1;
1054                                         kmbx = _thr_setrunnable_unlocked(pthread);
1055                                 }
1056                         }
1057                         return (kmbx);
1058                 }
1059
1060                 SIGADDSET(pthread->sigpend, sig);
1061                 if (info == NULL)
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) {
1071                         /*
1072                          * Setup a signal frame and save the current threads
1073                          * state:
1074                          */
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);
1080                         }
1081                 }
1082         }
1083         return (kmbx);
1084 }
1085
1086 /*
1087  * Send a signal to a specific thread (ala pthread_kill):
1088  */
1089 void
1090 _thr_sig_send(struct pthread *pthread, int sig)
1091 {
1092         struct pthread *curthread = _get_curthread();
1093         struct kse_mailbox *kmbx;
1094
1095         if (pthread->attr.flags & PTHREAD_SCOPE_SYSTEM) {
1096                 kse_thr_interrupt(&pthread->tcb->tcb_tmbx, KSE_INTR_SENDSIG, sig);
1097                 return;
1098         }
1099
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);
1109                 if (kmbx != NULL)
1110                         kse_wakeup(kmbx);
1111                 /* XXX
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.
1115                  */
1116                 if (pthread == curthread && curthread->check_pending)
1117                         _thr_sig_check_pending(curthread);
1118
1119         } else  {
1120                 THR_SCHED_UNLOCK(curthread, pthread);
1121         }
1122 }
1123
1124 static inline void
1125 thr_sigframe_restore(struct pthread *curthread, struct pthread_sigframe *psf)
1126 {
1127         kse_critical_t crit;
1128         struct kse *curkse;
1129
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);
1144 }
1145
1146 static inline void
1147 thr_sigframe_save(struct pthread *curthread, struct pthread_sigframe *psf)
1148 {
1149         kse_critical_t crit;
1150         struct kse *curkse;
1151
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);
1167 }
1168
1169 void
1170 _thr_signal_init(void)
1171 {
1172         struct sigaction act;
1173         __siginfohandler_t *sigfunc;
1174         int i;
1175         sigset_t sigset;
1176
1177         SIGFILLSET(sigset);
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) {
1183                         /*
1184                          * Abort this process if signal
1185                          * initialisation fails:
1186                          */
1187                         PANIC("Cannot read signal handler info");
1188                 }
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;
1195                         act.sa_sigaction =
1196                                 (__siginfohandler_t *)_thr_sig_handler;
1197                         __sys_sigaction(i, &act, NULL);
1198                 }
1199         }
1200         if (_thr_dump_enabled()) {
1201                 /*
1202                  * Install the signal handler for SIGINFO.  It isn't
1203                  * really needed, but it is nice to have for debugging
1204                  * purposes.
1205                  */
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,
1212                             NULL);
1213                         /*
1214                          * Abort this process if signal initialisation fails:
1215                          */
1216                         PANIC("Cannot initialize signal handler");
1217                 }
1218         }
1219         __sys_sigprocmask(SIG_SETMASK, &_thr_initial->sigmask, NULL);
1220         __sys_sigaltstack(NULL, &_thr_initial->sigstk);
1221 }
1222
1223 void
1224 _thr_signal_deinit(void)
1225 {
1226         int i;
1227         struct pthread *curthread = _get_curthread();
1228
1229         /* Clear process pending signals. */
1230         sigemptyset(&_thr_proc_sigpending);
1231
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) {
1236                 }
1237
1238                 /* Set the signal handler details: */
1239                 else if (__sys_sigaction(i, &_thread_sigact[i - 1],
1240                          NULL) != 0) {
1241                         /*
1242                          * Abort this process if signal
1243                          * initialisation fails:
1244                          */
1245                         PANIC("Cannot set signal handler info");
1246                 }
1247         }
1248         __sys_sigaltstack(&curthread->sigstk, NULL);
1249 }
1250