Maintain stack alignment in ____longjmp_chk on x86_64
[glibc.git] / sysdeps / unix / sysv / linux / libc_fatal.c
blob7287f4ef6c88e34c6c135e8e6f6fa5778f324c8c
1 /* Copyright (C) 1993-1995,1997,2000,2002-2005,2009
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <atomic.h>
21 #include <errno.h>
22 #include <fcntl.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/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 = __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 char *buf = do_abort ? malloc (total + 1) : NULL;
138 if (buf != NULL)
140 char *wp = buf;
141 for (int cnt = 0; cnt < nlist; ++cnt)
142 wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len);
143 *wp = '\0';
145 /* We have to free the old buffer since the application might
146 catch the SIGABRT signal. */
147 char *old = atomic_exchange_acq (&__abort_msg, buf);
148 free (old);
152 va_end (ap);
154 /* If we had no success writing the message, use syslog. */
155 if (! written)
156 vsyslog (LOG_ERR, fmt, ap_copy);
158 va_end (ap_copy);
160 if (do_abort)
162 if (do_abort > 1 && written)
164 void *addrs[64];
165 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
166 int n = __backtrace (addrs, naddrs);
167 if (n > 2)
169 #define strnsize(str) str, strlen (str)
170 #define writestr(str) write_not_cancel (fd, str)
171 writestr (strnsize ("======= Backtrace: =========\n"));
172 __backtrace_symbols_fd (addrs + 1, n - 1, fd);
174 writestr (strnsize ("======= Memory map: ========\n"));
175 int fd2 = open_not_cancel_2 ("/proc/self/maps", O_RDONLY);
176 char buf[1024];
177 ssize_t n2;
178 while ((n2 = read_not_cancel (fd2, buf, sizeof (buf))) > 0)
179 if (write_not_cancel (fd, buf, n2) != n2)
180 break;
181 close_not_cancel_no_status (fd2);
185 /* Terminate the process. */
186 abort ();
191 void
192 __libc_fatal (message)
193 const char *message;
195 /* The loop is added only to keep gcc happy. */
196 while (1)
197 __libc_message (1, "%s", message);
199 libc_hidden_def (__libc_fatal)