Add missing export of fallocate64 on 32-bit platforms.
[glibc.git] / sysdeps / unix / sysv / linux / libc_fatal.c
blobc7fac6ab514f144c32ace5a5e65d8ff0d814eab7
1 /* Copyright (C) 1993-1995,1997,2000,2002-2005 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <paths.h>
22 #include <stdarg.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sysdep.h>
28 #include <unistd.h>
29 #include <sys/syslog.h>
30 #include <execinfo.h>
32 /* Abort with an error message. */
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 = (void *) list->str;
120 iov[cnt].iov_len = list->len;
121 total += list->len;
122 list = list->next;
125 INTERNAL_SYSCALL_DECL (err);
126 ssize_t cnt;
128 cnt = INTERNAL_SYSCALL (writev, err, 3, fd, iov, nlist);
129 while (INTERNAL_SYSCALL_ERROR_P (cnt, err)
130 && INTERNAL_SYSCALL_ERRNO (cnt, err) == EINTR);
132 if (cnt == total)
133 written = true;
136 va_end (ap);
138 /* If we had no success writing the message, use syslog. */
139 if (! written)
140 vsyslog (LOG_ERR, fmt, ap_copy);
142 va_end (ap_copy);
144 if (do_abort)
146 if (do_abort > 1 && written)
148 void *addrs[64];
149 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
150 int n = __backtrace (addrs, naddrs);
151 if (n > 2)
153 #define strnsize(str) str, strlen (str)
154 #define writestr(str) write_not_cancel (fd, str)
155 writestr (strnsize ("======= Backtrace: =========\n"));
156 __backtrace_symbols_fd (addrs + 1, n - 1, fd);
158 writestr (strnsize ("======= Memory map: ========\n"));
159 int fd2 = open_not_cancel_2 ("/proc/self/maps", O_RDONLY);
160 char buf[1024];
161 ssize_t n2;
162 while ((n2 = read_not_cancel (fd2, buf, sizeof (buf))) > 0)
163 if (write_not_cancel (fd, buf, n2) != n2)
164 break;
165 close_not_cancel_no_status (fd2);
169 /* Terminate the process. */
170 abort ();
175 void
176 __libc_fatal (message)
177 const char *message;
179 /* The loop is added only to keep gcc happy. */
180 while (1)
181 __libc_message (1, "%s", message);
183 libc_hidden_def (__libc_fatal)