VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gpidfile.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 ///
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  void check() ;
99  ///< Throws an exception if the path is not absolute.
100  ///< The use of G::Daemon normally requires an absolute
101  ///< path because it may change the current working
102  ///< directory.
103 
104  Path path() const ;
105  ///< Returns the path as supplied to the constructor
106  ///< or init().
107 
108 private:
109  Path m_path ;
110 
111 private:
112  PidFile( const PidFile & ) ; // not implemented
113  void operator=( const PidFile & ) ; // not implemented
114  static bool mine( SignalSafe , const char * path ) ; // reentrant
115  static void create( const Path & pid_file ) ;
116  bool valid() const ;
117 } ;
118 
119 #endif
120 
void init(const Path &pid_file_path)
Used after default construction to make the object active.
Definition: gpidfile.cpp:46
void check()
Throws an exception if the path is not absolute.
Definition: gpidfile.cpp:94
An empty structure that is used to indicate a signal-safe, reentrant implementation.
Definition: gsignalsafe.h:36
Path path() const
Returns the path as supplied to the constructor or init().
Definition: gpidfile.cpp:106
~PidFile()
Destructor. Calls cleanup() to delete the file.
Definition: gpidfile.cpp:35
PidFile()
Default constructor.
Definition: gpidfile.cpp:31
static void cleanup(SignalSafe, const char *path)
Deletes the specified pid file if it contains this process's id.
Definition: gpidfile.cpp:79
void commit()
Creates the file and installs signal handlers to cleanup() the file on abnormal process termination...
Definition: gpidfile.cpp:100
A class for creating pid files.
Definition: gpidfile.h:55
A Path object represents a file system path.
Definition: gpath.h:72