]>
Commit | Line | Data |
---|---|---|
a600e5ce | 1 | /* Catastrophic failure reports. Generic POSIX.1 version. |
b168057a | 2 | Copyright (C) 1993-2015 Free Software Foundation, Inc. |
ebbad4cc | 3 | This file is part of the GNU C Library. |
28f540f4 | 4 | |
ebbad4cc | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
28f540f4 | 9 | |
ebbad4cc UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
28f540f4 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 18 | |
48dcd0ba | 19 | #include <atomic.h> |
1327439f UD |
20 | #include <errno.h> |
21 | #include <fcntl.h> | |
f8a3b5bf | 22 | #include <ldsodefs.h> |
1327439f UD |
23 | #include <paths.h> |
24 | #include <stdarg.h> | |
25 | #include <stdbool.h> | |
28f540f4 | 26 | #include <stdio.h> |
ef52edfc | 27 | #include <stdlib.h> |
7f811679 | 28 | #include <string.h> |
1327439f UD |
29 | #include <sysdep.h> |
30 | #include <unistd.h> | |
a600e5ce | 31 | #include <sys/mman.h> |
579e2d6e | 32 | #include <sys/uio.h> |
1327439f | 33 | #include <not-cancel.h> |
28f540f4 RM |
34 | |
35 | #ifdef FATAL_PREPARE_INCLUDE | |
36 | #include FATAL_PREPARE_INCLUDE | |
37 | #endif | |
38 | ||
a600e5ce RM |
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 | ||
1327439f UD |
58 | struct str_list |
59 | { | |
60 | const char *str; | |
61 | size_t len; | |
62 | struct str_list *next; | |
63 | }; | |
64 | ||
28f540f4 RM |
65 | /* Abort with an error message. */ |
66 | void | |
1327439f | 67 | __libc_message (int do_abort, const char *fmt, ...) |
28f540f4 | 68 | { |
1327439f | 69 | va_list ap; |
1327439f UD |
70 | int fd = -1; |
71 | ||
72 | va_start (ap, fmt); | |
28f540f4 RM |
73 | |
74 | #ifdef FATAL_PREPARE | |
75 | FATAL_PREPARE; | |
76 | #endif | |
77 | ||
1327439f UD |
78 | /* Open a descriptor for /dev/tty unless the user explicitly |
79 | requests errors on standard error. */ | |
84b3fd84 | 80 | const char *on_2 = __libc_secure_getenv ("LIBC_FATAL_STDERR_"); |
1327439f UD |
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') | |
28f540f4 | 92 | { |
1327439f | 93 | /* Find the next "%s" or the end of the string. */ |
8ad684db | 94 | const char *next = cp; |
1327439f | 95 | while (next[0] != '%' || next[1] != 's') |
28f540f4 | 96 | { |
1327439f UD |
97 | next = __strchrnul (next + 1, '%'); |
98 | ||
99 | if (next[0] == '\0') | |
100 | break; | |
28f540f4 | 101 | } |
1327439f UD |
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 | { | |
8ad684db | 135 | iov[cnt].iov_base = (char *) list->str; |
1327439f UD |
136 | iov[cnt].iov_len = list->len; |
137 | total += list->len; | |
138 | list = list->next; | |
139 | } | |
140 | ||
a600e5ce | 141 | written = WRITEV_FOR_FATAL (fd, iov, nlist, total); |
48dcd0ba | 142 | |
f8a3b5bf | 143 | if (do_abort) |
48dcd0ba | 144 | { |
f8a3b5bf UD |
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); | |
a600e5ce | 150 | if (__glibc_likely (buf != MAP_FAILED)) |
f8a3b5bf UD |
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 | } | |
48dcd0ba | 165 | } |
28f540f4 RM |
166 | } |
167 | ||
1327439f UD |
168 | va_end (ap); |
169 | ||
f895670d | 170 | if (do_abort) |
a600e5ce RM |
171 | { |
172 | BEFORE_ABORT (do_abort, written, fd); | |
173 | ||
174 | /* Kill the application. */ | |
175 | abort (); | |
176 | } | |
1327439f UD |
177 | } |
178 | ||
179 | ||
180 | void | |
181 | __libc_fatal (message) | |
182 | const char *message; | |
183 | { | |
f895670d UD |
184 | /* The loop is added only to keep gcc happy. */ |
185 | while (1) | |
186 | __libc_message (1, "%s", message); | |
28f540f4 | 187 | } |
9d79e037 | 188 | libc_hidden_def (__libc_fatal) |