1 | /* Catastrophic failure reports. Generic POSIX.1 version. |
2 | Copyright (C) 1993-2018 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <atomic.h> |
20 | #include <errno.h> |
21 | #include <fcntl.h> |
22 | #include <ldsodefs.h> |
23 | #include <paths.h> |
24 | #include <stdarg.h> |
25 | #include <stdbool.h> |
26 | #include <stdio.h> |
27 | #include <stdlib.h> |
28 | #include <string.h> |
29 | #include <sysdep.h> |
30 | #include <unistd.h> |
31 | #include <sys/mman.h> |
32 | #include <sys/uio.h> |
33 | #include <not-cancel.h> |
34 | |
35 | #ifdef FATAL_PREPARE_INCLUDE |
36 | #include FATAL_PREPARE_INCLUDE |
37 | #endif |
38 | |
39 | #ifndef WRITEV_FOR_FATAL |
40 | # define WRITEV_FOR_FATAL writev_for_fatal |
41 | static bool |
42 | writev_for_fatal (int fd, const struct iovec *iov, size_t niov, size_t total) |
43 | { |
44 | return TEMP_FAILURE_RETRY (__writev (fd, iov, niov)) == total; |
45 | } |
46 | #endif |
47 | |
48 | #ifndef BEFORE_ABORT |
49 | # define BEFORE_ABORT before_abort |
50 | static void |
51 | before_abort (int do_abort __attribute__ ((unused)), |
52 | bool written __attribute__ ((unused)), |
53 | int fd __attribute__ ((unused))) |
54 | { |
55 | } |
56 | #endif |
57 | |
58 | struct str_list |
59 | { |
60 | const char *str; |
61 | size_t len; |
62 | struct str_list *next; |
63 | }; |
64 | |
65 | /* Abort with an error message. */ |
66 | void |
67 | __libc_message (enum __libc_message_action action, const char *fmt, ...) |
68 | { |
69 | va_list ap; |
70 | int fd = -1; |
71 | |
72 | va_start (ap, fmt); |
73 | |
74 | #ifdef FATAL_PREPARE |
75 | FATAL_PREPARE; |
76 | #endif |
77 | |
78 | /* Don't call __libc_secure_getenv if we aren't doing backtrace, which |
79 | may access the corrupted stack. */ |
80 | if ((action & do_backtrace)) |
81 | { |
82 | /* Open a descriptor for /dev/tty unless the user explicitly |
83 | requests errors on standard error. */ |
84 | const char *on_2 = __libc_secure_getenv ("LIBC_FATAL_STDERR_" ); |
85 | if (on_2 == NULL || *on_2 == '\0') |
86 | fd = __open_nocancel (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY); |
87 | } |
88 | |
89 | if (fd == -1) |
90 | fd = STDERR_FILENO; |
91 | |
92 | struct str_list *list = NULL; |
93 | int nlist = 0; |
94 | |
95 | const char *cp = fmt; |
96 | while (*cp != '\0') |
97 | { |
98 | /* Find the next "%s" or the end of the string. */ |
99 | const char *next = cp; |
100 | while (next[0] != '%' || next[1] != 's') |
101 | { |
102 | next = __strchrnul (next + 1, '%'); |
103 | |
104 | if (next[0] == '\0') |
105 | break; |
106 | } |
107 | |
108 | /* Determine what to print. */ |
109 | const char *str; |
110 | size_t len; |
111 | if (cp[0] == '%' && cp[1] == 's') |
112 | { |
113 | str = va_arg (ap, const char *); |
114 | len = strlen (str); |
115 | cp += 2; |
116 | } |
117 | else |
118 | { |
119 | str = cp; |
120 | len = next - cp; |
121 | cp = next; |
122 | } |
123 | |
124 | struct str_list *newp = alloca (sizeof (struct str_list)); |
125 | newp->str = str; |
126 | newp->len = len; |
127 | newp->next = list; |
128 | list = newp; |
129 | ++nlist; |
130 | } |
131 | |
132 | bool written = false; |
133 | if (nlist > 0) |
134 | { |
135 | struct iovec *iov = alloca (nlist * sizeof (struct iovec)); |
136 | ssize_t total = 0; |
137 | |
138 | for (int cnt = nlist - 1; cnt >= 0; --cnt) |
139 | { |
140 | iov[cnt].iov_base = (char *) list->str; |
141 | iov[cnt].iov_len = list->len; |
142 | total += list->len; |
143 | list = list->next; |
144 | } |
145 | |
146 | written = WRITEV_FOR_FATAL (fd, iov, nlist, total); |
147 | |
148 | if ((action & do_abort)) |
149 | { |
150 | total = ((total + 1 + GLRO(dl_pagesize) - 1) |
151 | & ~(GLRO(dl_pagesize) - 1)); |
152 | struct abort_msg_s *buf = __mmap (NULL, total, |
153 | PROT_READ | PROT_WRITE, |
154 | MAP_ANON | MAP_PRIVATE, -1, 0); |
155 | if (__glibc_likely (buf != MAP_FAILED)) |
156 | { |
157 | buf->size = total; |
158 | char *wp = buf->msg; |
159 | for (int cnt = 0; cnt < nlist; ++cnt) |
160 | wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len); |
161 | *wp = '\0'; |
162 | |
163 | /* We have to free the old buffer since the application might |
164 | catch the SIGABRT signal. */ |
165 | struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg, |
166 | buf); |
167 | if (old != NULL) |
168 | __munmap (old, old->size); |
169 | } |
170 | } |
171 | } |
172 | |
173 | va_end (ap); |
174 | |
175 | if ((action & do_abort)) |
176 | { |
177 | if ((action & do_backtrace)) |
178 | BEFORE_ABORT (do_abort, written, fd); |
179 | |
180 | /* Kill the application. */ |
181 | abort (); |
182 | } |
183 | } |
184 | |
185 | |
186 | void |
187 | __libc_fatal (const char *message) |
188 | { |
189 | /* The loop is added only to keep gcc happy. */ |
190 | while (1) |
191 | __libc_message (do_abort | do_backtrace, "%s" , message); |
192 | } |
193 | libc_hidden_def (__libc_fatal) |
194 | |