]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdbsupport/scoped_ignore_signal.h
55a921cb332891a4a92d265533bddb7f34ef59d2
[thirdparty/binutils-gdb.git] / gdbsupport / scoped_ignore_signal.h
1 /* Support for ignoring signals.
2
3 Copyright (C) 2021 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #ifndef SCOPED_IGNORE_SIGNAL_H
21 #define SCOPED_IGNORE_SIGNAL_H
22
23 #include <signal.h>
24
25 /* RAII class used to ignore a signal in a scope. If sigprocmask is
26 supported, then the signal is only ignored by the calling thread.
27 Otherwise, the signal disposition is set to SIG_IGN, which affects
28 the whole process. */
29
30 template <int Sig>
31 class scoped_ignore_signal
32 {
33 public:
34 scoped_ignore_signal ()
35 {
36 #ifdef HAVE_SIGPROCMASK
37 sigset_t set, old_state;
38
39 sigemptyset (&set);
40 sigaddset (&set, Sig);
41 sigprocmask (SIG_BLOCK, &set, &old_state);
42 m_was_blocked = sigismember (&old_state, Sig);
43 #else
44 m_osig = signal (Sig, SIG_IGN);
45 #endif
46 }
47
48 ~scoped_ignore_signal ()
49 {
50 #ifdef HAVE_SIGPROCMASK
51 if (!m_was_blocked)
52 {
53 sigset_t set;
54 const timespec zero_timeout = {};
55
56 sigemptyset (&set);
57 sigaddset (&set, Sig);
58
59 /* If we got a pending Sig signal, consume it before
60 unblocking. */
61 sigtimedwait (&set, nullptr, &zero_timeout);
62
63 sigprocmask (SIG_UNBLOCK, &set, nullptr);
64 }
65 #else
66 signal (Sig, m_osig);
67 #endif
68 }
69
70 DISABLE_COPY_AND_ASSIGN (scoped_ignore_signal);
71
72 private:
73 #ifdef HAVE_SIGPROCMASK
74 bool m_was_blocked;
75 #else
76 sighandler_t m_osig;
77 #endif
78 };
79
80 struct scoped_ignore_signal_nop
81 {
82 /* Note, these can't both be "= default", because otherwise the
83 compiler warns that variables of this type are not used. */
84 scoped_ignore_signal_nop ()
85 {}
86 ~scoped_ignore_signal_nop ()
87 {}
88 DISABLE_COPY_AND_ASSIGN (scoped_ignore_signal_nop);
89 };
90
91 #ifdef SIGPIPE
92 using scoped_ignore_sigpipe = scoped_ignore_signal<SIGPIPE>;
93 #else
94 using scoped_ignore_sigpipe = scoped_ignore_signal_nop;
95 #endif
96
97 #endif /* SCOPED_IGNORE_SIGNAL_H */