1N/A/* Close standard output and standard error, exiting with a diagnostic on error.
1N/A
1N/A Copyright (C) 1998-2002, 2004, 2006, 2008-2010 Free Software Foundation,
1N/A Inc.
1N/A
1N/A This program is free software: you can redistribute it and/or modify
1N/A it under the terms of the GNU General Public License as published by
1N/A the Free Software Foundation; either version 3 of the License, or
1N/A (at your option) any later version.
1N/A
1N/A This program is distributed in the hope that it will be useful,
1N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A GNU General Public License for more details.
1N/A
1N/A You should have received a copy of the GNU General Public License
1N/A along with this program. If not, see <http://www.gnu.org/licenses/>. */
1N/A
1N/A#include <config.h>
1N/A
1N/A#include "closeout.h"
1N/A
1N/A#include <errno.h>
1N/A#include <stdbool.h>
1N/A#include <stdio.h>
1N/A#include <unistd.h>
1N/A
1N/A#include "gettext.h"
1N/A#define _(msgid) gettext (msgid)
1N/A
1N/A#include "close-stream.h"
1N/A#include "error.h"
1N/A#include "exitfail.h"
1N/A#include "quotearg.h"
1N/A
1N/Astatic const char *file_name;
1N/A
1N/A/* Set the file name to be reported in the event an error is detected
1N/A by close_stdout. */
1N/Avoid
1N/Aclose_stdout_set_file_name (const char *file)
1N/A{
1N/A file_name = file;
1N/A}
1N/A
1N/Astatic bool ignore_EPIPE /* = false */;
1N/A
1N/A/* Specify the reaction to an EPIPE error during the closing of stdout:
1N/A - If ignore = true, it shall be ignored.
1N/A - If ignore = false, it shall evoke a diagnostic, along with a nonzero
1N/A exit status.
1N/A The default is ignore = false.
1N/A
1N/A This setting matters only if the SIGPIPE signal is ignored (i.e. its
1N/A handler set to SIG_IGN) or blocked. Only particular programs need to
1N/A temporarily ignore SIGPIPE. If SIGPIPE is ignored or blocked because
1N/A it was ignored or blocked in the parent process when it created the
1N/A child process, it usually is a bug in the parent process: It is bad
1N/A practice to have SIGPIPE ignored or blocked while creating a child
1N/A process.
1N/A
1N/A EPIPE occurs when writing to a pipe or socket that has no readers now,
1N/A when SIGPIPE is ignored or blocked.
1N/A
1N/A The ignore = false setting is suitable for a scenario where it is normally
1N/A guaranteed that the pipe writer terminates before the pipe reader. In
1N/A this case, an EPIPE is an indication of a premature termination of the
1N/A pipe reader and should lead to a diagnostic and a nonzero exit status.
1N/A
1N/A The ignore = true setting is suitable for a scenario where you don't know
1N/A ahead of time whether the pipe writer or the pipe reader will terminate
1N/A first. In this case, an EPIPE is an indication that the pipe writer can
1N/A stop doing useless write() calls; this is what close_stdout does anyway.
1N/A EPIPE is part of the normal pipe/socket shutdown protocol in this case,
1N/A and should not lead to a diagnostic message. */
1N/A
1N/Avoid
1N/Aclose_stdout_set_ignore_EPIPE (bool ignore)
1N/A{
1N/A ignore_EPIPE = ignore;
1N/A}
1N/A
1N/A/* Close standard output. On error, issue a diagnostic and _exit
1N/A with status 'exit_failure'.
1N/A
1N/A Also close standard error. On error, _exit with status 'exit_failure'.
1N/A
1N/A Since close_stdout is commonly registered via 'atexit', POSIX
1N/A and the C standard both say that it should not call 'exit',
1N/A because the behavior is undefined if 'exit' is called more than
1N/A once. So it calls '_exit' instead of 'exit'. If close_stdout
1N/A is registered via atexit before other functions are registered,
1N/A the other functions can act before this _exit is invoked.
1N/A
1N/A Applications that use close_stdout should flush any streams
1N/A other than stdout and stderr before exiting, since the call to
1N/A _exit will bypass other buffer flushing. Applications should
1N/A be flushing and closing other streams anyway, to check for I/O
1N/A errors. Also, applications should not use tmpfile, since _exit
1N/A can bypass the removal of these files.
1N/A
1N/A It's important to detect such failures and exit nonzero because many
1N/A tools (most notably `make' and other build-management systems) depend
1N/A on being able to detect failure in other tools via their exit status. */
1N/A
1N/Avoid
1N/Aclose_stdout (void)
1N/A{
1N/A if (close_stream (stdout) != 0
1N/A && !(ignore_EPIPE && errno == EPIPE))
1N/A {
1N/A char const *write_error = _("write error");
1N/A if (file_name)
1N/A error (0, errno, "%s: %s", quotearg_colon (file_name),
1N/A write_error);
1N/A else
1N/A error (0, errno, "%s", write_error);
1N/A
1N/A _exit (exit_failure);
1N/A }
1N/A
1N/A if (close_stream (stderr) != 0)
1N/A _exit (exit_failure);
1N/A}