1 /* Copyright (C) 1993-2013 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 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.
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.
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/>. */
31 #include <sys/syslog.h>
34 /* Abort with an error message. */
35 #include <not-cancel.h>
37 #ifdef FATAL_PREPARE_INCLUDE
38 #include FATAL_PREPARE_INCLUDE
45 struct str_list *next;
49 /* Abort with an error message. */
51 __libc_message (int do_abort, const char *fmt, ...)
58 va_copy (ap_copy, ap);
64 /* Open a descriptor for /dev/tty unless the user explicitly
65 requests errors on standard error. */
66 const char *on_2 = __libc_secure_getenv ("LIBC_FATAL_STDERR_");
67 if (on_2 == NULL || *on_2 == '\0')
68 fd = open_not_cancel_2 (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY);
73 struct str_list *list = NULL;
79 /* Find the next "%s" or the end of the string. */
80 const char *next = cp;
81 while (next[0] != '%' || next[1] != 's')
83 next = __strchrnul (next + 1, '%');
89 /* Determine what to print. */
92 if (cp[0] == '%' && cp[1] == 's')
94 str = va_arg (ap, const char *);
105 struct str_list *newp = alloca (sizeof (struct str_list));
113 bool written = false;
116 struct iovec *iov = alloca (nlist * sizeof (struct iovec));
119 for (int cnt = nlist - 1; cnt >= 0; --cnt)
121 iov[cnt].iov_base = (void *) list->str;
122 iov[cnt].iov_len = list->len;
127 INTERNAL_SYSCALL_DECL (err);
130 cnt = INTERNAL_SYSCALL (writev, err, 3, fd, iov, nlist);
131 while (INTERNAL_SYSCALL_ERROR_P (cnt, err)
132 && INTERNAL_SYSCALL_ERRNO (cnt, err) == EINTR);
139 total = ((total + 1 + GLRO(dl_pagesize) - 1)
140 & ~(GLRO(dl_pagesize) - 1));
141 struct abort_msg_s *buf = __mmap (NULL, total,
142 PROT_READ | PROT_WRITE,
143 MAP_ANON | MAP_PRIVATE, -1, 0);
144 if (__builtin_expect (buf != MAP_FAILED, 1))
148 for (int cnt = 0; cnt < nlist; ++cnt)
149 wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len);
152 /* We have to free the old buffer since the application might
153 catch the SIGABRT signal. */
154 struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg,
157 __munmap (old, old->size);
164 /* If we had no success writing the message, use syslog. */
166 vsyslog (LOG_ERR, fmt, ap_copy);
172 if (do_abort > 1 && written)
175 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
176 int n = __backtrace (addrs, naddrs);
179 #define strnsize(str) str, strlen (str)
180 #define writestr(str) write_not_cancel (fd, str)
181 writestr (strnsize ("======= Backtrace: =========\n"));
182 __backtrace_symbols_fd (addrs + 1, n - 1, fd);
184 writestr (strnsize ("======= Memory map: ========\n"));
185 int fd2 = open_not_cancel_2 ("/proc/self/maps", O_RDONLY);
188 while ((n2 = read_not_cancel (fd2, buf, sizeof (buf))) > 0)
189 if (write_not_cancel (fd, buf, n2) != n2)
191 close_not_cancel_no_status (fd2);
195 /* Terminate the process. */
202 __libc_fatal (message)
205 /* The loop is added only to keep gcc happy. */
207 __libc_message (1, "%s", message);
209 libc_hidden_def (__libc_fatal)