E-MailRelay
gcleanup.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gcleanup.h
19///
20
21#ifndef G_CLEANUP_H
22#define G_CLEANUP_H
23
24#include "gdef.h"
25#include "gpath.h"
26#include "gsignalsafe.h"
27#include "gexception.h"
28
29namespace G
30{
31 class Cleanup ;
32}
33
34//| \class G::Cleanup
35/// A static interface for registering cleanup functions that are called when
36/// the process terminates abnormally. On unix this relates to signals like
37/// SIGTERM, SIGINT etc.
38///
40{
41public:
42 G_EXCEPTION( Error , "cleanup error" ) ;
43 struct Block /// A RAII class to temporarily block signal delivery.
44 {
45 explicit Block( bool active = true ) noexcept ;
46 ~Block() ;
47 bool m_active ;
48 Block( const Block & ) = delete ;
49 Block( Block && ) = delete ;
50 void operator=( const Block & ) = delete ;
51 void operator=( Block && ) = delete ;
52 } ;
53
54 static void init() ;
55 ///< An optional early-initialisation function. May be called more than once.
56
57 static void add( bool (*fn)(SignalSafe,const char*) , const char * arg ) ;
58 ///< Adds the given handler to the list of handlers that are to be called
59 ///< when the process terminates abnormally. The handler function must be
60 ///< fully reentrant, hence the SignalSafe dummy parameter. The 'arg'
61 ///< pointer is kept.
62
63 static void atexit( bool active = true ) ;
64 ///< Ensures that the cleanup functions are also called via atexit(), in
65 ///< addition to abnormal-termination signals.
66 ///<
67 ///< This can be useful when ancient third-party library code (eg. Xlib)
68 ///< might call exit(), but be careful to disable these exit handlers
69 ///< before normal termination by calling atexit(false).
70
71 static void block() noexcept ;
72 ///< Temporarily blocks signals until release()d. This should be used
73 ///< before creating threads so that only the main thread does signal
74 ///< handling.
75
76 static void release() noexcept ;
77 ///< Releases block()ed signals.
78
79 static const char * strdup( const char * ) ;
80 ///< A strdup() function that makes it clear in the stack trace
81 ///< that leaks are expected.
82
83 static const char * strdup( const std::string & ) ;
84 ///< A strdup() function that makes it clear in the stack trace
85 ///< that leaks are expected.
86
87public:
88 Cleanup() = delete ;
89} ;
90
91inline
92G::Cleanup::Block::Block( bool active ) noexcept :
93 m_active(active)
94{
95 if( m_active )
97}
98
99inline
100G::Cleanup::Block::~Block()
101{
102 if( m_active )
104}
105
106#endif
A static interface for registering cleanup functions that are called when the process terminates abno...
Definition: gcleanup.h:40
static const char * strdup(const char *)
A strdup() function that makes it clear in the stack trace that leaks are expected.
static void atexit(bool active=true)
Ensures that the cleanup functions are also called via atexit(), in addition to abnormal-termination ...
static void release() noexcept
Releases block()ed signals.
static void add(bool(*fn)(SignalSafe, const char *), const char *arg)
Adds the given handler to the list of handlers that are to be called when the process terminates abno...
static void block() noexcept
Temporarily blocks signals until release()d.
static void init()
An optional early-initialisation function. May be called more than once.
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:37
Low-level classes.
Definition: galign.h:28
A RAII class to temporarily block signal delivery.
Definition: gcleanup.h:44