2004-11-13 03:09:30 +00:00
|
|
|
/* Copyright (C) 1993,1994,1995,1997,2000,2004 Free Software Foundation, Inc.
|
1997-05-26 23:01:17 +00:00
|
|
|
This file is part of the GNU C Library.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1997-05-26 23:01:17 +00:00
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:58:11 +00:00
|
|
|
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.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
1997-05-26 23:01:17 +00:00
|
|
|
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
|
2001-07-06 04:58:11 +00:00
|
|
|
Lesser General Public License for more details.
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2001-07-06 04:58:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
02111-1307 USA. */
|
1995-02-18 01:27:10 +00:00
|
|
|
|
2004-11-13 03:09:30 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
1995-02-18 01:27:10 +00:00
|
|
|
#include <stdio.h>
|
2000-04-22 07:19:25 +00:00
|
|
|
#include <stdlib.h>
|
1996-06-23 01:31:00 +00:00
|
|
|
#include <string.h>
|
2004-11-13 03:09:30 +00:00
|
|
|
#include <sysdep.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <not-cancel.h>
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
#ifdef FATAL_PREPARE_INCLUDE
|
|
|
|
#include FATAL_PREPARE_INCLUDE
|
|
|
|
#endif
|
|
|
|
|
2004-11-13 03:09:30 +00:00
|
|
|
struct str_list
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
size_t len;
|
|
|
|
struct str_list *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1995-02-18 01:27:10 +00:00
|
|
|
/* Abort with an error message. */
|
|
|
|
void
|
2004-11-13 03:09:30 +00:00
|
|
|
__libc_message (int do_abort, const char *fmt, ...)
|
1995-02-18 01:27:10 +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);
|
1995-02-18 01:27:10 +00:00
|
|
|
|
|
|
|
#ifdef FATAL_PREPARE
|
|
|
|
FATAL_PREPARE;
|
|
|
|
#endif
|
|
|
|
|
2004-11-13 03:09:30 +00:00
|
|
|
/* Open a descriptor for /dev/tty unless the user explicitly
|
|
|
|
requests errors on standard error. */
|
|
|
|
const char *on_2 = __secure_getenv ("LIBC_FATAL_STDERR_");
|
|
|
|
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')
|
1995-02-18 01:27:10 +00:00
|
|
|
{
|
2004-11-13 03:09:30 +00:00
|
|
|
/* Find the next "%s" or the end of the string. */
|
|
|
|
char *next = cp;
|
|
|
|
while (next[0] != '%' || next[1] != 's')
|
1995-02-18 01:27:10 +00:00
|
|
|
{
|
2004-11-13 03:09:30 +00:00
|
|
|
next = __strchrnul (next + 1, '%');
|
|
|
|
|
|
|
|
if (next[0] == '\0')
|
|
|
|
break;
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
2004-11-13 03:09:30 +00:00
|
|
|
|
|
|
|
/* Determine what to print. */
|
|
|
|
const char *str;
|
|
|
|
size_t len;
|
|
|
|
if (cp[0] == '%' && cp[1] == 's')
|
|
|
|
{
|
|
|
|
str = va_arg (ap, const char *);
|
|
|
|
len = strlen (str);
|
|
|
|
cp += 2;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = list->str;
|
|
|
|
iov[cnt].iov_len = list->len;
|
|
|
|
total += list->len;
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TEMP_FAILURE_RETRY (__writev (fd, iov, nlist)) == total)
|
|
|
|
written = true;
|
1995-02-18 01:27:10 +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);
|
|
|
|
|
|
|
|
if (do_abort()
|
|
|
|
/* Kill the application. */
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
__libc_fatal (message)
|
|
|
|
const char *message;
|
|
|
|
{
|
|
|
|
__libc_message (1, "%s", message);
|
1995-02-18 01:27:10 +00:00
|
|
|
}
|
2003-07-22 23:56:53 +00:00
|
|
|
libc_hidden_def (__libc_fatal)
|