• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Close standard output and standard error, exiting with a diagnostic on error.
2 
3    Copyright (C) 1998-2002, 2004, 2006, 2008-2012 Free Software Foundation,
4    Inc.
5 
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18 
19 #include <config.h>
20 
21 #include "closeout.h"
22 
23 #include <errno.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 
28 #include "gettext.h"
29 #define _(msgid) gettext (msgid)
30 
31 #include "close-stream.h"
32 #include "error.h"
33 #include "exitfail.h"
34 #include "quotearg.h"
35 
36 static const char *file_name;
37 
38 /* Set the file name to be reported in the event an error is detected
39    by close_stdout.  */
40 void
close_stdout_set_file_name(const char * file)41 close_stdout_set_file_name (const char *file)
42 {
43   file_name = file;
44 }
45 
46 static bool ignore_EPIPE /* = false */;
47 
48 /* Specify the reaction to an EPIPE error during the closing of stdout:
49      - If ignore = true, it shall be ignored.
50      - If ignore = false, it shall evoke a diagnostic, along with a nonzero
51        exit status.
52    The default is ignore = false.
53 
54    This setting matters only if the SIGPIPE signal is ignored (i.e. its
55    handler set to SIG_IGN) or blocked.  Only particular programs need to
56    temporarily ignore SIGPIPE.  If SIGPIPE is ignored or blocked because
57    it was ignored or blocked in the parent process when it created the
58    child process, it usually is a bug in the parent process: It is bad
59    practice to have SIGPIPE ignored or blocked while creating a child
60    process.
61 
62    EPIPE occurs when writing to a pipe or socket that has no readers now,
63    when SIGPIPE is ignored or blocked.
64 
65    The ignore = false setting is suitable for a scenario where it is normally
66    guaranteed that the pipe writer terminates before the pipe reader.  In
67    this case, an EPIPE is an indication of a premature termination of the
68    pipe reader and should lead to a diagnostic and a nonzero exit status.
69 
70    The ignore = true setting is suitable for a scenario where you don't know
71    ahead of time whether the pipe writer or the pipe reader will terminate
72    first.  In this case, an EPIPE is an indication that the pipe writer can
73    stop doing useless write() calls; this is what close_stdout does anyway.
74    EPIPE is part of the normal pipe/socket shutdown protocol in this case,
75    and should not lead to a diagnostic message.  */
76 
77 void
close_stdout_set_ignore_EPIPE(bool ignore)78 close_stdout_set_ignore_EPIPE (bool ignore)
79 {
80   ignore_EPIPE = ignore;
81 }
82 
83 /* Close standard output.  On error, issue a diagnostic and _exit
84    with status 'exit_failure'.
85 
86    Also close standard error.  On error, _exit with status 'exit_failure'.
87 
88    Since close_stdout is commonly registered via 'atexit', POSIX
89    and the C standard both say that it should not call 'exit',
90    because the behavior is undefined if 'exit' is called more than
91    once.  So it calls '_exit' instead of 'exit'.  If close_stdout
92    is registered via atexit before other functions are registered,
93    the other functions can act before this _exit is invoked.
94 
95    Applications that use close_stdout should flush any streams
96    other than stdout and stderr before exiting, since the call to
97    _exit will bypass other buffer flushing.  Applications should
98    be flushing and closing other streams anyway, to check for I/O
99    errors.  Also, applications should not use tmpfile, since _exit
100    can bypass the removal of these files.
101 
102    It's important to detect such failures and exit nonzero because many
103    tools (most notably 'make' and other build-management systems) depend
104    on being able to detect failure in other tools via their exit status.  */
105 
106 void
close_stdout(void)107 close_stdout (void)
108 {
109   if (close_stream (stdout) != 0
110       && !(ignore_EPIPE && errno == EPIPE))
111     {
112       char const *write_error = _("write error");
113       if (file_name)
114         error (0, errno, "%s: %s", quotearg_colon (file_name),
115                write_error);
116       else
117         error (0, errno, "%s", write_error);
118 
119       _exit (exit_failure);
120     }
121 
122    if (close_stream (stderr) != 0)
123      _exit (exit_failure);
124 }
125