error.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000-2007, 2009-2020 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 <https://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. # 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. # include <libio/iolibio.h>
  68. # define fflush(s) _IO_fflush (s)
  69. # undef putc
  70. # define putc(c, fp) _IO_putc (c, fp)
  71. # include <bits/libc-lock.h>
  72. #else /* not _LIBC */
  73. # include <fcntl.h>
  74. # include <unistd.h>
  75. # if 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. # if GNULIB_MSVC_NOTHROW
  81. # include "msvc-nothrow.h"
  82. # else
  83. # include <io.h>
  84. # endif
  85. # endif
  86. /* The gnulib override of fcntl is not needed in this file. */
  87. # undef fcntl
  88. # if !(GNULIB_STRERROR_R_POSIX || HAVE_DECL_STRERROR_R)
  89. # ifndef HAVE_DECL_STRERROR_R
  90. "this configure-time declaration test was not run"
  91. # endif
  92. # if STRERROR_R_CHAR_P
  93. char *strerror_r (int errnum, char *buf, size_t buflen);
  94. # else
  95. int strerror_r (int errnum, char *buf, size_t buflen);
  96. # endif
  97. # endif
  98. # define program_name getprogname ()
  99. # if GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r
  100. # define __strerror_r strerror_r
  101. # endif /* GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r */
  102. #endif /* not _LIBC */
  103. #if !_LIBC
  104. /* Return non-zero if FD is open. */
  105. static int
  106. is_open (int fd)
  107. {
  108. # if defined _WIN32 && ! defined __CYGWIN__
  109. /* On native Windows: The initial state of unassigned standard file
  110. descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
  111. There is no fcntl, and the gnulib replacement fcntl does not support
  112. F_GETFL. */
  113. return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
  114. # else
  115. # ifndef F_GETFL
  116. # error Please port fcntl to your platform
  117. # endif
  118. return 0 <= fcntl (fd, F_GETFL);
  119. # endif
  120. }
  121. #endif
  122. static void
  123. flush_stdout (void)
  124. {
  125. #if !_LIBC
  126. int stdout_fd;
  127. # if GNULIB_FREOPEN_SAFER
  128. /* Use of gnulib's freopen-safer module normally ensures that
  129. fileno (stdout) == 1
  130. whenever stdout is open. */
  131. stdout_fd = STDOUT_FILENO;
  132. # else
  133. /* POSIX states that fileno (stdout) after fclose is unspecified. But in
  134. practice it is not a problem, because stdout is statically allocated and
  135. the fd of a FILE stream is stored as a field in its allocated memory. */
  136. stdout_fd = fileno (stdout);
  137. # endif
  138. /* POSIX states that fflush (stdout) after fclose is unspecified; it
  139. is safe in glibc, but not on all other platforms. fflush (NULL)
  140. is always defined, but too draconian. */
  141. if (0 <= stdout_fd && is_open (stdout_fd))
  142. #endif
  143. fflush (stdout);
  144. }
  145. static void
  146. print_errno_message (int errnum)
  147. {
  148. char const *s;
  149. #if _LIBC || GNULIB_STRERROR_R_POSIX || defined HAVE_STRERROR_R
  150. char errbuf[1024];
  151. # if _LIBC || (!GNULIB_STRERROR_R_POSIX && STRERROR_R_CHAR_P)
  152. s = __strerror_r (errnum, errbuf, sizeof errbuf);
  153. # else
  154. if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
  155. s = errbuf;
  156. else
  157. s = 0;
  158. # endif
  159. #else
  160. s = strerror (errnum);
  161. #endif
  162. #if !_LIBC
  163. if (! s)
  164. s = _("Unknown system error");
  165. #endif
  166. #if _LIBC
  167. __fxprintf (NULL, ": %s", s);
  168. #else
  169. fprintf (stderr, ": %s", s);
  170. #endif
  171. }
  172. static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3))
  173. error_tail (int status, int errnum, const char *message, va_list args)
  174. {
  175. #if _LIBC
  176. if (_IO_fwide (stderr, 0) > 0)
  177. {
  178. size_t len = strlen (message) + 1;
  179. wchar_t *wmessage = NULL;
  180. mbstate_t st;
  181. size_t res;
  182. const char *tmp;
  183. bool use_malloc = false;
  184. while (1)
  185. {
  186. if (__libc_use_alloca (len * sizeof (wchar_t)))
  187. wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
  188. else
  189. {
  190. if (!use_malloc)
  191. wmessage = NULL;
  192. wchar_t *p = (wchar_t *) realloc (wmessage,
  193. len * sizeof (wchar_t));
  194. if (p == NULL)
  195. {
  196. free (wmessage);
  197. fputws_unlocked (L"out of memory\n", stderr);
  198. return;
  199. }
  200. wmessage = p;
  201. use_malloc = true;
  202. }
  203. memset (&st, '\0', sizeof (st));
  204. tmp = message;
  205. res = mbsrtowcs (wmessage, &tmp, len, &st);
  206. if (res != len)
  207. break;
  208. if (__builtin_expect (len >= SIZE_MAX / sizeof (wchar_t) / 2, 0))
  209. {
  210. /* This really should not happen if everything is fine. */
  211. res = (size_t) -1;
  212. break;
  213. }
  214. len *= 2;
  215. }
  216. if (res == (size_t) -1)
  217. {
  218. /* The string cannot be converted. */
  219. if (use_malloc)
  220. {
  221. free (wmessage);
  222. use_malloc = false;
  223. }
  224. wmessage = (wchar_t *) L"???";
  225. }
  226. __vfwprintf (stderr, wmessage, args);
  227. if (use_malloc)
  228. free (wmessage);
  229. }
  230. else
  231. #endif
  232. vfprintf (stderr, message, args);
  233. ++error_message_count;
  234. if (errnum)
  235. print_errno_message (errnum);
  236. #if _LIBC
  237. __fxprintf (NULL, "\n");
  238. #else
  239. putc ('\n', stderr);
  240. #endif
  241. fflush (stderr);
  242. if (status)
  243. exit (status);
  244. }
  245. /* Print the program name and error message MESSAGE, which is a printf-style
  246. format string with optional args.
  247. If ERRNUM is nonzero, print its corresponding system error message.
  248. Exit with status STATUS if it is nonzero. */
  249. void
  250. error (int status, int errnum, const char *message, ...)
  251. {
  252. va_list args;
  253. #if defined _LIBC && defined __libc_ptf_call
  254. /* We do not want this call to be cut short by a thread
  255. cancellation. Therefore disable cancellation for now. */
  256. int state = PTHREAD_CANCEL_ENABLE;
  257. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  258. 0);
  259. #endif
  260. flush_stdout ();
  261. #ifdef _LIBC
  262. _IO_flockfile (stderr);
  263. #endif
  264. if (error_print_progname)
  265. (*error_print_progname) ();
  266. else
  267. {
  268. #if _LIBC
  269. __fxprintf (NULL, "%s: ", program_name);
  270. #else
  271. fprintf (stderr, "%s: ", program_name);
  272. #endif
  273. }
  274. va_start (args, message);
  275. error_tail (status, errnum, message, args);
  276. va_end (args);
  277. #ifdef _LIBC
  278. _IO_funlockfile (stderr);
  279. # ifdef __libc_ptf_call
  280. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  281. # endif
  282. #endif
  283. }
  284. /* Sometimes we want to have at most one error per line. This
  285. variable controls whether this mode is selected or not. */
  286. int error_one_per_line;
  287. void
  288. error_at_line (int status, int errnum, const char *file_name,
  289. unsigned int line_number, const char *message, ...)
  290. {
  291. va_list args;
  292. if (error_one_per_line)
  293. {
  294. static const char *old_file_name;
  295. static unsigned int old_line_number;
  296. if (old_line_number == line_number
  297. && (file_name == old_file_name
  298. || (old_file_name != NULL
  299. && file_name != NULL
  300. && strcmp (old_file_name, file_name) == 0)))
  301. /* Simply return and print nothing. */
  302. return;
  303. old_file_name = file_name;
  304. old_line_number = line_number;
  305. }
  306. #if defined _LIBC && defined __libc_ptf_call
  307. /* We do not want this call to be cut short by a thread
  308. cancellation. Therefore disable cancellation for now. */
  309. int state = PTHREAD_CANCEL_ENABLE;
  310. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  311. 0);
  312. #endif
  313. flush_stdout ();
  314. #ifdef _LIBC
  315. _IO_flockfile (stderr);
  316. #endif
  317. if (error_print_progname)
  318. (*error_print_progname) ();
  319. else
  320. {
  321. #if _LIBC
  322. __fxprintf (NULL, "%s:", program_name);
  323. #else
  324. fprintf (stderr, "%s:", program_name);
  325. #endif
  326. }
  327. #if _LIBC
  328. __fxprintf (NULL, file_name != NULL ? "%s:%u: " : " ",
  329. file_name, line_number);
  330. #else
  331. fprintf (stderr, file_name != NULL ? "%s:%u: " : " ",
  332. file_name, line_number);
  333. #endif
  334. va_start (args, message);
  335. error_tail (status, errnum, message, args);
  336. va_end (args);
  337. #ifdef _LIBC
  338. _IO_funlockfile (stderr);
  339. # ifdef __libc_ptf_call
  340. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  341. # endif
  342. #endif
  343. }
  344. #ifdef _LIBC
  345. /* Make the weak alias. */
  346. # undef error
  347. # undef error_at_line
  348. weak_alias (__error, error)
  349. weak_alias (__error_at_line, error_at_line)
  350. #endif