update from main archive 970124
[glibc.git] / sysdeps / generic / abort.c
blobc1969f4d1fee37313dc6735b056c076288f68a53
1 /* Copyright (C) 1991, 1993, 1995, 1996, 1997 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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <libc-lock.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 /* Try to get a machine dependent instruction which will make the
27 program crash. This is used in case everything else fails. */
28 #include <abort-instr.h>
29 #ifndef ABORT_INSTRUCTION
30 /* No such instruction is available. */
31 # define ABORT_INSTRUCTION
32 #endif
34 /* We must avoid to run in circles. Therefore we remember how far we
35 already got. */
36 static int stage;
38 /* We should be prepared for multiple threads trying to run abort. */
39 __libc_lock_define_initialized_recursive (static, lock);
42 /* Cause an abnormal program termination with core-dump. */
43 void
44 abort (void)
46 struct sigaction act;
47 sigset_t sigs;
49 /* First acquire the lock. */
50 __libc_lock_lock_recursive (lock);
52 /* Now it's for sure we are alone. But recursive calls are possible. */
54 /* Unlock SIGABRT. */
55 if (stage == 0)
57 ++stage;
58 if (__sigemptyset (&sigs) == 0 &&
59 __sigaddset (&sigs, SIGABRT) == 0)
60 __sigprocmask (SIG_UNBLOCK, &sigs, (sigset_t *) NULL);
63 /* Flush all streams. We cannot close them now because the user
64 might have registered a handler for SIGABRT. */
65 if (stage == 1)
67 ++stage;
68 fflush (NULL);
71 /* Send signal which possibly calls a user handler. */
72 if (stage == 2)
74 /* This stage is special: we must allow repeated calls of
75 `abort' when a user defined handler for SIGABRT is installed.
76 This is risky since the `raise' implementation might also
77 fail but I don't see another possibility. */
78 int save_stage = stage;
80 stage = 0;
81 __libc_lock_unlock_recursive (lock);
83 raise (SIGABRT);
85 __libc_lock_lock_recursive (lock);
86 stage = save_stage + 1;
89 /* There was a handler installed. Now remove it. */
90 if (stage == 3)
92 ++stage;
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);
100 /* Now close the streams which also flushes the output the user
101 defined handler might has produced. */
102 if (stage == 4)
104 ++stage;
105 __fcloseall ();
108 /* Try again. */
109 if (stage == 5)
111 ++stage;
112 raise (SIGABRT);
115 /* Now try to abort using the system specific command. */
116 if (stage == 6)
118 ++stage;
119 ABORT_INSTRUCTION;
122 /* If we can't signal ourselves and the abort instruction failed, exit. */
123 if (stage == 7)
125 ++stage;
126 _exit (127);
129 /* If even this fails try to use the provided instruction to crash
130 or otherwise make sure we never return. */
131 while (1)
132 /* Try for ever and ever. */
133 ABORT_INSTRUCTION;