daemon Class Reference

Daemon class. More...

#include <daemon.h>

Inheritance diagram for daemon:
Inheritance graph
[legend]
Collaboration diagram for daemon:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 daemon (const string &title)
virtual ~daemon (void)
bool checkpid (void)
void writepid (void)
virtual int main (void)
void daemonize (bool delayedexit=false)
void delayedexitok (void)
void delayedexiterror (const char *parm,...)
void delayedexiterror (const string &text)
void log (log::priority prio, const string &moduleName, const char *fmt,...)
void log (log::priority prio, const string &moduleName, const string &text)
void addlogtarget (log::logtype type, const string &target, unsigned char priorities=0xff, unsigned int maxsz=0)
void setforeground (void)
void disablepidcheck (void)
bool settargetuser (const string &uname)
void setlogmodulewidth (int i)
void settargetuid (uid_t uid)
void settargetuid (uid_t ruid, uid_t euid)
void settargetgid (gid_t gid)
void settargetgid (gid_t rgid, gid_t egid)
bool settargetgroups (const value &grouplist)
void sendevent (const string &type)
void sendevent (const statstring &type, const value &v)
valuewaitevent (void)
valuewaitevent (int msec)

Static Public Member Functions

static void termhandler (int sig)
static void huphandler (int sig)

Protected Member Functions

void stoplog (void)

Protected Attributes

bool _foreground
bool daemonized
uid_t tuid
uid_t teuid
gid_t tgid
gid_t tegid
int tgroupcount
gid_t * tgroups
bool pidcheck
eventq events

Detailed Description

Daemon class.

Acts like an application except that it will spawn in the background. A log facility is added in that allows the daemon to write log messages to a file or to syslog. The targets are described in the application resource file (rsrc:resources.xml) as children of the ["log"] node. A typical layout:

<?xml version="1.0" encoding="utf-8"?>
<grace.rsrc>
  <string id="application-id">org.example.svc.mydaemon</string>
  <section id="log">
    <logtarget>
      <string id="type">syslog</string>
      <string id="target">myAppname</string>
      <dict id="priorities">
        <string>emergency</string>
        <string>alert</string>
        <string>critical</string>
        <string>error</string>
      </dict>
    </logtarget>
    <logtarget>
      <string id="type">file</string>
      <string id="target">/var/log/myAppname.log</string>
    </logtarget>
  </section>
</grace.rsrc>

If you want to define your logging somewhere other than the resources.xml file, you should manually add the target(s) using daemon::addlogtarget() before daemonize() is called.

Log messages sent before the system is daemonized are sent to the stderr stream and queued up for later logging. The reasoning behind this is that it's not possible to start threads before the fork() calls that relate to daemonization have been executed.


Constructor & Destructor Documentation

daemon::daemon ( const string title  ) 

Constructor.

Parameters:
title The application-id.

References _foreground, daemonized, pidcheck, tegid, teuid, tgid, tgroupcount, tgroups, and tuid.


Member Function Documentation

void daemon::addlogtarget ( log::logtype  type,
const string target,
unsigned char  priorities = 0xff,
unsigned int  maxsz = 0 
)

Manually add a log target.

Parameters:
type The logtype.
target The target string (for log::file).
priorities Bitmask of which priorities to route.
maxsz Maximum size of the logfile (for log;:file).

References logtarget::f, log::file, logtarget::maxsize, logtarget::next, file::openappend(), logtarget::priorities, logtarget::target, and logtarget::type.

Referenced by log().

Here is the call graph for this function:

bool daemon::checkpid ( void   ) 

Check for another running instance.

Looks for a pid-file and sends a null-signal to the process to see if it is still running.

Returns:
Result, true if there is no other instance.

References application::creator, string::crop(), filesystem::exists(), filesystem::load(), filesystem::save(), string::str(), string::strlen(), and filesystem::transr().

Referenced by daemonize().

Here is the call graph for this function:

void daemon::daemonize ( bool  delayedexit = false  ) 

Spawn to the background.

Unless if setforeground() was called, this will fork the process to the background, detaching from the terminal. The exit of the parent process can optionally be delayed until a later point, when the spawned process is done with its initialization and validation (if it needs to run threads to do this, it has to fork early or the threads will break). In such delayed cases, the parent process will keep a pipe open on the standard output channel of the child and wait for a line of input. If this input does not contain the literal string "OK", the parent process assumes initialization went awry and will exit itself with a status of 1. Otherwise it will use exit(0).

Parameters:
delayedexit Flag for the delayed exit.

References _foreground, checkpid(), application::creator, daemonized, file::gets(), huphandler(), file::openread(), pidcheck, file::printf(), string::str(), string::strcasecmp(), tegid, termhandler(), teuid, tgid, tgroupcount, tgroups, tuid, file::writeln(), and writepid().

Here is the call graph for this function:

void daemon::delayedexiterror ( const char *  parm,
  ... 
)

Background initialization failure report.

If the process was daemonized with delayedexit set to true, this method should be called if initialization failed for some reason. Expects printf-formatted arguments or a single string.

References file::close(), string::printf_va(), and file::writeln().

Here is the call graph for this function:

