linux: Decorate __libc_fatal error buffer
[glibc.git] / sysdeps / posix / libc_fatal.c
blobf564d232bf8f2fca75f9da80df0745af6a39dac9
1 /* Catastrophic failure reports. Generic POSIX.1 version.
2 Copyright (C) 1993-2023 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 <https://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/mman.h>
32 #include <sys/uio.h>
33 #include <not-cancel.h>
34 #include <setvmaname.h>
36 #ifdef FATAL_PREPARE_INCLUDE
37 #include FATAL_PREPARE_INCLUDE
38 #endif
40 #ifndef WRITEV_FOR_FATAL
41 # define WRITEV_FOR_FATAL writev_for_fatal
42 static bool
43 writev_for_fatal (int fd, const struct iovec *iov, size_t niov, size_t total)
45 return TEMP_FAILURE_RETRY (__writev (fd, iov, niov)) == total;
47 #endif
49 /* Abort with an error message. */
50 void
51 __libc_message_impl (const char *fmt, ...)
53 va_list ap;
54 int fd = -1;
56 #ifdef FATAL_PREPARE
57 FATAL_PREPARE;
58 #endif
60 if (fd == -1)
61 fd = STDERR_FILENO;
63 struct iovec iov[LIBC_MESSAGE_MAX_ARGS * 2 - 1];
64 int iovcnt = 0;
65 ssize_t total = 0;
67 va_start (ap, fmt);
68 const char *cp = fmt;
69 while (*cp != '\0')
71 /* Find the next "%s" or the end of the string. */
72 const char *next = cp;
73 while (next[0] != '%' || next[1] != 's')
75 next = __strchrnul (next + 1, '%');
77 if (next[0] == '\0')
78 break;
81 /* Determine what to print. */
82 const char *str;
83 size_t len;
84 if (cp[0] == '%' && cp[1] == 's')
86 str = va_arg (ap, const char *);
87 len = strlen (str);
88 cp += 2;
90 else
92 str = cp;
93 len = next - cp;
94 cp = next;
97 iov[iovcnt].iov_base = (char *) str;
98 iov[iovcnt].iov_len = len;
99 total += len;
100 iovcnt++;
102 va_end (ap);
104 if (iovcnt > 0)
106 WRITEV_FOR_FATAL (fd, iov, iovcnt, total);
108 total = (total + 1 + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
109 struct abort_msg_s *buf = __mmap (NULL, total,
110 PROT_READ | PROT_WRITE,
111 MAP_ANON | MAP_PRIVATE, -1, 0);
112 if (__glibc_likely (buf != MAP_FAILED))
114 buf->size = total;
115 char *wp = buf->msg;
116 for (int cnt = 0; cnt < iovcnt; ++cnt)
117 wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len);
118 *wp = '\0';
120 __set_vma_name (buf, total, " glibc: fatal");
122 /* We have to free the old buffer since the application might
123 catch the SIGABRT signal. */
124 struct abort_msg_s *old = atomic_exchange_acquire (&__abort_msg,
125 buf);
126 if (old != NULL)
127 __munmap (old, old->size);
131 /* Kill the application. */
132 abort ();
136 void
137 __libc_fatal (const char *message)
139 /* The loop is added only to keep gcc happy. */
140 while (1)
141 __libc_message ("%s", message);
143 libc_hidden_def (__libc_fatal)