Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / posix / libc_fatal.c
blob74e40ee4d20674c36cecf95ae051e0da38a3f433
1 /* Catastrophic failure reports. Generic POSIX.1 version.
2 Copyright (C) 1993-2014 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/uio.h>
33 #include <not-cancel.h>
35 #ifdef FATAL_PREPARE_INCLUDE
36 #include FATAL_PREPARE_INCLUDE
37 #endif
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)
44 return TEMP_FAILURE_RETRY (__writev (fd, iov, niov)) == total;
46 #endif
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)))
56 #endif
58 struct str_list
60 const char *str;
61 size_t len;
62 struct str_list *next;
65 /* Abort with an error message. */
66 void
67 __libc_message (int do_abort, const char *fmt, ...)
69 va_list ap;
70 int fd = -1;
72 va_start (ap, fmt);
74 #ifdef FATAL_PREPARE
75 FATAL_PREPARE;
76 #endif
78 /* Open a descriptor for /dev/tty unless the user explicitly
79 requests errors on standard error. */
80 const char *on_2 = __libc_secure_getenv ("LIBC_FATAL_STDERR_");
81 if (on_2 == NULL || *on_2 == '\0')
82 fd = open_not_cancel_2 (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY);
84 if (fd == -1)
85 fd = STDERR_FILENO;
87 struct str_list *list = NULL;
88 int nlist = 0;
90 const char *cp = fmt;
91 while (*cp != '\0')
93 /* Find the next "%s" or the end of the string. */
94 const char *next = cp;
95 while (next[0] != '%' || next[1] != 's')
97 next = __strchrnul (next + 1, '%');
99 if (next[0] == '\0')
100 break;
103 /* Determine what to print. */
104 const char *str;
105 size_t len;
106 if (cp[0] == '%' && cp[1] == 's')
108 str = va_arg (ap, const char *);
109 len = strlen (str);
110 cp += 2;
112 else
114 str = cp;
115 len = next - cp;
116 cp = next;
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;
127 bool written = false;
128 if (nlist > 0)
130 struct iovec *iov = alloca (nlist * sizeof (struct iovec));
131 ssize_t total = 0;
133 for (int cnt = nlist - 1; cnt >= 0; --cnt)
135 iov[cnt].iov_base = (char *) list->str;
136 iov[cnt].iov_len = list->len;
137 total += list->len;
138 list = list->next;
141 written = WRITEV_FOR_FATAL (fd, iov, nlist, total);
143 if (do_abort)
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);
150 if (__glibc_likely (buf != MAP_FAILED))
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';
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);
168 va_end (ap);
170 if (do_abort)
172 BEFORE_ABORT (do_abort, written, fd);
174 /* Kill the application. */
175 abort ();
180 void
181 __libc_fatal (message)
182 const char *message;
184 /* The loop is added only to keep gcc happy. */
185 while (1)
186 __libc_message (1, "%s", message);
188 libc_hidden_def (__libc_fatal)