Daemon process
The Daemon class provides simple management for daemon processes. Specifically, it implements
- Safe startup. If the startup fails, the foreground process which launches the daemon will terminate with an appropriate error exit code.
- Straight forward application initialization. The daemon process is forked before even initializing the application. The initialization procedure doesn't need to cater for a later fork().
- Automatic pid file management. The daemon will not be started, if a valid pid file is found. Stale pid files are automatically removed.
- Console log management. It is possible, to redirect standard output and error to one or two log files. Messages pertaining to application initialization will be written to both the console and the log file whereas later messages will be directed to the log file only.
- Optional foreground execution. The daemon may be started in the foreground for debugging purposes. In this case, the console log file(s) is/are automatically suppressed.
Starting the daemon process proceeds along the following steps:
- The daemon is started by calling the daemon class instances start() member. This normally happens from the
main()
function generated by SENF_DAEMON_MAIN().
- configure() is called. This (virtual) member configures the daemon manager by calling the Daemon class parameter members.
- The log files are opened,
fork()
is called and the pid file is checked and created. The parent (foreground) process keeps running overseeing the daemon process.
- main() is called. This virtual member may optionally be overridden in the derived class. Here we assume, main() is not overridden so the default implementation is used.
- main() calls init().
- after init() returns, main() calls detach().
- detach() signals successful startup to the parent process. The parent process terminates leaving the daemon process running in the background.
- main() calls run()
- If run() ever returns, the daemon process terminates.
- Whenever the process terminates normally (not necessarily successfully), the pid file is automatically removed.
The parameter members are used from configure() to configure the daemon manager. See below for details. The default configure() implementation will scan the command line arguments to check for the following parameters:
–no-daemon | Run in foreground
|
–console-log= stdout[, stderr] | Set the console log file(s). If only stdout is specified (with no comma), the same log file configuration will be applied to the standard output and error stream. Otherwise each stream is assigned it's own log file. If either log file name is empty, the command will not change the log file of that stream, the default log file will be used. If the log file name is set to 'none', the log file will be disabled.
|
–pid-file= pidfile | Set pid file path
|
The default configure() implementation will use whatever parameters have been set beforehand as default values. These default values should be set in a derived class configure() implementation. After setting the default values, the configure() implementation may choose to call this default configure() implementation to scan for command line parameters. The default configure() implementation does not completely parse the command line arguments. It just checks, if any of above arguments are present and processes them. Other arguments are completely ignored. The command line parameters should be completely processed within init().
Definition at line 94 of file Daemon.hh.
void senf::Daemon::consoleLog |
( |
std::string const & |
path, |
|
|
StdStream |
which = Both |
|
) |
| |
Configure console log file.
May be called multiple times to set the log file for stdout and stderr separately. Any standard stream not assigned to a log file will be redirected to /dev/null
.
When running in the foreground, the log files will be ignored.
Definition at line 121 of file Daemon.cc.
void senf::Daemon::init |
( |
| ) |
|
|
protectedvirtual |
Called to initialize the main application.
While init() is running, the application still is connected to the controlling terminal. Error messages will be shown to the user.
This member is only called, if the default main() implementation is not overridden.
Definition at line 378 of file Daemon.cc.