fix regression from a745c4bfc8a9b5db4e48387170da0dc1d39e3abe
[uclibc-ng.git] / libc / stdlib / abort.c
blobdc2747f61b23fdf62bed7d2916db1e01cbbde054
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, see <http://www.gnu.org/licenses/>. */
18 /* Hacked up for uClibc by Erik Andersen */
20 #include <features.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <errno.h>
30 /* Defeat compiler optimization which assumes function addresses are never NULL */
31 static __always_inline int not_null_ptr(const void *p)
33 const void *q;
34 __asm__ (""
35 : "=r" (q) /* output */
36 : "0" (p) /* input */
38 return q != 0;
41 /* Our last ditch effort to commit suicide */
42 #ifdef __UCLIBC_ABORT_INSTRUCTION__
43 # define ABORT_INSTRUCTION __asm__(__UCLIBC_ABORT_INSTRUCTION__)
44 #else
45 # define ABORT_INSTRUCTION
46 # warning "no abort instruction defined for your arch"
47 #endif
49 static smallint been_there_done_that = 0;
51 /* Be prepared in case multiple threads try to abort() */
52 #include <bits/uClibc_mutex.h>
53 __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
55 /* Cause an abnormal program termination with core-dump */
56 void abort(void)
58 sigset_t sigs;
60 /* Make sure we acquire the lock before proceeding */
61 __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(mylock);
63 /* Unmask SIGABRT to be sure we can get it */
64 __sigemptyset(&sigs);
65 __sigaddset(&sigs, SIGABRT);
66 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
68 while (1) {
69 /* Try to suicide with a SIGABRT */
70 if (been_there_done_that == 0) {
71 been_there_done_that++;
73 #ifdef __UCLIBC_HAS_STDIO_SHUTDOWN_ON_ABORT__
74 /* If we are using stdio, try to shut it down. At the very least,
75 * this will attempt to commit all buffered writes. It may also
76 * unbuffer all writable files, or close them outright.
77 * Check the stdio routines for details. */
78 if (not_null_ptr(_stdio_term)) {
79 _stdio_term();
81 #endif
83 abort_it:
84 __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(mylock);
85 raise(SIGABRT);
86 __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(mylock);
89 /* Still here? Try to remove any signal handlers */
90 if (been_there_done_that == 1) {
91 struct sigaction act;
93 been_there_done_that++;
94 memset(&act, '\0', sizeof(struct sigaction));
95 if (SIG_DFL) /* if it's constant zero, already done */
96 act.sa_handler = SIG_DFL;
97 __sigfillset(&act.sa_mask);
98 sigaction(SIGABRT, &act, NULL);
100 goto abort_it;
103 /* Still here? Try to suicide with an illegal instruction */
104 if (been_there_done_that == 2) {
105 been_there_done_that++;
106 ABORT_INSTRUCTION;
109 /* Still here? Try to at least exit */
110 if (been_there_done_that == 3) {
111 been_there_done_that++;
112 _exit(127);
115 /* Still here? We're screwed. Sleepy time. Good night. */
116 while (1)
117 /* Try for ever and ever */
118 ABORT_INSTRUCTION;
121 libc_hidden_def(abort)