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
29 #include <sys/syslog.h>
32 /* Abort with an error message. */
33 #include <not-cancel.h>
35 #ifdef FATAL_PREPARE_INCLUDE
36 #include FATAL_PREPARE_INCLUDE
43 struct str_list
*next
;
47 /* Abort with an error message. */
49 __libc_message (int do_abort
, const char *fmt
, ...)
56 va_copy (ap_copy
, ap
);
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
);
71 struct str_list
*list
= NULL
;
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, '%');
87 /* Determine what to print. */
90 if (cp
[0] == '%' && cp
[1] == 's')
92 str
= va_arg (ap
, const char *);
103 struct str_list
*newp
= alloca (sizeof (struct str_list
));
111 bool written
= false;
114 struct iovec
*iov
= alloca (nlist
* sizeof (struct iovec
));
117 for (int cnt
= nlist
- 1; cnt
>= 0; --cnt
)
119 iov
[cnt
].iov_base
= (void *) list
->str
;
120 iov
[cnt
].iov_len
= list
->len
;
125 INTERNAL_SYSCALL_DECL (err
);
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
);
138 /* If we had no success writing the message, use syslog. */
140 vsyslog (LOG_ERR
, fmt
, ap_copy
);
146 if (do_abort
> 1 && written
)
149 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
150 int n
= __backtrace (addrs
, naddrs
);
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
);
162 while ((n2
= read_not_cancel (fd2
, buf
, sizeof (buf
))) > 0)
163 if (write_not_cancel (fd
, buf
, n2
) != n2
)
165 close_not_cancel_no_status (fd2
);
169 /* Terminate the process. */
176 __libc_fatal (message
)
179 /* The loop is added only to keep gcc happy. */
181 __libc_message (1, "%s", message
);
183 libc_hidden_def (__libc_fatal
)