1 | /* Catastrophic failure reports. Generic POSIX.1 version. |
2 | Copyright (C) 1993-2016 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 (int do_abort, 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 | /* Open a descriptor for /dev/tty unless the user explicitly |
79 | requests errors on standard error. */ |
80 | const char *on_2 = __libc_secure_getenv ("LIBC_FATAL_STDERR_" ); |
81 | if (on_2 == NULL || *on_2 == '\0') |
82 | fd = open_not_cancel_2 (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY); |
83 | |
84 | if (fd == -1) |
85 | fd = STDERR_FILENO; |
86 | |
87 | struct str_list *list = NULL; |
88 | int nlist = 0; |
89 | |
90 | const char *cp = fmt; |
91 | while (*cp != '\0') |
92 | { |
93 | /* Find the next "%s" or the end of the string. */ |
94 | const char *next = cp; |
95 | while (next[0] != '%' || next[1] != 's') |
96 | { |
97 | next = __strchrnul (next + 1, '%'); |
98 | |
99 | if (next[0] == '\0') |
100 | break; |
101 | } |
102 | |
103 | /* Determine what to print. */ |
104 | const char *str; |
105 | size_t len; |
106 | if (cp[0] == '%' && cp[1] == 's') |
107 | { |
108 | str = va_arg (ap, const char *); |
109 | len = strlen (str); |
110 | cp += 2; |
111 | } |
112 | else |
113 | { |
114 | str = cp; |
115 | len = next - cp; |
116 | cp = next; |
117 | } |
118 | |
119 | struct str_list *newp = alloca (sizeof (struct str_list)); |
120 | newp->str = str; |
121 | newp->len = len; |
122 | newp->next = list; |
123 | list = newp; |
124 | ++nlist; |
125 | } |
126 | |
127 | bool written = false; |
128 | if (nlist > 0) |
129 | { |
130 | struct iovec *iov = alloca (nlist * sizeof (struct iovec)); |
131 | ssize_t total = 0; |
132 | |
133 | for (int cnt = nlist - 1; cnt >= 0; --cnt) |
134 | { |
135 | iov[cnt].iov_base = (char *) list->str; |
136 | iov[cnt].iov_len = list->len; |
137 | total += list->len; |
138 | list = list->next; |
139 | } |
140 | |
141 | written = WRITEV_FOR_FATAL (fd, iov, nlist, total); |
142 | |
143 | if (do_abort) |
144 | { |
145 | total = ((total + 1 + GLRO(dl_pagesize) - 1) |
146 | & ~(GLRO(dl_pagesize) - 1)); |
147 | struct abort_msg_s *buf = __mmap (NULL, total, |
148 | PROT_READ | PROT_WRITE, |
149 | MAP_ANON | MAP_PRIVATE, -1, 0); |
150 | if (__glibc_likely (buf != MAP_FAILED)) |
151 | { |
152 | buf->size = total; |
153 | char *wp = buf->msg; |
154 | for (int cnt = 0; cnt < nlist; ++cnt) |
155 | wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len); |
156 | *wp = '\0'; |
157 | |
158 | /* We have to free the old buffer since the application might |
159 | catch the SIGABRT signal. */ |
160 | struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg, |
161 | buf); |
162 | if (old != NULL) |
163 | __munmap (old, old->size); |
164 | } |
165 | } |
166 | } |
167 | |
168 | va_end (ap); |
169 | |
170 | if (do_abort) |
171 | { |
172 | BEFORE_ABORT (do_abort, written, fd); |
173 | |
174 | /* Kill the application. */ |
175 | abort (); |
176 | } |
177 | } |
178 | |
179 | |
180 | void |
181 | __libc_fatal (const char *message) |
182 | { |
183 | /* The loop is added only to keep gcc happy. */ |
184 | while (1) |
185 | __libc_message (1, "%s" , message); |
186 | } |
187 | libc_hidden_def (__libc_fatal) |
188 | |