]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbsupport/scoped_ignore_signal.h
Introduce scoped_restore_signal
[thirdparty/binutils-gdb.git] / gdbsupport / scoped_ignore_signal.h
CommitLineData
6a7f1c20
PA
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. */
26
27template <int Sig>
28class scoped_ignore_signal
29{
30public:
31 scoped_ignore_signal ()
32 {
33 m_osig = signal (Sig, SIG_IGN);
34 }
35
36 ~scoped_ignore_signal ()
37 {
38 signal (Sig, m_osig);
39 }
40
41 DISABLE_COPY_AND_ASSIGN (scoped_ignore_signal);
42
43private:
44 sighandler_t m_osig = nullptr;
45};
46
47struct scoped_ignore_signal_nop
48{
49 /* Note, these can't both be "= default", because otherwise the
50 compiler warns that variables of this type are not used. */
51 scoped_ignore_signal_nop ()
52 {}
53 ~scoped_ignore_signal_nop ()
54 {}
55 DISABLE_COPY_AND_ASSIGN (scoped_ignore_signal_nop);
56};
57
58#ifdef SIGPIPE
59using scoped_ignore_sigpipe = scoped_ignore_signal<SIGPIPE>;
60#else
61using scoped_ignore_sigpipe = scoped_ignore_signal_nop;
62#endif
63
64#endif /* SCOPED_IGNORE_SIGNAL_H */