exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / sigpipe-die.c
blob3a22d2f9773c819ccc1ce46b88150a5698a399de
1 /* Report a SIGPIPE signal and exit.
2 Copyright (C) 2008-2024 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. */
19 #include <config.h>
21 /* Specification. */
22 #include "sigpipe-die.h"
24 #include <signal.h>
25 #include <stdlib.h>
27 #include <error.h>
28 #include "exitfail.h"
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
33 void
34 sigpipe_die (void)
36 error (exit_failure, 0, "%s",
37 _("error writing to a closed pipe or socket"));
39 /* Ensure that this function really does not return. */
40 abort ();
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. */
49 static void
50 sigpipe_die_handler (int sig)
52 if (prepare_die_hook != NULL)
53 (*prepare_die_hook) ();
54 sigpipe_die ();
57 void
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;
67 action.sa_flags = 0;
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). */
75 sigset_t sigpipe_set;
77 sigemptyset (&sigpipe_set);
78 sigaddset (&sigpipe_set, SIGPIPE);
79 sigprocmask (SIG_UNBLOCK, &sigpipe_set, NULL);