closeout.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Close standard output and standard error, exiting with a diagnostic on error.
  2. Copyright (C) 1998-2002, 2004, 2006, 2008-2020 Free Software Foundation,
  3. Inc.
  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. #include <config.h>
  15. #include "closeout.h"
  16. #include <errno.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include "gettext.h"
  21. #define _(msgid) gettext (msgid)
  22. #include "close-stream.h"
  23. #include "error.h"
  24. #include "exitfail.h"
  25. #include "quotearg.h"
  26. #ifndef __has_feature
  27. # define __has_feature(a) false
  28. #endif
  29. #if defined __SANITIZE_ADDRESS__ || __has_feature (address_sanitizer)
  30. enum { SANITIZE_ADDRESS = true };
  31. #else
  32. enum { SANITIZE_ADDRESS = false };
  33. #endif
  34. static const char *file_name;
  35. /* Set the file name to be reported in the event an error is detected
  36. by close_stdout. */
  37. void
  38. close_stdout_set_file_name (const char *file)
  39. {
  40. file_name = file;
  41. }
  42. static bool ignore_EPIPE /* = false */;
  43. /* Specify the reaction to an EPIPE error during the closing of stdout:
  44. - If ignore = true, it shall be ignored.
  45. - If ignore = false, it shall evoke a diagnostic, along with a nonzero
  46. exit status.
  47. The default is ignore = false.
  48. This setting matters only if the SIGPIPE signal is ignored (i.e. its
  49. handler set to SIG_IGN) or blocked. Only particular programs need to
  50. temporarily ignore SIGPIPE. If SIGPIPE is ignored or blocked because
  51. it was ignored or blocked in the parent process when it created the
  52. child process, it usually is a bug in the parent process: It is bad
  53. practice to have SIGPIPE ignored or blocked while creating a child
  54. process.
  55. EPIPE occurs when writing to a pipe or socket that has no readers now,
  56. when SIGPIPE is ignored or blocked.
  57. The ignore = false setting is suitable for a scenario where it is normally
  58. guaranteed that the pipe writer terminates before the pipe reader. In
  59. this case, an EPIPE is an indication of a premature termination of the
  60. pipe reader and should lead to a diagnostic and a nonzero exit status.
  61. The ignore = true setting is suitable for a scenario where you don't know
  62. ahead of time whether the pipe writer or the pipe reader will terminate
  63. first. In this case, an EPIPE is an indication that the pipe writer can
  64. stop doing useless write() calls; this is what close_stdout does anyway.
  65. EPIPE is part of the normal pipe/socket shutdown protocol in this case,
  66. and should not lead to a diagnostic message. */
  67. void
  68. close_stdout_set_ignore_EPIPE (bool ignore)
  69. {
  70. ignore_EPIPE = ignore;
  71. }
  72. /* Close standard output. On error, issue a diagnostic and _exit
  73. with status 'exit_failure'.
  74. Also close standard error. On error, _exit with status 'exit_failure'.
  75. Since close_stdout is commonly registered via 'atexit', POSIX
  76. and the C standard both say that it should not call 'exit',
  77. because the behavior is undefined if 'exit' is called more than
  78. once. So it calls '_exit' instead of 'exit'. If close_stdout
  79. is registered via atexit before other functions are registered,
  80. the other functions can act before this _exit is invoked.
  81. Applications that use close_stdout should flush any streams
  82. other than stdout and stderr before exiting, since the call to
  83. _exit will bypass other buffer flushing. Applications should
  84. be flushing and closing other streams anyway, to check for I/O
  85. errors. Also, applications should not use tmpfile, since _exit
  86. can bypass the removal of these files.
  87. It's important to detect such failures and exit nonzero because many
  88. tools (most notably 'make' and other build-management systems) depend
  89. on being able to detect failure in other tools via their exit status. */
  90. void
  91. close_stdout (void)
  92. {
  93. if (close_stream (stdout) != 0
  94. && !(ignore_EPIPE && errno == EPIPE))
  95. {
  96. char const *write_error = _("write error");
  97. if (file_name)
  98. error (0, errno, "%s: %s", quotearg_colon (file_name),
  99. write_error);
  100. else
  101. error (0, errno, "%s", write_error);
  102. _exit (exit_failure);
  103. }
  104. /* Close stderr only if not sanitizing, as sanitizers may report to
  105. stderr after this function returns. */
  106. if (!SANITIZE_ADDRESS && close_stream (stderr) != 0)
  107. _exit (exit_failure);
  108. }