// Custom includes
#include <string>
-#include <boost/utility.hpp>
//#include "DaemonTools.mpp"
///////////////////////////////hh.p////////////////////////////////////////
namespace senf {
- /** \brief Daemon process
-
- This 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.
- \code
- class MyDaemon : public senf::Daemon
- {
- void configure() {
- // Set configuration parameters like daemonize(), pidFile() etc.
- }
-
- void init() {
- // Initialize application. Setup all necessary objects. After init()
- // has completed, is startup should not fail anymore
- }
-
- void run() {
- // Main application code should be called here.
- }
- };
- \endcode
-
- The startup procedure is divided into three steps:
- \li 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.
- \li 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.
- \li 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.
-
-
-
- \ingroup process
- */
- class Daemon : boost::noncopyable
- {
- public:
- ///////////////////////////////////////////////////////////////////////////
- // Types
-
- /// Select standard stream to redirect
- enum StdStream {
- StdOut /** Standard output stream */
- , StdErr /** Standard error stream */
- , Both /** Both, standard output and error stream */
- };
-
- ///////////////////////////////////////////////////////////////////////////
- ///\name Structors and default members
- ///\{
-
- virtual ~Daemon();
-
- ///\}
- ///\name Parameters
- ///\{
-
- void daemonize(bool); ///< Configure whether to run in fore- or background
- bool daemon(); ///< \c true, if running as daemon
-
- void consoleLog(std::string, StdStream which = Both); ///< Configure console log file
- /**< May be called multiple times to set the log file
- for stdout and stderr seperately. Any standard stream
- not assigned to a log file will be redirected to
- <tt>/dev/null</tt>.
-
- When running in the foreground, the log files will be
- ignored. */
-
- void pidFile(std::string, bool unique = true); ///< Configure pid file
-
- ///\}
- ///\name Auxiliary helpers
- ///\{
-
- void detach(); ///< Detach into background now
- /**< This is \e not the same as forking. The process will
- already have forked into the background but until
- detach() is called (either automatically after init()
- returns or manually), the front end (foreground)
- process will wait for the background process to ensure
- successful startup. */
-
- int argc(); ///< Access command line parameter count
- char const ** argv(); ///< Access command line parameters
-
- void fail(int code=1); ///< Terminate startup with failure
-
- ///\}
-
- int start(int argc, char const ** argv); ///< Called from main() to launch daemon.
- /**< Normally not called directly but from the
- \ref SENF_DAEMON_MAIN macro. */
-
- protected:
- Daemon();
-
-# ifdef DOXYGEN
- protected:
-# else
- private:
-# endif
-
- virtual void configure(); ///< Called before forking to configure the daemon class
- virtual void main(); ///< Called after forking to execute the main application
- /**< The default implementation will call init(), detach()
- and then run(). It is preferred to override init() and
- run() if possible. */
- virtual void init(); ///< 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. */
- virtual void run(); ///< Called to execute main application
- /**< Called after detaching from the controlling
- terminal.
-
- This member is only called, if the default main()
- implementation is not overridden. */
- private:
-
- void fork();
- void pidfileCreate();
-
- int argc_;
- char const ** argv_;
-
- bool daemonize_;
- int stdout_;
- int stderr_;
- std::string pidfile_;
- bool unique_;
-
- bool detached_;
- };
-
- /** \brief Provide \c main() function
-
- This macro will provide a \c main() function to launch the daemon process defined in \a
- klass. \a klass must be a class derived from senf::Daemon.
-
- \ingroup process
- */
-# define SENF_DAEMON_MAIN(klass) \
- int main(int argc, char const ** argv) \
- { \
- klass instance; \
- return instance.start(argc, argv); \
- }
-
/// \addtogroup process
/// @{