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>
38 #include "un-namespace.h"
39 #include "libc_private.h"
41 #include "libc_private.h"
42 #include "thr_private.h"
44 /* #define DEBUG_SIGNAL */
46 #define DBG_MSG stdout_debug
52 struct sigaction sigact;
56 static struct usigaction _thr_sigact[_SIG_MAXSIG];
58 static inline struct usigaction *
59 __libc_sigaction_slot(int signo)
62 return (&_thr_sigact[signo - 1]);
65 static void thr_sighandler(int, siginfo_t *, void *);
66 static void handle_signal(struct sigaction *, int, siginfo_t *, ucontext_t *);
67 static void check_deferred_signal(struct pthread *);
68 static void check_suspend(struct pthread *);
69 static void check_cancel(struct pthread *curthread, ucontext_t *ucp);
71 int _sigtimedwait(const sigset_t *set, siginfo_t *info,
72 const struct timespec * timeout);
73 int _sigwaitinfo(const sigset_t *set, siginfo_t *info);
74 int _sigwait(const sigset_t *set, int *sig);
75 int _setcontext(const ucontext_t *);
76 int _swapcontext(ucontext_t *, const ucontext_t *);
78 static const sigset_t _thr_deferset={{
79 0xffffffff & ~(_SIG_BIT(SIGBUS)|_SIG_BIT(SIGILL)|_SIG_BIT(SIGFPE)|
80 _SIG_BIT(SIGSEGV)|_SIG_BIT(SIGTRAP)|_SIG_BIT(SIGSYS)),
85 static const sigset_t _thr_maskset={{
92 _thr_signal_block(struct pthread *curthread)
95 if (curthread->sigblock > 0) {
96 curthread->sigblock++;
99 __sys_sigprocmask(SIG_BLOCK, &_thr_maskset, &curthread->sigmask);
100 curthread->sigblock++;
104 _thr_signal_unblock(struct pthread *curthread)
106 if (--curthread->sigblock == 0)
107 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
111 _thr_send_sig(struct pthread *thread, int sig)
113 return thr_kill(thread->tid, sig);
117 remove_thr_signals(sigset_t *set)
119 if (SIGISMEMBER(*set, SIGCANCEL))
120 SIGDELSET(*set, SIGCANCEL);
123 static const sigset_t *
124 thr_remove_thr_signals(const sigset_t *set, sigset_t *newset)
127 remove_thr_signals(newset);
132 sigcancel_handler(int sig __unused,
133 siginfo_t *info __unused, ucontext_t *ucp)
135 struct pthread *curthread = _get_curthread();
138 if (THR_IN_CRITICAL(curthread))
141 check_suspend(curthread);
142 check_cancel(curthread, ucp);
146 typedef void (*ohandler)(int sig, int code, struct sigcontext *scp,
147 char *addr, __sighandler_t *catcher);
150 * The signal handler wrapper is entered with all signal masked.
153 thr_sighandler(int sig, siginfo_t *info, void *_ucp)
155 struct pthread *curthread;
157 struct sigaction act;
158 struct usigaction *usa;
162 curthread = _get_curthread();
164 usa = __libc_sigaction_slot(sig);
165 _thr_rwl_rdlock(&usa->lock);
167 _thr_rwl_unlock(&usa->lock);
169 curthread->deferred_run = 0;
172 * if a thread is in critical region, for example it holds low level locks,
173 * try to defer the signal processing, however if the signal is synchronous
174 * signal, it means a bad thing has happened, this is a programming error,
175 * resuming fault point can not help anything (normally causes deadloop),
176 * so here we let user code handle it immediately.
178 if (THR_IN_CRITICAL(curthread) && SIGISMEMBER(_thr_deferset, sig)) {
179 memcpy(&curthread->deferred_sigact, &act, sizeof(struct sigaction));
180 memcpy(&curthread->deferred_siginfo, info, sizeof(siginfo_t));
181 curthread->deferred_sigmask = ucp->uc_sigmask;
182 /* mask all signals, we will restore it later. */
183 ucp->uc_sigmask = _thr_deferset;
187 handle_signal(&act, sig, info, ucp);
191 handle_signal(struct sigaction *actp, int sig, siginfo_t *info, ucontext_t *ucp)
193 struct pthread *curthread = _get_curthread();
195 __siginfohandler_t *sigfunc;
202 /* add previous level mask */
203 SIGSETOR(actp->sa_mask, ucp->uc_sigmask);
205 /* add this signal's mask */
206 if (!(actp->sa_flags & SA_NODEFER))
207 SIGADDSET(actp->sa_mask, sig);
209 in_sigsuspend = curthread->in_sigsuspend;
210 curthread->in_sigsuspend = 0;
213 * If thread is in deferred cancellation mode, disable cancellation
215 * If user signal handler calls a cancellation point function, e.g,
216 * it calls write() to write data to file, because write() is a
217 * cancellation point, the thread is immediately cancelled if
218 * cancellation is pending, to avoid this problem while thread is in
219 * deferring mode, cancellation is temporarily disabled.
221 cancel_point = curthread->cancel_point;
222 cancel_async = curthread->cancel_async;
223 cancel_enable = curthread->cancel_enable;
224 curthread->cancel_point = 0;
226 curthread->cancel_enable = 0;
228 /* restore correct mask before calling user handler */
229 __sys_sigprocmask(SIG_SETMASK, &actp->sa_mask, NULL);
231 sigfunc = actp->sa_sigaction;
234 * We have already reset cancellation point flags, so if user's code
235 * longjmp()s out of its signal handler, wish its jmpbuf was set
236 * outside of a cancellation point, in most cases, this would be
237 * true. However, there is no way to save cancel_enable in jmpbuf,
238 * so after setjmps() returns once more, the user code may need to
239 * re-set cancel_enable flag by calling pthread_setcancelstate().
241 if ((actp->sa_flags & SA_SIGINFO) != 0) {
242 sigfunc(sig, info, ucp);
244 ((ohandler)sigfunc)(sig, info->si_code,
245 (struct sigcontext *)ucp, info->si_addr,
246 (__sighandler_t *)sigfunc);
250 curthread->in_sigsuspend = in_sigsuspend;
251 curthread->cancel_point = cancel_point;
252 curthread->cancel_enable = cancel_enable;
254 memcpy(&uc2, ucp, sizeof(uc2));
255 SIGDELSET(uc2.uc_sigmask, SIGCANCEL);
257 /* reschedule cancellation */
258 check_cancel(curthread, &uc2);
260 __sys_sigreturn(&uc2);
264 _thr_ast(struct pthread *curthread)
267 if (!THR_IN_CRITICAL(curthread)) {
268 check_deferred_signal(curthread);
269 check_suspend(curthread);
270 check_cancel(curthread, NULL);
274 /* reschedule cancellation */
276 check_cancel(struct pthread *curthread, ucontext_t *ucp)
279 if (__predict_true(!curthread->cancel_pending ||
280 !curthread->cancel_enable || curthread->no_cancel))
284 * Otherwise, we are in defer mode, and we are at
285 * cancel point, tell kernel to not block the current
286 * thread on next cancelable system call.
288 * There are three cases we should call thr_wake() to
289 * turn on TDP_WAKEUP or send SIGCANCEL in kernel:
290 * 1) we are going to call a cancelable system call,
291 * non-zero cancel_point means we are already in
292 * cancelable state, next system call is cancelable.
293 * 2) because _thr_ast() may be called by
294 * THR_CRITICAL_LEAVE() which is used by rtld rwlock
295 * and any libthr internal locks, when rtld rwlock
296 * is used, it is mostly caused my an unresolved PLT.
297 * those routines may clear the TDP_WAKEUP flag by
298 * invoking some system calls, in those cases, we
299 * also should reenable the flag.
300 * 3) thread is in sigsuspend(), and the syscall insists
301 * on getting a signal before it agrees to return.
303 if (curthread->cancel_point) {
304 if (curthread->in_sigsuspend && ucp) {
305 SIGADDSET(ucp->uc_sigmask, SIGCANCEL);
306 curthread->unblock_sigcancel = 1;
307 _thr_send_sig(curthread, SIGCANCEL);
309 thr_wake(curthread->tid);
310 } else if (curthread->cancel_async) {
312 * asynchronous cancellation mode, act upon
315 _pthread_exit_mask(PTHREAD_CANCELED,
316 ucp? &ucp->uc_sigmask : NULL);
321 check_deferred_signal(struct pthread *curthread)
324 struct sigaction act;
328 if (__predict_true(curthread->deferred_siginfo.si_signo == 0 ||
329 curthread->deferred_run))
332 curthread->deferred_run = 1;
333 uc_len = __getcontextx_size();
336 if (curthread->deferred_siginfo.si_signo == 0) {
337 curthread->deferred_run = 0;
340 __fillcontextx2((char *)uc);
341 act = curthread->deferred_sigact;
342 uc->uc_sigmask = curthread->deferred_sigmask;
343 memcpy(&info, &curthread->deferred_siginfo, sizeof(siginfo_t));
345 curthread->deferred_siginfo.si_signo = 0;
346 handle_signal(&act, info.si_signo, &info, uc);
350 check_suspend(struct pthread *curthread)
354 if (__predict_true((curthread->flags &
355 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
356 != THR_FLAGS_NEED_SUSPEND))
358 if (curthread == _single_thread)
360 if (curthread->force_exit)
364 * Blocks SIGCANCEL which other threads must send.
366 _thr_signal_block(curthread);
369 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
370 * because we are leaf code, we don't want to recursively call
373 curthread->critical_count++;
374 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
375 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
376 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
378 cycle = curthread->cycle;
380 /* Wake the thread suspending us. */
381 _thr_umtx_wake(&curthread->cycle, INT_MAX, 0);
384 * if we are from pthread_exit, we don't want to
385 * suspend, just go and die.
387 if (curthread->state == PS_DEAD)
389 curthread->flags |= THR_FLAGS_SUSPENDED;
390 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
391 _thr_umtx_wait_uint(&curthread->cycle, cycle, NULL, 0);
392 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
393 curthread->flags &= ~THR_FLAGS_SUSPENDED;
395 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
396 curthread->critical_count--;
398 _thr_signal_unblock(curthread);
402 _thr_signal_init(int dlopened)
404 struct sigaction act, nact, oact;
405 struct usigaction *usa;
410 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
411 for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
412 if (sig == SIGCANCEL)
414 error = __sys_sigaction(sig, NULL, &oact);
415 if (error == -1 || oact.sa_handler == SIG_DFL ||
416 oact.sa_handler == SIG_IGN)
418 usa = __libc_sigaction_slot(sig);
421 remove_thr_signals(&usa->sigact.sa_mask);
422 nact.sa_flags &= ~SA_NODEFER;
423 nact.sa_flags |= SA_SIGINFO;
424 nact.sa_sigaction = thr_sighandler;
425 nact.sa_mask = _thr_maskset;
426 (void)__sys_sigaction(sig, &nact, NULL);
428 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
431 /* Install SIGCANCEL handler. */
432 SIGFILLSET(act.sa_mask);
433 act.sa_flags = SA_SIGINFO;
434 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
435 __sys_sigaction(SIGCANCEL, &act, NULL);
437 /* Unblock SIGCANCEL */
438 SIGEMPTYSET(act.sa_mask);
439 SIGADDSET(act.sa_mask, SIGCANCEL);
440 __sys_sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL);
444 _thr_sigact_unload(struct dl_phdr_info *phdr_info)
447 struct pthread *curthread = _get_curthread();
448 struct urwlock *rwlp;
449 struct sigaction *actp;
450 struct usigaction *usa;
451 struct sigaction kact;
452 void (*handler)(int);
455 _thr_signal_block(curthread);
456 for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
457 usa = __libc_sigaction_slot(sig);
460 handler = actp->sa_handler;
461 if (handler != SIG_DFL && handler != SIG_IGN &&
462 __elf_phdr_match_addr(phdr_info, handler)) {
464 _thr_rwl_wrlock(rwlp);
465 if (handler != actp->sa_handler) {
466 _thr_rwl_unlock(rwlp);
469 actp->sa_handler = SIG_DFL;
470 actp->sa_flags = SA_SIGINFO;
471 SIGEMPTYSET(actp->sa_mask);
472 if (__sys_sigaction(sig, NULL, &kact) == 0 &&
473 kact.sa_handler != SIG_DFL &&
474 kact.sa_handler != SIG_IGN)
475 __sys_sigaction(sig, actp, NULL);
476 _thr_rwl_unlock(rwlp);
479 _thr_signal_unblock(curthread);
484 _thr_signal_prefork(void)
488 for (i = 1; i <= _SIG_MAXSIG; ++i)
489 _thr_rwl_rdlock(&__libc_sigaction_slot(i)->lock);
493 _thr_signal_postfork(void)
497 for (i = 1; i <= _SIG_MAXSIG; ++i)
498 _thr_rwl_unlock(&__libc_sigaction_slot(i)->lock);
502 _thr_signal_postfork_child(void)
506 for (i = 1; i <= _SIG_MAXSIG; ++i) {
507 bzero(&__libc_sigaction_slot(i) -> lock,
508 sizeof(struct urwlock));
513 _thr_signal_deinit(void)
518 __thr_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
520 struct sigaction newact, oldact, oldact2;
522 struct usigaction *usa;
525 if (!_SIG_VALID(sig) || sig == SIGCANCEL) {
532 usa = __libc_sigaction_slot(sig);
534 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
535 _thr_rwl_wrlock(&usa->lock);
538 oldact2 = usa->sigact;
542 * if a new sig handler is SIG_DFL or SIG_IGN,
543 * don't remove old handler from __libc_sigact[],
544 * so deferred signals still can use the handlers,
545 * multiple threads invoking sigaction itself is
546 * a race condition, so it is not a problem.
548 if (newact.sa_handler != SIG_DFL &&
549 newact.sa_handler != SIG_IGN) {
550 usa->sigact = newact;
551 remove_thr_signals(&usa->sigact.sa_mask);
552 newact.sa_flags &= ~SA_NODEFER;
553 newact.sa_flags |= SA_SIGINFO;
554 newact.sa_sigaction = thr_sighandler;
555 newact.sa_mask = _thr_maskset; /* mask all signals */
557 ret = __sys_sigaction(sig, &newact, &oldact);
560 usa->sigact = oldact2;
562 } else if (oact != NULL) {
563 ret = __sys_sigaction(sig, NULL, &oldact);
567 if (oldact.sa_handler != SIG_DFL && oldact.sa_handler != SIG_IGN) {
570 else if (oact != NULL)
571 oldact = usa->sigact;
574 _thr_rwl_unlock(&usa->lock);
575 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
587 __thr_sigprocmask(int how, const sigset_t *set, sigset_t *oset)
589 const sigset_t *p = set;
592 if (how != SIG_UNBLOCK) {
595 SIGDELSET(newset, SIGCANCEL);
599 return (__sys_sigprocmask(how, p, oset));
602 __weak_reference(_pthread_sigmask, pthread_sigmask);
605 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
608 if (__thr_sigprocmask(how, set, oset))
614 _sigsuspend(const sigset_t * set)
618 return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset)));
622 __thr_sigsuspend(const sigset_t * set)
624 struct pthread *curthread;
628 curthread = _get_curthread();
630 old = curthread->in_sigsuspend;
631 curthread->in_sigsuspend = 1;
632 _thr_cancel_enter(curthread);
633 ret = __sys_sigsuspend(thr_remove_thr_signals(set, &newset));
634 _thr_cancel_leave(curthread, 1);
635 curthread->in_sigsuspend = old;
636 if (curthread->unblock_sigcancel) {
637 curthread->unblock_sigcancel = 0;
639 SIGADDSET(newset, SIGCANCEL);
640 __sys_sigprocmask(SIG_UNBLOCK, &newset, NULL);
647 _sigtimedwait(const sigset_t *set, siginfo_t *info,
648 const struct timespec * timeout)
652 return (__sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
657 * Cancellation behavior:
658 * Thread may be canceled at start, if thread got signal,
659 * it is not canceled.
662 __thr_sigtimedwait(const sigset_t *set, siginfo_t *info,
663 const struct timespec * timeout)
665 struct pthread *curthread = _get_curthread();
669 _thr_cancel_enter(curthread);
670 ret = __sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
672 _thr_cancel_leave(curthread, (ret == -1));
677 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
681 return (__sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info));
685 * Cancellation behavior:
686 * Thread may be canceled at start, if thread got signal,
687 * it is not canceled.
690 __thr_sigwaitinfo(const sigset_t *set, siginfo_t *info)
692 struct pthread *curthread = _get_curthread();
696 _thr_cancel_enter(curthread);
697 ret = __sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info);
698 _thr_cancel_leave(curthread, ret == -1);
703 _sigwait(const sigset_t *set, int *sig)
707 return (__sys_sigwait(thr_remove_thr_signals(set, &newset), sig));
711 * Cancellation behavior:
712 * Thread may be canceled at start, if thread got signal,
713 * it is not canceled.
716 __thr_sigwait(const sigset_t *set, int *sig)
718 struct pthread *curthread = _get_curthread();
723 _thr_cancel_enter(curthread);
724 ret = __sys_sigwait(thr_remove_thr_signals(set, &newset), sig);
725 _thr_cancel_leave(curthread, (ret != 0));
726 } while (ret == EINTR);
731 __thr_setcontext(const ucontext_t *ucp)
739 if (!SIGISMEMBER(uc.uc_sigmask, SIGCANCEL))
740 return __sys_setcontext(ucp);
741 (void) memcpy(&uc, ucp, sizeof(uc));
742 SIGDELSET(uc.uc_sigmask, SIGCANCEL);
743 return (__sys_setcontext(&uc));
747 __thr_swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
751 if (oucp == NULL || ucp == NULL) {
755 if (SIGISMEMBER(ucp->uc_sigmask, SIGCANCEL)) {
756 (void) memcpy(&uc, ucp, sizeof(uc));
757 SIGDELSET(uc.uc_sigmask, SIGCANCEL);
760 return (__sys_swapcontext(oucp, ucp));