From ab01b091793316d28722ad1d7e6b869d6ec3911b Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 31 Aug 2008 16:11:47 +0200 Subject: [PATCH] New module 'sigpipe-die'. --- ChangeLog | 8 ++++++ MODULES.html.sh | 1 + lib/sigpipe-die.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/sigpipe-die.h | 74 +++++++++++++++++++++++++++++++++++++++++++++++ modules/sigpipe-die | 27 ++++++++++++++++++ 5 files changed, 192 insertions(+) create mode 100644 lib/sigpipe-die.c create mode 100644 lib/sigpipe-die.h create mode 100644 modules/sigpipe-die diff --git a/ChangeLog b/ChangeLog index 01c184d73..55fe08e6f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2008-08-31 Bruno Haible + 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 + 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 e35b510ee..486b45104 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 000000000..bb3c8b00e --- /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 . */ + +/* Written by Bruno Haible , 2008. */ + +#include + +/* Specification. */ +#include "sigpipe-die.h" + +#include +#include + +#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 000000000..e677943bf --- /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 . */ + +/* Written by Bruno Haible , 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 000000000..dc00736b1 --- /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 -- 2.11.0