| 1 | /* Catastrophic failure reports. Generic POSIX.1 version. |
| 2 | Copyright (C) 1993-2024 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 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. |
| 9 | |
| 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. |
| 14 | |
| 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/>. */ |
| 18 | |
| 19 | #include <atomic.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <ldsodefs.h> |
| 23 | #include <libc-pointer-arith.h> |
| 24 | #include <paths.h> |
| 25 | #include <stdarg.h> |
| 26 | #include <stdbool.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <sysdep.h> |
| 31 | #include <unistd.h> |
| 32 | #include <sys/mman.h> |
| 33 | #include <sys/uio.h> |
| 34 | #include <not-cancel.h> |
| 35 | #include <setvmaname.h> |
| 36 | |
| 37 | #ifdef FATAL_PREPARE_INCLUDE |
| 38 | #include FATAL_PREPARE_INCLUDE |
| 39 | #endif |
| 40 | |
| 41 | #ifndef WRITEV_FOR_FATAL |
| 42 | # define WRITEV_FOR_FATAL writev_for_fatal |
| 43 | static bool |
| 44 | writev_for_fatal (int fd, const struct iovec *iov, size_t niov, size_t total) |
| 45 | { |
| 46 | return TEMP_FAILURE_RETRY (__writev (fd, iov, niov)) == total; |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | /* Abort with an error message. */ |
| 51 | void |
| 52 | __libc_message_impl (const char *fmt, ...) |
| 53 | { |
| 54 | va_list ap; |
| 55 | int fd = -1; |
| 56 | |
| 57 | #ifdef FATAL_PREPARE |
| 58 | FATAL_PREPARE; |
| 59 | #endif |
| 60 | |
| 61 | if (fd == -1) |
| 62 | fd = STDERR_FILENO; |
| 63 | |
| 64 | struct iovec iov[LIBC_MESSAGE_MAX_ARGS * 2 - 1]; |
| 65 | int iovcnt = 0; |
| 66 | ssize_t total = 0; |
| 67 | |
| 68 | va_start (ap, fmt); |
| 69 | const char *cp = fmt; |
| 70 | while (*cp != '\0') |
| 71 | { |
| 72 | /* Find the next "%s" or the end of the string. */ |
| 73 | const char *next = cp; |
| 74 | while (next[0] != '%' || next[1] != 's') |
| 75 | { |
| 76 | next = __strchrnul (s: next + 1, c: '%'); |
| 77 | |
| 78 | if (next[0] == '\0') |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | /* Determine what to print. */ |
| 83 | const char *str; |
| 84 | size_t len; |
| 85 | if (cp[0] == '%' && cp[1] == 's') |
| 86 | { |
| 87 | str = va_arg (ap, const char *); |
| 88 | len = strlen (s: str); |
| 89 | cp += 2; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | str = cp; |
| 94 | len = next - cp; |
| 95 | cp = next; |
| 96 | } |
| 97 | |
| 98 | iov[iovcnt].iov_base = (char *) str; |
| 99 | iov[iovcnt].iov_len = len; |
| 100 | total += len; |
| 101 | iovcnt++; |
| 102 | } |
| 103 | va_end (ap); |
| 104 | |
| 105 | if (iovcnt > 0) |
| 106 | { |
| 107 | WRITEV_FOR_FATAL (fd, iov, niov: iovcnt, total); |
| 108 | |
| 109 | total = ALIGN_UP (total + sizeof (struct abort_msg_s) + 1, |
| 110 | GLRO(dl_pagesize)); |
| 111 | struct abort_msg_s *buf = __mmap (NULL, len: total, |
| 112 | PROT_READ | PROT_WRITE, |
| 113 | MAP_ANON | MAP_PRIVATE, fd: -1, offset: 0); |
| 114 | if (__glibc_likely (buf != MAP_FAILED)) |
| 115 | { |
| 116 | buf->size = total; |
| 117 | char *wp = buf->msg; |
| 118 | for (int cnt = 0; cnt < iovcnt; ++cnt) |
| 119 | wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len); |
| 120 | *wp = '\0'; |
| 121 | |
| 122 | __set_vma_name (start: buf, len: total, name: " glibc: fatal" ); |
| 123 | |
| 124 | /* We have to free the old buffer since the application might |
| 125 | catch the SIGABRT signal. */ |
| 126 | struct abort_msg_s *old = atomic_exchange_acquire (&__abort_msg, |
| 127 | buf); |
| 128 | if (old != NULL) |
| 129 | __munmap (addr: old, len: old->size); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* Kill the application. */ |
| 134 | abort (); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void |
| 139 | __libc_fatal (const char *message) |
| 140 | { |
| 141 | /* The loop is added only to keep gcc happy. */ |
| 142 | while (1) |
| 143 | __libc_message ("%s" , message); |
| 144 | } |
| 145 | libc_hidden_def (__libc_fatal) |
| 146 | |