E-MailRelay

gpidfile.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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 "gprocess.h"
28#include "gpath.h"
29#include <sys/types.h>
30#include <string>
31
32 namespace G
33{
34 class PidFile ;
35 class Daemon ;
36}
37
38//| \class G::PidFile
39/// A class for creating pid files. Works with G::Root and G::Daemon so that
40/// the pid file can get created very late in a daemon startup sequence.
41/// Installs a signal handler so that the pid file gets deleted on process
42/// termination.
43///
44/// Usage:
45/// \code
46/// G::Root::init("nobody") ;
47/// G::PidFile pid_file( path ) ;
48/// { G::Root _ ; pid_file.mkdir() ; }
49/// if( daemon ) G::Daemon::detach( pid_file.path() ) ;
50/// { G::Root _ ; pid_file.commit() ; }
51/// \endcode
52///
53/// \see G::Daemon
54///
55 class G::PidFile
56{
57 public:
58 G_EXCEPTION( Error , tx("invalid pid file") )
59
60 explicit PidFile( const Path & pid_file_path ) ;
61 ///< Constructor. A relative path is converted to
62 ///< an absolute path using the cwd. Use commit()
63 ///< to actually create the file.
64
65 PidFile() ;
66 ///< Default constructor. Constructs a do-nothing
67 ///< object.
68
69 ~PidFile() ;
70 ///< Destructor. Deletes the file.
71
72 void mkdir() ;
73 ///< Creates the directory if it does not already exist.
74 ///<
75 ///< The caller should switch effective user-id and
76 ///< umask as necessary.
77
78 void commit() ;
79 ///< Creates the pid file if a path has been defined.
80 ///< Also installs signal handlers to cleanup the file on
81 ///< abnormal process termination. Throws on error.
82 ///<
83 ///< The caller should switch effective user-id and
84 ///< umask as necessary.
85
86 bool committed() const ;
87 ///< Returns true if commit() has been called with
88 ///< a valid path().
89
90 Path path() const ;
91 ///< Returns the full path of the file.
92
93 public:
94 PidFile( const PidFile & ) = delete ;
95 PidFile( PidFile && ) = delete ;
96 PidFile & operator=( const PidFile & ) = delete ;
97 PidFile & operator=( PidFile && ) = delete ;
98
99 private:
100 static void create( const Path & pid_file ) ;
101 bool valid() const ;
102
103 private:
104 Path m_path ;
105 bool m_committed {false} ;
106} ;
107
108#endif
G::Path
A Path object represents a file system path.
Definition: gpath.h:82
G::PidFile
A class for creating pid files.
Definition: gpidfile.h:56
G::PidFile::committed
bool committed() const
Returns true if commit() has been called with a valid path().
Definition: gpidfile.cpp:104
G::PidFile::commit
void commit()
Creates the pid file if a path has been defined.
Definition: gpidfile.cpp:95
G::PidFile::PidFile
PidFile()
Default constructor.
G::PidFile::~PidFile
~PidFile()
Destructor. Deletes the file.
Definition: gpidfile.cpp:52
G::PidFile::mkdir
void mkdir()
Creates the directory if it does not already exist.
Definition: gpidfile.cpp:69
G::PidFile::path
Path path() const
Returns the full path of the file.
Definition: gpidfile.cpp:109
gexception.h
gpath.h
gprocess.h
gsignalsafe.h
G
Low-level classes.
Definition: garg.h:36
G::tx
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84