Daemon process management

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:

  • First, configure() is called. configure() should be as simple as possible. It just needs to set the daemon parameters. No further setup should be done here.
  • init() is called after fork() but while still connected to the terminal. init() should do all necessary application setup. Here, all configuration or user errors should be detected and properly diagnosed.
  • After init() returns, the application will detach from the terminal. Now run() is called to enter the application main loop.
Since there are times, where separating init() and run() into two separate functions is difficult, instead of defining init() and run(), the member main() may be defined. This member must call detach() as soon as initialization is completed to detach from the foreground terminal.
#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);
See also:
senf::Daemon class
SENF_DAEMON_MAIN() main() implementation macro