WvStreams
|
00001 /* -*- Mode: C++ -*- 00002 * Worldvisions Weaver Software: 00003 * Copyright (C) 1997-2005 Net Integration Technologies, Inc. 00004 * 00005 * High-level abstraction for creating daemon processes. Handles 00006 * command-line argument processing, forking into the background, 00007 * and signal handling. 00008 */ 00009 #ifndef __WVDAEMON_H 00010 #define __WVDAEMON_H 00011 00012 #include "wvstring.h" 00013 #include "wvargs.h" 00014 #include "wvlog.h" 00015 00016 class WvDaemon; 00017 00018 typedef wv::function<void()> WvDaemonCallback; 00019 00085 class WvDaemon 00086 { 00087 00088 static WvDaemon *singleton; 00089 00090 public: 00091 00093 WvString name; 00094 WvString version; 00097 WvString pid_file; 00100 bool daemonize; 00101 00104 WvArgs args; 00106 WvLog log; 00107 WvLog::LogLevel log_level; 00108 bool syslog; 00109 00110 public: 00111 00113 WvDaemonCallback load_callback; 00114 WvDaemonCallback start_callback; 00115 WvDaemonCallback run_callback; 00116 WvDaemonCallback stop_callback; 00117 WvDaemonCallback unload_callback; 00118 00119 protected: 00120 00121 virtual void do_load(); 00122 virtual void do_start(); 00123 virtual void do_run(); 00124 virtual void do_stop(); 00125 virtual void do_unload(); 00126 00127 private: 00128 volatile bool _want_to_die; 00129 volatile bool _want_to_restart; 00130 volatile int _exit_status; 00131 00132 void init(WvStringParm _name, 00133 WvStringParm _version, 00134 WvDaemonCallback _start_callback, 00135 WvDaemonCallback _run_callback, 00136 WvDaemonCallback _stop_callback); 00137 00138 int _run(const char *argv0); 00139 00140 bool set_daemonize(void *); 00141 00142 protected: 00143 00144 bool dec_log_level(void *) 00145 { 00146 if ((int)log_level > (int)WvLog::Critical) 00147 log_level = (WvLog::LogLevel)((int)log_level - 1); 00148 return true; 00149 } 00150 00151 bool inc_log_level(void *) 00152 { 00153 if ((int)log_level < (int)WvLog::Debug5) 00154 log_level = (WvLog::LogLevel)((int)log_level + 1); 00155 return true; 00156 } 00157 00158 WvStringList _extra_args; 00159 00160 public: 00161 00164 WvDaemon(WvStringParm _name, WvStringParm _version, 00165 WvDaemonCallback _start_callback, 00166 WvDaemonCallback _run_callback, 00167 WvDaemonCallback _stop_callback): 00168 log(_name, WvLog::Debug) 00169 { 00170 init(_name, _version, _start_callback, _run_callback, 00171 _stop_callback); 00172 } 00173 00174 virtual ~WvDaemon(); 00175 00177 int run(const char *argv0); 00179 int run(int argc, char **argv); 00180 00182 void restart() 00183 { 00184 _want_to_restart = true; 00185 } 00187 void die(int status = 0) 00188 { 00189 _want_to_die = true; 00190 _exit_status = status; 00191 } 00192 00194 bool want_to_restart() const 00195 { 00196 return _want_to_restart; 00197 } 00199 bool want_to_die() const 00200 { 00201 return _want_to_die; 00202 } 00203 00205 bool should_run() const 00206 { 00207 return !_want_to_die && !_want_to_restart; 00208 } 00209 00211 const WvStringList &extra_args() const 00212 { 00213 return _extra_args; 00214 } 00215 00216 static WvDaemon *me() 00217 { 00218 return singleton; 00219 } 00220 00221 public: 00222 const char *wstype() const { return "WvDaemon"; } 00223 }; 00224 00225 #endif // __WVDAEMON_H