always terminate by SIGABRT when abort is called
[musl.git] / src / exit / abort.c
blobd6bd546b40ec92e9063c388a081088f39cfb21bd
1 #include <stdlib.h>
2 #include <signal.h>
3 #include "syscall.h"
4 #include "pthread_impl.h"
5 #include "atomic.h"
6 #include "libc.h"
7 #include "ksigaction.h"
9 __attribute__((__visibility__("hidden")))
10 volatile int __abort_lock[1];
12 _Noreturn void abort(void)
14 raise(SIGABRT);
16 /* If there was a SIGABRT handler installed and it returned, or if
17 * SIGABRT was blocked or ignored, take an AS-safe lock to prevent
18 * sigaction from installing a new SIGABRT handler, uninstall any
19 * handler that may be present, and re-raise the signal to generate
20 * the default action of abnormal termination. */
21 __block_all_sigs(0);
22 LOCK(__abort_lock);
23 __syscall(SYS_rt_sigaction, SIGABRT,
24 &(struct k_sigaction){.handler = SIG_DFL}, 0, _NSIG/8);
25 __syscall(SYS_tkill, __pthread_self()->tid, SIGABRT);
26 __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
27 &(long[_NSIG/(8*sizeof(long))]){1UL<<(SIGABRT-1)}, 0, _NSIG/8);
29 /* Beyond this point should be unreachable. */
30 a_crash();
31 raise(SIGKILL);
32 _Exit(127);