Add new function __libc_message which performs the printing and simple format string...
[glibc.git] / sysdeps / posix / libc_fatal.c
blobfd97017579ce28d76c26b9dd3734cdc3c184f707
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 <not-cancel.h>
32 #ifdef FATAL_PREPARE_INCLUDE
33 #include FATAL_PREPARE_INCLUDE
34 #endif
36 struct str_list
38 const char *str;
39 size_t len;
40 struct str_list *next;
44 /* Abort with an error message. */
45 void
46 __libc_message (int do_abort, const char *fmt, ...)
48 va_list ap;
49 va_list ap_copy;
50 int fd = -1;
52 va_start (ap, fmt);
53 va_copy (ap_copy, ap);
55 #ifdef FATAL_PREPARE
56 FATAL_PREPARE;
57 #endif
59 /* Open a descriptor for /dev/tty unless the user explicitly
60 requests errors on standard error. */
61 const char *on_2 = __secure_getenv ("LIBC_FATAL_STDERR_");
62 if (on_2 == NULL || *on_2 == '\0')
63 fd = open_not_cancel_2 (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY);
65 if (fd == -1)
66 fd = STDERR_FILENO;
68 struct str_list *list = NULL;
69 int nlist = 0;
71 const char *cp = fmt;
72 while (*cp != '\0')
74 /* Find the next "%s" or the end of the string. */
75 char *next = cp;
76 while (next[0] != '%' || next[1] != 's')
78 next = __strchrnul (next + 1, '%');
80 if (next[0] == '\0')
81 break;
84 /* Determine what to print. */
85 const char *str;
86 size_t len;
87 if (cp[0] == '%' && cp[1] == 's')
89 str = va_arg (ap, const char *);
90 len = strlen (str);
91 cp += 2;
93 else
95 str = cp;
96 len = next - cp;
97 cp = next;
100 struct str_list *newp = alloca (sizeof (struct str_list));
101 newp->str = str;
102 newp->len = len;
103 newp->next = list;
104 list = newp;
105 ++nlist;
108 bool written = false;
109 if (nlist > 0)
111 struct iovec *iov = alloca (nlist * sizeof (struct iovec));
112 ssize_t total = 0;
114 for (int cnt = nlist - 1; cnt >= 0; --cnt)
116 iov[cnt].iov_base = list->str;
117 iov[cnt].iov_len = list->len;
118 total += list->len;
119 list = list->next;
122 if (TEMP_FAILURE_RETRY (__writev (fd, iov, nlist)) == total)
123 written = true;
126 va_end (ap);
128 /* If we had no success writing the message, use syslog. */
129 if (! written)
130 vsyslog (LOG_ERR, fmt, ap_copy);
132 if (do_abort()
133 /* Kill the application. */
134 abort ();
138 void
139 __libc_fatal (message)
140 const char *message;
142 __libc_message (1, "%s", message);
144 libc_hidden_def (__libc_fatal)