summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2008-08-31 16:11:47 +0200
committerBruno Haible <bruno@clisp.org>2008-08-31 16:11:47 +0200
commitab01b091793316d28722ad1d7e6b869d6ec3911b (patch)
tree70a55c2a47b10feee439ebd8a92345cc9abe440c
parente1165b4980e126d20d256666e05a6523e52d3796 (diff)
downloadgnulib-ab01b091793316d28722ad1d7e6b869d6ec3911b.tar.gz
New module 'sigpipe-die'.
-rw-r--r--ChangeLog8
-rwxr-xr-xMODULES.html.sh1
-rw-r--r--lib/sigpipe-die.c82
-rw-r--r--lib/sigpipe-die.h74
-rw-r--r--modules/sigpipe-die27
5 files changed, 192 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 01c184d732..55fe08e6f3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
2008-08-31 Bruno Haible <bruno@clisp.org>
+ New module 'sigpipe-die'.
+ * modules/sigpipe-die: New file.
+ * lib/sigpipe-die.h: New file.
+ * lib/sigpipe-die.c: New file.
+ * MODULES.html.sh (Signal handling): Add sigpipe-die.
+
+2008-08-31 Bruno Haible <bruno@clisp.org>
+
Don't override previously installed signal handlers.
* lib/fatal-signal.c (saved_sigactions): New variable.
(uninstall_handlers): Reset the signal to the saved handler, not
diff --git a/MODULES.html.sh b/MODULES.html.sh
index e35b510ee6..486b451043 100755
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -2390,6 +2390,7 @@ func_all_modules ()
func_module c-stack
func_module libsigsegv
func_module sig2str
+ func_module sigpipe-die
func_end_table
element="Internationalization functions"
diff --git a/lib/sigpipe-die.c b/lib/sigpipe-die.c
new file mode 100644
index 0000000000..bb3c8b00e6
--- /dev/null
+++ b/lib/sigpipe-die.c
@@ -0,0 +1,82 @@
+/* Report a SIGPIPE signal and exit.
+ Copyright (C) 2008 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2008. */
+
+#include <config.h>
+
+/* Specification. */
+#include "sigpipe-die.h"
+
+#include <signal.h>
+#include <stdlib.h>
+
+#include "error.h"
+#include "exitfail.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
+void
+sigpipe_die (void)
+{
+ error (exit_failure, 0, "%s",
+ _("error writing to a closed pipe or socket"));
+
+ /* Ensure that this function really does not return. */
+ abort ();
+}
+
+static void (*prepare_die_hook) (void);
+
+/* This is the signal handler for SIGPIPE. It is invoked synchronously,
+ therefore it can make library calls to malloc(), gettext(), exit() etc. -
+ although in general it is undefined behaviour to do such calls from
+ within signal handlers. */
+static void
+sigpipe_die_handler (int sig)
+{
+ if (prepare_die_hook != NULL)
+ (*prepare_die_hook) ();
+ sigpipe_die ();
+}
+
+void
+install_sigpipe_die_handler (void (*prepare_die) (void))
+{
+ prepare_die_hook = prepare_die;
+
+ /* Install the handler. */
+ {
+ struct sigaction action;
+
+ action.sa_handler = sigpipe_die_handler;
+ action.sa_flags = 0;
+ sigemptyset (&action.sa_mask);
+ if (sigaction (SIGPIPE, &action, NULL) >= 0)
+ {
+ /* Unblock the signal (just in case). This is needed because if the
+ signal was blocked in the parent process, it is also blocked in
+ this process: the mask of blocked signals is inherited across
+ fork/exec (except for SIGCHLD). */
+ sigset_t sigpipe_set;
+
+ sigemptyset (&sigpipe_set);
+ sigaddset (&sigpipe_set, SIGPIPE);
+ sigprocmask (SIG_UNBLOCK, &sigpipe_set, NULL);
+ }
+ }
+}
diff --git a/lib/sigpipe-die.h b/lib/sigpipe-die.h
new file mode 100644
index 0000000000..e677943bf9
--- /dev/null
+++ b/lib/sigpipe-die.h
@@ -0,0 +1,74 @@
+/* Report a SIGPIPE signal and exit.
+ Copyright (C) 2008 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2008. */
+
+/* SIGPIPE is the signal sent to a process calling write() on a pipe with no
+ readers. Together with the signal, the write() call is terminated with
+ return value -1, errno = EPIPE. Note that SIGPIPE is a *synchronous*
+ signal: it occurs only during write(), without delay (unless blocked).
+
+ The default reaction on SIGPIPE, namely terminating the process without
+ an error message, is suitable for programs which only produce output to
+ standard output and don't have side effects.
+
+ When a program has side effects, other than writing to standard output, the
+ suitable behaviour is either
+ (a) to exit with an error message
+ or - in certain cases, for example when writing to subprocesses -
+ (b) to continue the operation without writing to the pipe/socket with
+ no readers.
+
+ This file provides support for (a).
+ For (b), the program needs to know which of the output file descriptors
+ has no readers. This is usually implemented by blocking the SIGPIPE signal
+ and handling an EPIPE error indicator in all affected library calls
+ (write(), send(), fwrite(), fflush(), fclose(), etc.). */
+
+#ifndef _SIGPIPE_DIE_H
+#define _SIGPIPE_DIE_H
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+# ifndef __attribute__
+# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8)
+# define __attribute__(x)
+# endif
+# endif
+
+# ifndef ATTRIBUTE_NORETURN
+# define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
+# endif
+
+/* Emit an error message indicating a SIGPIPE signal, and terminate the
+ process with an error code. */
+extern void sigpipe_die (void) ATTRIBUTE_NORETURN;
+
+/* Install a SIGPIPE handler that invokes PREPARE_DIE and then emits an
+ error message and exits. PREPARE_DIE may be NULL, meaning a no-op. */
+extern void install_sigpipe_die_handler (void (*prepare_die) (void));
+
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif /* _SIGPIPE_DIE_H */
diff --git a/modules/sigpipe-die b/modules/sigpipe-die
new file mode 100644
index 0000000000..dc00736b11
--- /dev/null
+++ b/modules/sigpipe-die
@@ -0,0 +1,27 @@
+Description:
+Report a SIGPIPE signal and exit.
+
+Files:
+lib/sigpipe-die.h
+lib/sigpipe-die.c
+
+Depends-on:
+error
+gettext-h
+exitfail
+sigprocmask
+sigaction
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += sigpipe-die.h sigpipe-die.c
+
+Include:
+"sigpipe-die.h"
+
+License:
+GPL
+
+Maintainer:
+Bruno Haible