* sysdeps/pthread/sigaction.c: Use "" instead of <> to include self,
[glibc.git] / sysdeps / posix / libc_fatal.c
blob9dfac6800669008f459a8ff29f187288b2fd134f
1 /* Copyright (C) 1993,1994,1995,1997,2000,2004 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
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <paths.h>
22 #include <stdarg.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sysdep.h>
28 #include <unistd.h>
29 #include <sys/syslog.h>
30 #include <sys/uio.h>
31 #include <not-cancel.h>
33 #ifdef FATAL_PREPARE_INCLUDE
34 #include FATAL_PREPARE_INCLUDE
35 #endif
37 struct str_list
39 const char *str;
40 size_t len;
41 struct str_list *next;
45 /* Abort with an error message. */
46 void
47 __libc_message (int do_abort, const char *fmt, ...)
49 va_list ap;
50 va_list ap_copy;
51 int fd = -1;
53 va_start (ap, fmt);
54 va_copy (ap_copy, ap);
56 #ifdef FATAL_PREPARE
57 FATAL_PREPARE;
58 #endif
60 /* Open a descriptor for /dev/tty unless the user explicitly
61 requests errors on standard error. */
62 const char *on_2 = __secure_getenv ("LIBC_FATAL_STDERR_");
63 if (on_2 == NULL || *on_2 == '\0')
64 fd = open_not_cancel_2 (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY);
66 if (fd == -1)
67 fd = STDERR_FILENO;
69 struct str_list *list = NULL;
70 int nlist = 0;
72 const char *cp = fmt;
73 while (*cp != '\0')
75 /* Find the next "%s" or the end of the string. */
76 char *next = cp;
77 while (next[0] != '%' || next[1] != 's')
79 next = __strchrnul (next + 1, '%');
81 if (next[0] == '\0')
82 break;
85 /* Determine what to print. */
86 const char *str;
87 size_t len;
88 if (cp[0] == '%' && cp[1] == 's')
90 str = va_arg (ap, const char *);
91 len = strlen (str);
92 cp += 2;
94 else
96 str = cp;
97 len = next - cp;
98 cp = next;
101 struct str_list *newp = alloca (sizeof (struct str_list));
102 newp->str = str;
103 newp->len = len;
104 newp->next = list;
105 list = newp;
106 ++nlist;
109 bool written = false;
110 if (nlist > 0)
112 struct iovec *iov = alloca (nlist * sizeof (struct iovec));
113 ssize_t total = 0;
115 for (int cnt = nlist - 1; cnt >= 0; --cnt)
117 iov[cnt].iov_base = list->str;
118 iov[cnt].iov_len = list->len;
119 total += list->len;
120 list = list->next;
123 if (TEMP_FAILURE_RETRY (__writev (fd, iov, nlist)) == total)
124 written = true;
127 va_end (ap);
129 /* If we had no success writing the message, use syslog. */
130 if (! written)
131 vsyslog (LOG_ERR, fmt, ap_copy);
133 va_end (ap_copy);
135 if (do_abort)
136 /* Kill the application. */
137 abort ();
141 void
142 __libc_fatal (message)
143 const char *message;
145 /* The loop is added only to keep gcc happy. */
146 while (1)
147 __libc_message (1, "%s", message);
149 libc_hidden_def (__libc_fatal)