Rename __secure_getenv to secure_getenv
[glibc.git] / sysdeps / unix / sysv / linux / libc_fatal.c
blob6cc96d76e98fe1917b84ef735584583c53221db7
1 /* Copyright (C) 1993-2012 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/>. */
18 #include <atomic.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <ldsodefs.h>
22 #include <paths.h>
23 #include <stdarg.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sysdep.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <sys/syslog.h>
32 #include <execinfo.h>
34 /* Abort with an error message. */
35 #include <not-cancel.h>
37 #ifdef FATAL_PREPARE_INCLUDE
38 #include FATAL_PREPARE_INCLUDE
39 #endif
41 struct str_list
43 const char *str;
44 size_t len;
45 struct str_list *next;
49 /* Abort with an error message. */
50 void
51 __libc_message (int do_abort, const char *fmt, ...)
53 va_list ap;
54 va_list ap_copy;
55 int fd = -1;
57 va_start (ap, fmt);
58 va_copy (ap_copy, ap);
60 #ifdef FATAL_PREPARE
61 FATAL_PREPARE;
62 #endif
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);
70 if (fd == -1)
71 fd = STDERR_FILENO;
73 struct str_list *list = NULL;
74 int nlist = 0;
76 const char *cp = fmt;
77 while (*cp != '\0')
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, '%');
85 if (next[0] == '\0')
86 break;
89 /* Determine what to print. */
90 const char *str;
91 size_t len;
92 if (cp[0] == '%' && cp[1] == 's')
94 str = va_arg (ap, const char *);
95 len = strlen (str);
96 cp += 2;
98 else
100 str = cp;
101 len = next - cp;
102 cp = next;
105 struct str_list *newp = alloca (sizeof (struct str_list));
106 newp->str = str;
107 newp->len = len;
108 newp->next = list;
109 list = newp;
110 ++nlist;
113 bool written = false;
114 if (nlist > 0)
116 struct iovec *iov = alloca (nlist * sizeof (struct iovec));
117 ssize_t total = 0;
119 for (int cnt = nlist - 1; cnt >= 0; --cnt)
121 iov[cnt].iov_base = (void *) list->str;
122 iov[cnt].iov_len = list->len;
123 total += list->len;
124 list = list->next;
127 INTERNAL_SYSCALL_DECL (err);
128 ssize_t cnt;
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);
134 if (cnt == total)
135 written = true;
137 if (do_abort)
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))
146 buf->size = total;
147 char *wp = buf->msg;
148 for (int cnt = 0; cnt < nlist; ++cnt)
149 wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len);
150 *wp = '\0';
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,
155 buf);
156 if (old != NULL)
157 __munmap (old, old->size);
162 va_end (ap);
164 /* If we had no success writing the message, use syslog. */
165 if (! written)
166 vsyslog (LOG_ERR, fmt, ap_copy);
168 va_end (ap_copy);
170 if (do_abort)
172 if (do_abort > 1 && written)
174 void *addrs[64];
175 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
176 int n = __backtrace (addrs, naddrs);
177 if (n > 2)
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);
186 char buf[1024];
187 ssize_t n2;
188 while ((n2 = read_not_cancel (fd2, buf, sizeof (buf))) > 0)
189 if (write_not_cancel (fd, buf, n2) != n2)
190 break;
191 close_not_cancel_no_status (fd2);
195 /* Terminate the process. */
196 abort ();
201 void
202 __libc_fatal (message)
203 const char *message;
205 /* The loop is added only to keep gcc happy. */
206 while (1)
207 __libc_message (1, "%s", message);
209 libc_hidden_def (__libc_fatal)