Sort sysdeps/powerpc/fpu/libm-test-ulps
[glibc.git] / sysdeps / posix / libc_fatal.c
blob62acb9bead9eed1c3a732635575ce92c105d13e2
1 /* Copyright (C) 1993-1995,1997,2000,2004,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/syslog.h>
32 #include <sys/uio.h>
33 #include <not-cancel.h>
35 #ifdef FATAL_PREPARE_INCLUDE
36 #include FATAL_PREPARE_INCLUDE
37 #endif
39 struct str_list
41 const char *str;
42 size_t len;
43 struct str_list *next;
47 /* Abort with an error message. */
48 void
49 __libc_message (int do_abort, const char *fmt, ...)
51 va_list ap;
52 va_list ap_copy;
53 int fd = -1;
55 va_start (ap, fmt);
56 va_copy (ap_copy, ap);
58 #ifdef FATAL_PREPARE
59 FATAL_PREPARE;
60 #endif
62 /* Open a descriptor for /dev/tty unless the user explicitly
63 requests errors on standard error. */
64 const char *on_2 = __secure_getenv ("LIBC_FATAL_STDERR_");
65 if (on_2 == NULL || *on_2 == '\0')
66 fd = open_not_cancel_2 (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY);
68 if (fd == -1)
69 fd = STDERR_FILENO;
71 struct str_list *list = NULL;
72 int nlist = 0;
74 const char *cp = fmt;
75 while (*cp != '\0')
77 /* Find the next "%s" or the end of the string. */
78 const char *next = cp;
79 while (next[0] != '%' || next[1] != 's')
81 next = __strchrnul (next + 1, '%');
83 if (next[0] == '\0')
84 break;
87 /* Determine what to print. */
88 const char *str;
89 size_t len;
90 if (cp[0] == '%' && cp[1] == 's')
92 str = va_arg (ap, const char *);
93 len = strlen (str);
94 cp += 2;
96 else
98 str = cp;
99 len = next - cp;
100 cp = next;
103 struct str_list *newp = alloca (sizeof (struct str_list));
104 newp->str = str;
105 newp->len = len;
106 newp->next = list;
107 list = newp;
108 ++nlist;
111 bool written = false;
112 if (nlist > 0)
114 struct iovec *iov = alloca (nlist * sizeof (struct iovec));
115 ssize_t total = 0;
117 for (int cnt = nlist - 1; cnt >= 0; --cnt)
119 iov[cnt].iov_base = (char *) list->str;
120 iov[cnt].iov_len = list->len;
121 total += list->len;
122 list = list->next;
125 if (TEMP_FAILURE_RETRY (__writev (fd, iov, nlist)) == total)
126 written = true;
128 if (do_abort)
130 total = ((total + 1 + GLRO(dl_pagesize) - 1)
131 & ~(GLRO(dl_pagesize) - 1));
132 struct abort_msg_s *buf = __mmap (NULL, total,
133 PROT_READ | PROT_WRITE,
134 MAP_ANON | MAP_PRIVATE, -1, 0);
135 if (buf != MAP_FAILED)
137 buf->size = total;
138 char *wp = buf->msg;
139 for (int cnt = 0; cnt < nlist; ++cnt)
140 wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len);
141 *wp = '\0';
143 /* We have to free the old buffer since the application might
144 catch the SIGABRT signal. */
145 struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg,
146 buf);
147 if (old != NULL)
148 __munmap (old, old->size);
153 va_end (ap);
155 /* If we had no success writing the message, use syslog. */
156 if (! written)
157 vsyslog (LOG_ERR, fmt, ap_copy);
159 va_end (ap_copy);
161 if (do_abort)
162 /* Kill the application. */
163 abort ();
167 void
168 __libc_fatal (message)
169 const char *message;
171 /* The loop is added only to keep gcc happy. */
172 while (1)
173 __libc_message (1, "%s", message);
175 libc_hidden_def (__libc_fatal)