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 "thr_private.h"
43 /* #define DEBUG_SIGNAL */
45 #define DBG_MSG stdout_debug
51 struct sigaction sigact;
55 static struct usigaction _thr_sigact[_SIG_MAXSIG];
57 static void thr_sighandler(int, siginfo_t *, void *);
58 static void handle_signal(struct sigaction *, int, siginfo_t *, ucontext_t *);
59 static void check_deferred_signal(struct pthread *);
60 static void check_suspend(struct pthread *);
61 static void check_cancel(struct pthread *curthread, ucontext_t *ucp);
65 int __sigtimedwait(const sigset_t *set, siginfo_t *info,
66 const struct timespec * timeout);
67 int _sigtimedwait(const sigset_t *set, siginfo_t *info,
68 const struct timespec * timeout);
69 int __sigwaitinfo(const sigset_t *set, siginfo_t *info);
70 int _sigwaitinfo(const sigset_t *set, siginfo_t *info);
71 int ___sigwait(const sigset_t *set, int *sig);
72 int _sigwait(const sigset_t *set, int *sig);
73 int __sigsuspend(const sigset_t *sigmask);
74 int _sigaction(int, const struct sigaction *, struct sigaction *);
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,
147 struct sigcontext *scp, 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 = _get_curthread();
156 ucontext_t *ucp = _ucp;
157 struct sigaction act;
161 _thr_rwl_rdlock(&_thr_sigact[sig-1].lock);
162 act = _thr_sigact[sig-1].sigact;
163 _thr_rwl_unlock(&_thr_sigact[sig-1].lock);
165 curthread->deferred_run = 0;
168 * if a thread is in critical region, for example it holds low level locks,
169 * try to defer the signal processing, however if the signal is synchronous
170 * signal, it means a bad thing has happened, this is a programming error,
171 * resuming fault point can not help anything (normally causes deadloop),
172 * so here we let user code handle it immediately.
174 if (THR_IN_CRITICAL(curthread) && SIGISMEMBER(_thr_deferset, sig)) {
175 memcpy(&curthread->deferred_sigact, &act, sizeof(struct sigaction));
176 memcpy(&curthread->deferred_siginfo, info, sizeof(siginfo_t));
177 curthread->deferred_sigmask = ucp->uc_sigmask;
178 /* mask all signals, we will restore it later. */
179 ucp->uc_sigmask = _thr_deferset;
183 handle_signal(&act, sig, info, ucp);
187 handle_signal(struct sigaction *actp, int sig, siginfo_t *info, ucontext_t *ucp)
189 struct pthread *curthread = _get_curthread();
191 __siginfohandler_t *sigfunc;
198 /* add previous level mask */
199 SIGSETOR(actp->sa_mask, ucp->uc_sigmask);
201 /* add this signal's mask */
202 if (!(actp->sa_flags & SA_NODEFER))
203 SIGADDSET(actp->sa_mask, sig);
205 in_sigsuspend = curthread->in_sigsuspend;
206 curthread->in_sigsuspend = 0;
209 * If thread is in deferred cancellation mode, disable cancellation
211 * If user signal handler calls a cancellation point function, e.g,
212 * it calls write() to write data to file, because write() is a
213 * cancellation point, the thread is immediately cancelled if
214 * cancellation is pending, to avoid this problem while thread is in
215 * deferring mode, cancellation is temporarily disabled.
217 cancel_point = curthread->cancel_point;
218 cancel_async = curthread->cancel_async;
219 cancel_enable = curthread->cancel_enable;
220 curthread->cancel_point = 0;
222 curthread->cancel_enable = 0;
224 /* restore correct mask before calling user handler */
225 __sys_sigprocmask(SIG_SETMASK, &actp->sa_mask, NULL);
227 sigfunc = actp->sa_sigaction;
230 * We have already reset cancellation point flags, so if user's code
231 * longjmp()s out of its signal handler, wish its jmpbuf was set
232 * outside of a cancellation point, in most cases, this would be
233 * true. However, there is no way to save cancel_enable in jmpbuf,
234 * so after setjmps() returns once more, the user code may need to
235 * re-set cancel_enable flag by calling pthread_setcancelstate().
237 if ((actp->sa_flags & SA_SIGINFO) != 0)
238 (*(sigfunc))(sig, info, ucp);
240 ((ohandler)(*sigfunc))(
241 sig, info->si_code, (struct sigcontext *)ucp,
242 info->si_addr, (__sighandler_t *)sigfunc);
246 curthread->in_sigsuspend = in_sigsuspend;
247 curthread->cancel_point = cancel_point;
248 curthread->cancel_enable = cancel_enable;
250 memcpy(&uc2, ucp, sizeof(uc2));
251 SIGDELSET(uc2.uc_sigmask, SIGCANCEL);
253 /* reschedule cancellation */
254 check_cancel(curthread, &uc2);
256 __sys_sigreturn(&uc2);
260 _thr_ast(struct pthread *curthread)
263 if (!THR_IN_CRITICAL(curthread)) {
264 check_deferred_signal(curthread);
265 check_suspend(curthread);
266 check_cancel(curthread, NULL);
270 /* reschedule cancellation */
272 check_cancel(struct pthread *curthread, ucontext_t *ucp)
275 if (__predict_true(!curthread->cancel_pending ||
276 !curthread->cancel_enable || curthread->no_cancel))
280 * Otherwise, we are in defer mode, and we are at
281 * cancel point, tell kernel to not block the current
282 * thread on next cancelable system call.
284 * There are three cases we should call thr_wake() to
285 * turn on TDP_WAKEUP or send SIGCANCEL in kernel:
286 * 1) we are going to call a cancelable system call,
287 * non-zero cancel_point means we are already in
288 * cancelable state, next system call is cancelable.
289 * 2) because _thr_ast() may be called by
290 * THR_CRITICAL_LEAVE() which is used by rtld rwlock
291 * and any libthr internal locks, when rtld rwlock
292 * is used, it is mostly caused my an unresolved PLT.
293 * those routines may clear the TDP_WAKEUP flag by
294 * invoking some system calls, in those cases, we
295 * also should reenable the flag.
296 * 3) thread is in sigsuspend(), and the syscall insists
297 * on getting a signal before it agrees to return.
299 if (curthread->cancel_point) {
300 if (curthread->in_sigsuspend && ucp) {
301 SIGADDSET(ucp->uc_sigmask, SIGCANCEL);
302 curthread->unblock_sigcancel = 1;
303 _thr_send_sig(curthread, SIGCANCEL);
305 thr_wake(curthread->tid);
306 } else if (curthread->cancel_async) {
308 * asynchronous cancellation mode, act upon
311 _pthread_exit_mask(PTHREAD_CANCELED,
312 ucp? &ucp->uc_sigmask : NULL);
317 check_deferred_signal(struct pthread *curthread)
320 struct sigaction act;
324 if (__predict_true(curthread->deferred_siginfo.si_signo == 0 ||
325 curthread->deferred_run))
328 curthread->deferred_run = 1;
329 uc_len = __getcontextx_size();
332 if (curthread->deferred_siginfo.si_signo == 0) {
333 curthread->deferred_run = 0;
336 __fillcontextx2((char *)uc);
337 act = curthread->deferred_sigact;
338 uc->uc_sigmask = curthread->deferred_sigmask;
339 memcpy(&info, &curthread->deferred_siginfo, sizeof(siginfo_t));
341 curthread->deferred_siginfo.si_signo = 0;
342 handle_signal(&act, info.si_signo, &info, uc);
346 check_suspend(struct pthread *curthread)
350 if (__predict_true((curthread->flags &
351 (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED))
352 != THR_FLAGS_NEED_SUSPEND))
355 if (curthread->force_exit)
359 * Blocks SIGCANCEL which other threads must send.
361 _thr_signal_block(curthread);
364 * Increase critical_count, here we don't use THR_LOCK/UNLOCK
365 * because we are leaf code, we don't want to recursively call
368 curthread->critical_count++;
369 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
370 while ((curthread->flags & (THR_FLAGS_NEED_SUSPEND |
371 THR_FLAGS_SUSPENDED)) == THR_FLAGS_NEED_SUSPEND) {
373 cycle = curthread->cycle;
375 /* Wake the thread suspending us. */
376 _thr_umtx_wake(&curthread->cycle, INT_MAX, 0);
379 * if we are from pthread_exit, we don't want to
380 * suspend, just go and die.
382 if (curthread->state == PS_DEAD)
384 curthread->flags |= THR_FLAGS_SUSPENDED;
385 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
386 _thr_umtx_wait_uint(&curthread->cycle, cycle, NULL, 0);
387 THR_UMUTEX_LOCK(curthread, &(curthread)->lock);
388 curthread->flags &= ~THR_FLAGS_SUSPENDED;
390 THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock);
391 curthread->critical_count--;
393 _thr_signal_unblock(curthread);
397 _thr_signal_init(void)
399 struct sigaction act;
401 /* Install SIGCANCEL handler. */
402 SIGFILLSET(act.sa_mask);
403 act.sa_flags = SA_SIGINFO;
404 act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler;
405 __sys_sigaction(SIGCANCEL, &act, NULL);
407 /* Unblock SIGCANCEL */
408 SIGEMPTYSET(act.sa_mask);
409 SIGADDSET(act.sa_mask, SIGCANCEL);
410 __sys_sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL);
414 _thr_sigact_unload(struct dl_phdr_info *phdr_info)
417 struct pthread *curthread = _get_curthread();
418 struct urwlock *rwlp;
419 struct sigaction *actp;
420 struct sigaction kact;
421 void (*handler)(int);
424 _thr_signal_block(curthread);
425 for (sig = 1; sig <= _SIG_MAXSIG; sig++) {
426 actp = &_thr_sigact[sig-1].sigact;
428 handler = actp->sa_handler;
429 if (handler != SIG_DFL && handler != SIG_IGN &&
430 __elf_phdr_match_addr(phdr_info, handler)) {
431 rwlp = &_thr_sigact[sig-1].lock;
432 _thr_rwl_wrlock(rwlp);
433 if (handler != actp->sa_handler) {
434 _thr_rwl_unlock(rwlp);
437 actp->sa_handler = SIG_DFL;
438 actp->sa_flags = SA_SIGINFO;
439 SIGEMPTYSET(actp->sa_mask);
440 if (__sys_sigaction(sig, NULL, &kact) == 0 &&
441 kact.sa_handler != SIG_DFL &&
442 kact.sa_handler != SIG_IGN)
443 __sys_sigaction(sig, actp, NULL);
444 _thr_rwl_unlock(rwlp);
447 _thr_signal_unblock(curthread);
452 _thr_signal_prefork(void)
456 for (i = 1; i <= _SIG_MAXSIG; ++i)
457 _thr_rwl_rdlock(&_thr_sigact[i-1].lock);
461 _thr_signal_postfork(void)
465 for (i = 1; i <= _SIG_MAXSIG; ++i)
466 _thr_rwl_unlock(&_thr_sigact[i-1].lock);
470 _thr_signal_postfork_child(void)
474 for (i = 1; i <= _SIG_MAXSIG; ++i)
475 bzero(&_thr_sigact[i-1].lock, sizeof(struct urwlock));
479 _thr_signal_deinit(void)
483 __weak_reference(___pause, pause);
490 if (_sigprocmask(SIG_BLOCK, NULL, &oset) == -1)
492 return (__sigsuspend(&oset));
495 __weak_reference(_raise, raise);
500 return _thr_send_sig(_get_curthread(), sig);
503 __weak_reference(_sigaction, sigaction);
506 _sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
508 struct sigaction newact, oldact, oldact2;
510 int ret = 0, err = 0;
512 if (!_SIG_VALID(sig) || sig == SIGCANCEL) {
520 __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset);
521 _thr_rwl_wrlock(&_thr_sigact[sig-1].lock);
524 oldact2 = _thr_sigact[sig-1].sigact;
527 * if a new sig handler is SIG_DFL or SIG_IGN,
528 * don't remove old handler from _thr_sigact[],
529 * so deferred signals still can use the handlers,
530 * multiple threads invoking sigaction itself is
531 * a race condition, so it is not a problem.
533 if (newact.sa_handler != SIG_DFL &&
534 newact.sa_handler != SIG_IGN) {
535 _thr_sigact[sig-1].sigact = newact;
537 &_thr_sigact[sig-1].sigact.sa_mask);
538 newact.sa_flags &= ~SA_NODEFER;
539 newact.sa_flags |= SA_SIGINFO;
540 newact.sa_sigaction = thr_sighandler;
541 newact.sa_mask = _thr_maskset; /* mask all signals */
543 if ((ret = __sys_sigaction(sig, &newact, &oldact))) {
545 _thr_sigact[sig-1].sigact = oldact2;
547 } else if (oact != NULL) {
548 ret = __sys_sigaction(sig, NULL, &oldact);
552 if (oldact.sa_handler != SIG_DFL &&
553 oldact.sa_handler != SIG_IGN) {
556 else if (oact != NULL)
557 oldact = _thr_sigact[sig-1].sigact;
560 _thr_rwl_unlock(&_thr_sigact[sig-1].lock);
561 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
572 __weak_reference(_sigprocmask, sigprocmask);
575 _sigprocmask(int how, const sigset_t *set, sigset_t *oset)
577 const sigset_t *p = set;
580 if (how != SIG_UNBLOCK) {
583 SIGDELSET(newset, SIGCANCEL);
587 return (__sys_sigprocmask(how, p, oset));
590 __weak_reference(_pthread_sigmask, pthread_sigmask);
593 _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
595 if (_sigprocmask(how, set, oset))
600 __weak_reference(__sigsuspend, sigsuspend);
603 _sigsuspend(const sigset_t * set)
607 return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset)));
611 __sigsuspend(const sigset_t * set)
613 struct pthread *curthread;
617 curthread = _get_curthread();
619 old = curthread->in_sigsuspend;
620 curthread->in_sigsuspend = 1;
621 _thr_cancel_enter(curthread);
622 ret = __sys_sigsuspend(thr_remove_thr_signals(set, &newset));
623 _thr_cancel_leave(curthread, 1);
624 curthread->in_sigsuspend = old;
625 if (curthread->unblock_sigcancel) {
626 curthread->unblock_sigcancel = 0;
628 SIGADDSET(newset, SIGCANCEL);
629 __sys_sigprocmask(SIG_UNBLOCK, &newset, NULL);
635 __weak_reference(___sigwait, sigwait);
636 __weak_reference(__sigtimedwait, sigtimedwait);
637 __weak_reference(__sigwaitinfo, sigwaitinfo);
640 _sigtimedwait(const sigset_t *set, siginfo_t *info,
641 const struct timespec * timeout)
645 return (__sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
650 * Cancellation behavior:
651 * Thread may be canceled at start, if thread got signal,
652 * it is not canceled.
655 __sigtimedwait(const sigset_t *set, siginfo_t *info,
656 const struct timespec * timeout)
658 struct pthread *curthread = _get_curthread();
662 _thr_cancel_enter(curthread);
663 ret = __sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info,
665 _thr_cancel_leave(curthread, (ret == -1));
670 _sigwaitinfo(const sigset_t *set, siginfo_t *info)
674 return (__sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info));
678 * Cancellation behavior:
679 * Thread may be canceled at start, if thread got signal,
680 * it is not canceled.
683 __sigwaitinfo(const sigset_t *set, siginfo_t *info)
685 struct pthread *curthread = _get_curthread();
689 _thr_cancel_enter(curthread);
690 ret = __sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info);
691 _thr_cancel_leave(curthread, ret == -1);
696 _sigwait(const sigset_t *set, int *sig)
700 return (__sys_sigwait(thr_remove_thr_signals(set, &newset), sig));
704 * Cancellation behavior:
705 * Thread may be canceled at start, if thread got signal,
706 * it is not canceled.
709 ___sigwait(const sigset_t *set, int *sig)
711 struct pthread *curthread = _get_curthread();
716 _thr_cancel_enter(curthread);
717 ret = __sys_sigwait(thr_remove_thr_signals(set, &newset), sig);
718 _thr_cancel_leave(curthread, (ret != 0));
719 } while (ret == EINTR);
723 __weak_reference(_setcontext, setcontext);
725 _setcontext(const ucontext_t *ucp)
729 (void) memcpy(&uc, ucp, sizeof(uc));
730 remove_thr_signals(&uc.uc_sigmask);
731 return __sys_setcontext(&uc);
734 __weak_reference(_swapcontext, swapcontext);
736 _swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
740 (void) memcpy(&uc, ucp, sizeof(uc));
741 remove_thr_signals(&uc.uc_sigmask);
742 return __sys_swapcontext(oucp, &uc);