E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gpidfile.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 gpidfile.h
19 ///
20
21 #ifndef G_PIDFILE_H
22 #define G_PIDFILE_H
23
24 #include "gdef.h"
25 #include "gexception.h"
26 #include "gsignalsafe.h"
27 #include "gpath.h"
28 #include <sys/types.h>
29 #include <string>
30
31 namespace G
32 {
33 class PidFile ;
34 class Daemon ;
35 }
36
37 /// \class G::PidFile
38 /// A class for creating pid files. Works with G::Root and G::Daemon so that
39 /// the pid file can get created very late in a daemon startup sequence.
40 /// Installs a signal handler so that the pid file gets deleted on process
41 /// termination.
42 ///
43 /// Usage:
44 /// \code
45 /// G::PidFile pid_file ;
46 /// if( !path.empty() )
47 /// { pid_file.init(path) ; pid_file.check() ; }
48 /// G::Root::init("nobody") ;
49 /// if( daemon ) G::Daemon::detach( pid_file ) ;
50 /// { G::Root claim_root ; pid_file.commit() ; }
51 /// \endcode
52 ///
53 /// \see G::Daemon
54 ///
55 class G::PidFile
56 {
57 public:
58 G_EXCEPTION( Error , "invalid pid file" ) ;
59
60 static void cleanup( SignalSafe , const char * path ) ;
61 ///< Deletes the specified pid file if it contains this
62 ///< process's id. Claims root privilege to read
63 ///< and delete the pid file (see G::Root).
64 ///<
65 ///< This is not normally needed by client code since it
66 ///< is installed as a signal handler (see G::Cleanup)
67 ///< and called from the destructor.
68 ///<
69 ///< Signal-safe, reentrant implementation.
70
71 explicit PidFile( const Path & pid_file_path ) ;
72 ///< Constructor. The path should normally be an
73 ///< absolute path. Use commit() to actually create
74 ///< the file.
75
76 PidFile() ;
77 ///< Default constructor. Constructs a do-nothing
78 ///< object. Initialise with init().
79
80 void init( const Path & pid_file_path ) ;
81 ///< Used after default construction to make the object
82 ///< active. Use commit() to actually create the file.
83
84 ~PidFile() ;
85 ///< Destructor. Calls cleanup() to delete the file.
86
87 void commit() ;
88 ///< Creates the file and installs signal handlers to
89 ///< cleanup() the file on abnormal process termination.
90 ///<
91 ///< Does nothing if no pid file path has been defined.
92 ///< Throws on error.
93 ///<
94 ///< The caller is responsible for setting the file
95 ///< ownership and permissions by switching effecive
96 ///< user-id and umask.
97
98 bool committed() const ;
99 ///< Returns true if commit() has been called with
100 ///< a valid path().
101
102 void check() ;
103 ///< Throws an exception if the path is not absolute.
104 ///< The use of G::Daemon normally requires an absolute
105 ///< path because it may change the current working
106 ///< directory.
107
108 Path path() const ;
109 ///< Returns the path as supplied to the constructor
110 ///< or init().
111
112 private:
113 Path m_path ;
114 bool m_committed ;
115
116 private:
117 PidFile( const PidFile & ) ; // not implemented
118 void operator=( const PidFile & ) ; // not implemented
119 static bool mine( SignalSafe , const char * path ) ; // reentrant
120 static void create( const Path & pid_file ) ;
121 static std::string * new_string_ignore_leak( const std::string & ) ;
122 bool valid() const ;
123 } ;
124
125 #endif
126
G::PidFile::init
void init(const Path &pid_file_path)
Used after default construction to make the object active.
Definition: gpidfile.cpp:48
G::PidFile::check
void check()
Throws an exception if the path is not absolute.
Definition: gpidfile.cpp:96
G::SignalSafe
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
G::PidFile::path
Path path() const
Returns the path as supplied to the constructor or init().
Definition: gpidfile.cpp:116
G::PidFile::~PidFile
~PidFile()
Destructor. Calls cleanup() to delete the file.
Definition: gpidfile.cpp:36
gsignalsafe.h
G::PidFile::PidFile
PidFile()
Default constructor.
Definition: gpidfile.cpp:31
G::PidFile::cleanup
static void cleanup(SignalSafe, const char *path)
Deletes the specified pid file if it contains this process's id.
Definition: gpidfile.cpp:81
G::PidFile::committed
bool committed() const
Returns true if commit() has been called with a valid path().
Definition: gpidfile.cpp:111
G
Low-level classes.
Definition: garg.h:29
G::PidFile::commit
void commit()
Creates the file and installs signal handlers to cleanup() the file on abnormal process termination...
Definition: gpidfile.cpp:102
gexception.h
gpath.h
G::PidFile
A class for creating pid files.
Definition: gpidfile.h:55
G::Path
A Path object represents a file system path.
Definition: gpath.h:72