Back to index

glibc  2.9
libc_fatal.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993,1994,1995,1997,2000,2004,2005
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <fcntl.h>
00022 #include <paths.h>
00023 #include <stdarg.h>
00024 #include <stdbool.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <sysdep.h>
00029 #include <unistd.h>
00030 #include <sys/syslog.h>
00031 #include <sys/uio.h>
00032 #include <not-cancel.h>
00033 
00034 #ifdef FATAL_PREPARE_INCLUDE
00035 #include FATAL_PREPARE_INCLUDE
00036 #endif
00037 
00038 struct str_list
00039 {
00040   const char *str;
00041   size_t len;
00042   struct str_list *next;
00043 };
00044 
00045 
00046 /* Abort with an error message.  */
00047 void
00048 __libc_message (int do_abort, const char *fmt, ...)
00049 {
00050   va_list ap;
00051   va_list ap_copy;
00052   int fd = -1;
00053 
00054   va_start (ap, fmt);
00055   va_copy (ap_copy, ap);
00056 
00057 #ifdef FATAL_PREPARE
00058   FATAL_PREPARE;
00059 #endif
00060 
00061   /* Open a descriptor for /dev/tty unless the user explicitly
00062      requests errors on standard error.  */
00063   const char *on_2 = __secure_getenv ("LIBC_FATAL_STDERR_");
00064   if (on_2 == NULL || *on_2 == '\0')
00065     fd = open_not_cancel_2 (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY);
00066 
00067   if (fd == -1)
00068     fd = STDERR_FILENO;
00069 
00070   struct str_list *list = NULL;
00071   int nlist = 0;
00072 
00073   const char *cp = fmt;
00074   while (*cp != '\0')
00075     {
00076       /* Find the next "%s" or the end of the string.  */
00077       const char *next = cp;
00078       while (next[0] != '%' || next[1] != 's')
00079        {
00080          next = __strchrnul (next + 1, '%');
00081 
00082          if (next[0] == '\0')
00083            break;
00084        }
00085 
00086       /* Determine what to print.  */
00087       const char *str;
00088       size_t len;
00089       if (cp[0] == '%' && cp[1] == 's')
00090        {
00091          str = va_arg (ap, const char *);
00092          len = strlen (str);
00093          cp += 2;
00094        }
00095       else
00096        {
00097          str = cp;
00098          len = next - cp;
00099          cp = next;
00100        }
00101 
00102       struct str_list *newp = alloca (sizeof (struct str_list));
00103       newp->str = str;
00104       newp->len = len;
00105       newp->next = list;
00106       list = newp;
00107       ++nlist;
00108     }
00109 
00110   bool written = false;
00111   if (nlist > 0)
00112     {
00113       struct iovec *iov = alloca (nlist * sizeof (struct iovec));
00114       ssize_t total = 0;
00115 
00116       for (int cnt = nlist - 1; cnt >= 0; --cnt)
00117        {
00118          iov[cnt].iov_base = (char *) list->str;
00119          iov[cnt].iov_len = list->len;
00120          total += list->len;
00121          list = list->next;
00122        }
00123 
00124       if (TEMP_FAILURE_RETRY (__writev (fd, iov, nlist)) == total)
00125        written = true;
00126     }
00127 
00128   va_end (ap);
00129 
00130   /* If we  had no success writing the message, use syslog.  */
00131   if (! written)
00132     vsyslog (LOG_ERR, fmt, ap_copy);
00133 
00134   va_end (ap_copy);
00135 
00136   if (do_abort)
00137     /* Kill the application.  */
00138     abort ();
00139 }
00140 
00141 
00142 void
00143 __libc_fatal (message)
00144      const char *message;
00145 {
00146   /* The loop is added only to keep gcc happy.  */
00147   while (1)
00148     __libc_message (1, "%s", message);
00149 }
00150 libc_hidden_def (__libc_fatal)