00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #ifndef HH_SENF_Utils_Daemon_Daemon_
00027 #define HH_SENF_Utils_Daemon_Daemon_ 1
00028
00029
00030 #include <senf/Utils/Logger/SenfLog.hh>
00031
00032
00033
00034
00035 namespace senf {
00036
00103 class Daemon : boost::noncopyable
00104 {
00105 public:
00106 SENF_LOG_CLASS_AREA();
00107
00108
00109
00110
00112 enum StdStream {
00113 StdOut
00114 , StdErr
00115 , Both
00116 };
00117
00118
00120
00121
00122 virtual ~Daemon();
00123
00124
00126
00127
00128 void daemonize(bool);
00129 bool daemon();
00130
00131 void consoleLog(std::string const &, StdStream which = Both);
00133
00141 void pidFile(std::string const &);
00142
00146
00148
00149
00150 void detach();
00151
00158 int argc();
00159 char const ** argv();
00160 void removeDaemonArgs();
00161
00162 static void exit(unsigned code=0);
00163
00164 void logReopen();
00165
00168
00169
00170 int start(int argc, char const ** argv);
00171
00174 static Daemon & instance();
00175
00176 protected:
00177 Daemon();
00178
00179 virtual void configure();
00180
00183 # ifdef DOXYGEN
00184 protected:
00185 # else
00186 private:
00187 # endif
00188
00189 virtual void main();
00190
00193 virtual void init();
00194
00200 virtual void run();
00201
00206 private:
00207 void openLog();
00208 void fork();
00209 bool pidfileCreate();
00210 void installSighandlers();
00211
00212 int argc_;
00213 char const ** argv_;
00214
00215 bool daemonize_;
00216 std::string stdoutLog_;
00217 std::string stderrLog_;
00218 int stdout_;
00219 int stderr_;
00220 std::string pidfile_;
00221 bool pidfileCreated_;
00222
00223 bool detached_;
00224
00225 static Daemon * instance_;
00226 };
00227
00235 # define SENF_DAEMON_MAIN(klass) \
00236 int main(int argc, char const ** argv) \
00237 { \
00238 klass instance; \
00239 return instance.start(argc, argv); \
00240 }
00241
00242 }
00243
00244
00245 #include "Daemon.cci"
00246
00247
00248 #endif
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259