Commit | Line | Data |
---|---|---|
42a4f53d | 1 | /* Copyright (C) 2016-2019 Free Software Foundation, Inc. |
f348d89a PA |
2 | |
3 | This file is part of GDB. | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | ||
18 | #include "common-defs.h" | |
19 | #include "signals-state-save-restore.h" | |
21987b9c | 20 | #include "gdbsupport/gdb-sigmask.h" |
f348d89a PA |
21 | |
22 | #include <signal.h> | |
23 | ||
24 | /* The original signal actions and mask. */ | |
25 | ||
26 | #ifdef HAVE_SIGACTION | |
27 | static struct sigaction original_signal_actions[NSIG]; | |
28 | ||
f348d89a PA |
29 | static sigset_t original_signal_mask; |
30 | #endif | |
31 | ||
32 | /* See signals-state-save-restore.h. */ | |
33 | ||
34 | void | |
e379cee6 | 35 | save_original_signals_state (bool quiet) |
f348d89a PA |
36 | { |
37 | #ifdef HAVE_SIGACTION | |
38 | int i; | |
39 | int res; | |
40 | ||
21987b9c | 41 | res = gdb_sigmask (0, NULL, &original_signal_mask); |
f348d89a | 42 | if (res == -1) |
669f9429 | 43 | perror_with_name (("sigprocmask")); |
f348d89a | 44 | |
e379cee6 PA |
45 | bool found_preinstalled = false; |
46 | ||
f348d89a PA |
47 | for (i = 1; i < NSIG; i++) |
48 | { | |
49 | struct sigaction *oldact = &original_signal_actions[i]; | |
50 | ||
51 | res = sigaction (i, NULL, oldact); | |
52 | if (res == -1 && errno == EINVAL) | |
53 | { | |
54 | /* Some signal numbers in the range are invalid. */ | |
55 | continue; | |
56 | } | |
57 | else if (res == -1) | |
669f9429 | 58 | perror_with_name (("sigaction")); |
f348d89a PA |
59 | |
60 | /* If we find a custom signal handler already installed, then | |
e379cee6 PA |
61 | this function was called too late. This is a warning instead |
62 | of an internal error because this can also happen if you | |
63 | LD_PRELOAD a library that installs a signal handler early via | |
64 | __attribute__((constructor)), like libSegFault.so. */ | |
65 | if (!quiet | |
66 | && oldact->sa_handler != SIG_DFL | |
67 | && oldact->sa_handler != SIG_IGN) | |
68 | { | |
69 | found_preinstalled = true; | |
70 | ||
71 | /* Use raw fprintf here because we're being called in early | |
db422fb2 | 72 | startup, before GDB's filtered streams are created. */ |
e379cee6 PA |
73 | fprintf (stderr, |
74 | _("warning: Found custom handler for signal " | |
75 | "%d (%s) preinstalled.\n"), i, | |
76 | strsignal (i)); | |
77 | } | |
78 | } | |
79 | ||
80 | if (found_preinstalled) | |
81 | { | |
82 | fprintf (stderr, _("\ | |
83 | Some signal dispositions inherited from the environment (SIG_DFL/SIG_IGN)\n\ | |
84 | won't be propagated to spawned programs.\n")); | |
f348d89a PA |
85 | } |
86 | #endif | |
87 | } | |
88 | ||
89 | /* See signals-state-save-restore.h. */ | |
90 | ||
91 | void | |
92 | restore_original_signals_state (void) | |
93 | { | |
94 | #ifdef HAVE_SIGACTION | |
95 | int i; | |
96 | int res; | |
97 | ||
98 | for (i = 1; i < NSIG; i++) | |
99 | { | |
100 | res = sigaction (i, &original_signal_actions[i], NULL); | |
101 | if (res == -1 && errno == EINVAL) | |
102 | { | |
103 | /* Some signal numbers in the range are invalid. */ | |
104 | continue; | |
105 | } | |
106 | else if (res == -1) | |
669f9429 | 107 | perror_with_name (("sigaction")); |
f348d89a PA |
108 | } |
109 | ||
21987b9c | 110 | res = gdb_sigmask (SIG_SETMASK, &original_signal_mask, NULL); |
f348d89a | 111 | if (res == -1) |
669f9429 | 112 | perror_with_name (("sigprocmask")); |
f348d89a PA |
113 | #endif |
114 | } |