Mercurial > gnulib
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 |
rev | line source |
---|---|
10382 | 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 | 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 | |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
10382 | 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) | |
35 { | |
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 | 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) | |
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)) | |
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 { | |
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 | 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 | 80 } |
81 } | |
82 } |