powerd++  0.4.4
pidfile.hpp
Go to the documentation of this file.
1 
9 #ifndef _POWERDXX_SYS_PIDFILE_HPP_
10 #define _POWERDXX_SYS_PIDFILE_HPP_
11 
12 #include "error.hpp" /* sys::sc_error */
13 
14 #include <libutil.h> /* pidfile_*() */
15 
16 namespace sys {
17 
23 namespace pid {
24 
28 struct error {};
29 
34 class Pidfile final {
35  private:
40  pid_t otherpid;
41 
48  pidfh * pfh;
49 
50  public:
62  Pidfile(char const * const pfname, mode_t const mode) :
63  otherpid{0}, pfh{pidfile_open(pfname, mode, &this->otherpid)} {
64  if (this->pfh == nullptr) {
65  switch (errno) {
66  case EEXIST:
67  throw this->otherpid;
68  default:
69  throw sc_error<error>{errno};
70  }
71  }
72  }
73 
78  pidfile_remove(this->pfh);
79  }
80 
84  pid_t other() { return this->otherpid; }
85 
92  void write() {
93  if (pidfile_write(this->pfh) == -1) {
94  throw sc_error<error>{errno};
95  }
96  }
97 };
98 
99 } /* namespace pid */
100 
101 } /* namespace sys */
102 
103 #endif /* _POWERDXX_SYS_PIDFILE_HPP_ */
pidfile_open
pidfh * pidfile_open(const char *, mode_t, pid_t *)
Intercept calls to pidfile_open().
Definition: libloadplay.cpp:1769
pidfile_remove
int pidfile_remove(pidfh *)
Intercept calls to pidfile_remove().
Definition: libloadplay.cpp:1795
sys::sc_error
Can be thrown by syscall function wrappers if the function returned with an error.
Definition: error.hpp:26
sys::pid::Pidfile::write
void write()
Write PID to the file, should be called after daemon().
Definition: pidfile.hpp:92
sys
Wrappers around native system interfaces.
sys::pid::Pidfile::otherpid
pid_t otherpid
In case of failure to acquire the lock, the PID of the other process holding it is stored here.
Definition: pidfile.hpp:40
sys::pid::Pidfile::~Pidfile
~Pidfile()
Removes the pidfile.
Definition: pidfile.hpp:77
pidfile_write
int pidfile_write(pidfh *)
Intercept calls to pidfile_write().
Definition: libloadplay.cpp:1779
sys::pid::Pidfile
A wrapper around the pidfile_* family of commands implementing the RAII pattern.
Definition: pidfile.hpp:34
sys::pid::Pidfile::Pidfile
Pidfile(char const *const pfname, mode_t const mode)
Attempts to open the pidfile.
Definition: pidfile.hpp:62
sys::pid::error
The domain error type.
Definition: pidfile.hpp:28
sys::pid::Pidfile::other
pid_t other()
Returns the PID of the other process holding the lock.
Definition: pidfile.hpp:84
sys::pid::Pidfile::pfh
pidfh * pfh
Pointer to the pidfile state data structure.
Definition: pidfile.hpp:48
error.hpp
Provides system call error handling.