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