The Daemon class provides the infrastructure to implement robust daemon processes. A daemon process is implemented by deriving from senf::Daemon and implementing the necessary (virtual) member functions.
#include <senf/Utils/Daemon.hh> class MyDaemon : public senf::Daemon { void configure() { // Set configuration parameters like daemonize(), pidFile() etc consoleLog("MyDaemon.log"); // The default version provided by senf::Daemon will parse some special command line // parameters to configure the daemon manager. You may optionally call this version // here after setting default parameters senf::Daemon::configure(); } void init() { // Initialize application. Setup all necessary objects. After init() // has completed, the startup should not fail } void run() { // Main application code should be called here. } }; // Provide main() function SENF_DAEMON_MAIN(MyDaemon);
The startup procedure is divided into three steps:
#include <senf/Utils/Daemon.hh> class MyDaemon : public senf::Daemon { // 'configure()' like above. Don't implement 'init()' or 'run()' if you implement 'main()'. void main() { // Initialize application. Setup all necessary objects. When implementing main(), the // objects will most often live on the stack. MyAppObject app; if (some_error) // Call Daemon::exit() to terminate execution prematurely exit(1); // After initialization is complete, you *must* call 'detach()'. detach() // Now we can start the application main loop app.run(); } }; // Provide main() function SENF_DAEMON_MAIN(MyDaemon);