Public Member Functions | Protected Member Functions | Protected Attributes

base::Daemon Class Reference

Generic Daemon class. More...

#include <libppbase/base/Daemon.h>

Inheritance diagram for base::Daemon:
base::Mutex base::MTSocketDaemon base::STEventDaemon

List of all members.

Public Member Functions

virtual ~Daemon ()
 Destructor.
LoggetLog ()
 Gets the log object.
ConfiggetConfig ()
 Returns the configuration.
bool getForeground () const
 Returns foreground flag.
const StringgetName () 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)

Detailed Description

Generic Daemon class.

Author:
Christoph Schwarz
Version:
$Id: Daemon.h 21 2010-09-05 04:18:17Z cschwarz1 $

Constructor & Destructor Documentation

base::Daemon::Daemon ( const String name  )  [protected]

Constructor.

Parameters:
name daemon name

Member Function Documentation

Process::pid_type base::Daemon::daemonize ( bool  createpid = true,
bool  exitparent = true 
) [protected]

Goes into background.

Parameters:
createpid true to create a pid file
exitparent true if the parent should exit
Returns:
0 if it is the child, otherwise the pid of the child
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

Parameters:
force force dump, otherwise it is only dumped if the last dump was more than one minute ago
Returns:
true if the state has been dumped, false if not

Reimplemented in base::MTSocketDaemon, and base::STEventDaemon.

bool base::Daemon::getForeground (  )  const

Returns foreground flag.

Returns:
the foreground flag
gid_t base::Daemon::getGroupID (  )  const [protected]

Returns the group id that the daemon should run under.

Returns:
the group id
Log* base::Daemon::getLog (  ) 

Gets the log object.

Returns:
the log object
const String& base::Daemon::getName (  )  const

Gets the name of the daemon.

Returns:
the daemon name
Process::pid_type base::Daemon::getPid (  )  const

Returns pid.

Returns:
the daemon's process id
time_t base::Daemon::getTime (  )  const

Get current time.

Returns:
the current time as unix timestamp
void base::Daemon::getUptime ( String s  ) 

Returns uptime.

Parameters:
s returns the uptime
uid_t base::Daemon::getUserID (  )  const [protected]

Returns the user id that the daemon should run under.

Returns:
the user id
virtual bool base::Daemon::handleArgument ( int  arg  )  [protected, virtual]

Handles a command line argument.

Parameters:
arg the argument
Returns:
true if the argument has been handeled, false if not
bool base::Daemon::hasExplicitUGID (  )  const [protected]

Checks if a user/group id has been configured.

Returns:
true if yes, false if not
void base::Daemon::log ( Log::levels  level,
const char *  func,
const char *  fmt,
  ... 
)

Logs a message.

Parameters:
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.

Returns:
the daemon's exit code

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).

Returns:
true if it needs root privileges, false if not

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.

Parameters:
argc number of arguments
argv array containing arguments
args valid command line arguments for getopt()
Returns:
flag: success
int base::Daemon::service ( const char **  argv  ) 

Service method, initializes daemon.

Parameters:
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.

Parameters:
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.

Parameters:
level the log level
func the function logging the message
fmt the message in printf style
ap arguments to the message

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