Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / libc_fatal.c
blob58a5a7f83870c73978c774b218c789a42fcc128c
1 /* Copyright (C) 1993-1995,1997,2000,2002-2005,2009,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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/syslog.h>
33 #include <execinfo.h>
35 /* Abort with an error message. */
36 #include <not-cancel.h>
38 #ifdef FATAL_PREPARE_INCLUDE
39 #include FATAL_PREPARE_INCLUDE
40 #endif
42 struct str_list
44 const char *str;
45 size_t len;
46 struct str_list *next;
50 /* Abort with an error message. */
51 void
52 __libc_message (int do_abort, const char *fmt, ...)
54 va_list ap;
55 va_list ap_copy;
56 int fd = -1;
58 va_start (ap, fmt);
59 va_copy (ap_copy, ap);
61 #ifdef FATAL_PREPARE
62 FATAL_PREPARE;
63 #endif
65 /* Open a descriptor for /dev/tty unless the user explicitly
66 requests errors on standard error. */
67 const char *on_2 = __secure_getenv ("LIBC_FATAL_STDERR_");
68 if (on_2 == NULL || *on_2 == '\0')
69 fd = open_not_cancel_2 (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY);
71 if (fd == -1)
72 fd = STDERR_FILENO;
74 struct str_list *list = NULL;
75 int nlist = 0;
77 const char *cp = fmt;
78 while (*cp != '\0')
80 /* Find the next "%s" or the end of the string. */
81 const char *next = cp;
82 while (next[0] != '%' || next[1] != 's')
84 next = __strchrnul (next + 1, '%');
86 if (next[0] == '\0')
87 break;
90 /* Determine what to print. */
91 const char *str;
92 size_t len;
93 if (cp[0] == '%' && cp[1] == 's')
95 str = va_arg (ap, const char *);
96 len = strlen (str);
97 cp += 2;
99 else
101 str = cp;
102 len = next - cp;
103 cp = next;
106 struct str_list *newp = alloca (sizeof (struct str_list));
107 newp->str = str;
108 newp->len = len;
109 newp->next = list;
110 list = newp;
111 ++nlist;
114 bool written = false;
115 if (nlist > 0)
117 struct iovec *iov = alloca (nlist * sizeof (struct iovec));
118 ssize_t total = 0;
120 for (int cnt = nlist - 1; cnt >= 0; --cnt)
122 iov[cnt].iov_base = (void *) list->str;
123 iov[cnt].iov_len = list->len;
124 total += list->len;
125 list = list->next;
128 INTERNAL_SYSCALL_DECL (err);
129 ssize_t cnt;
131 cnt = INTERNAL_SYSCALL (writev, err, 3, fd, iov, nlist);
132 while (INTERNAL_SYSCALL_ERROR_P (cnt, err)
133 && INTERNAL_SYSCALL_ERRNO (cnt, err) == EINTR);
135 if (cnt == total)
136 written = true;
138 if (do_abort)
140 total = ((total + 1 + GLRO(dl_pagesize) - 1)
141 & ~(GLRO(dl_pagesize) - 1));
142 struct abort_msg_s *buf = __mmap (NULL, total,
143 PROT_READ | PROT_WRITE,
144 MAP_ANON | MAP_PRIVATE, -1, 0);
145 if (__builtin_expect (buf != MAP_FAILED, 1))
147 buf->size = total;
148 char *wp = buf->msg;
149 for (int cnt = 0; cnt < nlist; ++cnt)
150 wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len);
151 *wp = '\0';
153 /* We have to free the old buffer since the application might
154 catch the SIGABRT signal. */
155 struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg,
156 buf);
157 if (old != NULL)
158 __munmap (old, old->size);
163 va_end (ap);
165 /* If we had no success writing the message, use syslog. */
166 if (! written)
167 vsyslog (LOG_ERR, fmt, ap_copy);
169 va_end (ap_copy);
171 if (do_abort)
173 if (do_abort > 1 && written)
175 void *addrs[64];
176 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
177 int n = __backtrace (addrs, naddrs);
178 if (n > 2)
180 #define strnsize(str) str, strlen (str)
181 #define writestr(str) write_not_cancel (fd, str)
182 writestr (strnsize ("======= Backtrace: =========\n"));
183 __backtrace_symbols_fd (addrs + 1, n - 1, fd);
185 writestr (strnsize ("======= Memory map: ========\n"));
186 int fd2 = open_not_cancel_2 ("/proc/self/maps", O_RDONLY);
187 char buf[1024];
188 ssize_t n2;
189 while ((n2 = read_not_cancel (fd2, buf, sizeof (buf))) > 0)
190 if (write_not_cancel (fd, buf, n2) != n2)
191 break;
192 close_not_cancel_no_status (fd2);
196 /* Terminate the process. */
197 abort ();
202 void
203 __libc_fatal (message)
204 const char *message;
206 /* The loop is added only to keep gcc happy. */
207 while (1)
208 __libc_message (1, "%s", message);
210 libc_hidden_def (__libc_fatal)