#include <libppbase/base/Daemon.h>
Public Member Functions | |
virtual | ~Daemon () |
Destructor. | |
Log * | getLog () |
Gets the log object. | |
Config * | getConfig () |
Returns the configuration. | |
bool | getForeground () const |
Returns foreground flag. | |
const String & | getName () const |
Gets the name of the daemon. | |
Process::pid_type | getPid () const |
Returns pid. | |
time_t | getTime () const |
Get current time. | |
void | getUptime (String &s) |
Returns uptime. | |
void | log (Log::levels level, const char *func, const char *fmt,...) |
Logs a message. | |
bool | readArguments (int argc, const char **argv, const char *args="c:d:fhqsvVw") |
Parses command line arguments and configures daemon. | |
int | service (const char **argv) |
Service method, initializes daemon. | |
Protected Member Functions | |
Daemon (const String &name) | |
Constructor. | |
virtual void | config () |
Configures the daemon. | |
virtual void | createThreads (bool init) |
Creates threads. | |
Process::pid_type | daemonize (bool createpid=true, bool exitparent=true) |
Goes into background. | |
void | dropRootPrivileges () |
Changes uid to an non-privileged user. | |
virtual bool | dumpState (bool force) |
Dumps the daemon state into the log file. | |
gid_t | getGroupID () const |
Returns the group id that the daemon should run under. | |
uid_t | getUserID () const |
Returns the user id that the daemon should run under. | |
virtual bool | handleArgument (int arg) |
Handles a command line argument. | |
bool | hasExplicitUGID () const |
Checks if a user/group id has been configured. | |
virtual int | loop ()=0 |
The main loop. | |
virtual bool | needRootPrivileges () |
Checks if the daemon needs root privileges (e.g. to allocate ports under 1024). | |
int | serviceMain () |
Service method, runs loop() and catches exceptions. | |
virtual void | shutdown () |
Can be overridden by a derived daemon to do shudown cleanup. | |
virtual void | startup () |
Can be overridden by a derived daemon to do startup initialization. | |
virtual void | terminateThreads () |
Terminates all worker threads. | |
virtual void | usage (const char *pgm, bool err) const |
Displays usage information. | |
virtual void | vlog (Log::levels level, const char *func, const char *fmt, va_list ap) |
Logs a message. | |
Protected Attributes | |
Config | _cfg |
daemon configuration | |
String | _cfgfile |
configuration file | |
bool | _foreground |
flag: run in foreground (-f switch) | |
u_longlong_t | _laststatedump |
timestamp of last state dump | |
Log * | _log |
log object | |
bool | _logstderr |
flag: log to stderr | |
int | _maxopenfiles |
maximum number of open files per process | |
String | _name |
name of daemon | |
Process::pid_type | _pid |
process id of daemon process | |
time_t | _t_now |
current time | |
time_t | _t_start |
start time of daemon | |
int | _fdreserve |
number of file descriptors to reserve | |
String | _pidfile |
pid file name | |
bool | _watcher |
flag: use watcher mode (-w switch) |
Generic Daemon class.
base::Daemon::Daemon | ( | const String & | name | ) | [protected] |
Constructor.
name | daemon name |
Process::pid_type base::Daemon::daemonize | ( | bool | createpid = true , |
|
bool | exitparent = true | |||
) | [protected] |
Goes into background.
createpid | true to create a pid file | |
exitparent | true if the parent should exit |
virtual bool base::Daemon::dumpState | ( | bool | force | ) | [protected, virtual] |
Dumps the daemon state into the log file.
The daemon state contains information on all worker threads
force | force dump, otherwise it is only dumped if the last dump was more than one minute ago |
Reimplemented in base::MTSocketDaemon, and base::STEventDaemon.
bool base::Daemon::getForeground | ( | ) | const |
Returns foreground flag.
gid_t base::Daemon::getGroupID | ( | ) | const [protected] |
Returns the group id that the daemon should run under.
Log* base::Daemon::getLog | ( | ) |
Gets the log object.
const String& base::Daemon::getName | ( | ) | const |
Gets the name of the daemon.
Process::pid_type base::Daemon::getPid | ( | ) | const |
Returns pid.
time_t base::Daemon::getTime | ( | ) | const |
Get current time.
void base::Daemon::getUptime | ( | String & | s | ) |
Returns uptime.
s | returns the uptime |
uid_t base::Daemon::getUserID | ( | ) | const [protected] |
Returns the user id that the daemon should run under.
virtual bool base::Daemon::handleArgument | ( | int | arg | ) | [protected, virtual] |
Handles a command line argument.
arg | the argument |
bool base::Daemon::hasExplicitUGID | ( | ) | const [protected] |
Checks if a user/group id has been configured.
void base::Daemon::log | ( | Log::levels | level, | |
const char * | func, | |||
const char * | fmt, | |||
... | ||||
) |
Logs a message.
level | the log level | |
func | the function logging the message | |
fmt | the message in printf style | |
... | arguments to the message |
virtual int base::Daemon::loop | ( | ) | [protected, pure virtual] |
The main loop.
Implemented in base::MTSocketDaemon, and base::STEventDaemon.
virtual bool base::Daemon::needRootPrivileges | ( | ) | [protected, virtual] |
Checks if the daemon needs root privileges (e.g. to allocate ports under 1024).
Reimplemented in base::MTSocketDaemon.
bool base::Daemon::readArguments | ( | int | argc, | |
const char ** | argv, | |||
const char * | args = "c:d:fhqsvVw" | |||
) |
Parses command line arguments and configures daemon.
argc | number of arguments | |
argv | array containing arguments | |
args | valid command line arguments for getopt() |
int base::Daemon::service | ( | const char ** | argv | ) |
Service method, initializes daemon.
argv | the command line arguments |
virtual void base::Daemon::shutdown | ( | ) | [protected, virtual] |
Can be overridden by a derived daemon to do shudown cleanup.
It is called by the framework when the daemon shuts down, after all framework-controlled threads have terminated and sockets have been released
virtual void base::Daemon::startup | ( | ) | [protected, virtual] |
Can be overridden by a derived daemon to do startup initialization.
It is called by the framework when the daemon starts up, after configuration has been read and before threads are created and sockets are allocated
virtual void base::Daemon::usage | ( | const char * | pgm, | |
bool | err | |||
) | const [protected, virtual] |
Displays usage information.
pgm | the program name (pass argv[0] here); | |
err | true to output it on standard error, false to output on standard out |
virtual void base::Daemon::vlog | ( | Log::levels | level, | |
const char * | func, | |||
const char * | fmt, | |||
va_list | ap | |||
) | [protected, virtual] |
Logs a message.
level | the log level | |
func | the function logging the message | |
fmt | the message in printf style | |
ap | arguments to the message |