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 WvCallback<void, WvDaemon &, void *> WvDaemonCallback; 00019 00081 class WvDaemon 00082 { 00083 00084 public: 00085 00087 WvString name; 00088 WvString version; 00091 WvString pid_file; 00094 bool daemonize; 00095 00098 WvArgs args; 00100 WvLog log; 00101 WvLog::LogLevel log_level; 00102 bool syslog; 00103 00104 protected: 00105 00107 WvDaemonCallback start_callback; 00108 WvDaemonCallback run_callback; 00109 WvDaemonCallback stop_callback; 00110 00111 private: 00112 00113 void *ud; 00114 00115 volatile bool _want_to_die; 00116 volatile bool _want_to_restart; 00117 volatile int _exit_status; 00118 00119 int _run(const char *argv0); 00120 00121 bool set_daemonize(void *); 00122 00123 protected: 00124 00125 bool dec_log_level(void *) 00126 { 00127 if ((int)log_level > (int)WvLog::Critical) 00128 log_level = (WvLog::LogLevel)((int)log_level - 1); 00129 return true; 00130 } 00131 00132 bool inc_log_level(void *) 00133 { 00134 if ((int)log_level < (int)WvLog::Debug5) 00135 log_level = (WvLog::LogLevel)((int)log_level + 1); 00136 return true; 00137 } 00138 00139 WvStringList _extra_args; 00140 00141 public: 00142 00145 WvDaemon(WvStringParm _name, WvStringParm _version, 00146 WvDaemonCallback _start_callback, 00147 WvDaemonCallback _run_callback, 00148 WvDaemonCallback _stop_callback, 00149 void *_ud = NULL); 00150 00152 int run(const char *argv0); 00154 int run(int argc, char **argv); 00155 00157 void restart() 00158 { 00159 _want_to_restart = true; 00160 } 00162 void die(int status = 0) 00163 { 00164 _want_to_die = true; 00165 _exit_status = status; 00166 } 00167 00169 bool want_to_restart() const 00170 { 00171 return _want_to_restart; 00172 } 00174 bool want_to_die() const 00175 { 00176 return _want_to_die; 00177 } 00178 00180 bool should_run() const 00181 { 00182 return !_want_to_die && !_want_to_restart; 00183 } 00184 00186 const WvStringList &extra_args() const 00187 { 00188 return _extra_args; 00189 } 00190 }; 00191 00192 #endif // __WVDAEMON_H