fac8cbd0517a11df3e2266d81324a3716b9d6b33
[platform/upstream/glibc.git] / sysdeps / posix / libc_fatal.c
1 /* Copyright (C) 1993,1994,1995,1997,2000,2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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.
13
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.  */
18
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>
31
32 #ifdef FATAL_PREPARE_INCLUDE
33 #include FATAL_PREPARE_INCLUDE
34 #endif
35
36 struct str_list
37 {
38   const char *str;
39   size_t len;
40   struct str_list *next;
41 };
42
43
44 /* Abort with an error message.  */
45 void
46 __libc_message (int do_abort, const char *fmt, ...)
47 {
48   va_list ap;
49   va_list ap_copy;
50   int fd = -1;
51
52   va_start (ap, fmt);
53   va_copy (ap_copy, ap);
54
55 #ifdef FATAL_PREPARE
56   FATAL_PREPARE;
57 #endif
58
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);
64
65   if (fd == -1)
66     fd = STDERR_FILENO;
67
68   struct str_list *list = NULL;
69   int nlist = 0;
70
71   const char *cp = fmt;
72   while (*cp != '\0')
73     {
74       /* Find the next "%s" or the end of the string.  */
75       char *next = cp;
76       while (next[0] != '%' || next[1] != 's')
77         {
78           next = __strchrnul (next + 1, '%');
79
80           if (next[0] == '\0')
81             break;
82         }
83
84       /* Determine what to print.  */
85       const char *str;
86       size_t len;
87       if (cp[0] == '%' && cp[1] == 's')
88         {
89           str = va_arg (ap, const char *);
90           len = strlen (str);
91           cp += 2;
92         }
93       else
94         {
95           str = cp;
96           len = next - cp;
97           cp = next;
98         }
99
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;
106     }
107
108   bool written = false;
109   if (nlist > 0)
110     {
111       struct iovec *iov = alloca (nlist * sizeof (struct iovec));
112       ssize_t total = 0;
113
114       for (int cnt = nlist - 1; cnt >= 0; --cnt)
115         {
116           iov[cnt].iov_base = list->str;
117           iov[cnt].iov_len = list->len;
118           total += list->len;
119           list = list->next;
120         }
121
122       if (TEMP_FAILURE_RETRY (__writev (fd, iov, nlist)) == total)
123         written = true;
124     }
125
126   va_end (ap);
127
128   /* If we  had no success writing the message, use syslog.  */
129   if (! written)
130     vsyslog (LOG_ERR, fmt, ap_copy);
131
132   va_end (ap_copy);
133
134   if (do_abort()
135       /* Kill the application.  */
136       abort ();
137 }
138
139
140 void
141 __libc_fatal (message)
142      const char *message;
143 {
144   __libc_message (1, "%s", message);
145 }
146 libc_hidden_def (__libc_fatal)