WvStreams
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
WvDaemon Class Reference

WvDaemon - High-level abstraction for creating daemon processes. More...

#include <wvdaemon.h>

Inheritance diagram for WvDaemon:
Inheritance graph
[legend]

Public Member Functions

 WvDaemon (WvStringParm _name, WvStringParm _version, WvDaemonCallback _start_callback, WvDaemonCallback _run_callback, WvDaemonCallback _stop_callback)
 Construct a new daemon; requires the name, version, and optional userdata to be passed to the callbacks.
 
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 WvStringListextra_args () const
 Remaining args.
 
const char * wstype () const
 

Static Public Member Functions

static WvDaemonme ()
 

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
 
WvDaemonCallback load_callback
 See the class description.
 
WvDaemonCallback start_callback
 
WvDaemonCallback run_callback
 
WvDaemonCallback stop_callback
 
WvDaemonCallback unload_callback
 

Protected Member Functions

virtual void do_load ()
 
virtual void do_start ()
 
virtual void do_run ()
 
virtual void do_stop ()
 
virtual void do_unload ()
 
bool dec_log_level (void *)
 
bool inc_log_level (void *)
 

Protected Attributes

WvStringList _extra_args
 

Detailed Description

WvDaemon - High-level abstraction for creating daemon processes.

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|–quiet: 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 five protected member callbacks:

WvDaemon::load_callback: Called as soon as the arguments are processed and the process has (optionally) daemonized WvDaemon::start_callback: Called after WvDaemon::load_callback and after restarting due to SIGHUP WvDaemon::run_callback: The main loop callback.

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);
}
WvDaemon - High-level abstraction for creating daemon processes.
Definition wvdaemon.h:86
bool should_run() const
Whether the daemon should continue runnning.
Definition wvdaemon.h:205
int run(const char *argv0)
Run the daemon with no argument processing. Returns exit status.
Definition wvdaemon.cc:119
void die(int status=0)
Force the daemon to exit as soon as the run callback exits.
Definition wvdaemon.h:187

!

Definition at line 85 of file wvdaemon.h.

Constructor & Destructor Documentation

◆ WvDaemon()

WvDaemon::WvDaemon ( WvStringParm  _name,
WvStringParm  _version,
WvDaemonCallback  _start_callback,
WvDaemonCallback  _run_callback,
WvDaemonCallback  _stop_callback 
)
inline

Construct a new daemon; requires the name, version, and optional userdata to be passed to the callbacks.

Definition at line 164 of file wvdaemon.h.

◆ ~WvDaemon()

WvDaemon::~WvDaemon ( )
virtual

Definition at line 114 of file wvdaemon.cc.

Member Function Documentation

◆ do_load()

void WvDaemon::do_load ( )
protectedvirtual

Definition at line 245 of file wvdaemon.cc.

◆ do_start()

void WvDaemon::do_start ( )
protectedvirtual

Definition at line 299 of file wvdaemon.cc.

◆ do_run()

void WvDaemon::do_run ( )
protectedvirtual

Definition at line 306 of file wvdaemon.cc.

◆ do_stop()

void WvDaemon::do_stop ( )
protectedvirtual

Definition at line 313 of file wvdaemon.cc.

◆ do_unload()

void WvDaemon::do_unload ( )
protectedvirtual

Definition at line 320 of file wvdaemon.cc.

◆ dec_log_level()

bool WvDaemon::dec_log_level ( void *  )
inlineprotected

Definition at line 144 of file wvdaemon.h.

◆ inc_log_level()

bool WvDaemon::inc_log_level ( void *  )
inlineprotected

Definition at line 151 of file wvdaemon.h.

◆ run() [1/2]

int WvDaemon::run ( const char *  argv0)

Run the daemon with no argument processing. Returns exit status.

Definition at line 119 of file wvdaemon.cc.

References daemonize, log, and name.

Referenced by run().

◆ run() [2/2]

int WvDaemon::run ( int  argc,
char **  argv 
)

Run the daemon after doing argument processing. Returns exit status.

Definition at line 204 of file wvdaemon.cc.

References args, WvArgs::process(), and run().

◆ restart()

void WvDaemon::restart ( )
inline

Force the daemon to restart as soon as the run callback exits.

Definition at line 182 of file wvdaemon.h.

◆ die()

void WvDaemon::die ( int  status = 0)
inline

Force the daemon to exit as soon as the run callback exits.

Definition at line 187 of file wvdaemon.h.

◆ want_to_restart()

bool WvDaemon::want_to_restart ( ) const
inline

Whether the daemon will restart when the run callback exits.

Definition at line 194 of file wvdaemon.h.

◆ want_to_die()

bool WvDaemon::want_to_die ( ) const
inline

Whether the daemon will quit when the run callback exits.

Definition at line 199 of file wvdaemon.h.

◆ should_run()

bool WvDaemon::should_run ( ) const
inline

Whether the daemon should continue runnning.

Definition at line 205 of file wvdaemon.h.

◆ extra_args()

const WvStringList & WvDaemon::extra_args ( ) const
inline

Remaining args.

Definition at line 211 of file wvdaemon.h.

◆ me()

static WvDaemon * WvDaemon::me ( )
inlinestatic

Definition at line 216 of file wvdaemon.h.

◆ wstype()

const char * WvDaemon::wstype ( ) const
inline

Definition at line 222 of file wvdaemon.h.

Member Data Documentation

◆ name

WvString WvDaemon::name

The name and version of the daemon; used for -V and logging.

Definition at line 93 of file wvdaemon.h.

Referenced by run().

◆ version

WvString WvDaemon::version

Definition at line 94 of file wvdaemon.h.

◆ pid_file

WvString WvDaemon::pid_file

The path to the pid file to use for the daemon; defaults to /var/run/name.pid, where name is above.

Definition at line 97 of file wvdaemon.h.

◆ daemonize

bool WvDaemon::daemonize

Whether the daemon should daemonize by default (it can be changed by the default options); defaults to false.

Definition at line 100 of file wvdaemon.h.

Referenced by run().

◆ args

WvArgs WvDaemon::args

The arguments the daemon accepts; the defaults are described above.

Definition at line 104 of file wvdaemon.h.

Referenced by run().

◆ log

WvLog WvDaemon::log

The daemon's log mechanism.

Definition at line 106 of file wvdaemon.h.

Referenced by run().

◆ log_level

WvLog::LogLevel WvDaemon::log_level

Definition at line 107 of file wvdaemon.h.

◆ syslog

bool WvDaemon::syslog

Definition at line 108 of file wvdaemon.h.

◆ load_callback

WvDaemonCallback WvDaemon::load_callback

See the class description.

Definition at line 113 of file wvdaemon.h.

◆ start_callback

WvDaemonCallback WvDaemon::start_callback

Definition at line 114 of file wvdaemon.h.

◆ run_callback

WvDaemonCallback WvDaemon::run_callback

Definition at line 115 of file wvdaemon.h.

◆ stop_callback

WvDaemonCallback WvDaemon::stop_callback

Definition at line 116 of file wvdaemon.h.

◆ unload_callback

WvDaemonCallback WvDaemon::unload_callback

Definition at line 117 of file wvdaemon.h.

◆ _extra_args

WvStringList WvDaemon::_extra_args
protected

Definition at line 158 of file wvdaemon.h.


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