void daemon::delayedexitok ( void   ) 

Background initialization success report.

If the process was daemonized with delayedexit set to true, this method should be called once all background initialization has proceeded succesfully.

References file::close(), and file::writeln().

Here is the call graph for this function:

void daemon::disablepidcheck ( void   )  [inline]

Call this to disable checking of the pid-file during daemonize().

References pidcheck.

void daemon::huphandler ( int  sig  )  [static]

Static handler for SIGHUP.

Sends a 'reconfigure' event.

References sendevent().

Referenced by daemonize().

Here is the call graph for this function:

void daemon::log ( log::priority  prio,
const string moduleName,
const string text 
)

Send log message to the logthread.

Variation that accepts one string to accommodate format. Deprecated in favor of using global log::write.

Parameters:
prio The message priority.
moduleName A short description of the affected application subsystem.
text The log text.

References _foreground, addlogtarget(), log::alert, log::application, value::clear(), log::critical, daemonized, log::debug, log::emergency, log::error, value::exists(), log::file, log::info, value::ival(), lockbase::lockw(), application::rsrc, thread::sendevent(), thread::start(), logthread::startupCondition, log::syslog, lockbase::unlock(), conditional::wait(), log::warning, and file::writeln().

Here is the call graph for this function:

void daemon::log ( log::priority  prio,
const string moduleName,
const char *  fmt,
  ... 
)

Send log message to the logthread.

Deprecated in favor of global log::write.

Parameters:
prio The message priority.
moduleName A short description of the affected application subsystem.
fmt Printf-style formatted arguments.

References string::printf_va().

Referenced by log::write().

Here is the call graph for this function:

void daemon::sendevent ( const statstring type,
const value v 
)

Send an event to the main thread.

Parameters:
type The event type.
v Event parameter data.

References events, and eventq::send().

Here is the call graph for this function:

void daemon::sendevent ( const string type  ) 

Send an empty event to the main thread, with only the type() set.

Parameters:
type The event type.

References events, and eventq::send().

Referenced by huphandler(), and termhandler().

Here is the call graph for this function:

void daemon::setforeground ( void   ) 

Do not spawn to background.

This method flags the daemonize() method not to do the actual fork()s.

References _foreground.

void daemon::setlogmodulewidth ( int  i  )  [inline]

Set the desired width of the 'module' part of a log line in log::file logs.

References logthread::setmodulewidth().

Here is the call graph for this function:

void daemon::settargetgid ( gid_t  rgid,
gid_t  egid 
) [inline]

Set the real and effective groupid that will be active when we daemonize.

Parameters:
rgid The real gid.
egid The effective gid.

References tegid, and tgid.

void daemon::settargetgid ( gid_t  gid  )  [inline]

Set the real and effective groupid that will be active when we daemonize.

Parameters:
gid The real and effective gid.

References tegid, and tgid.

Referenced by settargetuser().

bool daemon::settargetgroups ( const value grouplist  ) 

Set the additional target groups.

Parameters:
grouplist An array of group names.

References value::count(), value::isempty(), tgroupcount, tgroups, systemclass::userdb, and value::uval().

Here is the call graph for this function:

void daemon::settargetuid ( uid_t  ruid,
uid_t  euid 
) [inline]

Set the real and effective userid that will be active when we daemonize.

Parameters:
ruid The real uid.
euid The effective uid.

References teuid, and tuid.

void daemon::settargetuid ( uid_t  uid  )  [inline]

Set the real and effective userid that will be active when we daemonize.

Parameters:
uid The real and effective uid.

References teuid, and tuid.

Referenced by settargetuser().

bool daemon::settargetuser ( const string uname  ) 

Set a target user/group for the spawned process, as specified by a unix account.

Will use the specified uid and primary gid for the account.

Parameters:
uname The unix username.

References systemclass::userdbFunctions::getpwnam(), settargetgid(), settargetuid(), and systemclass::userdb.

Here is the call graph for this function:

void daemon::termhandler ( int  sig  )  [static]

Static handler for SIGTERM.

Sends a 'shutdown' event.

References sendevent().

Referenced by daemonize().

Here is the call graph for this function:

value * daemon::waitevent ( int  msec  ) 

Wait a given amount of time for an event.

Parameters:
msec Timeout in milliseconds.
Returns:
A new event object or NULL.

References events, and eventq::waitevent().

Here is the call graph for this function:

value * daemon::waitevent ( void   ) 

Indefinitely wait for a new event.

Returns:
A new event object.

References events, and eventq::waitevent().

Here is the call graph for this function:

void daemon::writepid ( void   ) 

Write the daemon's pidfile to /var/run/application-id.pid.

This call is normally handdled by daemonize(). For daemons with a target uid, the pidfile is opened and written to by the invoking user (usually root at that point).

References filesystem::chown(), file::close(), application::creator, file::openwrite(), file::printf(), systemclass::proc, systemclass::procFunctions::self(), string::strlen(), tegid, teuid, filesystem::transr(), and filesystem::transw().

Referenced by daemonize().

Here is the call graph for this function:


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

Generated on Sun Feb 28 10:35:29 2010 for GRACE by  doxygen 1.6.1