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>
33 #include <sys/syscall.h>
39 #include "un-namespace.h"
40 #include "libc_private.h"
42 #include "libc_private.h"
43 #include "thr_private.h"
45 /* #define DEBUG_SIGNAL */
47 #define DBG_MSG stdout_debug
53 struct sigaction sigact;
57 static struct usigaction _thr_sigact[_SIG_MAXSIG];
59 static inline struct usigaction *
60 __libc_sigaction_slot(int signo)
63 return (&_thr_sigact[signo - 1]);
66 static void thr_sighandler(int, siginfo_t *, void *);
67 static void handle_signal(struct sigaction *, int, siginfo_t *, ucontext_t *);
68 static void check_deferred_signal(struct pthread *);
69 static void check_suspend(struct pthread *);
70 static void check_cancel(struct pthread *curthread, ucontext_t *ucp);
72 int _sigtimedwait(const sigset_t *set, siginfo_t *info,
73 const struct timespec * timeout);
74 int _sigwaitinfo(const sigset_t *set, siginfo_t *info);
75 int _sigwait(const sigset_t *set, int *sig);
76 int _setcontext(const ucontext_t *);
77 int _swapcontext(ucontext_t *, const ucontext_t *);
79 static const sigset_t _thr_deferset={{
80 0xffffffff & ~(_SIG_BIT(SIGBUS)|_SIG_BIT(SIGILL)|_SIG_BIT(SIGFPE)|
81 _SIG_BIT(SIGSEGV)|_SIG_BIT(SIGTRAP)|_SIG_BIT(SIGSYS)),
86 static const sigset_t _thr_maskset={{
93 _thr_signal_block(struct pthread *curthread)
96 if (curthread->sigblock > 0) {
97 curthread->sigblock++;
100 __sys_sigprocmask(SIG_BLOCK, &_thr_maskset, &curthread->sigmask);
101 curthread->sigblock++;
105 _thr_signal_unblock(struct pthread *curthread)
107 if (--curthread->sigblock == 0)
108 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
112 _thr_send_sig(struct pthread *thread, int sig)
114 return thr_kill(thread->tid, sig);
118 remove_thr_signals(sigset_t *set)
120 if (SIGISMEMBER(*set, SIGCANCEL))
121 SIGDELSET(*set, SIGCANCEL);
124 static const sigset_t *
125 thr_remove_thr_signals(const sigset_t *set, sigset_t *newset)
128 remove_thr_signals(newset);
133 sigcancel_handler(int sig __unused,
134 siginfo_t *info __unused, ucontext_t *ucp)
136 struct pthread *curthread = _get_curthread();
139 if (THR_IN_CRITICAL(curthread))
142 check_suspend(curthread);
143 check_cancel(curthread, ucp);
147 typedef void (*ohandler)(int sig, int code, struct sigcontext *scp,
148 char *addr, __sighandler_t *catcher);
151 * The signal handler wrapper is entered with all signal masked.
154 thr_sighandler(int sig, siginfo_t *info, void *_ucp)
156 struct pthread *curthread;
158 struct sigaction act;
159 struct usigaction *usa;
163 curthread = _get_curthread();
165 usa = __libc_sigaction_slot(sig);
166 _thr_rwl_rdlock(&usa->lock);
168 _thr_rwl_unlock(&usa->lock);
170 curthread->deferred_run = 0;
173 * if a thread is in critical region, for example it holds low level locks,
174 * try to defer the signal processing, however if the signal is synchronous
175 * signal, it means a bad thing has happened, this is a programming error,
176 * resuming fault point can not help anything (normally causes deadloop),
177 * so here we let user code handle it immediately.
179 if (THR_IN_CRITICAL(curthread) && SIGISMEMBER(_thr_deferset, sig)) {
180 memcpy(&curthread->deferred_sigact, &act, sizeof(struct sigaction));
181 memcpy(&curthread->deferred_siginfo, info, sizeof(siginfo_t));
182 curthread->deferred_sigmask = ucp->uc_sigmask;
183 /* mask all signals, we will restore it later. */
184 ucp->uc_sigmask = _thr_deferset;
188 handle_signal(&act, sig, info, ucp);
192 handle_signal(struct sigaction *actp, int sig, siginfo_t *info, ucontext_t *ucp)
194 struct pthread *curthread = _get_curthread();
196 __siginfohandler_t *sigfunc;
203 /* add previous level mask */
204 SIGSETOR(actp->sa_mask, ucp->uc_sigmask);
206 /* add this signal's mask */
207 if (!(actp->sa_flags & SA_NODEFER))
208 SIGADDSET(actp->sa_mask, sig);
210 in_sigsuspend = curthread->in_sigsuspend;
211 curthread->in_sigsuspend = 0;
214 * If thread is in deferred cancellation mode, disable cancellation
216 * If user signal handler calls a cancellation point function, e.g,
217 * it calls write() to write data to file, because write() is a
218 * cancellation point, the thread is immediately cancelled if
219 * cancellation is pending, to avoid this problem while thread is in
220 * deferring mode, cancellation is temporarily disabled.
222 cancel_point = curthread->cancel_point;
223 cancel_async = curthread->cancel_async;
224 cancel_enable = curthread->cancel_enable;
225 curthread->cancel_point = 0;
227 curthread->cancel_enable = 0;
229 /* restore correct mask before calling user handler */
230 __sys_sigprocmask(SIG_SETMASK, &actp->sa_mask, NULL);
232 sigfunc = actp->sa_sigaction;
235 * We have already reset cancellation point flags, so if user's code
236 * longjmp()s out of its signal handler, wish its jmpbuf was set
237 * outside of a cancellation point, in most cases, this would be
238 * true. However, there is no way to save cancel_enable in jmpbuf,
239 * so after setjmps() returns once more, the user code may need to
240 * re-set cancel_enable flag by calling pthread_setcancelstate().
242 if ((actp->sa_flags & SA_SIGINFO) != 0) {
243 sigfunc(sig, info, ucp);
245 ((ohandler)sigfunc)(sig, info->si_code,
246 (struct sigcontext *)ucp, info->si_addr,
247 (__sighandler_t *)sigfunc);
251 curthread->in_sigsuspend = in_sigsuspend;
252 curthread->cancel_point = cancel_point;
253 curthread->cancel_enable = cancel_enable;
255 memcpy(&uc2, ucp, sizeof(uc2));
256 SIGDELSET(uc2.uc_sigmask, SIGCANCEL);
258 /* reschedule cancellation */
259 check_cancel(curthread, &uc2);
261 syscall(SYS_sigreturn, &uc2);
265 _thr_ast(struct pthread *curthread)
268 if (!THR_IN_CRITICAL(curthread)) {
269 check_deferred_signal(curthread);
270 check_suspend(curthread);
271 check_cancel(curthread, NULL);
275 /* reschedule cancellation */
277 check_cancel(struct pthread *curthread, ucontext_t *ucp)
280 if (__predict_true(!curthread->cancel_pending ||
281 !curthread->cancel_enable || curthread->no_cancel))
285 * Otherwise, we are in defer mode, and we are at
286 * cancel point, tell kernel to not block the current
287 * thread on next cancelable system call.
289 * There are three cases we should call thr_wake() to
290 * turn on TDP_WAKEUP or send SIGCANCEL in kernel:
291 * 1) we are going to call a cancelable system call,
292 * non-zero cancel_point means we are already in
293 * cancelable state, next system call is cancelable.
294 * 2) because _thr_ast() may be called by
295 * THR_CRITICAL_LEAVE() which is used by rtld rwlock
296 * and any libthr internal locks, when rtld rwlock
297 * is used, it is mostly caused by an unresolved PLT.
298 * Those routines may clear the TDP_WAKEUP flag by
299 * invoking some system calls, in those cases, we
300 * also should reenable the flag.
301 * 3) thread is in sigsuspend(), and the syscall insists
302 * on getting a signal before it agrees to return.
304 if (curthread->cancel_point) {
305 if (curthread->in_sigsuspend && ucp) {
306 SIGADDSET(ucp->uc_sigmask, SIGCANCEL);
307 curthread->unblock_sigcancel = 1;
308 _thr_send_sig(curthread, SIGCANCEL);
310 thr_wake(curthread->tid);
311 } else if (curthread->cancel_async) {
313 * asynchronous cancellation mode, act upon
316 _pthread_exit_mask(PTHREAD_CANCELED,
317 ucp? &ucp->uc_sigmask : NULL);
322 check_deferred_signal(struct pthread *curthread)
325 struct sigaction act;
329 if (__predict_true(curthread->deferred_siginfo.si_signo == 0 ||
330 curthread->deferred_run))
333 curthread->deferred_run = 1;
334 uc_len = __getcontextx_size();
337 if (curthread->deferred_siginfo.si_signo == 0) {
338 curthread->deferred_run = 0;
341 __fillcontextx2((char *)uc);
342 act = curthread->deferred_sigact;
343 uc->uc_sigmask = curthread->deferred_sigmask;
344 memcpy(&info, &curthread->deferred_siginfo, sizeof(siginfo_t));
346 curthread->deferred_siginfo.si_signo = 0;
347 handle_signal(&act, info.si_signo, &info, uc);
351 check_suspend(struct pthread *curthread)
355 if (__predict_true((curthread->flags &
356 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
357 != THR_FLAGS_NEED_SUSPEND))
359 if (curthread == _single_thread)
361 if (curthread->force_exit)
365 * Blocks SIGCANCEL which other threads must send.
367 _thr_signal_block(curthread);
370 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
371 * because we are leaf code, we don't want to recursively call
374 curthread->critical_count++;
375 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
376 while ((curthread->flags & THR_FLAGS_NEED_SUSPEND) != 0) {
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);
394 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
395 curthread->critical_count--;
397 _thr_signal_unblock(curthread);
401 _thr_signal_init(int dlopened)
403 struct sigaction act, nact, oact;
404 struct usigaction *usa;
409 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
410 for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
411 if (sig == SIGCANCEL)
413 error = __sys_sigaction(sig, NULL, &oact);
414 if (error == -1 || oact.sa_handler == SIG_DFL ||
415 oact.sa_handler == SIG_IGN)
417 usa = __libc_sigaction_slot(sig);
420 remove_thr_signals(&usa->sigact.sa_mask);
421 nact.sa_flags &= ~SA_NODEFER;
422 nact.sa_flags |= SA_SIGINFO;
423 nact.sa_sigaction = thr_sighandler;
424 nact.sa_mask = _thr_maskset;
425 (void)__sys_sigaction(sig, &nact, NULL);
427 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
430 /* Install SIGCANCEL handler. */
431 SIGFILLSET(act.sa_mask);
432 act.sa_flags = SA_SIGINFO;
433 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
434 __sys_sigaction(SIGCANCEL, &act, NULL);
436 /* Unblock SIGCANCEL */
437 SIGEMPTYSET(act.sa_mask);
438 SIGADDSET(act.sa_mask, SIGCANCEL);
439 __sys_sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL);
443 _thr_sigact_unload(struct dl_phdr_info *phdr_info)
446 struct pthread *curthread = _get_curthread();
447 struct urwlock *rwlp;
448 struct sigaction *actp;
449 struct usigaction *usa;
450 struct sigaction kact;
451 void (*handler)(int);
454 _thr_signal_block(curthread);
455 for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
456 usa = __libc_sigaction_slot(sig);
459 handler = actp->sa_handler;
460 if (handler != SIG_DFL && handler != SIG_IGN &&
461 __elf_phdr_match_addr(phdr_info, handler)) {
463 _thr_rwl_wrlock(rwlp);
464 if (handler != actp->sa_handler) {
465 _thr_rwl_unlock(rwlp);
468 actp->sa_handler = SIG_DFL;
469 actp->sa_flags = SA_SIGINFO;
470 SIGEMPTYSET(actp->sa_mask);
471 if (__sys_sigaction(sig, NULL, &kact) == 0 &&
472 kact.sa_handler != SIG_DFL &&
473 kact.sa_handler != SIG_IGN)
474 __sys_sigaction(sig, actp, NULL);
475 _thr_rwl_unlock(rwlp);
478 _thr_signal_unblock(curthread);
483 _thr_signal_prefork(void)
487 for (i = 1; i <= _SIG_MAXSIG; ++i)
488 _thr_rwl_rdlock(&__libc_sigaction_slot(i)->lock);
492 _thr_signal_postfork(void)
496 for (i = 1; i <= _SIG_MAXSIG; ++i)
497 _thr_rwl_unlock(&__libc_sigaction_slot(i)->lock);
501 _thr_signal_postfork_child(void)
505 for (i = 1; i <= _SIG_MAXSIG; ++i) {
506 bzero(&__libc_sigaction_slot(i) -> lock,
507 sizeof(struct urwlock));
512 _thr_signal_deinit(void)
517 __thr_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
519 struct sigaction newact, oldact, oldact2;
521 struct usigaction *usa;
524 if (!_SIG_VALID(sig) || sig == SIGCANCEL) {
531 usa = __libc_sigaction_slot(sig);
533 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
534 _thr_rwl_wrlock(&usa->lock);
537 oldact2 = usa->sigact;
541 * if a new sig handler is SIG_DFL or SIG_IGN,
542 * don't remove old handler from __libc_sigact[],
543 * so deferred signals still can use the handlers,
544 * multiple threads invoking sigaction itself is
545 * a race condition, so it is not a problem.
547 if (newact.sa_handler != SIG_DFL &&
548 newact.sa_handler != SIG_IGN) {
549 usa->sigact = newact;
550 remove_thr_signals(&usa->sigact.sa_mask);
551 newact.sa_flags &= ~SA_NODEFER;
552 newact.sa_flags |= SA_SIGINFO;
553 newact.sa_sigaction = thr_sighandler;
554 newact.sa_mask = _thr_maskset; /* mask all signals */
556 ret = __sys_sigaction(sig, &newact, &oldact);
559 usa->sigact = oldact2;
561 } else if (oact != NULL) {
562 ret = __sys_sigaction(sig, NULL, &oldact);
566 if (oldact.sa_handler != SIG_DFL && oldact.sa_handler != SIG_IGN) {
569 else if (oact != NULL)
570 oldact = usa->sigact;
573 _thr_rwl_unlock(&usa->lock);
574 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
586 __thr_sigprocmask(int how, const sigset_t *set, sigset_t *oset)
588 const sigset_t *p = set;
591 if (how != SIG_UNBLOCK) {
594 SIGDELSET(newset, SIGCANCEL);
598 return (__sys_sigprocmask(how, p, oset));
601 __weak_reference(_pthread_sigmask, pthread_sigmask);
604 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
607 if (__thr_sigprocmask(how, set, oset))
613 _sigsuspend(const sigset_t * set)
617 return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset)));
621 __thr_sigsuspend(const sigset_t * set)
623 struct pthread *curthread;
627 curthread = _get_curthread();
629 old = curthread->in_sigsuspend;
630 curthread->in_sigsuspend = 1;
631 _thr_cancel_enter(curthread);
632 ret = __sys_sigsuspend(thr_remove_thr_signals(set, &newset));
633 _thr_cancel_leave(curthread, 1);
634 curthread->in_sigsuspend = old;
635 if (curthread->unblock_sigcancel) {
636 curthread->unblock_sigcancel = 0;
638 SIGADDSET(newset, SIGCANCEL);
639 __sys_sigprocmask(SIG_UNBLOCK, &newset, NULL);
646 _sigtimedwait(const sigset_t *set, siginfo_t *info,
647 const struct timespec * timeout)
651 return (__sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
656 * Cancellation behavior:
657 * Thread may be canceled at start, if thread got signal,
658 * it is not canceled.
661 __thr_sigtimedwait(const sigset_t *set, siginfo_t *info,
662 const struct timespec * timeout)
664 struct pthread *curthread = _get_curthread();
668 _thr_cancel_enter(curthread);
669 ret = __sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
671 _thr_cancel_leave(curthread, (ret == -1));
676 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
680 return (__sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info));
684 * Cancellation behavior:
685 * Thread may be canceled at start, if thread got signal,
686 * it is not canceled.
689 __thr_sigwaitinfo(const sigset_t *set, siginfo_t *info)
691 struct pthread *curthread = _get_curthread();
695 _thr_cancel_enter(curthread);
696 ret = __sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info);
697 _thr_cancel_leave(curthread, ret == -1);
702 _sigwait(const sigset_t *set, int *sig)
706 return (__sys_sigwait(thr_remove_thr_signals(set, &newset), sig));
710 * Cancellation behavior:
711 * Thread may be canceled at start, if thread got signal,
712 * it is not canceled.
715 __thr_sigwait(const sigset_t *set, int *sig)
717 struct pthread *curthread = _get_curthread();
722 _thr_cancel_enter(curthread);
723 ret = __sys_sigwait(thr_remove_thr_signals(set, &newset), sig);
724 _thr_cancel_leave(curthread, (ret != 0));
725 } while (ret == EINTR);
730 __thr_setcontext(const ucontext_t *ucp)
738 if (!SIGISMEMBER(uc.uc_sigmask, SIGCANCEL))
739 return __sys_setcontext(ucp);
740 (void) memcpy(&uc, ucp, sizeof(uc));
741 SIGDELSET(uc.uc_sigmask, SIGCANCEL);
742 return (__sys_setcontext(&uc));
746 __thr_swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
750 if (oucp == NULL || ucp == NULL) {
754 if (SIGISMEMBER(ucp->uc_sigmask, SIGCANCEL)) {
755 (void) memcpy(&uc, ucp, sizeof(uc));
756 SIGDELSET(uc.uc_sigmask, SIGCANCEL);
759 return (__sys_swapcontext(oucp, ucp));