3 // Copyright (C) 2006 Stefan Bund <g0dil@senf.berlios.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the
17 // Free Software Foundation, Inc.,
18 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 \brief DaemonTools public header */
23 /** \defgroup process Process Management
27 \li Daemon manager watching the real daemon. Auto-restart, when the daemon fails
29 \li Provide access to the config console indirectly via the daemon manager. This allows to
30 connect to the daemon manager console even if the app is not running
32 \li For this to be efficient, the daemon manager must be it's own executable (e.g. senf-launch)
34 \li auto-detect whether called from senf-launch or not
36 \li when daemon is running, the console is transparently forwarded to the daemon. The daemon
37 however can still access the daemon manager as a subgroup
39 \li No idea, whether this is sensible: Make the daemon manager completely self-contained (not
40 dependent on any external OS support) by providing our own log-file rotation support.
42 This collection of utilities provides help in managing daemon processes.
44 \idea A closeall()/closemost() function which is useful when starting child processes. We'll use
45 getrlimit to now the biggest filehandle and close all of em. closemost() takes a number of
46 file handles as arg and will keep those open.
48 \idea We might want to add other oft used utitlities: chroot(), setreuid(), pipes() / IPC ...
51 #ifndef HH_DaemonTools_
52 #define HH_DaemonTools_ 1
56 #include <boost/utility.hpp>
58 //#include "DaemonTools.mpp"
59 ///////////////////////////////hh.p////////////////////////////////////////
63 /** \brief Daemon process
65 This class provides the infrastructure to implement robust daemon processes. A daemon
66 process is implemented by deriving from senf::Daemon and implementing the necessary
67 (virtual) member functions.
69 class MyDaemon : public senf::Daemon
72 // Set configuration parameters like daemonize(), pidFile() etc.
76 // Initialize application. Setup all necessary objects. After init()
77 // has completed, is startup should not fail anymore
81 // Main application code should be called here.
86 The startup procedure is divided into three steps:
87 \li First, configure() is called. configure() should be as simple as possible. It just needs
88 to set the daemon parameters. No further setup should be done here.
89 \li init() is called after fork() but while still connected to the terminal. init() should
90 do all necessary application setup. Here, all configuration or user errors should be
91 detected and properly diagnosed.
92 \li After init() returns, the application will detach from the terminal. Now run() is called
93 to enter the application main loop.
95 Since there are times, where separating init() and run() into two separate functions is
96 difficult, instead of defining init() and run(), the member main() may be defined. This
97 member must call detach() as soon as initialization is completed to detach from the
104 class Daemon : boost::noncopyable
107 ///////////////////////////////////////////////////////////////////////////
110 /// Select standard stream to redirect
112 StdOut /** Standard output stream */
113 , StdErr /** Standard error stream */
114 , Both /** Both, standard output and error stream */
117 ///////////////////////////////////////////////////////////////////////////
118 ///\name Structors and default members
127 void daemonize(bool); ///< Configure whether to run in fore- or background
128 bool daemon(); ///< \c true, if running as daemon
130 void consoleLog(std::string, StdStream which = Both); ///< Configure console log file
131 /**< May be called multiple times to set the log file
132 for stdout and stderr seperately. Any standard stream
133 not assigned to a log file will be redirected to
136 When running in the foreground, the log files will be
139 void pidFile(std::string, bool unique = true); ///< Configure pid file
142 ///\name Auxiliary helpers
145 void detach(); ///< Detach into background now
146 /**< This is \e not the same as forking. The process will
147 already have forked into the background but until
148 detach() is called (either automatically after init()
149 returns or manually), the front end (foreground)
150 process will wait for the background process to ensure
151 successful startup. */
153 int argc(); ///< Access command line parameter count
154 char const ** argv(); ///< Access command line parameters
156 void fail(int code=1); ///< Terminate startup with failure
160 int start(int argc, char const ** argv); ///< Called from main() to launch daemon.
161 /**< Normally not called directly but from the
162 \ref SENF_DAEMON_MAIN macro. */
173 virtual void configure(); ///< Called before forking to configure the daemon class
174 virtual void main(); ///< Called after forking to execute the main application
175 /**< The default implementation will call init(), detach()
176 and then run(). It is preferred to override init() and
177 run() if possible. */
178 virtual void init(); ///< Called to initialize the main application
179 /**< While init() is running, the application still is
180 connected to the controlling terminal. Error messages
181 will be shown to the user.
183 This member is only called, if the default main()
184 implementation is not overridden. */
185 virtual void run(); ///< Called to execute main application
186 /**< Called after detaching from the controlling
189 This member is only called, if the default main()
190 implementation is not overridden. */
194 void pidfileCreate();
202 std::string pidfile_;
208 /** \brief Provide \c main() function
210 This macro will provide a \c main() function to launch the daemon process defined in \a
211 klass. \a klass must be a class derived from senf::Daemon.
215 # define SENF_DAEMON_MAIN(klass) \
216 int main(int argc, char const ** argv) \
219 return instance.start(argc, argv); \
222 /// \addtogroup process
225 void daemonize(); ///< Make the current process a daemon process
226 /**< daemonize() will fork, detach from the controlling
227 terminal and start a new process group. */
228 void redirect_stdio(std::string const & path = "/dev/null"); ///< Redirect STDIN, STDOUT and STDERR
229 /**< All standard file-descriptors will be redirected to the
230 given path defaulting to <tt>/dev/null</tt>
231 \param[in] path path to redirect to */
236 ///////////////////////////////hh.e////////////////////////////////////////
237 //#include "DaemonTools.cci"
238 //#include "DaemonTools.ct"
239 //#include "DaemonTools.cti"
240 //#include "DaemonTools.mpp"
247 // c-file-style: "senf"
248 // indent-tabs-mode: nil
249 // ispell-local-dictionary: "american"
250 // compile-command: "scons -u test"
251 // comment-column: 40