Daemon process More...

#include <senf/Utils/Daemon/Daemon.hh>

Inheritance diagram for senf::Daemon:

Public Types

enum  StdStream { StdOut, StdErr, Both }
 Select standard stream to redirect. More...
 

Public Member Functions

int start (int argc, char const **argv)
 Called from main() to launch daemon. More...
 

Static Public Member Functions

static Daemoninstance ()
 Return the Daemon instance. More...
 

Protected Member Functions

 Daemon ()
 
virtual void configure ()
 Called before forking to configure the daemon class. More...
 
virtual void main ()
 Called after forking to execute the main application. More...
 
virtual void init ()
 Called to initialize the main application. More...
 
virtual void run ()
 Called to execute main application. More...
 

Structors and default members

virtual ~Daemon ()
 

Parameters

void daemonize (bool)
 Configure whether to run in fore- or background. More...
 
bool daemon ()
 true, if running as daemon More...
 
void consoleLog (std::string const &, StdStream which=Both)
 Configure console log file. More...
 
void catchExceptions (bool flag)
 
void pidFile (std::string const &)
 Configure pid file. More...
 

Auxiliary helpers

void detach ()
 Detach into background now. More...
 
int argc ()
 Access command line parameter count. More...
 
char const ** argv ()
 Access command line parameters. More...
 
void removeDaemonArgs ()
 Remove the daemon arguments from argc()/argv() More...
 
void logReopen ()
 Reopen the log files. More...
 
static void exit (unsigned code=0)
 Terminate daemon with failure. More...
 

Detailed Description

Daemon process

The Daemon class provides simple management for daemon processes. Specifically, it implements

  • Safe startup. If the startup fails, the foreground process which launches the daemon will terminate with an appropriate error exit code.
  • Straight forward application initialization. The daemon process is forked before even initializing the application. The initialization procedure doesn't need to cater for a later fork().
  • Automatic pid file management. The daemon will not be started, if a valid pid file is found. Stale pid files are automatically removed.
  • Console log management. It is possible, to redirect standard output and error to one or two log files. Messages pertaining to application initialization will be written to both the console and the log file whereas later messages will be directed to the log file only.
  • Optional foreground execution. The daemon may be started in the foreground for debugging purposes. In this case, the console log file(s) is/are automatically suppressed.

Starting the daemon process proceeds along the following steps:

  • The daemon is started by calling the daemon class instances start() member. This normally happens from the main() function generated by SENF_DAEMON_MAIN().
  • configure() is called. This (virtual) member configures the daemon manager by calling the Daemon class parameter members.
  • The log files are opened, fork() is called and the pid file is checked and created. The parent (foreground) process keeps running overseeing the daemon process.
  • main() is called. This virtual member may optionally be overridden in the derived class. Here we assume, main() is not overridden so the default implementation is used.
  • main() calls init().
  • after init() returns, main() calls detach().
  • detach() signals successful startup to the parent process. The parent process terminates leaving the daemon process running in the background.
  • main() calls run()
  • If run() ever returns, the daemon process terminates.
  • Whenever the process terminates normally (not necessarily successfully), the pid file is automatically removed.

The parameter members are used from configure() to configure the daemon manager. See below for details. The default configure() implementation will scan the command line arguments to check for the following parameters:

–no-daemon

Run in foreground

–console-log=stdout[,stderr]

Set the console log file(s). If only stdout is specified (with no comma), the same log file configuration will be applied to the standard output and error stream. Otherwise each stream is assigned it's own log file. If either log file name is empty, the command will not change the log file of that stream, the default log file will be used. If the log file name is set to 'none', the log file will be disabled.

–pid-file=pidfile

Set pid file path

The default configure() implementation will use whatever parameters have been set beforehand as default values. These default values should be set in a derived class configure() implementation. After setting the default values, the configure() implementation may choose to call this default configure() implementation to scan for command line parameters. The default configure() implementation does not completely parse the command line arguments. It just checks, if any of above arguments are present and processes them. Other arguments are completely ignored. The command line parameters should be completely processed within init().

Definition at line 94 of file Daemon.hh.

