1 /* Catastrophic failure reports. Generic POSIX.1 version.
2 Copyright (C) 1993-2015 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/>. */
33 #include <not-cancel.h>
35 #ifdef FATAL_PREPARE_INCLUDE
36 #include FATAL_PREPARE_INCLUDE
39 #ifndef WRITEV_FOR_FATAL
40 # define WRITEV_FOR_FATAL writev_for_fatal
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
;
49 # define BEFORE_ABORT before_abort
51 before_abort (int do_abort
__attribute__ ((unused
)),
52 bool written
__attribute__ ((unused
)),
53 int fd
__attribute__ ((unused
)))
62 struct str_list
*next
;
65 /* Abort with an error message. */
67 __libc_message (int do_abort
, const char *fmt
, ...)
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
);
87 struct str_list
*list
= NULL
;
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, '%');
103 /* Determine what to print. */
106 if (cp
[0] == '%' && cp
[1] == 's')
108 str
= va_arg (ap
, const char *);
119 struct str_list
*newp
= alloca (sizeof (struct str_list
));
127 bool written
= false;
130 struct iovec
*iov
= alloca (nlist
* sizeof (struct iovec
));
133 for (int cnt
= nlist
- 1; cnt
>= 0; --cnt
)
135 iov
[cnt
].iov_base
= (char *) list
->str
;
136 iov
[cnt
].iov_len
= list
->len
;
141 written
= WRITEV_FOR_FATAL (fd
, iov
, nlist
, total
);
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
))
154 for (int cnt
= 0; cnt
< nlist
; ++cnt
)
155 wp
= mempcpy (wp
, iov
[cnt
].iov_base
, iov
[cnt
].iov_len
);
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
,
163 __munmap (old
, old
->size
);
172 BEFORE_ABORT (do_abort
, written
, fd
);
174 /* Kill the application. */
181 __libc_fatal (message
)
184 /* The loop is added only to keep gcc happy. */
186 __libc_message (1, "%s", message
);
188 libc_hidden_def (__libc_fatal
)