#include <wvdaemon.h>
Inheritance diagram for WvDaemon:
WvDaemon makes it easy to create daemon processes that support forking into the background and detaching from terminals, management of the .pid file and the log file, and handling of the SIGTERM|SIGINT|SIGQUIT and SIGHUP signals.
By default, daemons implemented through WvDaemon provide the following command line options:
-q|--quit: decrease the log level by one -v|--verbose: increase the log level by one -d|--daemonize: fork into the background (implies --syslog) -s|--syslog: write log entries to the syslog() facility --no-syslog: do not write log entries to the syslog() facility -V|--version: print the program name and version number and exit immediately
These default arguments can be changed or appended to through the public member WvDaemon::args of type WvArgs.
By default, daemons run in the foreground for debugging purposes; you must pass the -d parameter to force them into the background.
The actual functionality of WvDaemon is implemented through three protected member callbacks:
WvDaemon::start_callback: Called when the daemon first runs or after restarting due to SIGHUP WvDaemon::run_callback: The main loop callback. It should return if it ever expects that the daemon should exit or restart, ie. after having called WvDaemon::die() or WvDaemon::restart() WvDaemon::stop_callback: Called when the daemon is exiting or right before restarting due to SIGHUP
Sample usage:
#include <wvstreams/wvdaemon.h> void run(WvDaemon &daemon, void *) { int i = 1; while (daemon.should_run()) { wvout->print("Loop %s\n", i++); sleep(1); if (i == 17) daemon.die(); // Exit after 16 seconds } } int main(int argc, char **argv) { WvDaemon daemon("Sample Daemon", "0.1", WvDaemonCallback(), run, WvDaemonCallback()); return daemon.run(argc, argv); }
Definition at line 81 of file wvdaemon.h.
Public Member Functions | |
WvDaemon (WvStringParm _name, WvStringParm _version, WvDaemonCallback _start_callback, WvDaemonCallback _run_callback, WvDaemonCallback _stop_callback, void *_ud=NULL) | |
Construct a new daemon; requires the name, version, and three callbacks for the functionality of the daemon. | |
int | run (const char *argv0) |
Run the daemon with no argument processing. Returns exit status. | |
int | run (int argc, char **argv) |
Run the daemon after doing argument processing. Returns exit status. | |
void | restart () |
Force the daemon to restart as soon as the run callback exits. | |
void | die (int status=0) |
Force the daemon to exit as soon as the run callback exits. | |
bool | want_to_restart () const |
Whether the daemon will restart when the run callback exits. | |
bool | want_to_die () const |
Whether the daemon will quit when the run callback exits. | |
bool | should_run () const |
Whether the daemon should continue runnning. | |
const WvStringList & | extra_args () const |
Remaining args. | |
Public Attributes | |
WvString | name |
The name and version of the daemon; used for -V and logging. | |
WvString | version |
WvString | pid_file |
The path to the pid file to use for the daemon; defaults to /var/run/name.pid, where name is above. | |
bool | daemonize |
Whether the daemon should daemonize by default (it can be changed by the default options); defaults to false. | |
WvArgs | args |
The arguments the daemon accepts; the defaults are described above. | |
WvLog | log |
The daemon's log mechanism. | |
WvLog::LogLevel | log_level |
bool | syslog |
Protected Member Functions | |
bool | dec_log_level (void *) |
bool | inc_log_level (void *) |
Protected Attributes | |
WvDaemonCallback | start_callback |
See the class description. | |
WvDaemonCallback | run_callback |
WvDaemonCallback | stop_callback |
WvStringList | _extra_args |