1 | /* Copyright (C) 1991-2017 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 Lesser General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2.1 of the 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 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ |
17 | |
18 | #include <libc-lock.h> |
19 | #include <signal.h> |
20 | #include <stdio.h> |
21 | #include <stdlib.h> |
22 | #include <string.h> |
23 | #include <unistd.h> |
24 | #include <sigsetops.h> |
25 | |
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 |
33 | |
34 | #include <libio/libioP.h> |
35 | #define fflush(s) _IO_flush_all_lockp (0) |
36 | |
37 | /* Exported variable to locate abort message in core files etc. */ |
38 | struct abort_msg_s *__abort_msg __attribute__ ((nocommon)); |
39 | libc_hidden_def (__abort_msg) |
40 | |
41 | /* We must avoid to run in circles. Therefore we remember how far we |
42 | already got. */ |
43 | static int stage; |
44 | |
45 | /* We should be prepared for multiple threads trying to run abort. */ |
46 | __libc_lock_define_initialized_recursive (static, lock); |
47 | |
48 | |
49 | /* Cause an abnormal program termination with core-dump. */ |
50 | void |
51 | abort (void) |
52 | { |
53 | struct sigaction act; |
54 | sigset_t sigs; |
55 | |
56 | /* First acquire the lock. */ |
57 | __libc_lock_lock_recursive (lock); |
58 | |
59 | /* Now it's for sure we are alone. But recursive calls are possible. */ |
60 | |
61 | /* Unblock SIGABRT. */ |
62 | if (stage == 0) |
63 | { |
64 | ++stage; |
65 | __sigemptyset (&sigs); |
66 | __sigaddset (&sigs, SIGABRT); |
67 | __sigprocmask (SIG_UNBLOCK, &sigs, 0); |
68 | } |
69 | |
70 | /* Flush all streams. We cannot close them now because the user |
71 | might have registered a handler for SIGABRT. */ |
72 | if (stage == 1) |
73 | { |
74 | ++stage; |
75 | fflush (NULL); |
76 | } |
77 | |
78 | /* Send signal which possibly calls a user handler. */ |
79 | if (stage == 2) |
80 | { |
81 | /* This stage is special: we must allow repeated calls of |
82 | `abort' when a user defined handler for SIGABRT is installed. |
83 | This is risky since the `raise' implementation might also |
84 | fail but I don't see another possibility. */ |
85 | int save_stage = stage; |
86 | |
87 | stage = 0; |
88 | __libc_lock_unlock_recursive (lock); |
89 | |
90 | raise (SIGABRT); |
91 | |
92 | __libc_lock_lock_recursive (lock); |
93 | stage = save_stage + 1; |
94 | } |
95 | |
96 | /* There was a handler installed. Now remove it. */ |
97 | if (stage == 3) |
98 | { |
99 | ++stage; |
100 | memset (&act, '\0', sizeof (struct sigaction)); |
101 | act.sa_handler = SIG_DFL; |
102 | __sigfillset (&act.sa_mask); |
103 | act.sa_flags = 0; |
104 | __sigaction (SIGABRT, &act, NULL); |
105 | } |
106 | |
107 | /* Now close the streams which also flushes the output the user |
108 | defined handler might has produced. */ |
109 | if (stage == 4) |
110 | { |
111 | ++stage; |
112 | __fcloseall (); |
113 | } |
114 | |
115 | /* Try again. */ |
116 | if (stage == 5) |
117 | { |
118 | ++stage; |
119 | raise (SIGABRT); |
120 | } |
121 | |
122 | /* Now try to abort using the system specific command. */ |
123 | if (stage == 6) |
124 | { |
125 | ++stage; |
126 | ABORT_INSTRUCTION; |
127 | } |
128 | |
129 | /* If we can't signal ourselves and the abort instruction failed, exit. */ |
130 | if (stage == 7) |
131 | { |
132 | ++stage; |
133 | _exit (127); |
134 | } |
135 | |
136 | /* If even this fails try to use the provided instruction to crash |
137 | or otherwise make sure we never return. */ |
138 | while (1) |
139 | /* Try for ever and ever. */ |
140 | ABORT_INSTRUCTION; |
141 | } |
142 | libc_hidden_def (abort) |
143 | |