Update copyright notices with scripts/update-copyrights.
[glibc.git] / sysdeps / posix / libc_fatal.c
blob8d416006b52f97dbc4aee5888fd2c407cce297b3
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/>. */
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/syslog.h>
31 #include <sys/uio.h>
32 #include <not-cancel.h>
34 #ifdef FATAL_PREPARE_INCLUDE
35 #include FATAL_PREPARE_INCLUDE
36 #endif
38 struct str_list
40 const char *str;
41 size_t len;
42 struct str_list *next;
46 /* Abort with an error message. */
47 void
48 __libc_message (int do_abort, const char *fmt, ...)
50 va_list ap;
51 va_list ap_copy;
52 int fd = -1;
54 va_start (ap, fmt);
55 va_copy (ap_copy, ap);
57 #ifdef FATAL_PREPARE
58 FATAL_PREPARE;
59 #endif
61 /* Open a descriptor for /dev/tty unless the user explicitly
62 requests errors on standard error. */
63 const char *on_2 = __libc_secure_getenv ("LIBC_FATAL_STDERR_");
64 if (on_2 == NULL || *on_2 == '\0')
65 fd = open_not_cancel_2 (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY);
67 if (fd == -1)
68 fd = STDERR_FILENO;
70 struct str_list *list = NULL;
71 int nlist = 0;
73 const char *cp = fmt;
74 while (*cp != '\0')
76 /* Find the next "%s" or the end of the string. */
77 const char *next = cp;
78 while (next[0] != '%' || next[1] != 's')
80 next = __strchrnul (next + 1, '%');
82 if (next[0] == '\0')
83 break;
86 /* Determine what to print. */
87 const char *str;
88 size_t len;
89 if (cp[0] == '%' && cp[1] == 's')
91 str = va_arg (ap, const char *);
92 len = strlen (str);
93 cp += 2;
95 else
97 str = cp;
98 len = next - cp;
99 cp = next;
102 struct str_list *newp = alloca (sizeof (struct str_list));
103 newp->str = str;
104 newp->len = len;
105 newp->next = list;
106 list = newp;
107 ++nlist;
110 bool written = false;
111 if (nlist > 0)
113 struct iovec *iov = alloca (nlist * sizeof (struct iovec));
114 ssize_t total = 0;
116 for (int cnt = nlist - 1; cnt >= 0; --cnt)
118 iov[cnt].iov_base = (char *) list->str;
119 iov[cnt].iov_len = list->len;
120 total += list->len;
121 list = list->next;
124 if (TEMP_FAILURE_RETRY (__writev (fd, iov, nlist)) == total)
125 written = true;
127 if (do_abort)
129 total = ((total + 1 + GLRO(dl_pagesize) - 1)
130 & ~(GLRO(dl_pagesize) - 1));
131 struct abort_msg_s *buf = __mmap (NULL, total,
132 PROT_READ | PROT_WRITE,
133 MAP_ANON | MAP_PRIVATE, -1, 0);
134 if (buf != MAP_FAILED)
136 buf->size = total;
137 char *wp = buf->msg;
138 for (int cnt = 0; cnt < nlist; ++cnt)
139 wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len);
140 *wp = '\0';
142 /* We have to free the old buffer since the application might
143 catch the SIGABRT signal. */
144 struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg,
145 buf);
146 if (old != NULL)
147 __munmap (old, old->size);
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)
161 /* Kill the application. */
162 abort ();
166 void
167 __libc_fatal (message)
168 const char *message;
170 /* The loop is added only to keep gcc happy. */
171 while (1)
172 __libc_message (1, "%s", message);
174 libc_hidden_def (__libc_fatal)