]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/common/signals-state-save-restore.c
Fix PR gdb/18653: gdb disturbs inferior's inherited signal dispositions
[thirdparty/binutils-gdb.git] / gdb / common / signals-state-save-restore.c
CommitLineData
f348d89a
PA
1/* Copyright (C) 2016 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
26static 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. */
32static sigset_t original_signal_mask;
33#endif
34
35/* See signals-state-save-restore.h. */
36
37void
38save_original_signals_state (void)
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 for (i = 1; i < NSIG; i++)
49 {
50 struct sigaction *oldact = &original_signal_actions[i];
51
52 res = sigaction (i, NULL, oldact);
53 if (res == -1 && errno == EINVAL)
54 {
55 /* Some signal numbers in the range are invalid. */
56 continue;
57 }
58 else if (res == -1)
59 perror_with_name ("sigaction");
60
61 /* If we find a custom signal handler already installed, then
62 this function was called too late. */
63 if (oldact->sa_handler != SIG_DFL && oldact->sa_handler != SIG_IGN)
64 internal_error (__FILE__, __LINE__, _("unexpected signal handler"));
65 }
66#endif
67}
68
69/* See signals-state-save-restore.h. */
70
71void
72restore_original_signals_state (void)
73{
74#ifdef HAVE_SIGACTION
75 int i;
76 int res;
77
78 for (i = 1; i < NSIG; i++)
79 {
80 res = sigaction (i, &original_signal_actions[i], NULL);
81 if (res == -1 && errno == EINVAL)
82 {
83 /* Some signal numbers in the range are invalid. */
84 continue;
85 }
86 else if (res == -1)
87 perror_with_name ("sigaction");
88 }
89
90 res = sigprocmask (SIG_SETMASK, &original_signal_mask, NULL);
91 if (res == -1)
92 perror_with_name ("sigprocmask");
93#endif
94}