Blame lib/error.c

Packit 709fb3
/* Error handler for noninteractive utilities
Packit 709fb3
   Copyright (C) 1990-1998, 2000-2007, 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
   This file is part of the GNU C Library.
Packit 709fb3
Packit 709fb3
   This program is free software: you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3 of the License, or
Packit 709fb3
   (at your option) any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License
Packit 709fb3
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
Packit 709fb3
Packit 709fb3
#if !_LIBC
Packit 709fb3
# include <config.h>
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#include "error.h"
Packit 709fb3
Packit 709fb3
#include <stdarg.h>
Packit 709fb3
#include <stdio.h>
Packit 709fb3
#include <stdlib.h>
Packit 709fb3
#include <string.h>
Packit 709fb3
Packit 709fb3
#if !_LIBC && ENABLE_NLS
Packit 709fb3
# include "gettext.h"
Packit 709fb3
# define _(msgid) gettext (msgid)
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
# include <libintl.h>
Packit 709fb3
# include <stdbool.h>
Packit 709fb3
# include <stdint.h>
Packit 709fb3
# include <wchar.h>
Packit 709fb3
# define mbsrtowcs __mbsrtowcs
Packit 709fb3
# define USE_UNLOCKED_IO 0
Packit 709fb3
# define _GL_ATTRIBUTE_FORMAT_PRINTF(a, b)
Packit 709fb3
# define _GL_ARG_NONNULL(a)
Packit 709fb3
#else
Packit 709fb3
# include "getprogname.h"
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#if USE_UNLOCKED_IO
Packit 709fb3
# include "unlocked-io.h"
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#ifndef _
Packit 709fb3
# define _(String) String
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
/* If NULL, error will flush stdout, then print on stderr the program
Packit 709fb3
   name, a colon and a space.  Otherwise, error will call this
Packit 709fb3
   function without parameters instead.  */
Packit 709fb3
void (*error_print_progname) (void);
Packit 709fb3
Packit 709fb3
/* This variable is incremented each time 'error' is called.  */
Packit 709fb3
unsigned int error_message_count;
Packit 709fb3
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
/* In the GNU C library, there is a predefined variable for this.  */
Packit 709fb3
Packit 709fb3
# define program_name program_invocation_name
Packit 709fb3
# include <errno.h>
Packit 709fb3
# include <limits.h>
Packit 709fb3
# include <libio/libioP.h>
Packit 709fb3
Packit 709fb3
/* In GNU libc we want do not want to use the common name 'error' directly.
Packit 709fb3
   Instead make it a weak alias.  */
Packit 709fb3
extern void __error (int status, int errnum, const char *message, ...)
Packit 709fb3
     __attribute__ ((__format__ (__printf__, 3, 4)));
Packit 709fb3
extern void __error_at_line (int status, int errnum, const char *file_name,
Packit 709fb3
                             unsigned int line_number, const char *message,
Packit 709fb3
                             ...)
Packit 709fb3
     __attribute__ ((__format__ (__printf__, 5, 6)));
