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
16class WvDaemon;
17
18typedef wv::function<void()> WvDaemonCallback;
19
86{
87
88 static WvDaemon *singleton;
89
90 public:
91
94 WvString version;
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
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
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
WvArgs - Sane command-line argument processing for WvStreams.
Definition wvargs.h:62
WvDaemon - High-level abstraction for creating daemon processes.
Definition wvdaemon.h:86
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
WvString name
The name and version of the daemon; used for -V and logging.
Definition wvdaemon.h:93
WvDaemonCallback load_callback
See the class description.
Definition wvdaemon.h:113
const WvStringList & extra_args() const
Remaining args.
Definition wvdaemon.h:211
bool should_run() const
Whether the daemon should continue runnning.
Definition wvdaemon.h:205
int run(const char *argv0)
Run the daemon with no argument processing. Returns exit status.
Definition wvdaemon.cc:119
bool daemonize
Whether the daemon should daemonize by default (it can be changed by the default options); defaults t...
Definition wvdaemon.h:100
WvLog log
The daemon's log mechanism.
Definition wvdaemon.h:106
bool want_to_restart() const
Whether the daemon will restart when the run callback exits.
Definition wvdaemon.h:194
void die(int status=0)
Force the daemon to exit as soon as the run callback exits.
Definition wvdaemon.h:187
bool want_to_die() const
Whether the daemon will quit when the run callback exits.
Definition wvdaemon.h:199
WvArgs args
The arguments the daemon accepts; the defaults are described above.
Definition wvdaemon.h:104
void restart()
Force the daemon to restart as soon as the run callback exits.
Definition wvdaemon.h:182
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
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition wvstring.h:94
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
Definition wvlog.h:57
This is a WvList of WvStrings, and is a really handy way to parse strings.
WvString is an implementation of a simple and efficient printable-string class.
Definition wvstring.h:330