Daemon.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Utils_Daemon_Daemon_
18 #define HH_SENF_Utils_Daemon_Daemon_ 1
19 
20 // Custom includes
22 
23 //#include "Daemon.mpp"
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 
26 namespace senf {
27 
94  class Daemon : boost::noncopyable
95  {
96  public:
98 
99  //-/////////////////////////////////////////////////////////////////////////
100  // Types
101 
103  enum StdStream {
106  , Both
107  };
108 
109  //-/////////////////////////////////////////////////////////////////////////
111  //\{
112 
113  virtual ~Daemon();
114 
115  //\}
117  //\{
118 
119  void daemonize(bool);
120  bool daemon();
121 
122  void consoleLog(std::string const &, StdStream which = Both);
124 
132  void catchExceptions(bool flag);
133  void pidFile(std::string const &);
134 
138  //\}
140  //\{
141 
142  void detach();
143 
150  int argc();
151  char const ** argv();
152  void removeDaemonArgs();
153 
154  static void exit(unsigned code=0);
155 
156  void logReopen();
157 
160  //\}
161 
162  int start(int argc, char const ** argv);
163 
166  static Daemon & instance();
167 
168  protected:
169  Daemon();
170 
171  virtual void configure();
172 
175 # ifdef DOXYGEN
176  protected:
177 # else
178  private:
179 # endif
180 
181  virtual void main();
182 
185  virtual void init();
186 
192  virtual void run();
193 
198  private:
199  void openLog();
200  void fork();
201  bool pidfileCreate();
202  void installSighandlers();
203 
204  int argc_;
205  char const ** argv_;
206 
207  bool daemonize_;
208  std::string stdoutLog_;
209  std::string stderrLog_;
210  int stdout_;
211  int stderr_;
212  std::string pidfile_;
213  bool pidfileCreated_;
214  bool catchExceptions_;
215  bool detached_;
216 
217  static Daemon * instance_;
218  };
219 
227 # define SENF_DAEMON_MAIN(klass) \
228  int main(int argc, char const ** argv) \
229  { \
230  klass instance; \
231  return instance.start(argc, argv); \
232  }
233 
234 }
235 
236 //-/////////////////////////////////////////////////////////////////////////////////////////////////
237 //#include "Daemon.cci"
238 //#include "Daemon.ct"
239 //#include "Daemon.cti"
240 #endif
241 
242 
243 // Local Variables:
244 // mode: c++
245 // fill-column: 100
246 // comment-column: 40
247 // c-file-style: "senf"
248 // indent-tabs-mode: nil
249 // ispell-local-dictionary: "american"
250 // compile-command: "scons -u test"
251 // End:
void detach()
Detach into background now.
Definition: Daemon.cc:198
void daemonize(bool)
Configure whether to run in fore- or background.
Definition: Daemon.cc:72
virtual void configure()
Called before forking to configure the daemon class.
Definition: Daemon.cc:341
char const ** argv()
Access command line parameters.
Definition: Daemon.cc:92
Daemon process
Definition: Daemon.hh:94
virtual void run()
Called to execute main application.
Definition: Daemon.cc:381
#define SENF_LOG_CLASS_AREA()
StdStream
Select standard stream to redirect.
Definition: Daemon.hh:103
static void exit(unsigned code=0)
Terminate daemon with failure.
Definition: Daemon.cc:248
void catchExceptions(bool flag)
Definition: Daemon.cc:77
Definition: Daemon.hh:26
void pidFile(std::string const &)
Configure pid file.
Definition: Daemon.cc:186
void consoleLog(std::string const &, StdStream which=Both)
Configure console log file.
Definition: Daemon.cc:121
int start(int argc, char const **argv)
Called from main() to launch daemon.
Definition: Daemon.cc:263
static Daemon & instance()
Return the Daemon instance.
Definition: Daemon.cc:314
int argc()
Access command line parameter count.
Definition: Daemon.cc:87
virtual void init()
Called to initialize the main application.
Definition: Daemon.cc:378
bool daemon()
true, if running as daemon
Definition: Daemon.cc:82
void logReopen()
Reopen the log files.
Definition: Daemon.cc:157
void removeDaemonArgs()
Remove the daemon arguments from argc()/argv()
Definition: Daemon.cc:114
virtual void main()
Called after forking to execute the main application.
Definition: Daemon.cc:371
virtual ~Daemon()
Definition: Daemon.cc:60