Packit 709fb3
# define error __error
Packit 709fb3
# define error_at_line __error_at_line
Packit 709fb3
Packit 709fb3
# include <libio/iolibio.h>
Packit 709fb3
# define fflush(s) _IO_fflush (s)
Packit 709fb3
# undef putc
Packit 709fb3
# define putc(c, fp) _IO_putc (c, fp)
Packit 709fb3
Packit 709fb3
# include <bits/libc-lock.h>
Packit 709fb3
Packit 709fb3
#else /* not _LIBC */
Packit 709fb3
Packit 709fb3
# include <fcntl.h>
Packit 709fb3
# include <unistd.h>
Packit 709fb3
Packit 709fb3
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 709fb3
/* Get declarations of the native Windows API functions.  */
Packit 709fb3
#  define WIN32_LEAN_AND_MEAN
Packit 709fb3
#  include <windows.h>
Packit 709fb3
/* Get _get_osfhandle.  */
Packit 709fb3
#  if GNULIB_MSVC_NOTHROW
Packit 709fb3
#   include "msvc-nothrow.h"
Packit 709fb3
#  else
Packit 709fb3
#   include <io.h>
Packit 709fb3
#  endif
Packit 709fb3
# endif
Packit 709fb3
Packit 709fb3
/* The gnulib override of fcntl is not needed in this file.  */
Packit 709fb3
# undef fcntl
Packit 709fb3
Packit 709fb3
# if !(GNULIB_STRERROR_R_POSIX || HAVE_DECL_STRERROR_R)
Packit 709fb3
#  ifndef HAVE_DECL_STRERROR_R
Packit 709fb3
"this configure-time declaration test was not run"
Packit 709fb3
#  endif
Packit 709fb3
#  if STRERROR_R_CHAR_P
Packit 709fb3
char *strerror_r (int errnum, char *buf, size_t buflen);
Packit 709fb3
#  else
Packit 709fb3
int strerror_r (int errnum, char *buf, size_t buflen);
Packit 709fb3
#  endif
Packit 709fb3
# endif
Packit 709fb3
Packit 709fb3
#define program_name getprogname ()
Packit 709fb3
Packit 709fb3
# if GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r
Packit 709fb3
#  define __strerror_r strerror_r
Packit 709fb3
# endif /* GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r */
Packit 709fb3
#endif  /* not _LIBC */
Packit 709fb3
Packit 709fb3
#if !_LIBC
Packit 709fb3
/* Return non-zero if FD is open.  */
Packit 709fb3
static int
Packit 709fb3
is_open (int fd)
Packit 709fb3
{
Packit 709fb3
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 709fb3
  /* On native Windows: The initial state of unassigned standard file
Packit 709fb3
     descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
Packit 709fb3
     There is no fcntl, and the gnulib replacement fcntl does not support
Packit 709fb3
     F_GETFL.  */
Packit 709fb3
  return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
Packit 709fb3
# else
Packit 709fb3
#  ifndef F_GETFL
Packit 709fb3
#   error Please port fcntl to your platform
Packit 709fb3
#  endif
Packit 709fb3
  return 0 <= fcntl (fd, F_GETFL);
Packit 709fb3
# endif
Packit 709fb3
}
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
static void
Packit 709fb3
flush_stdout (void)
Packit 709fb3
{
Packit 709fb3
#if !_LIBC
Packit 709fb3
  int stdout_fd;
Packit 709fb3
Packit 709fb3
# if GNULIB_FREOPEN_SAFER
Packit 709fb3
  /* Use of gnulib's freopen-safer module normally ensures that
Packit 709fb3
       fileno (stdout) == 1
Packit 709fb3
     whenever stdout is open.  */
Packit 709fb3
  stdout_fd = STDOUT_FILENO;
Packit 709fb3
# else
Packit 709fb3
  /* POSIX states that fileno (stdout) after fclose is unspecified.  But in
Packit 709fb3
     practice it is not a problem, because stdout is statically allocated and
Packit 709fb3
     the fd of a FILE stream is stored as a field in its allocated memory.  */
Packit 709fb3
  stdout_fd = fileno (stdout);
Packit 709fb3
# endif
Packit 709fb3
  /* POSIX states that fflush (stdout) after fclose is unspecified; it
Packit 709fb3
     is safe in glibc, but not on all other platforms.  fflush (NULL)
Packit 709fb3
     is always defined, but too draconian.  */
Packit 709fb3
  if (0 <= stdout_fd && is_open (stdout_fd))
Packit 709fb3
#endif
Packit 709fb3
    fflush (stdout);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
static void
Packit 709fb3
print_errno_message (int errnum)
Packit 709fb3
{
Packit 709fb3
  char const *s;
Packit 709fb3
Packit 709fb3
#if _LIBC || GNULIB_STRERROR_R_POSIX || defined HAVE_STRERROR_R
Packit 709fb3
  char errbuf[1024];
Packit 709fb3
# if _LIBC || (!GNULIB_STRERROR_R_POSIX && STRERROR_R_CHAR_P)
Packit 709fb3
  s = __strerror_r (errnum, errbuf, sizeof errbuf);
Packit 709fb3
# else
Packit 709fb3
  if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
Packit 709fb3
    s = errbuf;
Packit 709fb3
  else
Packit 709fb3
    s = 0;
Packit 709fb3
# endif
Packit 709fb3
#else
Packit 709fb3
  s = strerror (errnum);
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#if !_LIBC
Packit 709fb3
  if (! s)
Packit 709fb3
    s = _("Unknown system error");
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
#if _LIBC
Packit 709fb3
  __fxprintf (NULL, ": %s", s);
Packit 709fb3
#else
Packit 709fb3
  fprintf (stderr, ": %s", s);
Packit 709fb3
#endif
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3))
Packit 709fb3
error_tail (int status, int errnum, const char *message, va_list args)
Packit 709fb3
{
Packit 709fb3
#if _LIBC
Packit 709fb3
  if (_IO_fwide (stderr, 0) > 0)
Packit 709fb3
    {
Packit 709fb3
      size_t len = strlen (message) + 1;
Packit 709fb3
      wchar_t *wmessage = NULL;
Packit 709fb3
      mbstate_t st;
Packit 709fb3
      size_t res;
Packit 709fb3
      const char *tmp;
Packit 709fb3
      bool use_malloc = false;
Packit 709fb3
Packit 709fb3
      while (1)
Packit 709fb3
        {
Packit 709fb3
          if (__libc_use_alloca (len * sizeof (wchar_t)))
Packit 709fb3
            wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
Packit 709fb3
          else
Packit 709fb3
            {
Packit 709fb3
              if (!use_malloc)
Packit 709fb3
                wmessage = NULL;
Packit 709fb3
Packit 709fb3
              wchar_t *p = (wchar_t *) realloc (wmessage,
Packit 709fb3
                                                len * sizeof (wchar_t));
Packit 709fb3
              if (p == NULL)
Packit 709fb3
                {
Packit 709fb3
                  free (wmessage);
Packit 709fb3
                  fputws_unlocked (L"out of memory\n", stderr);
Packit 709fb3
                  return;
Packit 709fb3
                }
Packit 709fb3
              wmessage = p;
Packit 709fb3
              use_malloc = true;
Packit 709fb3
            }
Packit 709fb3
Packit 709fb3
          memset (&st, '\0', sizeof (st));
Packit 709fb3
          tmp = message;
Packit 709fb3
Packit 709fb3
          res = mbsrtowcs (wmessage, &tmp, len, &st);
Packit 709fb3
          if (res != len)
Packit 709fb3
            break;
Packit 709fb3
Packit 709fb3
          if (__builtin_expect (len >= SIZE_MAX / sizeof (wchar_t) / 2, 0))
Packit 709fb3
            {
Packit 709fb3
              /* This really should not happen if everything is fine.  */
Packit 709fb3
              res = (size_t) -1;
Packit 709fb3
              break;
Packit 709fb3
            }
Packit 709fb3
Packit 709fb3
          len *= 2;
Packit 709fb3
        }
Packit 709fb3
Packit 709fb3
      if (res == (size_t) -1)
Packit 709fb3
        {
Packit 709fb3
          /* The string cannot be converted.  */
Packit 709fb3
          if (use_malloc)
Packit 709fb3
            {
Packit 709fb3
              free (wmessage);
Packit 709fb3
              use_malloc = false;
Packit 709fb3
            }
Packit 709fb3
          wmessage = (wchar_t *) L"???";
Packit 709fb3
        }
Packit 709fb3
Packit 709fb3
      __vfwprintf (stderr, wmessage, args);
Packit 709fb3
Packit 709fb3
      if (use_malloc)
Packit 709fb3
        free (wmessage);
Packit 709fb3
    }
Packit 709fb3
  else
Packit 709fb3
#endif
Packit 709fb3
    vfprintf (stderr, message, args);
Packit 709fb3
Packit 709fb3
  ++error_message_count;
Packit 709fb3
  if (errnum)
Packit 709fb3
    print_errno_message (errnum);
Packit 709fb3
#if _LIBC
Packit 709fb3
  __fxprintf (NULL, "\n");
Packit 709fb3
#else
Packit 709fb3
  putc ('\n', stderr);
Packit 709fb3
#endif
Packit 709fb3
  fflush (stderr);
Packit 709fb3
  if (status)
Packit 709fb3
    exit (status);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
Packit 709fb3
/* Print the program name and error message MESSAGE, which is a printf-style
Packit 709fb3
   format string with optional args.
Packit 709fb3
   If ERRNUM is nonzero, print its corresponding system error message.
Packit 709fb3
   Exit with status STATUS if it is nonzero.  */
Packit 709fb3
void
Packit 709fb3
error (int status, int errnum, const char *message, ...)
Packit 709fb3
{
Packit 709fb3
  va_list args;
Packit 709fb3
Packit 709fb3
#if defined _LIBC && defined __libc_ptf_call
Packit 709fb3
  /* We do not want this call to be cut short by a thread
Packit 709fb3
     cancellation.  Therefore disable cancellation for now.  */
Packit 709fb3
  int state = PTHREAD_CANCEL_ENABLE;
Packit 709fb3
  __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
Packit 709fb3
                   0);
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  flush_stdout ();
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
  _IO_flockfile (stderr);
Packit 709fb3
#endif
Packit 709fb3
  if (error_print_progname)
Packit 709fb3
    (*error_print_progname) ();
Packit 709fb3
  else
Packit 709fb3
    {
Packit 709fb3
#if _LIBC
Packit 709fb3
      __fxprintf (NULL, "%s: ", program_name);
Packit 709fb3
#else
Packit 709fb3
      fprintf (stderr, "%s: ", program_name);
Packit 709fb3
#endif
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
  va_start (args, message);
Packit 709fb3
  error_tail (status, errnum, message, args);
Packit 709fb3
  va_end (args);
Packit 709fb3
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
  _IO_funlockfile (stderr);
Packit 709fb3
# ifdef __libc_ptf_call
Packit 709fb3
  __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
Packit 709fb3
# endif
Packit 709fb3
#endif
Packit 709fb3
}
Packit 709fb3

Packit 709fb3
/* Sometimes we want to have at most one error per line.  This
Packit 709fb3
   variable controls whether this mode is selected or not.  */
Packit 709fb3
int error_one_per_line;
Packit 709fb3
Packit 709fb3
void
Packit 709fb3
error_at_line (int status, int errnum, const char *file_name,
Packit 709fb3
               unsigned int line_number, const char *message, ...)
Packit 709fb3
{
Packit 709fb3
  va_list args;
Packit 709fb3
Packit 709fb3
  if (error_one_per_line)
Packit 709fb3
    {
Packit 709fb3
      static const char *old_file_name;
Packit 709fb3
      static unsigned int old_line_number;
Packit 709fb3
Packit 709fb3
      if (old_line_number == line_number
Packit 709fb3
          && (file_name == old_file_name
Packit 709fb3
              || (old_file_name != NULL
Packit 709fb3
                  && file_name != NULL
Packit 709fb3
                  && strcmp (old_file_name, file_name) == 0)))
Packit 709fb3
Packit 709fb3
        /* Simply return and print nothing.  */
Packit 709fb3
        return;
Packit 709fb3
Packit 709fb3
      old_file_name = file_name;
Packit 709fb3
      old_line_number = line_number;
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
#if defined _LIBC && defined __libc_ptf_call
Packit 709fb3
  /* We do not want this call to be cut short by a thread
Packit 709fb3
     cancellation.  Therefore disable cancellation for now.  */
Packit 709fb3
  int state = PTHREAD_CANCEL_ENABLE;
Packit 709fb3
  __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
Packit 709fb3
                   0);
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  flush_stdout ();
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
  _IO_flockfile (stderr);
Packit 709fb3
#endif
Packit 709fb3
  if (error_print_progname)
Packit 709fb3
    (*error_print_progname) ();
Packit 709fb3
  else
Packit 709fb3
    {
Packit 709fb3
#if _LIBC
Packit 709fb3
      __fxprintf (NULL, "%s:", program_name);
Packit 709fb3
#else
Packit 709fb3
      fprintf (stderr, "%s:", program_name);
Packit 709fb3
#endif
Packit 709fb3
    }
Packit 709fb3
Packit 709fb3
#if _LIBC
Packit 709fb3
  __fxprintf (NULL, file_name != NULL ? "%s:%u: " : " ",
Packit 709fb3
              file_name, line_number);
Packit 709fb3
#else
Packit 709fb3
  fprintf (stderr, file_name != NULL ? "%s:%u: " : " ",
Packit 709fb3
           file_name, line_number);
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
  va_start (args, message);
Packit 709fb3
  error_tail (status, errnum, message, args);
Packit 709fb3
  va_end (args);
Packit 709fb3
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
  _IO_funlockfile (stderr);
Packit 709fb3
# ifdef __libc_ptf_call
Packit 709fb3
  __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
Packit 709fb3
# endif
Packit 709fb3
#endif
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#ifdef _LIBC
Packit 709fb3
/* Make the weak alias.  */
Packit 709fb3
# undef error
Packit 709fb3
# undef error_at_line
Packit 709fb3
weak_alias (__error, error)
Packit 709fb3
weak_alias (__error_at_line, error_at_line)
Packit 709fb3
#endif