OSDN Git Service

Fix locking
[uclinux-h8/uClibc.git] / libc / stdlib / abort.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA.  */
18
19 /* Hacked up for uClibc by Erik Andersen */
20
21 #define _GNU_SOURCE
22 #include <features.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <errno.h>
30
31
32 /* Our last ditch effort to commit suicide */ 
33 #if defined(__i386__)
34 #define ABORT_INSTRUCTION asm ("hlt")
35 #elif defined(__ia64__)
36 #define ABORT_INSTRUCTION asm ("break 0")
37 #elif defined(__mc68000__)
38 #define ABORT_INSTRUCTION asm (".long 0xffffffff")
39 #elif defined(__mips__)
40 #define ABORT_INSTRUCTION asm ("break 255")
41 #elif defined(__s390__)
42 #define ABORT_INSTRUCTION asm (".word 0")
43 #elif defined(__sparc__)
44 #define ABORT_INSTRUCTION asm ("unimp 0xf00")
45 #elif defined(__x86_64__)
46 #define ABORT_INSTRUCTION asm ("hlt")
47 #else
48 #define ABORT_INSTRUCTION
49 #endif
50
51 extern void weak_function _stdio_term(void);
52 extern void _exit __P((int __status)) __attribute__ ((__noreturn__));
53 static int been_there_done_that = 0;
54
55 /* Be prepared in case multiple threads try to abort().  */
56 #ifdef __UCLIBC_HAS_THREADS__
57 #include <pthread.h>
58 static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
59 # define LOCK   pthread_mutex_lock(&mylock)
60 # define UNLOCK pthread_mutex_unlock(&mylock);
61 #else
62 # define LOCK
63 # define UNLOCK
64 #endif
65
66
67 /* Cause an abnormal program termination with core-dump.  */
68 void abort(void)
69 {
70     sigset_t sigset;
71
72       /* Make sure we acquire the lock before proceeding.  */
73       LOCK;
74
75     /* Unmask SIGABRT to be sure we can get it */
76     if (__sigemptyset(&sigset) == 0 && __sigaddset(&sigset, SIGABRT) == 0) {
77         sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *) NULL);
78     }
79
80     /* If we are using stdio, try to shut it down.  At the very least,
81          * this will attempt to commit all buffered writes.  It may also
82          * unbuffer all writable files, or close them outright.
83          * Check the stdio routines for details. */
84     if (_stdio_term)
85                 _stdio_term();
86
87     while (1) {
88         /* Try to suicide with a SIGABRT.  */
89         if (been_there_done_that == 0) {
90             been_there_done_that++;
91             UNLOCK;
92             raise(SIGABRT);
93             LOCK;
94         }
95
96         /* Still here?  Try to remove any signal handlers.  */
97         if (been_there_done_that == 1) {
98             struct sigaction act;
99
100             been_there_done_that++;
101             memset (&act, '\0', sizeof (struct sigaction));
102             act.sa_handler = SIG_DFL;
103             __sigfillset (&act.sa_mask);
104             act.sa_flags = 0;
105             sigaction (SIGABRT, &act, NULL);
106         }
107
108         /* Still here?  Try to suicide with an illegal instruction */
109         if (been_there_done_that == 2) {
110             been_there_done_that++;
111             ABORT_INSTRUCTION;
112         }
113
114         /* Still here?  Try to at least exit */
115         if (been_there_done_that == 3) {
116             been_there_done_that++;
117             _exit (127);
118         }
119
120         /* Still here?  We're screwed.  Sleepy time.  Good night */
121         while (1)
122             /* Try for ever and ever.  */
123             ABORT_INSTRUCTION;
124     }
125 }
126