senf::Daemon Class Reference
Daemon process
More...
#include <senf/Utils/Daemon/Daemon.hh>
List of all members.
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 103 of file Daemon.hh.
|
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.
|
Static Public Member Functions
|
static Daemon & |
instance () |
|
Return the Daemon instance.
|
Structors and default members
|
virtual |
~Daemon () |
Parameters
|
void |
daemonize (bool) |
|
Configure whether to run in fore- or background.
|
bool |
daemon () |
|
true , if running as daemon
|
void |
consoleLog (std::string const &, StdStream which=Both) |
|
Configure console log file.
|
void |
pidFile (std::string const &) |
|
Configure pid file.
|
Auxiliary helpers
|
void |
detach () |
|
Detach into background now.
|
int |
argc () |
|
Access command line parameter count.
|
char const ** |
argv () |
|
Access command line parameters.
|
void |
removeDaemonArgs () |
|
Remove the daemon arguments from argc()/argv().
|
void |
logReopen () |
|
Reopen the log files.
|
static void |
exit (unsigned code=0) |
|
Terminate daemon with failure.
|
Protected Member Functions
|
|
Daemon () |
virtual void |
configure () |
|
Called before forking to configure the daemon class.
|
virtual void |
main () |
|
Called after forking to execute the main application.
|
virtual void |
init () |
|
Called to initialize the main application.
|
virtual void |
run () |
|
Called to execute main application.
|
Member Enumeration Documentation
enum senf::Daemon:: |
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 112 of file Daemon.hh.
Constructor & Destructor Documentation
senf::Daemon:: |
~Daemon |
() |
|
|
|
|
Member Function Documentation
int senf::Daemon:: |
argc |
() |
|
|
|
|
Access command line parameter count.
Definition at line 89 of file Daemon.cc.
char const ** senf::Daemon:: |
argv |
() |
|
|
|
|
Access command line parameters.
Definition at line 94 of file Daemon.cc.
void senf::Daemon:: |
configure |
() |
|
|
|
|
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 315 of file Daemon.cc.
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 117 of file Daemon.cc.
bool senf::Daemon:: |
daemon |
() |
|
|
|
|
true
, if running as daemon
Definition at line 84 of file Daemon.cc.
void senf::Daemon:: |
daemonize |
( |
bool |
v |
) |
|
Configure whether to run in fore- or background.
Definition at line 79 of file Daemon.cc.
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 194 of file Daemon.cc.
void senf::Daemon:: |
exit |
( |
unsigned |
code = 0
|
) |
|
Terminate daemon with failure.
Definition at line 243 of file Daemon.cc.
void senf::Daemon:: |
init |
() |
|
|
|
|
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 352 of file Daemon.cc.
void senf::Daemon:: |
logReopen |
() |
|
|
|
|
Reopen the log files.
This is used when rotating the logs. By default, SIGHUP calls logReopen.
Definition at line 153 of file Daemon.cc.
void senf::Daemon:: |
main |
() |
|
|
|
|
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 345 of file Daemon.cc.
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 182 of file Daemon.cc.
void senf::Daemon:: |
removeDaemonArgs |
() |
|
|
|
|
void senf::Daemon:: |
run |
() |
|
|
|
|
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 355 of file Daemon.cc.
int senf::Daemon:: |
start |
( |
int |
argc, |
|
|
|
char const ** |
argv |
) |
|
The documentation for this class was generated from the following files: