4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer NETwork research (NET)
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Daemon public header */
30 #include <boost/utility.hpp>
32 //#include "Daemon.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
37 /** \brief Daemon process
39 This class provides the infrastructure to implement robust daemon processes. A daemon
40 process is implemented by deriving from senf::Daemon and implementing the necessary
41 (virtual) member functions.
43 class MyDaemon : public senf::Daemon
46 // Set configuration parameters like daemonize(), pidFile() etc.
50 // Initialize application. Setup all necessary objects. After init()
51 // has completed, is startup should not fail anymore
55 // Main application code should be called here.
60 The startup procedure is divided into three steps:
61 \li First, configure() is called. configure() should be as simple as possible. It just needs
62 to set the daemon parameters. No further setup should be done here.
63 \li init() is called after fork() but while still connected to the terminal. init() should
64 do all necessary application setup. Here, all configuration or user errors should be
65 detected and properly diagnosed.
66 \li After init() returns, the application will detach from the terminal. Now run() is called
67 to enter the application main loop.
69 Since there are times, where separating init() and run() into two separate functions is
70 difficult, instead of defining init() and run(), the member main() may be defined. This
71 member must call detach() as soon as initialization is completed to detach from the
74 class Daemon : boost::noncopyable
77 ///////////////////////////////////////////////////////////////////////////
80 /// Select standard stream to redirect
82 StdOut /** Standard output stream */
83 , StdErr /** Standard error stream */
84 , Both /** Both, standard output and error stream */
87 ///////////////////////////////////////////////////////////////////////////
88 ///\name Structors and default members
97 void daemonize(bool); ///< Configure whether to run in fore- or background
98 bool daemon(); ///< \c true, if running as daemon
100 void consoleLog(std::string, StdStream which = Both); ///< Configure console log file
101 /**< May be called multiple times to set the log file
102 for stdout and stderr seperately. Any standard stream
103 not assigned to a log file will be redirected to
106 When running in the foreground, the log files will be
109 void pidFile(std::string, bool unique = true); ///< Configure pid file
112 ///\name Auxiliary helpers
115 void detach(); ///< Detach into background now
116 /**< This is \e not the same as forking. The process will
117 already have forked into the background but until
118 detach() is called (either automatically after init()
119 returns or manually), the front end (foreground)
120 process will wait for the background process to ensure
121 successful startup. */
123 int argc(); ///< Access command line parameter count
124 char const ** argv(); ///< Access command line parameters
126 void fail(int code=1); ///< Terminate startup with failure
130 int start(int argc, char const ** argv); ///< Called from main() to launch daemon.
131 /**< Normally not called directly but from the
132 \ref SENF_DAEMON_MAIN macro. */
143 virtual void configure(); ///< Called before forking to configure the daemon class
144 virtual void main(); ///< Called after forking to execute the main application
145 /**< The default implementation will call init(), detach()
146 and then run(). It is preferred to override init() and
147 run() if possible. */
148 virtual void init(); ///< Called to initialize the main application
149 /**< While init() is running, the application still is
150 connected to the controlling terminal. Error messages
151 will be shown to the user.
153 This member is only called, if the default main()
154 implementation is not overridden. */
155 virtual void run(); ///< Called to execute main application
156 /**< Called after detaching from the controlling
159 This member is only called, if the default main()
160 implementation is not overridden. */
164 void pidfileCreate();
172 std::string pidfile_;
178 /** \brief Provide \c main() function
180 This macro will provide a \c main() function to launch the daemon process defined in \a
181 klass. \a klass must be a class derived from senf::Daemon.
185 # define SENF_DAEMON_MAIN(klass) \
186 int main(int argc, char const ** argv) \
189 return instance.start(argc, argv); \
194 ///////////////////////////////hh.e////////////////////////////////////////
195 //#include "Daemon.cci"
196 //#include "Daemon.ct"
197 //#include "Daemon.cti"
204 // comment-column: 40
205 // c-file-style: "senf"
206 // indent-tabs-mode: nil
207 // ispell-local-dictionary: "american"
208 // compile-command: "scons -u test"