GDBserver

signals-state-save-restore.c
Go to the documentation of this file.
1 /* Copyright (C) 2016-2018 Free Software Foundation, Inc.
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"
20
21 #include <signal.h>
22
23 /* The original signal actions and mask. */
24
25 #ifdef HAVE_SIGACTION
26 static struct sigaction original_signal_actions[NSIG];
27
28 /* Note that we use sigprocmask without worrying about threads because
29 the save/restore functions are called either from main, or after a
30 fork. In both cases, we know the calling process is single
31 threaded. */
32 static sigset_t original_signal_mask;
33 #endif
34
35 /* See signals-state-save-restore.h. */
36
37 void
38 save_original_signals_state (bool quiet)
39 {
40 #ifdef HAVE_SIGACTION
41 int i;
42 int res;
43
44 res = sigprocmask (0, NULL, &original_signal_mask);
45 if (res == -1)
46 perror_with_name (("sigprocmask"));
47
48 bool found_preinstalled = false;
49
50 for (i = 1; i < NSIG; i++)
51 {
52 struct sigaction *oldact = &original_signal_actions[i];
53
54 res = sigaction (i, NULL, oldact);
55 if (res == -1 && errno == EINVAL)
56 {
57 /* Some signal numbers in the range are invalid. */
58 continue;
59 }
60 else if (res == -1)
61 perror_with_name (("sigaction"));
62
63 /* If we find a custom signal handler already installed, then
64 this function was called too late. This is a warning instead
65 of an internal error because this can also happen if you
66 LD_PRELOAD a library that installs a signal handler early via
67 __attribute__((constructor)), like libSegFault.so. */
68 if (!quiet
69 && oldact->sa_handler != SIG_DFL
70 && oldact->sa_handler != SIG_IGN)
71 {
72 found_preinstalled = true;
73
74 /* Use raw fprintf here because we're being called in early
75 startup, because GDB's filtered streams are are
76 created. */
77 fprintf (stderr,
78 _("warning: Found custom handler for signal "
79 "%d (%s) preinstalled.\n"), i,
80 strsignal (i));
81 }
82 }
83
84 if (found_preinstalled)
85 {
86 fprintf (stderr, _("\
87 Some signal dispositions inherited from the environment (SIG_DFL/SIG_IGN)\n\
88 won't be propagated to spawned programs.\n"));
89 }
90 #endif
91 }
92
93 /* See signals-state-save-restore.h. */
94
95 void
96 restore_original_signals_state (void)
97 {
98 #ifdef HAVE_SIGACTION
99 int i;
100 int res;
101
102 for (i = 1; i < NSIG; i++)
103 {
104 res = sigaction (i, &original_signal_actions[i], NULL);
105 if (res == -1 && errno == EINVAL)
106 {
107 /* Some signal numbers in the range are invalid. */
108 continue;
109 }
110 else if (res == -1)
111 perror_with_name (("sigaction"));
112 }
113
114 res = sigprocmask (SIG_SETMASK, &original_signal_mask, NULL);
115 if (res == -1)
116 perror_with_name (("sigprocmask"));
117 #endif
118 }
signals-state-save-restore.h
_
#define _(String)
Definition: gdb_locale.h:35
signal.h
common-defs.h
restore_original_signals_state
void restore_original_signals_state(void)
Definition: signals-state-save-restore.c:96
errno
#define errno
Definition: wincecompat.h:24
save_original_signals_state
void save_original_signals_state(bool quiet)
Definition: signals-state-save-restore.c:38
perror_with_name
void perror_with_name(const char *string)
Definition: utils.c:57