1 /* Report a SIGPIPE signal and exit.
2 Copyright (C) 2008-2021 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
18
19 #include <config.h>
20
21 /* Specification. */
22 #include "sigpipe-die.h"
23
24 #include <signal.h>
25 #include <stdlib.h>
26
27 #include "error.h"
28 #include "exitfail.h"
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32
33 void
34 sigpipe_die (void)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
35 {
36 error (exit_failure, 0, "%s",
37 _("error writing to a closed pipe or socket"));
38
39 /* Ensure that this function really does not return. */
40 abort ();
41 }
42
43 static void (*prepare_die_hook) (void);
44
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. */
49 static void
50 sigpipe_die_handler (int sig)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
51 {
52 if (prepare_die_hook != NULL)
53 (*prepare_die_hook) ();
54 sigpipe_die ();
55 }
56
57 void
58 install_sigpipe_die_handler (void (*prepare_die) (void))
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
59 {
60 prepare_die_hook = prepare_die;
61
62 /* Install the handler. */
63 {
64 struct sigaction action;
65
66 action.sa_handler = sigpipe_die_handler;
67 action.sa_flags = 0;
68 sigemptyset (&action.sa_mask);
69 if (sigaction (SIGPIPE, &action, NULL) >= 0)
70 {
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). */
75 sigset_t sigpipe_set;
76
77 sigemptyset (&sigpipe_set);
78 sigaddset (&sigpipe_set, SIGPIPE);
79 sigprocmask (SIG_UNBLOCK, &sigpipe_set, NULL);
80 }
81 }
82 }