E-MailRelay
Public Member Functions | Static Public Member Functions | List of all members
G::PidFile Class Reference

A class for creating pid files. More...

#include <gpidfile.h>

Public Member Functions

 PidFile (const Path &pid_file_path)
 Constructor. More...
 
 PidFile ()
 Default constructor. More...
 
void init (const Path &pid_file_path)
 Used after default construction to make the object active. More...
 
 ~PidFile ()
 Destructor. Calls cleanup() to delete the file. More...
 
void commit ()
 Creates the file and installs signal handlers to cleanup() the file on abnormal process termination. More...
 
bool committed () const
 Returns true if commit() has been called with a valid path(). More...
 
void check ()
 Throws an exception if the path is not absolute. More...
 
Path path () const
 Returns the path as supplied to the constructor or init(). More...
 
 PidFile (const PidFile &)=delete
 
 PidFile (PidFile &&)=delete
 
void operator= (const PidFile &)=delete
 
void operator= (PidFile &&)=delete
 

Static Public Member Functions

static bool cleanup (SignalSafe, const char *path) noexcept
 Deletes the specified pid file if it contains this process's id. More...
 

Detailed Description

A class for creating pid files.

Works with G::Root and G::Daemon so that the pid file can get created very late in a daemon startup sequence. Installs a signal handler so that the pid file gets deleted on process termination.

Usage:

G::PidFile pid_file ;
if( !path.empty() )
{ pid_file.init(path) ; pid_file.check() ; }
G::Root::init("nobody") ;
if( daemon ) G::Daemon::detach( pid_file ) ;
{ G::Root claim_root ; pid_file.commit() ; }
static void detach()
Detaches from the parent environment.
bool empty() const noexcept
Returns true if size() is zero.
Definition: gpath.h:203
A class for creating pid files.
Definition: gpidfile.h:57
void commit()
Creates the file and installs signal handlers to cleanup() the file on abnormal process termination.
Definition: gpidfile.cpp:132
void check()
Throws an exception if the path is not absolute.
Definition: gpidfile.cpp:126
Path path() const
Returns the path as supplied to the constructor or init().
Definition: gpidfile.cpp:146
void init(const Path &pid_file_path)
Used after default construction to make the object active.
Definition: gpidfile.cpp:54
A class which acquires the process's special privileges on construction and releases them on destruct...
Definition: groot.h:52
static void init(const std::string &nobody, bool fixed_group=false)
< Desctructor.
Definition: groot.cpp:80
See also
G::Daemon

Definition at line 56 of file gpidfile.h.

Constructor & Destructor Documentation

◆ PidFile() [1/2]

G::PidFile::PidFile ( const Path pid_file_path)
explicit

Constructor.

The path should normally be an absolute path. Use commit() to actually create the file.

Definition at line 49 of file gpidfile.cpp.

◆ PidFile() [2/2]

G::PidFile::PidFile ( )
default

Default constructor.

Constructs a do-nothing object. Initialise with init().

◆ ~PidFile()

G::PidFile::~PidFile ( )

Destructor. Calls cleanup() to delete the file.

Definition at line 36 of file gpidfile.cpp.

Member Function Documentation

◆ check()

void G::PidFile::check ( )

Throws an exception if the path is not absolute.

The use of G::Daemon normally requires an absolute path because it may change the current working directory.

Definition at line 126 of file gpidfile.cpp.

◆ cleanup()

bool G::PidFile::cleanup ( SignalSafe  safe,
const char *  path 
)
staticnoexcept

Deletes the specified pid file if it contains this process's id.

Claims root privilege to read and delete the pid file (see G::Root::atExit()).

This is not normally needed by client code since it is installed as a signal handler (see G::Cleanup) and called from the destructor.

Signal-safe, reentrant implementation.

Definition at line 106 of file gpidfile.cpp.

◆ commit()

void G::PidFile::commit ( )

Creates the file and installs signal handlers to cleanup() the file on abnormal process termination.

Does nothing if no pid file path has been defined. Throws on error.

The caller is responsible for setting the file ownership and permissions by switching effecive user-id and umask.

Definition at line 132 of file gpidfile.cpp.

◆ committed()

bool G::PidFile::committed ( ) const

Returns true if commit() has been called with a valid path().

Definition at line 141 of file gpidfile.cpp.

◆ init()

void G::PidFile::init ( const Path pid_file_path)

Used after default construction to make the object active.

Use commit() to actually create the file.

Definition at line 54 of file gpidfile.cpp.

◆ path()

G::Path G::PidFile::path ( ) const

Returns the path as supplied to the constructor or init().

Definition at line 146 of file gpidfile.cpp.


The documentation for this class was generated from the following files: