1 /* Report a SIGPIPE signal and exit.
2 Copyright (C) 2008-2020 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
22 #include "sigpipe-die.h"
31 #define _(msgid) gettext (msgid)
36 error (exit_failure
, 0, "%s",
37 _("error writing to a closed pipe or socket"));
39 /* Ensure that this function really does not return. */
43 static void (*prepare_die_hook
) (void);
45 /* This is the signal handler for SIGPIPE. It is invoked synchronously,
46 therefore it can make library calls to malloc(), gettext(), exit() etc. -
47 although in general it is undefined behaviour to do such calls from
48 within signal handlers. */
50 sigpipe_die_handler (int sig
)
52 if (prepare_die_hook
!= NULL
)
53 (*prepare_die_hook
) ();
58 install_sigpipe_die_handler (void (*prepare_die
) (void))
60 prepare_die_hook
= prepare_die
;
62 /* Install the handler. */
64 struct sigaction action
;
66 action
.sa_handler
= sigpipe_die_handler
;
68 sigemptyset (&action
.sa_mask
);
69 if (sigaction (SIGPIPE
, &action
, NULL
) >= 0)
71 /* Unblock the signal (just in case). This is needed because if the
72 signal was blocked in the parent process, it is also blocked in
73 this process: the mask of blocked signals is inherited across
74 fork/exec (except for SIGCHLD). */
77 sigemptyset (&sigpipe_set
);
78 sigaddset (&sigpipe_set
, SIGPIPE
);
79 sigprocmask (SIG_UNBLOCK
, &sigpipe_set
, NULL
);