stdio: Remove __libc_message alloca usage
[glibc.git] / sysdeps / posix / libc_fatal.c
blobcf28387ee607e315efbfdfc634e0f7a1e34f88d8
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>
35 #ifdef FATAL_PREPARE_INCLUDE
36 #include FATAL_PREPARE_INCLUDE
37 #endif
39 #ifndef WRITEV_FOR_FATAL
40 # define WRITEV_FOR_FATAL writev_for_fatal
41 static bool
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;
46 #endif
48 /* Abort with an error message. */
49 void
50 __libc_message_impl (const char *fmt, ...)
52 va_list ap;
53 int fd = -1;
55 #ifdef FATAL_PREPARE
56 FATAL_PREPARE;
57 #endif
59 if (fd == -1)
60 fd = STDERR_FILENO;
62 struct iovec iov[LIBC_MESSAGE_MAX_ARGS * 2 - 1];
63 int iovcnt = 0;
64 ssize_t total = 0;
66 va_start (ap, fmt);
67 const char *cp = fmt;
68 while (*cp != '\0')
70 /* Find the next "%s" or the end of the string. */
71 const char *next = cp;
72 while (next[0] != '%' || next[1] != 's')
74 next = __strchrnul (next + 1, '%');
76 if (next[0] == '\0')
77 break;
80 /* Determine what to print. */
81 const char *str;
82 size_t len;
83 if (cp[0] == '%' && cp[1] == 's')
85 str = va_arg (ap, const char *);
86 len = strlen (str);
87 cp += 2;
89 else
91 str = cp;
92 len = next - cp;
93 cp = next;
96 iov[iovcnt].iov_base = (char *) str;
97 iov[iovcnt].iov_len = len;
98 total += len;
99 iovcnt++;
101 va_end (ap);
103 if (iovcnt > 0)
105 WRITEV_FOR_FATAL (fd, iov, iovcnt, total);
107 total = (total + 1 + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
108 struct abort_msg_s *buf = __mmap (NULL, total,
109 PROT_READ | PROT_WRITE,
110 MAP_ANON | MAP_PRIVATE, -1, 0);
111 if (__glibc_likely (buf != MAP_FAILED))
113 buf->size = total;
114 char *wp = buf->msg;
115 for (int cnt = 0; cnt < iovcnt; ++cnt)
116 wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len);
117 *wp = '\0';
119 /* We have to free the old buffer since the application might
120 catch the SIGABRT signal. */
121 struct abort_msg_s *old = atomic_exchange_acquire (&__abort_msg,
122 buf);
123 if (old != NULL)
124 __munmap (old, old->size);
128 /* Kill the application. */
129 abort ();
133 void
134 __libc_fatal (const char *message)
136 /* The loop is added only to keep gcc happy. */
137 while (1)
138 __libc_message ("%s", message);
140 libc_hidden_def (__libc_fatal)