xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/scoped_ignore_sigttou.h (revision 345cf9fb81bd0411c53e25d62cd93bdcaa865312)
1 /* Support for signoring SIGTTOU.
2 
3    Copyright (C) 2003-2023 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_SIGTTOU_H
21 #define SCOPED_IGNORE_SIGTTOU_H
22 
23 #include "gdbsupport/scoped_ignore_signal.h"
24 #include "gdbsupport/job-control.h"
25 
26 #ifdef SIGTTOU
27 
28 /* Simple wrapper that allows lazy initialization / destruction of T.
29    Slightly more efficient than gdb::optional, because it doesn't
30    carry storage to track whether the object has been initialized.  */
31 template<typename T>
32 class lazy_init
33 {
34 public:
35   void emplace ()
36   {
37     new (&m_u.obj) T ();
38   }
39 
40   void reset ()
41   {
42     m_u.obj.~T ();
43   }
44 
45 private:
46   union u
47   {
48     /* Must define ctor/dtor if T has non-trivial ctor/dtor.  */
49     u () {}
50     ~u () {}
51 
52     T obj;
53   } m_u;
54 };
55 
56 /* RAII class used to ignore SIGTTOU in a scope.  This isn't simply
57    scoped_ignore_signal<SIGTTOU> because we want to check the
58    `job_control' global.  */
59 
60 class scoped_ignore_sigttou
61 {
62 public:
63   scoped_ignore_sigttou ()
64   {
65     if (job_control)
66       m_ignore_signal.emplace ();
67   }
68 
69   ~scoped_ignore_sigttou ()
70   {
71     if (job_control)
72       m_ignore_signal.reset ();
73   }
74 
75   DISABLE_COPY_AND_ASSIGN (scoped_ignore_sigttou);
76 
77 private:
78   lazy_init<scoped_ignore_signal<SIGTTOU, false>> m_ignore_signal;
79 };
80 
81 #else
82 
83 using scoped_ignore_sigttou = scoped_ignore_signal_nop;
84 
85 #endif
86 
87 #endif /* SCOPED_IGNORE_SIGTTOU_H */
88