/usr/include/wvstreams/wvdaemon.h is in libwvstreams-dev 4.6.1-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | /* -*- Mode: C++ -*-
* Worldvisions Weaver Software:
* Copyright (C) 1997-2005 Net Integration Technologies, Inc.
*
* High-level abstraction for creating daemon processes. Handles
* command-line argument processing, forking into the background,
* and signal handling.
*/
#ifndef __WVDAEMON_H
#define __WVDAEMON_H
#include "wvstring.h"
#include "wvargs.h"
#include "wvlog.h"
class WvDaemon;
typedef wv::function<void()> WvDaemonCallback;
/*!
@brief WvDaemon - High-level abstraction for creating daemon processes.
WvDaemon makes it easy to create daemon processes that support forking
into the background and detaching from terminals, management of the .pid
file and the log file, and handling of the SIGTERM|SIGINT|SIGQUIT
and SIGHUP signals.
By default, daemons implemented through WvDaemon provide the following
command line options:
-q|--quiet: decrease the log level by one
-v|--verbose: increase the log level by one
-d|--daemonize: fork into the background (implies --syslog)
-s|--syslog: write log entries to the syslog() facility
--no-syslog: do not write log entries to the syslog() facility
-V|--version: print the program name and version number and exit immediately
These default arguments can be changed or appended to through the public member
WvDaemon::args of type WvArgs.
By default, daemons run in the foreground for debugging purposes; you must
pass the -d parameter to force them into the background.
The actual functionality of WvDaemon is implemented through five protected
member callbacks:
WvDaemon::load_callback: Called as soon as the arguments are processed
and the process has (optionally) daemonized
WvDaemon::start_callback: Called after WvDaemon::load_callback and after
restarting due to SIGHUP
WvDaemon::run_callback: The main loop callback.
- It must return if it ever expects that the daemon should
exit or restart, ie. after having called WvDaemon::die()
or WvDaemon::restart(); otherwise the daemon will never exit
WvDaemon::stop_callback: Called when the daemon is exiting or right before
restarting due to SIGHUP
WvDaemon::unload_callback: Called right before the daemon exits
Sample usage:
@code
#include <wvstreams/wvdaemon.h>
void run(WvDaemon &daemon, void *)
{
int i = 1;
while (daemon.should_run())
{
wvout->print("Loop %s\n", i++);
sleep(1);
if (i == 17)
daemon.die(); // Exit after 16 seconds
}
}
int main(int argc, char **argv)
{
WvDaemon daemon("Sample Daemon", "0.1",
WvDaemonCallback(), run, WvDaemonCallback());
return daemon.run(argc, argv);
}
@endcode
!*/
class WvDaemon
{
static WvDaemon *singleton;
public:
//! The name and version of the daemon; used for -V and logging
WvString name;
WvString version;
//! The path to the pid file to use for the daemon; defaults
//! to /var/run/name.pid, where name is above
WvString pid_file;
//! Whether the daemon should daemonize by default (it can
//! be changed by the default options); defaults to false
bool daemonize;
//! The arguments the daemon accepts; the defaults are described
//! above.
WvArgs args;
//! The daemon's log mechanism
WvLog log;
WvLog::LogLevel log_level;
bool syslog;
public:
//! See the class description
WvDaemonCallback load_callback;
WvDaemonCallback start_callback;
WvDaemonCallback run_callback;
WvDaemonCallback stop_callback;
WvDaemonCallback unload_callback;
protected:
virtual void do_load();
virtual void do_start();
virtual void do_run();
virtual void do_stop();
virtual void do_unload();
private:
volatile bool _want_to_die;
volatile bool _want_to_restart;
volatile int _exit_status;
void init(WvStringParm _name,
WvStringParm _version,
WvDaemonCallback _start_callback,
WvDaemonCallback _run_callback,
WvDaemonCallback _stop_callback);
int _run(const char *argv0);
bool set_daemonize(void *);
protected:
bool dec_log_level(void *)
{
if ((int)log_level > (int)WvLog::Critical)
log_level = (WvLog::LogLevel)((int)log_level - 1);
return true;
}
bool inc_log_level(void *)
{
if ((int)log_level < (int)WvLog::Debug5)
log_level = (WvLog::LogLevel)((int)log_level + 1);
return true;
}
WvStringList _extra_args;
public:
//! Construct a new daemon; requires the name, version,
//! and optional userdata to be passed to the callbacks
WvDaemon(WvStringParm _name, WvStringParm _version,
WvDaemonCallback _start_callback,
WvDaemonCallback _run_callback,
WvDaemonCallback _stop_callback):
log(_name, WvLog::Debug)
{
init(_name, _version, _start_callback, _run_callback,
_stop_callback);
}
virtual ~WvDaemon();
//! Run the daemon with no argument processing. Returns exit status.
int run(const char *argv0);
//! Run the daemon after doing argument processing. Returns exit status.
int run(int argc, char **argv);
//! Force the daemon to restart as soon as the run callback exits
void restart()
{
_want_to_restart = true;
}
//! Force the daemon to exit as soon as the run callback exits
void die(int status = 0)
{
_want_to_die = true;
_exit_status = status;
}
//! Whether the daemon will restart when the run callback exits
bool want_to_restart() const
{
return _want_to_restart;
}
//! Whether the daemon will quit when the run callback exits
bool want_to_die() const
{
return _want_to_die;
}
//! Whether the daemon should continue runnning
bool should_run() const
{
return !_want_to_die && !_want_to_restart;
}
//! Remaining args
const WvStringList &extra_args() const
{
return _extra_args;
}
static WvDaemon *me()
{
return singleton;
}
public:
const char *wstype() const { return "WvDaemon"; }
};
#endif // __WVDAEMON_H
|