uClibc-cristi

uClibc-cristi Git Source Tree

Root/libc/stdlib/abort.c

1/* Copyright (C) 1991 Free Software Foundation, Inc.
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
19/* Hacked up for uClibc by Erik Andersen */
20
21#include <features.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <signal.h>
27#include <errno.h>
28
29libc_hidden_proto(abort)
30
31/* Experimentally off - libc_hidden_proto(memset) */
32libc_hidden_proto(sigaction)
33libc_hidden_proto(sigprocmask)
34libc_hidden_proto(raise)
35libc_hidden_proto(_exit)
36
37/* Our last ditch effort to commit suicide */
38#ifdef __UCLIBC_ABORT_INSTRUCTION__
39# define ABORT_INSTRUCTION __asm__(__UCLIBC_ABORT_INSTRUCTION__)
40#else
41# define ABORT_INSTRUCTION
42# warning "no abort instruction defined for your arch"
43#endif
44
45#ifdef __UCLIBC_HAS_STDIO_SHUTDOWN_ON_ABORT__
46extern void weak_function _stdio_term(void) attribute_hidden;
47#endif
48static smallint been_there_done_that = 0;
49
50/* Be prepared in case multiple threads try to abort() */
51#include <bits/uClibc_mutex.h>
52__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
53
54/* Cause an abnormal program termination with core-dump */
55void abort(void)
56{
57    sigset_t sigs;
58
59    /* Make sure we acquire the lock before proceeding */
60    __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(mylock);
61
62    /* Unmask SIGABRT to be sure we can get it */
63    if (__sigemptyset(&sigs) == 0 && __sigaddset(&sigs, SIGABRT) == 0) {
64        sigprocmask(SIG_UNBLOCK, &sigs, (sigset_t *) NULL);
65    }
66
67    while (1) {
68        /* Try to suicide with a SIGABRT */
69        if (been_there_done_that == 0) {
70            been_there_done_that++;
71
72#ifdef __UCLIBC_HAS_STDIO_SHUTDOWN_ON_ABORT__
73            /* If we are using stdio, try to shut it down. At the very least,
74             * this will attempt to commit all buffered writes. It may also
75             * unbuffer all writable files, or close them outright.
76             * Check the stdio routines for details. */
77            if (_stdio_term) {
78                _stdio_term();
79            }
80#endif
81
82abort_it:
83            __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(mylock);
84            raise(SIGABRT);
85            __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(mylock);
86        }
87
88        /* Still here? Try to remove any signal handlers */
89        if (been_there_done_that == 1) {
90            struct sigaction act;
91
92            been_there_done_that++;
93            memset(&act, '\0', sizeof(struct sigaction));
94            act.sa_handler = SIG_DFL;
95            __sigfillset(&act.sa_mask);
96            act.sa_flags = 0;
97            sigaction(SIGABRT, &act, NULL);
98
99            goto abort_it;
100        }
101
102        /* Still here? Try to suicide with an illegal instruction */
103        if (been_there_done_that == 2) {
104            been_there_done_that++;
105            ABORT_INSTRUCTION;
106        }
107
108        /* Still here? Try to at least exit */
109        if (been_there_done_that == 3) {
110            been_there_done_that++;
111            _exit(127);
112        }
113
114        /* Still here? We're screwed. Sleepy time. Good night. */
115        while (1)
116            /* Try for ever and ever */
117            ABORT_INSTRUCTION;
118    }
119}
120libc_hidden_def(abort)
121

Archive Download this file