Back to index

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