error.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000-2007, 2009-2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  15. #if !_LIBC
  16. # include <config.h>
  17. #endif
  18. #include "error.h"
  19. #include <stdarg.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #if !_LIBC && ENABLE_NLS
  24. # include "gettext.h"
  25. # define _(msgid) gettext (msgid)
  26. #endif
  27. #ifdef _LIBC
  28. # include <libintl.h>
  29. # include <stdbool.h>
  30. # include <stdint.h>
  31. # include <wchar.h>
  32. # define mbsrtowcs __mbsrtowcs
  33. # define USE_UNLOCKED_IO 0
  34. # define _GL_ATTRIBUTE_FORMAT_PRINTF(a, b)
  35. # define _GL_ARG_NONNULL(a)
  36. #else
  37. # include "getprogname.h"
  38. #endif
  39. #if USE_UNLOCKED_IO
  40. # include "unlocked-io.h"
  41. #endif
  42. #ifndef _
  43. # define _(String) String
  44. #endif
  45. /* If NULL, error will flush stdout, then print on stderr the program
  46. name, a colon and a space. Otherwise, error will call this
  47. function without parameters instead. */
  48. void (*error_print_progname) (void);
  49. /* This variable is incremented each time 'error' is called. */
  50. unsigned int error_message_count;
  51. #ifdef _LIBC
  52. /* In the GNU C library, there is a predefined variable for this. */
  53. # define program_name program_invocation_name
  54. # include <errno.h>
  55. # include <limits.h>
  56. # error #include <libio/libioP.h>
  57. /* In GNU libc we want do not want to use the common name 'error' directly.
  58. Instead make it a weak alias. */
  59. extern void __error (int status, int errnum, const char *message, ...)
  60. __attribute__ ((__format__ (__printf__, 3, 4)));
  61. extern void __error_at_line (int status, int errnum, const char *file_name,
  62. unsigned int line_number, const char *message,
  63. ...)
  64. __attribute__ ((__format__ (__printf__, 5, 6)));
  65. # define error __error
  66. # define error_at_line __error_at_line
  67. # error #include <libio/iolibio.h>
  68. # define fflush(s) _IO_fflush (s)
  69. # undef putc
  70. # define putc(c, fp) _IO_putc (c, fp)
  71. # error #include <bits/libc-lock.h>
  72. #else /* not _LIBC */
  73. # include <fcntl.h>
  74. # include <unistd.h>
  75. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  76. /* Get declarations of the native Windows API functions. */
  77. # define WIN32_LEAN_AND_MEAN
  78. # include <windows.h>
  79. /* Get _get_osfhandle. */
  80. # include "msvc-nothrow.h"
  81. # endif
  82. /* The gnulib override of fcntl is not needed in this file. */
  83. # undef fcntl
  84. # if !HAVE_DECL_STRERROR_R
  85. # ifndef HAVE_DECL_STRERROR_R
  86. "this configure-time declaration test was not run"
  87. # endif
  88. # if STRERROR_R_CHAR_P
  89. char *strerror_r ();
  90. # else
  91. int strerror_r ();
  92. # endif
  93. # endif
  94. #define program_name getprogname ()
  95. # if HAVE_STRERROR_R || defined strerror_r
  96. # define __strerror_r strerror_r
  97. # endif /* HAVE_STRERROR_R || defined strerror_r */
  98. #endif /* not _LIBC */
  99. #if !_LIBC
  100. /* Return non-zero if FD is open. */
  101. static int
  102. is_open (int fd)
  103. {
  104. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  105. /* On native Windows: The initial state of unassigned standard file
  106. descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
  107. There is no fcntl, and the gnulib replacement fcntl does not support
  108. F_GETFL. */
  109. return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
  110. # else
  111. # ifndef F_GETFL
  112. # error Please port fcntl to your platform
  113. # endif
  114. return 0 <= fcntl (fd, F_GETFL);
  115. # endif
  116. }
  117. #endif
  118. static void
  119. flush_stdout (void)
  120. {
  121. #if !_LIBC
  122. int stdout_fd;
  123. # if GNULIB_FREOPEN_SAFER
  124. /* Use of gnulib's freopen-safer module normally ensures that
  125. fileno (stdout) == 1
  126. whenever stdout is open. */
  127. stdout_fd = STDOUT_FILENO;
  128. # else
  129. /* POSIX states that fileno (stdout) after fclose is unspecified. But in
  130. practice it is not a problem, because stdout is statically allocated and
  131. the fd of a FILE stream is stored as a field in its allocated memory. */
  132. stdout_fd = fileno (stdout);
  133. # endif
  134. /* POSIX states that fflush (stdout) after fclose is unspecified; it
  135. is safe in glibc, but not on all other platforms. fflush (NULL)
  136. is always defined, but too draconian. */
  137. if (0 <= stdout_fd && is_open (stdout_fd))
  138. #endif
  139. fflush (stdout);
  140. }
  141. static void
  142. print_errno_message (int errnum)
  143. {
  144. char const *s;
  145. #if defined HAVE_STRERROR_R || _LIBC
  146. char errbuf[1024];
  147. # if _LIBC || STRERROR_R_CHAR_P
  148. s = __strerror_r (errnum, errbuf, sizeof errbuf);
  149. # else
  150. if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
  151. s = errbuf;
  152. else
  153. s = 0;
  154. # endif
  155. #else
  156. s = strerror (errnum);
  157. #endif
  158. #if !_LIBC
  159. if (! s)
  160. s = _("Unknown system error");
  161. #endif
  162. #if _LIBC
  163. __fxprintf (NULL, ": %s", s);
  164. #else
  165. fprintf (stderr, ": %s", s);
  166. #endif
  167. }
  168. static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3))
  169. error_tail (int status, int errnum, const char *message, va_list args)
  170. {
  171. #if _LIBC
  172. if (_IO_fwide (stderr, 0) > 0)
  173. {
  174. size_t len = strlen (message) + 1;
  175. wchar_t *wmessage = NULL;
  176. mbstate_t st;
  177. size_t res;
  178. const char *tmp;
  179. bool use_malloc = false;
  180. while (1)
  181. {
  182. if (__libc_use_alloca (len * sizeof (wchar_t)))
  183. wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
  184. else
  185. {
  186. if (!use_malloc)
  187. wmessage = NULL;
  188. wchar_t *p = (wchar_t *) realloc (wmessage,
  189. len * sizeof (wchar_t));
  190. if (p == NULL)
  191. {
  192. free (wmessage);
  193. fputws_unlocked (L"out of memory\n", stderr);
  194. return;
  195. }
  196. wmessage = p;
  197. use_malloc = true;
  198. }
  199. memset (&st, '\0', sizeof (st));
  200. tmp = message;
  201. res = mbsrtowcs (wmessage, &tmp, len, &st);
  202. if (res != len)
  203. break;
  204. if (__builtin_expect (len >= SIZE_MAX / sizeof (wchar_t) / 2, 0))
  205. {
  206. /* This really should not happen if everything is fine. */
  207. res = (size_t) -1;
  208. break;
  209. }
  210. len *= 2;
  211. }
  212. if (res == (size_t) -1)
  213. {
  214. /* The string cannot be converted. */
  215. if (use_malloc)
  216. {
  217. free (wmessage);
  218. use_malloc = false;
  219. }
  220. wmessage = (wchar_t *) L"???";
  221. }
  222. __vfwprintf (stderr, wmessage, args);
  223. if (use_malloc)
  224. free (wmessage);
  225. }
  226. else
  227. #endif
  228. vfprintf (stderr, message, args);
  229. va_end (args);
  230. ++error_message_count;
  231. if (errnum)
  232. print_errno_message (errnum);
  233. #if _LIBC
  234. __fxprintf (NULL, "\n");
  235. #else
  236. putc ('\n', stderr);
  237. #endif
  238. fflush (stderr);
  239. if (status)
  240. exit (status);
  241. }
  242. /* Print the program name and error message MESSAGE, which is a printf-style
  243. format string with optional args.
  244. If ERRNUM is nonzero, print its corresponding system error message.
  245. Exit with status STATUS if it is nonzero. */
  246. void
  247. error (int status, int errnum, const char *message, ...)
  248. {
  249. va_list args;
  250. #if defined _LIBC && defined __libc_ptf_call
  251. /* We do not want this call to be cut short by a thread
  252. cancellation. Therefore disable cancellation for now. */
  253. int state = PTHREAD_CANCEL_ENABLE;
  254. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  255. 0);
  256. #endif
  257. flush_stdout ();
  258. #ifdef _LIBC
  259. _IO_flockfile (stderr);
  260. #endif
  261. if (error_print_progname)
  262. (*error_print_progname) ();
  263. else
  264. {
  265. #if _LIBC
  266. __fxprintf (NULL, "%s: ", program_name);
  267. #else
  268. fprintf (stderr, "%s: ", program_name);
  269. #endif
  270. }
  271. va_start (args, message);
  272. error_tail (status, errnum, message, args);
  273. #ifdef _LIBC
  274. _IO_funlockfile (stderr);
  275. # ifdef __libc_ptf_call
  276. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  277. # endif
  278. #endif
  279. }
  280. /* Sometimes we want to have at most one error per line. This
  281. variable controls whether this mode is selected or not. */
  282. int error_one_per_line;
  283. void
  284. error_at_line (int status, int errnum, const char *file_name,
  285. unsigned int line_number, const char *message, ...)
  286. {
  287. va_list args;
  288. if (error_one_per_line)
  289. {
  290. static const char *old_file_name;
  291. static unsigned int old_line_number;
  292. if (old_line_number == line_number
  293. && (file_name == old_file_name
  294. || (old_file_name != NULL
  295. && file_name != NULL
  296. && strcmp (old_file_name, file_name) == 0)))
  297. /* Simply return and print nothing. */
  298. return;
  299. old_file_name = file_name;
  300. old_line_number = line_number;
  301. }
  302. #if defined _LIBC && defined __libc_ptf_call
  303. /* We do not want this call to be cut short by a thread
  304. cancellation. Therefore disable cancellation for now. */
  305. int state = PTHREAD_CANCEL_ENABLE;
  306. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  307. 0);
  308. #endif
  309. flush_stdout ();
  310. #ifdef _LIBC
  311. _IO_flockfile (stderr);
  312. #endif
  313. if (error_print_progname)
  314. (*error_print_progname) ();
  315. else
  316. {
  317. #if _LIBC
  318. __fxprintf (NULL, "%s:", program_name);
  319. #else
  320. fprintf (stderr, "%s:", program_name);
  321. #endif
  322. }
  323. #if _LIBC
  324. __fxprintf (NULL, file_name != NULL ? "%s:%u: " : " ",
  325. file_name, line_number);
  326. #else
  327. fprintf (stderr, file_name != NULL ? "%s:%u: " : " ",
  328. file_name, line_number);
  329. #endif
  330. va_start (args, message);
  331. error_tail (status, errnum, message, args);
  332. #ifdef _LIBC
  333. _IO_funlockfile (stderr);
  334. # ifdef __libc_ptf_call
  335. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  336. # endif
  337. #endif
  338. }
  339. #ifdef _LIBC
  340. /* Make the weak alias. */
  341. # undef error
  342. # undef error_at_line
  343. weak_alias (__error, error)
  344. weak_alias (__error_at_line, error_at_line)
  345. #endif