Back to index

nagios-plugins  1.4.16
error.c
Go to the documentation of this file.
00001 /* Error handler for noninteractive utilities
00002    Copyright (C) 1990-1998, 2000-2007, 2009-2010 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    This program is free software: you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 3 of the License, or
00008    (at your option) any later version.
00009 
00010    This program 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
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00017 
00018 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
00019 
00020 #if !_LIBC
00021 # include <config.h>
00022 #endif
00023 
00024 #include "error.h"
00025 
00026 #include <stdarg.h>
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #if !_LIBC && ENABLE_NLS
00032 # include "gettext.h"
00033 # define _(msgid) gettext (msgid)
00034 #endif
00035 
00036 #ifdef _LIBC
00037 # include <libintl.h>
00038 # include <stdbool.h>
00039 # include <stdint.h>
00040 # include <wchar.h>
00041 # define mbsrtowcs __mbsrtowcs
00042 #endif
00043 
00044 #if USE_UNLOCKED_IO
00045 # include "unlocked-io.h"
00046 #endif
00047 
00048 #ifndef _
00049 # define _(String) String
00050 #endif
00051 
00052 /* If NULL, error will flush stdout, then print on stderr the program
00053    name, a colon and a space.  Otherwise, error will call this
00054    function without parameters instead.  */
00055 void (*error_print_progname) (void);
00056 
00057 /* This variable is incremented each time `error' is called.  */
00058 unsigned int error_message_count;
00059 
00060 #ifdef _LIBC
00061 /* In the GNU C library, there is a predefined variable for this.  */
00062 
00063 # define program_name program_invocation_name
00064 # include <errno.h>
00065 # include <limits.h>
00066 # include <libio/libioP.h>
00067 
00068 /* In GNU libc we want do not want to use the common name `error' directly.
00069    Instead make it a weak alias.  */
00070 extern void __error (int status, int errnum, const char *message, ...)
00071      __attribute__ ((__format__ (__printf__, 3, 4)));
00072 extern void __error_at_line (int status, int errnum, const char *file_name,
00073                              unsigned int line_number, const char *message,
00074                              ...)
00075      __attribute__ ((__format__ (__printf__, 5, 6)));;
00076 # define error __error
00077 # define error_at_line __error_at_line
00078 
00079 # include <libio/iolibio.h>
00080 # define fflush(s) INTUSE(_IO_fflush) (s)
00081 # undef putc
00082 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
00083 
00084 # include <bits/libc-lock.h>
00085 
00086 #else /* not _LIBC */
00087 
00088 # include <fcntl.h>
00089 # include <unistd.h>
00090 
00091 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
00092 /* Get declarations of the Win32 API functions.  */
00093 #  define WIN32_LEAN_AND_MEAN
00094 #  include <windows.h>
00095 # endif
00096 
00097 /* The gnulib override of fcntl is not needed in this file.  */
00098 # undef fcntl
00099 
00100 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
00101 #  ifndef HAVE_DECL_STRERROR_R
00102 "this configure-time declaration test was not run"
00103 #  endif
00104 char *strerror_r ();
00105 # endif
00106 
00107 /* The calling program should define program_name and set it to the
00108    name of the executing program.  */
00109 extern char *program_name;
00110 
00111 # if HAVE_STRERROR_R || defined strerror_r
00112 #  define __strerror_r strerror_r
00113 # endif /* HAVE_STRERROR_R || defined strerror_r */
00114 #endif  /* not _LIBC */
00115 
00116 #if !_LIBC
00117 /* Return non-zero if FD is open.  */
00118 static inline int
00119 is_open (int fd)
00120 {
00121 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
00122   /* On Win32: The initial state of unassigned standard file descriptors is
00123      that they are open but point to an INVALID_HANDLE_VALUE.  There is no
00124      fcntl, and the gnulib replacement fcntl does not support F_GETFL.  */
00125   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
00126 # else
00127 #  ifndef F_GETFL
00128 #   error Please port fcntl to your platform
00129 #  endif
00130   return 0 <= fcntl (fd, F_GETFL);
00131 # endif
00132 }
00133 #endif
00134 
00135 static inline void
00136 flush_stdout (void)
00137 {
00138 #if !_LIBC
00139   int stdout_fd;
00140 
00141 # if GNULIB_FREOPEN_SAFER
00142   /* Use of gnulib's freopen-safer module normally ensures that
00143        fileno (stdout) == 1
00144      whenever stdout is open.  */
00145   stdout_fd = STDOUT_FILENO;
00146 # else
00147   /* POSIX states that fileno (stdout) after fclose is unspecified.  But in
00148      practice it is not a problem, because stdout is statically allocated and
00149      the fd of a FILE stream is stored as a field in its allocated memory.  */
00150   stdout_fd = fileno (stdout);
00151 # endif
00152   /* POSIX states that fflush (stdout) after fclose is unspecified; it
00153      is safe in glibc, but not on all other platforms.  fflush (NULL)
00154      is always defined, but too draconian.  */
00155   if (0 <= stdout_fd && is_open (stdout_fd))
00156 #endif
00157     fflush (stdout);
00158 }
00159 
00160 static void
00161 print_errno_message (int errnum)
00162 {
00163   char const *s;
00164 
00165 #if defined HAVE_STRERROR_R || _LIBC
00166   char errbuf[1024];
00167 # if STRERROR_R_CHAR_P || _LIBC
00168   s = __strerror_r (errnum, errbuf, sizeof errbuf);
00169 # else
00170   if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
00171     s = errbuf;
00172   else
00173     s = 0;
00174 # endif
00175 #else
00176   s = strerror (errnum);
00177 #endif
00178 
00179 #if !_LIBC
00180   if (! s)
00181     s = _("Unknown system error");
00182 #endif
00183 
00184 #if _LIBC
00185   __fxprintf (NULL, ": %s", s);
00186 #else
00187   fprintf (stderr, ": %s", s);
00188 #endif
00189 }
00190 
00191 static void
00192 error_tail (int status, int errnum, const char *message, va_list args)
00193 {
00194 #if _LIBC
00195   if (_IO_fwide (stderr, 0) > 0)
00196     {
00197 # define ALLOCA_LIMIT 2000
00198       size_t len = strlen (message) + 1;
00199       wchar_t *wmessage = NULL;
00200       mbstate_t st;
00201       size_t res;
00202       const char *tmp;
00203       bool use_malloc = false;
00204 
00205       while (1)
00206         {
00207           if (__libc_use_alloca (len * sizeof (wchar_t)))
00208             wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
00209           else
00210             {
00211               if (!use_malloc)
00212                 wmessage = NULL;
00213 
00214               wchar_t *p = (wchar_t *) realloc (wmessage,
00215                                                 len * sizeof (wchar_t));
00216               if (p == NULL)
00217                 {
00218                   free (wmessage);
00219                   fputws_unlocked (L"out of memory\n", stderr);
00220                   return;
00221                 }
00222               wmessage = p;
00223               use_malloc = true;
00224             }
00225 
00226           memset (&st, '\0', sizeof (st));
00227           tmp = message;
00228 
00229           res = mbsrtowcs (wmessage, &tmp, len, &st);
00230           if (res != len)
00231             break;
00232 
00233           if (__builtin_expect (len >= SIZE_MAX / 2, 0))
00234             {
00235               /* This really should not happen if everything is fine.  */
00236               res = (size_t) -1;
00237               break;
00238             }
00239 
00240           len *= 2;
00241         }
00242 
00243       if (res == (size_t) -1)
00244         {
00245           /* The string cannot be converted.  */
00246           if (use_malloc)
00247             {
00248               free (wmessage);
00249               use_malloc = false;
00250             }
00251           wmessage = (wchar_t *) L"???";
00252         }
00253 
00254       __vfwprintf (stderr, wmessage, args);
00255 
00256       if (use_malloc)
00257         free (wmessage);
00258     }
00259   else
00260 #endif
00261     vfprintf (stderr, message, args);
00262   va_end (args);
00263 
00264   ++error_message_count;
00265   if (errnum)
00266     print_errno_message (errnum);
00267 #if _LIBC
00268   __fxprintf (NULL, "\n");
00269 #else
00270   putc ('\n', stderr);
00271 #endif
00272   fflush (stderr);
00273   if (status)
00274     exit (status);
00275 }
00276 
00277 
00278 /* Print the program name and error message MESSAGE, which is a printf-style
00279    format string with optional args.
00280    If ERRNUM is nonzero, print its corresponding system error message.
00281    Exit with status STATUS if it is nonzero.  */
00282 void
00283 error (int status, int errnum, const char *message, ...)
00284 {
00285   va_list args;
00286 
00287 #if defined _LIBC && defined __libc_ptf_call
00288   /* We do not want this call to be cut short by a thread
00289      cancellation.  Therefore disable cancellation for now.  */
00290   int state = PTHREAD_CANCEL_ENABLE;
00291   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
00292                    0);
00293 #endif
00294 
00295   flush_stdout ();
00296 #ifdef _LIBC
00297   _IO_flockfile (stderr);
00298 #endif
00299   if (error_print_progname)
00300     (*error_print_progname) ();
00301   else
00302     {
00303 #if _LIBC
00304       __fxprintf (NULL, "%s: ", program_name);
00305 #else
00306       fprintf (stderr, "%s: ", program_name);
00307 #endif
00308     }
00309 
00310   va_start (args, message);
00311   error_tail (status, errnum, message, args);
00312 
00313 #ifdef _LIBC
00314   _IO_funlockfile (stderr);
00315 # ifdef __libc_ptf_call
00316   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
00317 # endif
00318 #endif
00319 }
00320 
00321 /* Sometimes we want to have at most one error per line.  This
00322    variable controls whether this mode is selected or not.  */
00323 int error_one_per_line;
00324 
00325 void
00326 error_at_line (int status, int errnum, const char *file_name,
00327                unsigned int line_number, const char *message, ...)
00328 {
00329   va_list args;
00330 
00331   if (error_one_per_line)
00332     {
00333       static const char *old_file_name;
00334       static unsigned int old_line_number;
00335 
00336       if (old_line_number == line_number
00337           && (file_name == old_file_name
00338               || strcmp (old_file_name, file_name) == 0))
00339         /* Simply return and print nothing.  */
00340         return;
00341 
00342       old_file_name = file_name;
00343       old_line_number = line_number;
00344     }
00345 
00346 #if defined _LIBC && defined __libc_ptf_call
00347   /* We do not want this call to be cut short by a thread
00348      cancellation.  Therefore disable cancellation for now.  */
00349   int state = PTHREAD_CANCEL_ENABLE;
00350   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
00351                    0);
00352 #endif
00353 
00354   flush_stdout ();
00355 #ifdef _LIBC
00356   _IO_flockfile (stderr);
00357 #endif
00358   if (error_print_progname)
00359     (*error_print_progname) ();
00360   else
00361     {
00362 #if _LIBC
00363       __fxprintf (NULL, "%s:", program_name);
00364 #else
00365       fprintf (stderr, "%s:", program_name);
00366 #endif
00367     }
00368 
00369 #if _LIBC
00370   __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
00371               file_name, line_number);
00372 #else
00373   fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
00374            file_name, line_number);
00375 #endif
00376 
00377   va_start (args, message);
00378   error_tail (status, errnum, message, args);
00379 
00380 #ifdef _LIBC
00381   _IO_funlockfile (stderr);
00382 # ifdef __libc_ptf_call
00383   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
00384 # endif
00385 #endif
00386 }
00387 
00388 #ifdef _LIBC
00389 /* Make the weak alias.  */
00390 # undef error
00391 # undef error_at_line
00392 weak_alias (__error, error)
00393 weak_alias (__error_at_line, error_at_line)
00394 #endif