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 |
377 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
379 cycle = curthread->cycle;
381 /* Wake the thread suspending us. */
382 _thr_umtx_wake(&curthread->cycle, INT_MAX, 0);
385 * if we are from pthread_exit, we don't want to
386 * suspend, just go and die.
388 if (curthread->state == PS_DEAD)
390 curthread->flags |= THR_FLAGS_SUSPENDED;
391 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
392 _thr_umtx_wait_uint(&curthread->cycle, cycle, NULL, 0);
393 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
394 curthread->flags &= ~THR_FLAGS_SUSPENDED;
396 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
397 curthread->critical_count--;
399 _thr_signal_unblock(curthread);
403 _thr_signal_init(int dlopened)
405 struct sigaction act, nact, oact;
406 struct usigaction *usa;
411 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
412 for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
413 if (sig == SIGCANCEL)
415 error = __sys_sigaction(sig, NULL, &oact);
416 if (error == -1 || oact.sa_handler == SIG_DFL ||
417 oact.sa_handler == SIG_IGN)
419 usa = __libc_sigaction_slot(sig);
422 remove_thr_signals(&usa->sigact.sa_mask);
423 nact.sa_flags &= ~SA_NODEFER;
424 nact.sa_flags |= SA_SIGINFO;
425 nact.sa_sigaction = thr_sighandler;
426 nact.sa_mask = _thr_maskset;
427 (void)__sys_sigaction(sig, &nact, NULL);
429 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
432 /* Install SIGCANCEL handler. */
433 SIGFILLSET(act.sa_mask);
434 act.sa_flags = SA_SIGINFO;
435 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
436 __sys_sigaction(SIGCANCEL, &act, NULL);
438 /* Unblock SIGCANCEL */
439 SIGEMPTYSET(act.sa_mask);
440 SIGADDSET(act.sa_mask, SIGCANCEL);
441 __sys_sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL);
445 _thr_sigact_unload(struct dl_phdr_info *phdr_info)
448 struct pthread *curthread = _get_curthread();
449 struct urwlock *rwlp;
450 struct sigaction *actp;
451 struct usigaction *usa;
452 struct sigaction kact;
453 void (*handler)(int);
456 _thr_signal_block(curthread);
457 for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
458 usa = __libc_sigaction_slot(sig);
461 handler = actp->sa_handler;
462 if (handler != SIG_DFL && handler != SIG_IGN &&
463 __elf_phdr_match_addr(phdr_info, handler)) {
465 _thr_rwl_wrlock(rwlp);
466 if (handler != actp->sa_handler) {
467 _thr_rwl_unlock(rwlp);
470 actp->sa_handler = SIG_DFL;
471 actp->sa_flags = SA_SIGINFO;
472 SIGEMPTYSET(actp->sa_mask);
473 if (__sys_sigaction(sig, NULL, &kact) == 0 &&
474 kact.sa_handler != SIG_DFL &&
475 kact.sa_handler != SIG_IGN)
476 __sys_sigaction(sig, actp, NULL);
477 _thr_rwl_unlock(rwlp);
480 _thr_signal_unblock(curthread);
485 _thr_signal_prefork(void)
489 for (i = 1; i <= _SIG_MAXSIG; ++i)
490 _thr_rwl_rdlock(&__libc_sigaction_slot(i)->lock);
494 _thr_signal_postfork(void)
498 for (i = 1; i <= _SIG_MAXSIG; ++i)
499 _thr_rwl_unlock(&__libc_sigaction_slot(i)->lock);
503 _thr_signal_postfork_child(void)
507 for (i = 1; i <= _SIG_MAXSIG; ++i) {
508 bzero(&__libc_sigaction_slot(i) -> lock,
509 sizeof(struct urwlock));
514 _thr_signal_deinit(void)
519 __thr_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
521 struct sigaction newact, oldact, oldact2;
523 struct usigaction *usa;
526 if (!_SIG_VALID(sig) || sig == SIGCANCEL) {
533 usa = __libc_sigaction_slot(sig);
535 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
536 _thr_rwl_wrlock(&usa->lock);
539 oldact2 = usa->sigact;
543 * if a new sig handler is SIG_DFL or SIG_IGN,
544 * don't remove old handler from __libc_sigact[],
545 * so deferred signals still can use the handlers,
546 * multiple threads invoking sigaction itself is
547 * a race condition, so it is not a problem.
549 if (newact.sa_handler != SIG_DFL &&
550 newact.sa_handler != SIG_IGN) {
551 usa->sigact = newact;
552 remove_thr_signals(&usa->sigact.sa_mask);
553 newact.sa_flags &= ~SA_NODEFER;
554 newact.sa_flags |= SA_SIGINFO;
555 newact.sa_sigaction = thr_sighandler;
556 newact.sa_mask = _thr_maskset; /* mask all signals */
558 ret = __sys_sigaction(sig, &newact, &oldact);
561 usa->sigact = oldact2;
563 } else if (oact != NULL) {
564 ret = __sys_sigaction(sig, NULL, &oldact);
568 if (oldact.sa_handler != SIG_DFL && oldact.sa_handler != SIG_IGN) {
571 else if (oact != NULL)
572 oldact = usa->sigact;
575 _thr_rwl_unlock(&usa->lock);
576 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
588 __thr_sigprocmask(int how, const sigset_t *set, sigset_t *oset)
590 const sigset_t *p = set;
593 if (how != SIG_UNBLOCK) {
596 SIGDELSET(newset, SIGCANCEL);
600 return (__sys_sigprocmask(how, p, oset));
603 __weak_reference(_pthread_sigmask, pthread_sigmask);
606 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
609 if (__thr_sigprocmask(how, set, oset))
615 _sigsuspend(const sigset_t * set)
619 return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset)));
623 __thr_sigsuspend(const sigset_t * set)
625 struct pthread *curthread;
629 curthread = _get_curthread();
631 old = curthread->in_sigsuspend;
632 curthread->in_sigsuspend = 1;
633 _thr_cancel_enter(curthread);
634 ret = __sys_sigsuspend(thr_remove_thr_signals(set, &newset));
635 _thr_cancel_leave(curthread, 1);
636 curthread->in_sigsuspend = old;
637 if (curthread->unblock_sigcancel) {
638 curthread->unblock_sigcancel = 0;
640 SIGADDSET(newset, SIGCANCEL);
641 __sys_sigprocmask(SIG_UNBLOCK, &newset, NULL);
648 _sigtimedwait(const sigset_t *set, siginfo_t *info,
649 const struct timespec * timeout)
653 return (__sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
658 * Cancellation behavior:
659 * Thread may be canceled at start, if thread got signal,
660 * it is not canceled.
663 __thr_sigtimedwait(const sigset_t *set, siginfo_t *info,
664 const struct timespec * timeout)
666 struct pthread *curthread = _get_curthread();
670 _thr_cancel_enter(curthread);
671 ret = __sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
673 _thr_cancel_leave(curthread, (ret == -1));
678 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
682 return (__sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info));
686 * Cancellation behavior:
687 * Thread may be canceled at start, if thread got signal,
688 * it is not canceled.
691 __thr_sigwaitinfo(const sigset_t *set, siginfo_t *info)
693 struct pthread *curthread = _get_curthread();
697 _thr_cancel_enter(curthread);
698 ret = __sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info);
699 _thr_cancel_leave(curthread, ret == -1);
704 _sigwait(const sigset_t *set, int *sig)
708 return (__sys_sigwait(thr_remove_thr_signals(set, &newset), sig));
712 * Cancellation behavior:
713 * Thread may be canceled at start, if thread got signal,
714 * it is not canceled.
717 __thr_sigwait(const sigset_t *set, int *sig)
719 struct pthread *curthread = _get_curthread();
724 _thr_cancel_enter(curthread);
725 ret = __sys_sigwait(thr_remove_thr_signals(set, &newset), sig);
726 _thr_cancel_leave(curthread, (ret != 0));
727 } while (ret == EINTR);
732 __thr_setcontext(const ucontext_t *ucp)
740 if (!SIGISMEMBER(uc.uc_sigmask, SIGCANCEL))
741 return __sys_setcontext(ucp);
742 (void) memcpy(&uc, ucp, sizeof(uc));
743 SIGDELSET(uc.uc_sigmask, SIGCANCEL);
744 return (__sys_setcontext(&uc));
748 __thr_swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
752 if (oucp == NULL || ucp == NULL) {
756 if (SIGISMEMBER(ucp->uc_sigmask, SIGCANCEL)) {
757 (void) memcpy(&uc, ucp, sizeof(uc));
758 SIGDELSET(uc.uc_sigmask, SIGCANCEL);
761 return (__sys_swapcontext(oucp, ucp));