WvStreams
wvdaemon.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2005 Net Integration Technologies, Inc.
4  *
5  * High-level abstraction for creating daemon processes. Handles
6  * command-line argument processing, forking into the background,
7  * and signal handling.
8  */
9 #ifndef __WVDAEMON_H
10 #define __WVDAEMON_H
11 
12 #include "wvstring.h"
13 #include "wvargs.h"
14 #include "wvlog.h"
15 
16 class WvDaemon;
17 
18 typedef wv::function<void()> WvDaemonCallback;
19 
85 class WvDaemon
86 {
87 
88  static WvDaemon *singleton;
89 
90  public:
91 
94  WvString version;
100  bool daemonize;
101 
107  WvLog::LogLevel log_level;
108  bool syslog;
109 
110  public:
111 
113  WvDaemonCallback load_callback;
114  WvDaemonCallback start_callback;
115  WvDaemonCallback run_callback;
116  WvDaemonCallback stop_callback;
117  WvDaemonCallback unload_callback;
118 
119  protected:
120 
121  virtual void do_load();
122  virtual void do_start();
123  virtual void do_run();
124  virtual void do_stop();
125  virtual void do_unload();
126 
127  private:
128  volatile bool _want_to_die;
129  volatile bool _want_to_restart;
130  volatile int _exit_status;
131 
132  void init(WvStringParm _name,
133  WvStringParm _version,
134  WvDaemonCallback _start_callback,
135  WvDaemonCallback _run_callback,
136  WvDaemonCallback _stop_callback);
137 
138  int _run(const char *argv0);
139 
140  bool set_daemonize(void *);
141 
142  protected:
143 
144  bool dec_log_level(void *)
145  {
146  if ((int)log_level > (int)WvLog::Critical)
147  log_level = (WvLog::LogLevel)((int)log_level - 1);
148  return true;
149  }
150 
151  bool inc_log_level(void *)
152  {
153  if ((int)log_level < (int)WvLog::Debug5)
154  log_level = (WvLog::LogLevel)((int)log_level + 1);
155  return true;
156  }
157 
158  WvStringList _extra_args;
159 
160  public:
161 
164  WvDaemon(WvStringParm _name, WvStringParm _version,
165  WvDaemonCallback _start_callback,
166  WvDaemonCallback _run_callback,
167  WvDaemonCallback _stop_callback):
168  log(_name, WvLog::Debug)
169  {
170  init(_name, _version, _start_callback, _run_callback,
171  _stop_callback);
172  }
173 
174  virtual ~WvDaemon();
175 
177  int run(const char *argv0);
179  int run(int argc, char **argv);
180 
182  void restart()
183  {
184  _want_to_restart = true;
185  }
187  void die(int status = 0)
188  {
189  _want_to_die = true;
190  _exit_status = status;
191  }
192 
194  bool want_to_restart() const
195  {
196  return _want_to_restart;
197  }
199  bool want_to_die() const
200  {
201  return _want_to_die;
202  }
203 
205  bool should_run() const
206  {
207  return !_want_to_die && !_want_to_restart;
208  }
209 
211  const WvStringList &extra_args() const
212  {
213  return _extra_args;
214  }
215 
216  static WvDaemon *me()
217  {
218  return singleton;
219  }
220 
221  public:
222  const char *wstype() const { return "WvDaemon"; }
223 };
224 
225 #endif // __WVDAEMON_H
WvDaemon::args
WvArgs args
The arguments the daemon accepts; the defaults are described above.
Definition: wvdaemon.h:104
WvDaemon::daemonize
bool daemonize
Whether the daemon should daemonize by default (it can be changed by the default options); defaults t...
Definition: wvdaemon.h:100
WvDaemon::pid_file
WvString pid_file
The path to the pid file to use for the daemon; defaults to /var/run/name.pid, where name is above.
Definition: wvdaemon.h:97
WvDaemon::restart
void restart()
Force the daemon to restart as soon as the run callback exits.
Definition: wvdaemon.h:182
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvLog
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
Definition: wvlog.h:56
WvArgs
WvArgs - Sane command-line argument processing for WvStreams.
Definition: wvargs.h:61
WvDaemon::want_to_restart
bool want_to_restart() const
Whether the daemon will restart when the run callback exits.
Definition: wvdaemon.h:194
WvDaemon::log
WvLog log
The daemon's log mechanism.
Definition: wvdaemon.h:106
WvDaemon::name
WvString name
The name and version of the daemon; used for -V and logging.
Definition: wvdaemon.h:93
WvDaemon::die
void die(int status=0)
Force the daemon to exit as soon as the run callback exits.
Definition: wvdaemon.h:187
WvDaemon
WvDaemon - High-level abstraction for creating daemon processes.
Definition: wvdaemon.h:85
WvDaemon::run
int run(const char *argv0)
Run the daemon with no argument processing. Returns exit status.
Definition: wvdaemon.cc:119
WvDaemon::WvDaemon
WvDaemon(WvStringParm _name, WvStringParm _version, WvDaemonCallback _start_callback, WvDaemonCallback _run_callback, WvDaemonCallback _stop_callback)
Construct a new daemon; requires the name, version, and optional userdata to be passed to the callbac...
Definition: wvdaemon.h:164
WvDaemon::extra_args
const WvStringList & extra_args() const
Remaining args.
Definition: wvdaemon.h:211
WvStringList
This is a WvList of WvStrings, and is a really handy way to parse strings.
Definition: wvstringlist.h:27
WvDaemon::load_callback
WvDaemonCallback load_callback
See the class description.
Definition: wvdaemon.h:113
WvDaemon::want_to_die
bool want_to_die() const
Whether the daemon will quit when the run callback exits.
Definition: wvdaemon.h:199
WvDaemon::should_run
bool should_run() const
Whether the daemon should continue runnning.
Definition: wvdaemon.h:205