2012-07-25 17:46:22 +00:00
|
|
|
/* Copyright (C) 1993-2012 Free Software Foundation, Inc.
|
2002-12-15 11:24:42 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
2002-12-15 11:24:42 +00:00
|
|
|
|
2009-06-15 23:17:09 +00:00
|
|
|
#include <atomic.h>
|
2004-11-13 03:09:30 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2011-05-15 04:34:48 +00:00
|
|
|
#include <ldsodefs.h>
|
2004-11-13 03:09:30 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <stdarg.h>
|
2004-11-15 22:00:25 +00:00
|
|
|
#include <stdbool.h>
|
2002-12-15 11:24:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-11-13 03:09:30 +00:00
|
|
|
#include <sysdep.h>
|
|
|
|
#include <unistd.h>
|
2011-05-15 04:34:48 +00:00
|
|
|
#include <sys/mman.h>
|
2004-11-13 03:09:30 +00:00
|
|
|
#include <sys/syslog.h>
|
2005-02-22 22:50:56 +00:00
|
|
|
#include <execinfo.h>
|
2002-12-15 11:24:42 +00:00
|
|
|
|
2004-11-13 03:09:30 +00:00
|
|
|
/* Abort with an error message. */
|
|
|
|
#include <not-cancel.h>
|
|
|
|
|
|
|
|
#ifdef FATAL_PREPARE_INCLUDE
|
|
|
|
#include FATAL_PREPARE_INCLUDE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct str_list
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
size_t len;
|
|
|
|
struct str_list *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-12-15 11:24:42 +00:00
|
|
|
/* Abort with an error message. */
|
|
|
|
void
|
2004-11-13 03:09:30 +00:00
|
|
|
__libc_message (int do_abort, const char *fmt, ...)
|
2002-12-15 11:24:42 +00:00
|
|
|
{
|
2004-11-13 03:09:30 +00:00
|
|
|
va_list ap;
|
|
|
|
va_list ap_copy;
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
va_copy (ap_copy, ap);
|
|
|
|
|
|
|
|
#ifdef FATAL_PREPARE
|
|
|
|
FATAL_PREPARE;
|
|
|
|
#endif
|
2002-12-15 11:24:42 +00:00
|
|
|
|
2004-11-13 03:09:30 +00:00
|
|
|
/* Open a descriptor for /dev/tty unless the user explicitly
|
|
|
|
requests errors on standard error. */
|
2012-07-25 17:46:22 +00:00
|
|
|
const char *on_2 = __libc_secure_getenv ("LIBC_FATAL_STDERR_");
|
2004-11-13 03:09:30 +00:00
|
|
|
if (on_2 == NULL || *on_2 == '\0')
|
|
|
|
fd = open_not_cancel_2 (_PATH_TTY, O_RDWR | O_NOCTTY | O_NDELAY);
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
fd = STDERR_FILENO;
|
|
|
|
|
|
|
|
struct str_list *list = NULL;
|
|
|
|
int nlist = 0;
|
|
|
|
|
|
|
|
const char *cp = fmt;
|
|
|
|
while (*cp != '\0')
|
2002-12-15 11:24:42 +00:00
|
|
|
{
|
2004-11-13 03:09:30 +00:00
|
|
|
/* Find the next "%s" or the end of the string. */
|
|
|
|
const char *next = cp;
|
|
|
|
while (next[0] != '%' || next[1] != 's')
|
|
|
|
{
|
|
|
|
next = __strchrnul (next + 1, '%');
|
|
|
|
|
|
|
|
if (next[0] == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine what to print. */
|
|
|
|
const char *str;
|
|
|
|
size_t len;
|
|
|
|
if (cp[0] == '%' && cp[1] == 's')
|
2002-12-15 11:24:42 +00:00
|
|
|
{
|
2004-11-13 03:09:30 +00:00
|
|
|
str = va_arg (ap, const char *);
|
|
|
|
len = strlen (str);
|
|
|
|
cp += 2;
|
2002-12-15 11:24:42 +00:00
|
|
|
}
|
2004-11-13 03:09:30 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
str = cp;
|
|
|
|
len = next - cp;
|
|
|
|
cp = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct str_list *newp = alloca (sizeof (struct str_list));
|
|
|
|
newp->str = str;
|
|
|
|
newp->len = len;
|
|
|
|
newp->next = list;
|
|
|
|
list = newp;
|
|
|
|
++nlist;
|
2002-12-15 11:24:42 +00:00
|
|
|
}
|
|
|
|
|
2004-11-13 03:09:30 +00:00
|
|
|
bool written = false;
|
|
|
|
if (nlist > 0)
|
|
|
|
{
|
|
|
|
struct iovec *iov = alloca (nlist * sizeof (struct iovec));
|
|
|
|
ssize_t total = 0;
|
|
|
|
|
|
|
|
for (int cnt = nlist - 1; cnt >= 0; --cnt)
|
|
|
|
{
|
|
|
|
iov[cnt].iov_base = (void *) list->str;
|
|
|
|
iov[cnt].iov_len = list->len;
|
|
|
|
total += list->len;
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERNAL_SYSCALL_DECL (err);
|
|
|
|
ssize_t cnt;
|
|
|
|
do
|
|
|
|
cnt = INTERNAL_SYSCALL (writev, err, 3, fd, iov, nlist);
|
|
|
|
while (INTERNAL_SYSCALL_ERROR_P (cnt, err)
|
|
|
|
&& INTERNAL_SYSCALL_ERRNO (cnt, err) == EINTR);
|
2002-12-15 11:24:42 +00:00
|
|
|
|
2004-11-13 03:09:30 +00:00
|
|
|
if (cnt == total)
|
|
|
|
written = true;
|
2009-06-15 23:17:09 +00:00
|
|
|
|
2011-05-15 04:34:48 +00:00
|
|
|
if (do_abort)
|
2009-06-15 23:17:09 +00:00
|
|
|
{
|
2011-05-15 04:34:48 +00:00
|
|
|
total = ((total + 1 + GLRO(dl_pagesize) - 1)
|
|
|
|
& ~(GLRO(dl_pagesize) - 1));
|
|
|
|
struct abort_msg_s *buf = __mmap (NULL, total,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
|
|
if (__builtin_expect (buf != MAP_FAILED, 1))
|
|
|
|
{
|
|
|
|
buf->size = total;
|
|
|
|
char *wp = buf->msg;
|
|
|
|
for (int cnt = 0; cnt < nlist; ++cnt)
|
|
|
|
wp = mempcpy (wp, iov[cnt].iov_base, iov[cnt].iov_len);
|
|
|
|
*wp = '\0';
|
|
|
|
|
|
|
|
/* We have to free the old buffer since the application might
|
|
|
|
catch the SIGABRT signal. */
|
|
|
|
struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg,
|
|
|
|
buf);
|
|
|
|
if (old != NULL)
|
|
|
|
__munmap (old, old->size);
|
|
|
|
}
|
2009-06-15 23:17:09 +00:00
|
|
|
}
|
2004-11-13 03:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
/* If we had no success writing the message, use syslog. */
|
|
|
|
if (! written)
|
|
|
|
vsyslog (LOG_ERR, fmt, ap_copy);
|
|
|
|
|
2004-11-13 05:43:36 +00:00
|
|
|
va_end (ap_copy);
|
|
|
|
|
2004-11-13 03:09:30 +00:00
|
|
|
if (do_abort)
|
* sysdeps/unix/sysv/linux/libc_fatal.c: Print backtrace and memory
map if requested.
* debug/chk_fail.c: Request backtrace and memory map dump.
* Versions.def: Add GLIBC_2.4 for libc.
* debug/fgets_chk.c: New file.
* debug/fgets_u_chk.c: New file.
* debug/getcwd_chk.c: New file.
* debug/getwd_chk.c: New file.
* debug/readlink_chk.c: New file.
* debug/read_chk.c: New file.
* debug/pread_chk.c: New file.
* debug/pread64_chk.c: New file.
* debug/recv_chk.c: New file.
* debug/recvfrom_chk.c: New file.
* debug/Versions: Add all new functions with version GLIBC_2.4.
* debug/Makefile (routines): Add fgets_chk, fgets_u_chk, read_chk,
pread_chk, pread64_chk, recv_chk, recvfrom_chk, readlink_chk,
getwd_chk, and getcwd_chk. Plus appropriate CFLAGS definitions.
* debug/tst-chk1.c: Add more tests.
* libio/bits/stdio2.h: Add macros for fgets and fgets_unlocked.
* include/stdio.h: Declare __fgets_chk and __fgets_unlocked_chk.
* posix/unistd.h: Include <bits/unistd.h> for fortification.
* posix/bits/unistd.h: New file.
* posix/Makefile (headers): Add bits/unistd.h.
* socket/sys/socket.h: Include <bits/socket2.h> for fortification.
* socket/bits/socket2.h: New file.
* socket/Makefile (headers): Add bits/socket2.h.
* string/bits/string3.h: Extend memset macro to check for zero 3rd
parameter and use __memset_zero_constant_len_parameter in that case.
* sysdeps/generic/memset_chk.c: Add
__memset_zero_constant_len_parameter alias and linker warning.
* debug/Versions: Add __memset_zero_constant_len_parameter to libc
with version GLIBC_2.4.
* sysdeps/generic/bits/types.h: Don't unnecessarily use __extension__
in __STD_TYPE definition.
2005-02-21 Jakub Jelinek <jakub@redhat.com>
* malloc/malloc.c (malloc_printerr): If MALLOC_CHECK_={5,7}, print
the error message rather than program name.
2005-02-21 Ulrich Drepper <drepper@redhat.com>
2005-02-21 23:14:10 +00:00
|
|
|
{
|
|
|
|
if (do_abort > 1 && written)
|
|
|
|
{
|
|
|
|
void *addrs[64];
|
|
|
|
#define naddrs (sizeof (addrs) / sizeof (addrs[0]))
|
|
|
|
int n = __backtrace (addrs, naddrs);
|
|
|
|
if (n > 2)
|
|
|
|
{
|
|
|
|
#define strnsize(str) str, strlen (str)
|
2005-02-22 23:11:01 +00:00
|
|
|
#define writestr(str) write_not_cancel (fd, str)
|
|
|
|
writestr (strnsize ("======= Backtrace: =========\n"));
|
* sysdeps/unix/sysv/linux/libc_fatal.c: Print backtrace and memory
map if requested.
* debug/chk_fail.c: Request backtrace and memory map dump.
* Versions.def: Add GLIBC_2.4 for libc.
* debug/fgets_chk.c: New file.
* debug/fgets_u_chk.c: New file.
* debug/getcwd_chk.c: New file.
* debug/getwd_chk.c: New file.
* debug/readlink_chk.c: New file.
* debug/read_chk.c: New file.
* debug/pread_chk.c: New file.
* debug/pread64_chk.c: New file.
* debug/recv_chk.c: New file.
* debug/recvfrom_chk.c: New file.
* debug/Versions: Add all new functions with version GLIBC_2.4.
* debug/Makefile (routines): Add fgets_chk, fgets_u_chk, read_chk,
pread_chk, pread64_chk, recv_chk, recvfrom_chk, readlink_chk,
getwd_chk, and getcwd_chk. Plus appropriate CFLAGS definitions.
* debug/tst-chk1.c: Add more tests.
* libio/bits/stdio2.h: Add macros for fgets and fgets_unlocked.
* include/stdio.h: Declare __fgets_chk and __fgets_unlocked_chk.
* posix/unistd.h: Include <bits/unistd.h> for fortification.
* posix/bits/unistd.h: New file.
* posix/Makefile (headers): Add bits/unistd.h.
* socket/sys/socket.h: Include <bits/socket2.h> for fortification.
* socket/bits/socket2.h: New file.
* socket/Makefile (headers): Add bits/socket2.h.
* string/bits/string3.h: Extend memset macro to check for zero 3rd
parameter and use __memset_zero_constant_len_parameter in that case.
* sysdeps/generic/memset_chk.c: Add
__memset_zero_constant_len_parameter alias and linker warning.
* debug/Versions: Add __memset_zero_constant_len_parameter to libc
with version GLIBC_2.4.
* sysdeps/generic/bits/types.h: Don't unnecessarily use __extension__
in __STD_TYPE definition.
2005-02-21 Jakub Jelinek <jakub@redhat.com>
* malloc/malloc.c (malloc_printerr): If MALLOC_CHECK_={5,7}, print
the error message rather than program name.
2005-02-21 Ulrich Drepper <drepper@redhat.com>
2005-02-21 23:14:10 +00:00
|
|
|
__backtrace_symbols_fd (addrs + 1, n - 1, fd);
|
|
|
|
|
2005-02-22 23:11:01 +00:00
|
|
|
writestr (strnsize ("======= Memory map: ========\n"));
|
|
|
|
int fd2 = open_not_cancel_2 ("/proc/self/maps", O_RDONLY);
|
* sysdeps/unix/sysv/linux/libc_fatal.c: Print backtrace and memory
map if requested.
* debug/chk_fail.c: Request backtrace and memory map dump.
* Versions.def: Add GLIBC_2.4 for libc.
* debug/fgets_chk.c: New file.
* debug/fgets_u_chk.c: New file.
* debug/getcwd_chk.c: New file.
* debug/getwd_chk.c: New file.
* debug/readlink_chk.c: New file.
* debug/read_chk.c: New file.
* debug/pread_chk.c: New file.
* debug/pread64_chk.c: New file.
* debug/recv_chk.c: New file.
* debug/recvfrom_chk.c: New file.
* debug/Versions: Add all new functions with version GLIBC_2.4.
* debug/Makefile (routines): Add fgets_chk, fgets_u_chk, read_chk,
pread_chk, pread64_chk, recv_chk, recvfrom_chk, readlink_chk,
getwd_chk, and getcwd_chk. Plus appropriate CFLAGS definitions.
* debug/tst-chk1.c: Add more tests.
* libio/bits/stdio2.h: Add macros for fgets and fgets_unlocked.
* include/stdio.h: Declare __fgets_chk and __fgets_unlocked_chk.
* posix/unistd.h: Include <bits/unistd.h> for fortification.
* posix/bits/unistd.h: New file.
* posix/Makefile (headers): Add bits/unistd.h.
* socket/sys/socket.h: Include <bits/socket2.h> for fortification.
* socket/bits/socket2.h: New file.
* socket/Makefile (headers): Add bits/socket2.h.
* string/bits/string3.h: Extend memset macro to check for zero 3rd
parameter and use __memset_zero_constant_len_parameter in that case.
* sysdeps/generic/memset_chk.c: Add
__memset_zero_constant_len_parameter alias and linker warning.
* debug/Versions: Add __memset_zero_constant_len_parameter to libc
with version GLIBC_2.4.
* sysdeps/generic/bits/types.h: Don't unnecessarily use __extension__
in __STD_TYPE definition.
2005-02-21 Jakub Jelinek <jakub@redhat.com>
* malloc/malloc.c (malloc_printerr): If MALLOC_CHECK_={5,7}, print
the error message rather than program name.
2005-02-21 Ulrich Drepper <drepper@redhat.com>
2005-02-21 23:14:10 +00:00
|
|
|
char buf[1024];
|
|
|
|
ssize_t n2;
|
2005-02-22 23:11:01 +00:00
|
|
|
while ((n2 = read_not_cancel (fd2, buf, sizeof (buf))) > 0)
|
|
|
|
if (write_not_cancel (fd, buf, n2) != n2)
|
* sysdeps/unix/sysv/linux/libc_fatal.c: Print backtrace and memory
map if requested.
* debug/chk_fail.c: Request backtrace and memory map dump.
* Versions.def: Add GLIBC_2.4 for libc.
* debug/fgets_chk.c: New file.
* debug/fgets_u_chk.c: New file.
* debug/getcwd_chk.c: New file.
* debug/getwd_chk.c: New file.
* debug/readlink_chk.c: New file.
* debug/read_chk.c: New file.
* debug/pread_chk.c: New file.
* debug/pread64_chk.c: New file.
* debug/recv_chk.c: New file.
* debug/recvfrom_chk.c: New file.
* debug/Versions: Add all new functions with version GLIBC_2.4.
* debug/Makefile (routines): Add fgets_chk, fgets_u_chk, read_chk,
pread_chk, pread64_chk, recv_chk, recvfrom_chk, readlink_chk,
getwd_chk, and getcwd_chk. Plus appropriate CFLAGS definitions.
* debug/tst-chk1.c: Add more tests.
* libio/bits/stdio2.h: Add macros for fgets and fgets_unlocked.
* include/stdio.h: Declare __fgets_chk and __fgets_unlocked_chk.
* posix/unistd.h: Include <bits/unistd.h> for fortification.
* posix/bits/unistd.h: New file.
* posix/Makefile (headers): Add bits/unistd.h.
* socket/sys/socket.h: Include <bits/socket2.h> for fortification.
* socket/bits/socket2.h: New file.
* socket/Makefile (headers): Add bits/socket2.h.
* string/bits/string3.h: Extend memset macro to check for zero 3rd
parameter and use __memset_zero_constant_len_parameter in that case.
* sysdeps/generic/memset_chk.c: Add
__memset_zero_constant_len_parameter alias and linker warning.
* debug/Versions: Add __memset_zero_constant_len_parameter to libc
with version GLIBC_2.4.
* sysdeps/generic/bits/types.h: Don't unnecessarily use __extension__
in __STD_TYPE definition.
2005-02-21 Jakub Jelinek <jakub@redhat.com>
* malloc/malloc.c (malloc_printerr): If MALLOC_CHECK_={5,7}, print
the error message rather than program name.
2005-02-21 Ulrich Drepper <drepper@redhat.com>
2005-02-21 23:14:10 +00:00
|
|
|
break;
|
2005-02-22 23:11:01 +00:00
|
|
|
close_not_cancel_no_status (fd2);
|
* sysdeps/unix/sysv/linux/libc_fatal.c: Print backtrace and memory
map if requested.
* debug/chk_fail.c: Request backtrace and memory map dump.
* Versions.def: Add GLIBC_2.4 for libc.
* debug/fgets_chk.c: New file.
* debug/fgets_u_chk.c: New file.
* debug/getcwd_chk.c: New file.
* debug/getwd_chk.c: New file.
* debug/readlink_chk.c: New file.
* debug/read_chk.c: New file.
* debug/pread_chk.c: New file.
* debug/pread64_chk.c: New file.
* debug/recv_chk.c: New file.
* debug/recvfrom_chk.c: New file.
* debug/Versions: Add all new functions with version GLIBC_2.4.
* debug/Makefile (routines): Add fgets_chk, fgets_u_chk, read_chk,
pread_chk, pread64_chk, recv_chk, recvfrom_chk, readlink_chk,
getwd_chk, and getcwd_chk. Plus appropriate CFLAGS definitions.
* debug/tst-chk1.c: Add more tests.
* libio/bits/stdio2.h: Add macros for fgets and fgets_unlocked.
* include/stdio.h: Declare __fgets_chk and __fgets_unlocked_chk.
* posix/unistd.h: Include <bits/unistd.h> for fortification.
* posix/bits/unistd.h: New file.
* posix/Makefile (headers): Add bits/unistd.h.
* socket/sys/socket.h: Include <bits/socket2.h> for fortification.
* socket/bits/socket2.h: New file.
* socket/Makefile (headers): Add bits/socket2.h.
* string/bits/string3.h: Extend memset macro to check for zero 3rd
parameter and use __memset_zero_constant_len_parameter in that case.
* sysdeps/generic/memset_chk.c: Add
__memset_zero_constant_len_parameter alias and linker warning.
* debug/Versions: Add __memset_zero_constant_len_parameter to libc
with version GLIBC_2.4.
* sysdeps/generic/bits/types.h: Don't unnecessarily use __extension__
in __STD_TYPE definition.
2005-02-21 Jakub Jelinek <jakub@redhat.com>
* malloc/malloc.c (malloc_printerr): If MALLOC_CHECK_={5,7}, print
the error message rather than program name.
2005-02-21 Ulrich Drepper <drepper@redhat.com>
2005-02-21 23:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the process. */
|
|
|
|
abort ();
|
|
|
|
}
|
2004-11-13 03:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
__libc_fatal (message)
|
|
|
|
const char *message;
|
|
|
|
{
|
2004-11-15 22:00:25 +00:00
|
|
|
/* The loop is added only to keep gcc happy. */
|
|
|
|
while (1)
|
|
|
|
__libc_message (1, "%s", message);
|
2002-12-15 11:24:42 +00:00
|
|
|
}
|
2003-07-22 23:56:53 +00:00
|
|
|
libc_hidden_def (__libc_fatal)
|