Member Enumeration Documentation

◆ StdStream

Select standard stream to redirect.

Enumerator
StdOut 

Standard output stream

StdErr 

Standard error stream

Both 

Both, standard output and error stream

Definition at line 103 of file Daemon.hh.

Constructor & Destructor Documentation

◆ ~Daemon()

senf::Daemon::~Daemon ( )
virtual

Definition at line 60 of file Daemon.cc.

◆ Daemon()

senf::Daemon::Daemon ( )
protected

Definition at line 323 of file Daemon.cc.

Member Function Documentation

◆ argc()

int senf::Daemon::argc ( )

Access command line parameter count.

Definition at line 87 of file Daemon.cc.

◆ argv()

char const ** senf::Daemon::argv ( )

Access command line parameters.

Definition at line 92 of file Daemon.cc.

◆ catchExceptions()

void senf::Daemon::catchExceptions ( bool  flag)

Definition at line 77 of file Daemon.cc.

◆ configure()

void senf::Daemon::configure ( )
protectedvirtual

Called before forking to configure the daemon class.

This default implementation will parser some command line parameters. See the class documentation above.

Definition at line 341 of file Daemon.cc.

◆ consoleLog()

void senf::Daemon::consoleLog ( std::string const &  path,
StdStream  which = Both 
)

Configure console log file.

May be called multiple times to set the log file for stdout and stderr separately. Any standard stream not assigned to a log file will be redirected to /dev/null.

When running in the foreground, the log files will be ignored.

Definition at line 121 of file Daemon.cc.

◆ daemon()

bool senf::Daemon::daemon ( )

true, if running as daemon

Definition at line 82 of file Daemon.cc.

◆ daemonize()

void senf::Daemon::daemonize ( bool  v)

Configure whether to run in fore- or background.

Definition at line 72 of file Daemon.cc.

◆ detach()

void senf::Daemon::detach ( )

Detach into background now.

This is not the same as forking. The process will already have forked into the background but until detach() is called (either automatically after init() returns or manually), the front end (foreground) process will wait for the background process to ensure successful startup.

Definition at line 198 of file Daemon.cc.

◆ exit()

void senf::Daemon::exit ( unsigned  code = 0)
static

Terminate daemon with failure.

Definition at line 248 of file Daemon.cc.

◆ init()

void senf::Daemon::init ( )
protectedvirtual

Called to initialize the main application.

While init() is running, the application still is connected to the controlling terminal. Error messages will be shown to the user.

This member is only called, if the default main() implementation is not overridden.

Definition at line 378 of file Daemon.cc.

◆ instance()

senf::Daemon & senf::Daemon::instance ( )
static

Return the Daemon instance.

Definition at line 314 of file Daemon.cc.

◆ logReopen()

void senf::Daemon::logReopen ( )

Reopen the log files.

This is used when rotating the logs. By default, SIGHUP calls logReopen.

Definition at line 157 of file Daemon.cc.

◆ main()

void senf::Daemon::main ( )
protectedvirtual

Called after forking to execute the main application.

The default implementation will call init(), detach() and then run(). It is preferred to override init() and run() if possible.

Definition at line 371 of file Daemon.cc.

◆ pidFile()

void senf::Daemon::pidFile ( std::string const &  f)

Configure pid file.

If a pid file is configured it will be checked on daemon startup. If another running instance of the daemon is detected, starting the daemon will fail.

Definition at line 186 of file Daemon.cc.

◆ removeDaemonArgs()

void senf::Daemon::removeDaemonArgs ( )

Remove the daemon arguments from argc()/argv()

Definition at line 114 of file Daemon.cc.

◆ run()

void senf::Daemon::run ( )
protectedvirtual

Called to execute main application.

Called after detaching from the controlling terminal.

This member is only called, if the default main() implementation is not overridden.

Definition at line 381 of file Daemon.cc.

◆ start()

int senf::Daemon::start ( int  argc,
char const **  argv 
)

Called from main() to launch daemon.

Normally not called directly but from the SENF_DAEMON_MAIN macro.

Definition at line 263 of file Daemon.cc.


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