2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
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 unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "namespace.h"
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/signalvar.h>
37 #include "un-namespace.h"
38 #include "libc_private.h"
40 #include "thr_private.h"
42 /* #define DEBUG_SIGNAL */
44 #define DBG_MSG stdout_debug
50 struct sigaction sigact;
54 static struct usigaction _thr_sigact[_SIG_MAXSIG];
56 static void thr_sighandler(int, siginfo_t *, void *);
57 static void handle_signal(struct sigaction *, int, siginfo_t *, ucontext_t *);
58 static void check_deferred_signal(struct pthread *);
59 static void check_suspend(struct pthread *);
60 static void check_cancel(struct pthread *curthread, ucontext_t *ucp);
64 int __sigtimedwait(const sigset_t *set, siginfo_t *info,
65 const struct timespec * timeout);
66 int _sigtimedwait(const sigset_t *set, siginfo_t *info,
67 const struct timespec * timeout);
68 int __sigwaitinfo(const sigset_t *set, siginfo_t *info);
69 int _sigwaitinfo(const sigset_t *set, siginfo_t *info);
70 int ___sigwait(const sigset_t *set, int *sig);
71 int _sigwait(const sigset_t *set, int *sig);
72 int __sigsuspend(const sigset_t *sigmask);
73 int _sigaction(int, const struct sigaction *, struct sigaction *);
74 int _setcontext(const ucontext_t *);
75 int _swapcontext(ucontext_t *, const ucontext_t *);
77 static const sigset_t _thr_deferset={{
78 0xffffffff & ~(_SIG_BIT(SIGBUS)|_SIG_BIT(SIGILL)|_SIG_BIT(SIGFPE)|
79 _SIG_BIT(SIGSEGV)|_SIG_BIT(SIGTRAP)|_SIG_BIT(SIGSYS)),
84 static const sigset_t _thr_maskset={{
91 _thr_signal_block(struct pthread *curthread)
94 if (curthread->sigblock > 0) {
95 curthread->sigblock++;
98 __sys_sigprocmask(SIG_BLOCK, &_thr_maskset, &curthread->sigmask);
99 curthread->sigblock++;
103 _thr_signal_unblock(struct pthread *curthread)
105 if (--curthread->sigblock == 0)
106 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
110 _thr_send_sig(struct pthread *thread, int sig)
112 return thr_kill(thread->tid, sig);
116 remove_thr_signals(sigset_t *set)
118 if (SIGISMEMBER(*set, SIGCANCEL))
119 SIGDELSET(*set, SIGCANCEL);
122 static const sigset_t *
123 thr_remove_thr_signals(const sigset_t *set, sigset_t *newset)
126 remove_thr_signals(newset);
131 sigcancel_handler(int sig __unused,
132 siginfo_t *info __unused, ucontext_t *ucp)
134 struct pthread *curthread = _get_curthread();
137 if (THR_IN_CRITICAL(curthread))
140 check_suspend(curthread);
141 check_cancel(curthread, ucp);
145 typedef void (*ohandler)(int sig, int code,
146 struct sigcontext *scp, char *addr, __sighandler_t *catcher);
149 * The signal handler wrapper is entered with all signal masked.
152 thr_sighandler(int sig, siginfo_t *info, void *_ucp)
154 struct pthread *curthread = _get_curthread();
155 ucontext_t *ucp = _ucp;
156 struct sigaction act;
160 _thr_rwl_rdlock(&_thr_sigact[sig-1].lock);
161 act = _thr_sigact[sig-1].sigact;
162 _thr_rwl_unlock(&_thr_sigact[sig-1].lock);
166 * if a thread is in critical region, for example it holds low level locks,
167 * try to defer the signal processing, however if the signal is synchronous
168 * signal, it means a bad thing has happened, this is a programming error,
169 * resuming fault point can not help anything (normally causes deadloop),
170 * so here we let user code handle it immediately.
172 if (THR_IN_CRITICAL(curthread) && SIGISMEMBER(_thr_deferset, sig)) {
173 memcpy(&curthread->deferred_sigact, &act, sizeof(struct sigaction));
174 memcpy(&curthread->deferred_siginfo, info, sizeof(siginfo_t));
175 curthread->deferred_sigmask = ucp->uc_sigmask;
176 /* mask all signals, we will restore it later. */
177 ucp->uc_sigmask = _thr_deferset;
181 handle_signal(&act, sig, info, ucp);
185 handle_signal(struct sigaction *actp, int sig, siginfo_t *info, ucontext_t *ucp)
187 struct pthread *curthread = _get_curthread();
189 __siginfohandler_t *sigfunc;
196 /* add previous level mask */
197 SIGSETOR(actp->sa_mask, ucp->uc_sigmask);
199 /* add this signal's mask */
200 if (!(actp->sa_flags & SA_NODEFER))
201 SIGADDSET(actp->sa_mask, sig);
203 in_sigsuspend = curthread->in_sigsuspend;
204 curthread->in_sigsuspend = 0;
207 * if thread is in deferred cancellation mode, disable cancellation
209 * if user signal handler calls a cancellation point function, e.g,
210 * it calls write() to write data to file, because write() is a
211 * cancellation point, the thread is immediately cancelled if
212 * cancellation is pending, to avoid this problem while thread is in
213 * deferring mode, cancellation is temporarily disabled.
215 cancel_point = curthread->cancel_point;
216 cancel_async = curthread->cancel_async;
217 cancel_enable = curthread->cancel_enable;
218 curthread->cancel_point = 0;
220 curthread->cancel_enable = 0;
222 /* restore correct mask before calling user handler */
223 __sys_sigprocmask(SIG_SETMASK, &actp->sa_mask, NULL);
225 sigfunc = actp->sa_sigaction;
228 * We have already reset cancellation point flags, so if user's code
229 * longjmp()s out of its signal handler, wish its jmpbuf was set
230 * outside of a cancellation point, in most cases, this would be
231 * true. however, ther is no way to save cancel_enable in jmpbuf,
232 * so after setjmps() returns once more, the user code may need to
233 * re-set cancel_enable flag by calling pthread_setcancelstate().
235 if ((actp->sa_flags & SA_SIGINFO) != 0)
236 (*(sigfunc))(sig, info, ucp);
238 ((ohandler)(*sigfunc))(
239 sig, info->si_code, (struct sigcontext *)ucp,
240 info->si_addr, (__sighandler_t *)sigfunc);
244 curthread->in_sigsuspend = in_sigsuspend;
245 curthread->cancel_point = cancel_point;
246 curthread->cancel_enable = cancel_enable;
248 memcpy(&uc2, ucp, sizeof(uc2));
249 SIGDELSET(uc2.uc_sigmask, SIGCANCEL);
251 /* reschedule cancellation */
252 check_cancel(curthread, &uc2);
254 __sys_sigreturn(&uc2);
258 _thr_ast(struct pthread *curthread)
261 if (!THR_IN_CRITICAL(curthread)) {
262 check_deferred_signal(curthread);
263 check_suspend(curthread);
264 check_cancel(curthread, NULL);
268 /* reschedule cancellation */
270 check_cancel(struct pthread *curthread, ucontext_t *ucp)
273 if (__predict_true(!curthread->cancel_pending ||
274 !curthread->cancel_enable || curthread->no_cancel))
278 * Otherwise, we are in defer mode, and we are at
279 * cancel point, tell kernel to not block the current
280 * thread on next cancelable system call.
282 * There are three cases we should call thr_wake() to
283 * turn on TDP_WAKEUP or send SIGCANCEL in kernel:
284 * 1) we are going to call a cancelable system call,
285 * non-zero cancel_point means we are already in
286 * cancelable state, next system call is cancelable.
287 * 2) because _thr_ast() may be called by
288 * THR_CRITICAL_LEAVE() which is used by rtld rwlock
289 * and any libthr internal locks, when rtld rwlock
290 * is used, it is mostly caused my an unresolved PLT.
291 * those routines may clear the TDP_WAKEUP flag by
292 * invoking some system calls, in those cases, we
293 * also should reenable the flag.
294 * 3) thread is in sigsuspend(), and the syscall insists
295 * on getting a signal before it agrees to return.
297 if (curthread->cancel_point) {
298 if (curthread->in_sigsuspend && ucp) {
299 SIGADDSET(ucp->uc_sigmask, SIGCANCEL);
300 curthread->unblock_sigcancel = 1;
301 _thr_send_sig(curthread, SIGCANCEL);
303 thr_wake(curthread->tid);
304 } else if (curthread->cancel_async) {
306 * asynchronous cancellation mode, act upon
309 _pthread_exit_mask(PTHREAD_CANCELED,
310 ucp? &ucp->uc_sigmask : NULL);
315 check_deferred_signal(struct pthread *curthread)
318 struct sigaction act;
321 if (__predict_true(curthread->deferred_siginfo.si_signo == 0))
324 if (curthread->deferred_siginfo.si_signo != 0) {
325 act = curthread->deferred_sigact;
326 uc.uc_sigmask = curthread->deferred_sigmask;
327 memcpy(&info, &curthread->deferred_siginfo, sizeof(siginfo_t));
329 curthread->deferred_siginfo.si_signo = 0;
330 if (act.sa_flags & SA_RESETHAND) {
331 struct sigaction tact;
334 tact.sa_handler = SIG_DFL;
335 _sigaction(info.si_signo, &tact, NULL);
337 handle_signal(&act, info.si_signo, &info, &uc);
342 check_suspend(struct pthread *curthread)
346 if (__predict_true((curthread->flags &
347 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
348 != THR_FLAGS_NEED_SUSPEND))
351 if (curthread->force_exit)
355 * Blocks SIGCANCEL which other threads must send.
357 _thr_signal_block(curthread);
360 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
361 * because we are leaf code, we don't want to recursively call
364 curthread->critical_count++;
365 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
366 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
367 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
369 cycle = curthread->cycle;
371 /* Wake the thread suspending us. */
372 _thr_umtx_wake(&curthread->cycle, INT_MAX, 0);
375 * if we are from pthread_exit, we don't want to
376 * suspend, just go and die.
378 if (curthread->state == PS_DEAD)
380 curthread->flags |= THR_FLAGS_SUSPENDED;
381 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
382 _thr_umtx_wait_uint(&curthread->cycle, cycle, NULL, 0);
383 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
384 curthread->flags &= ~THR_FLAGS_SUSPENDED;
386 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
387 curthread->critical_count--;
389 _thr_signal_unblock(curthread);
393 _thr_signal_init(void)
395 struct sigaction act;
397 /* Install SIGCANCEL handler. */
398 SIGFILLSET(act.sa_mask);
399 act.sa_flags = SA_SIGINFO;
400 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
401 __sys_sigaction(SIGCANCEL, &act, NULL);
403 /* Unblock SIGCANCEL */
404 SIGEMPTYSET(act.sa_mask);
405 SIGADDSET(act.sa_mask, SIGCANCEL);
406 __sys_sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL);
410 _thr_sigact_unload(struct dl_phdr_info *phdr_info)
413 struct pthread *curthread = _get_curthread();
414 struct urwlock *rwlp;
415 struct sigaction *actp;
416 struct sigaction kact;
417 void (*handler)(int);
420 _thr_signal_block(curthread);
421 for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
422 actp = &_thr_sigact[sig-1].sigact;
424 handler = actp->sa_handler;
425 if (handler != SIG_DFL && handler != SIG_IGN &&
426 __elf_phdr_match_addr(phdr_info, handler)) {
427 rwlp = &_thr_sigact[sig-1].lock;
428 _thr_rwl_wrlock(rwlp);
429 if (handler != actp->sa_handler) {
430 _thr_rwl_unlock(rwlp);
433 actp->sa_handler = SIG_DFL;
434 actp->sa_flags = SA_SIGINFO;
435 SIGEMPTYSET(actp->sa_mask);
436 if (__sys_sigaction(sig, NULL, &kact) == 0 &&
437 kact.sa_handler != SIG_DFL &&
438 kact.sa_handler != SIG_IGN)
439 __sys_sigaction(sig, actp, NULL);
440 _thr_rwl_unlock(rwlp);
443 _thr_signal_unblock(curthread);
448 _thr_signal_prefork(void)
452 for (i = 1; i < _SIG_MAXSIG; ++i)
453 _thr_rwl_rdlock(&_thr_sigact[i-1].lock);
457 _thr_signal_postfork(void)
461 for (i = 1; i < _SIG_MAXSIG; ++i)
462 _thr_rwl_unlock(&_thr_sigact[i-1].lock);
466 _thr_signal_postfork_child(void)
470 for (i = 1; i < _SIG_MAXSIG; ++i)
471 bzero(&_thr_sigact[i-1].lock, sizeof(struct urwlock));
475 _thr_signal_deinit(void)
479 __weak_reference(___pause, pause);
486 if (_sigprocmask(SIG_BLOCK, NULL, &oset) == -1)
488 return (__sigsuspend(&oset));
491 __weak_reference(_raise, raise);
496 return _thr_send_sig(_get_curthread(), sig);
499 __weak_reference(_sigaction, sigaction);
502 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
504 struct sigaction newact, oldact, oldact2;
506 int ret = 0, err = 0;
508 if (!_SIG_VALID(sig) || sig == SIGCANCEL) {
516 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
517 _thr_rwl_wrlock(&_thr_sigact[sig-1].lock);
520 oldact2 = _thr_sigact[sig-1].sigact;
523 * if a new sig handler is SIG_DFL or SIG_IGN,
524 * don't remove old handler from _thr_sigact[],
525 * so deferred signals still can use the handlers,
526 * multiple threads invoking sigaction itself is
527 * a race condition, so it is not a problem.
529 if (newact.sa_handler != SIG_DFL &&
530 newact.sa_handler != SIG_IGN) {
531 _thr_sigact[sig-1].sigact = newact;
533 &_thr_sigact[sig-1].sigact.sa_mask);
534 newact.sa_flags &= ~SA_NODEFER;
535 newact.sa_flags |= SA_SIGINFO;
536 newact.sa_sigaction = thr_sighandler;
537 newact.sa_mask = _thr_maskset; /* mask all signals */
539 if ((ret = __sys_sigaction(sig, &newact, &oldact))) {
541 _thr_sigact[sig-1].sigact = oldact2;
543 } else if (oact != NULL) {
544 ret = __sys_sigaction(sig, NULL, &oldact);
548 if (oldact.sa_handler != SIG_DFL &&
549 oldact.sa_handler != SIG_IGN) {
552 else if (oact != NULL)
553 oldact = _thr_sigact[sig-1].sigact;
556 _thr_rwl_unlock(&_thr_sigact[sig-1].lock);
557 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
568 __weak_reference(_sigprocmask, sigprocmask);
571 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
573 const sigset_t *p = set;
576 if (how != SIG_UNBLOCK) {
579 SIGDELSET(newset, SIGCANCEL);
583 return (__sys_sigprocmask(how, p, oset));
586 __weak_reference(_pthread_sigmask, pthread_sigmask);
589 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
591 if (_sigprocmask(how, set, oset))
596 __weak_reference(__sigsuspend, sigsuspend);
599 _sigsuspend(const sigset_t * set)
603 return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset)));
607 __sigsuspend(const sigset_t * set)
609 struct pthread *curthread;
613 curthread = _get_curthread();
615 old = curthread->in_sigsuspend;
616 curthread->in_sigsuspend = 1;
617 _thr_cancel_enter(curthread);
618 ret = __sys_sigsuspend(thr_remove_thr_signals(set, &newset));
619 _thr_cancel_leave(curthread, 1);
620 curthread->in_sigsuspend = old;
621 if (curthread->unblock_sigcancel) {
622 curthread->unblock_sigcancel = 0;
624 SIGADDSET(newset, SIGCANCEL);
625 __sys_sigprocmask(SIG_UNBLOCK, &newset, NULL);
631 __weak_reference(___sigwait, sigwait);
632 __weak_reference(__sigtimedwait, sigtimedwait);
633 __weak_reference(__sigwaitinfo, sigwaitinfo);
636 _sigtimedwait(const sigset_t *set, siginfo_t *info,
637 const struct timespec * timeout)
641 return (__sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
646 * Cancellation behavior:
647 * Thread may be canceled at start, if thread got signal,
648 * it is not canceled.
651 __sigtimedwait(const sigset_t *set, siginfo_t *info,
652 const struct timespec * timeout)
654 struct pthread *curthread = _get_curthread();
658 _thr_cancel_enter(curthread);
659 ret = __sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
661 _thr_cancel_leave(curthread, (ret == -1));
666 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
670 return (__sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info));
674 * Cancellation behavior:
675 * Thread may be canceled at start, if thread got signal,
676 * it is not canceled.
679 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
681 struct pthread *curthread = _get_curthread();
685 _thr_cancel_enter(curthread);
686 ret = __sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info);
687 _thr_cancel_leave(curthread, ret == -1);
692 _sigwait(const sigset_t *set, int *sig)
696 return (__sys_sigwait(thr_remove_thr_signals(set, &newset), sig));
700 * Cancellation behavior:
701 * Thread may be canceled at start, if thread got signal,
702 * it is not canceled.
705 ___sigwait(const sigset_t *set, int *sig)
707 struct pthread *curthread = _get_curthread();
712 _thr_cancel_enter(curthread);
713 ret = __sys_sigwait(thr_remove_thr_signals(set, &newset), sig);
714 _thr_cancel_leave(curthread, (ret != 0));
715 } while (ret == EINTR);
719 __weak_reference(_setcontext, setcontext);
721 _setcontext(const ucontext_t *ucp)
725 (void) memcpy(&uc, ucp, sizeof(uc));
726 remove_thr_signals(&uc.uc_sigmask);
727 return __sys_setcontext(&uc);
730 __weak_reference(_swapcontext, swapcontext);
732 _swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
736 (void) memcpy(&uc, ucp, sizeof(uc));
737 remove_thr_signals(&uc.uc_sigmask);
738 return __sys_swapcontext(oucp, &uc);