annotate lib/sigpipe-die.c @ 40186:8964917f9574

autoupdate
author Karl Berry <karl@freefriends.org>
date Mon, 18 Feb 2019 08:02:49 -0800
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10382
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Report a SIGPIPE signal and exit.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2008-2019 Free Software Foundation, Inc.
10382
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
10382
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include "sigpipe-die.h"
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <signal.h>
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <stdlib.h>
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include "error.h"
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include "exitfail.h"
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include "gettext.h"
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #define _(msgid) gettext (msgid)
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 void
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 sigpipe_die (void)
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 {
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 error (exit_failure, 0, "%s",
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10382
diff changeset
37 _("error writing to a closed pipe or socket"));
10382
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 /* Ensure that this function really does not return. */
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 abort ();
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 }
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 static void (*prepare_die_hook) (void);
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 /* This is the signal handler for SIGPIPE. It is invoked synchronously,
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 therefore it can make library calls to malloc(), gettext(), exit() etc. -
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 although in general it is undefined behaviour to do such calls from
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 within signal handlers. */
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 static void
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 sigpipe_die_handler (int sig)
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 {
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 if (prepare_die_hook != NULL)
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 (*prepare_die_hook) ();
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 sigpipe_die ();
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 }
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 void
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 install_sigpipe_die_handler (void (*prepare_die) (void))
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 {
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 prepare_die_hook = prepare_die;
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 /* Install the handler. */
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 {
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 struct sigaction action;
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 action.sa_handler = sigpipe_die_handler;
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 action.sa_flags = 0;
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 sigemptyset (&action.sa_mask);
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 if (sigaction (SIGPIPE, &action, NULL) >= 0)
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10382
diff changeset
71 /* Unblock the signal (just in case). This is needed because if the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10382
diff changeset
72 signal was blocked in the parent process, it is also blocked in
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10382
diff changeset
73 this process: the mask of blocked signals is inherited across
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10382
diff changeset
74 fork/exec (except for SIGCHLD). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10382
diff changeset
75 sigset_t sigpipe_set;
10382
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10382
diff changeset
77 sigemptyset (&sigpipe_set);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10382
diff changeset
78 sigaddset (&sigpipe_set, SIGPIPE);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10382
diff changeset
79 sigprocmask (SIG_UNBLOCK, &sigpipe_set, NULL);
10382
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 }
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 }
682dd60a37e4 New module 'sigpipe-die'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 }