Daemon class. More...
#include <daemon.h>


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) |
| value * | waitevent (void) |
| value * | waitevent (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 |
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.
| daemon::daemon | ( | const string & | title | ) |
Constructor.
| title | The application-id. |
References _foreground, daemonized, pidcheck, tegid, teuid, tgid, tgroupcount, tgroups, and tuid.
| void daemon::addlogtarget | ( | log::logtype | type, | |
| const string & | target, | |||
| unsigned char | priorities = 0xff, |
|||
| unsigned int | maxsz = 0 | |||
| ) |
Manually add a log target.
| 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().

| 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.
References application::creator, string::crop(), filesystem::exists(), filesystem::load(), filesystem::save(), string::str(), string::strlen(), and filesystem::transr().
Referenced by daemonize().

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

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

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

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

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

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

| void daemon::sendevent | ( | const statstring & | type, | |
| const value & | v | |||
| ) |
Send an event to the main thread.
| type | The event type. | |
| v | Event parameter data. |
References events, and eventq::send().

| void daemon::sendevent | ( | const string & | type | ) |
Send an empty event to the main thread, with only the type() set.
| type | The event type. |
References events, and eventq::send().
Referenced by huphandler(), and termhandler().

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

| void daemon::settargetgid | ( | gid_t | rgid, | |
| gid_t | egid | |||
| ) | [inline] |
| void daemon::settargetgid | ( | gid_t | gid | ) | [inline] |
Set the real and effective groupid that will be active when we daemonize.
| gid | The real and effective gid. |
Referenced by settargetuser().
| bool daemon::settargetgroups | ( | const value & | grouplist | ) |
Set the additional target groups.
| grouplist | An array of group names. |
References value::count(), value::isempty(), tgroupcount, tgroups, systemclass::userdb, and value::uval().

| void daemon::settargetuid | ( | uid_t | ruid, | |
| uid_t | euid | |||
| ) | [inline] |
| void daemon::settargetuid | ( | uid_t | uid | ) | [inline] |
Set the real and effective userid that will be active when we daemonize.
| uid | The real and effective uid. |
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.
| uname | The unix username. |
References systemclass::userdbFunctions::getpwnam(), settargetgid(), settargetuid(), and systemclass::userdb.

| void daemon::termhandler | ( | int | sig | ) | [static] |
Static handler for SIGTERM.
Sends a 'shutdown' event.
References sendevent().
Referenced by daemonize().

| value * daemon::waitevent | ( | int | msec | ) |
Wait a given amount of time for an event.
| msec | Timeout in milliseconds. |
References events, and eventq::waitevent().

| value * daemon::waitevent | ( | void | ) |
Indefinitely wait for a new event.
References events, and eventq::waitevent().

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

1.6.1