00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // GenServer.h 00004 //------------------------------------------------------------------------------ 00005 // Copyright (c) 1999-2005 by Vladislav Grinchenko 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Library General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 2 of the License, or (at your option) any later version. 00011 //------------------------------------------------------------------------------ 00012 #ifndef GENSERVER_H 00013 #define GENSERVER_H 00014 00015 extern "C" { 00016 #include <stdio.h> /* printf, sprintf */ 00017 #include <unistd.h> 00018 #include <stdlib.h> /* getopt() */ 00019 #include <string.h> /* strlen */ 00020 #include <errno.h> /* errno */ 00021 #include <signal.h> /* kill */ 00022 #include <sys/types.h> /* open */ 00023 #include <sys/stat.h> /* open */ 00024 #include <fcntl.h> /* open */ 00025 #include <limits.h> /* PATH_MAX */ 00026 #include <sys/resource.h> /* getrlimit */ 00027 #include <syslog.h> 00028 #include <assert.h> 00029 } 00030 00031 00032 #include <iostream> 00033 #include <sstream> 00034 #include <string> 00035 #include <vector> 00036 00037 using std::string; 00038 using std::vector; 00039 00040 #include "assa/Assure.h" 00041 #include "assa/Handlers.h" 00042 #include "assa/SigHandlers.h" 00043 #include "assa/Fork.h" 00044 #include "assa/Reactor.h" 00045 #include "assa/CmdLineOpts.h" 00046 #include "assa/PidFileLock.h" 00047 00048 namespace ASSA { 00049 00057 class GenServer : 00058 public virtual EventHandler, 00059 public CmdLineOpts 00060 { 00061 public: 00064 enum LogFlag { 00065 KEEPLOG, 00068 RMLOG 00070 }; 00071 00072 public: 00076 GenServer (); 00077 00079 virtual ~GenServer() {} 00080 00099 virtual void init (int* argc, char* argv[], const char* help_info); 00100 00105 virtual int fini (void) { return 0; } 00106 00110 virtual int suspend (void) { return 0; } 00111 00115 virtual int resume (void) { return 0; } 00116 00120 virtual void init_service () =0; 00121 00126 virtual void process_events () =0; 00127 00133 virtual void fatal_signal_hook () { /*--- empty ---*/ } 00134 00138 int handle_signal (int signum_); 00139 00147 bool service_is_active () { return (!m_graceful_quit); } 00148 00152 void stop_service (); 00153 00159 void set_version (const string& release_, int revision_); 00160 00162 string get_version (); 00163 00165 void set_author (const string& author_); 00166 00174 void set_flags (LogFlag logf_) { m_log_flag = logf_; } 00175 00177 virtual void display_help (); 00178 00180 string get_proc_name () { return m_proc_name; } 00181 00185 void set_proc_name (string proc_name_) { m_proc_name = proc_name_; } 00186 00188 string get_cmdline_name () { return m_cmdline_name; } 00189 00196 string get_default_config_file () { return m_default_config_file; } 00197 00201 string get_config_file () { return m_config_file; } 00202 00204 string get_port () { return m_port; } 00205 00209 void set_port (string port_) { m_port = port_; } 00210 00213 SigHandlers& get_sig_manager () { return m_sig_dispatcher; } 00214 00217 Reactor* get_reactor () { return &m_reactor; } 00218 00220 static bool become_daemon (); 00221 00223 int get_exit_value () const { return m_exit_value; } 00224 00225 protected: 00227 void set_exit_value (int v_) { m_exit_value = v_; } 00228 00229 protected: 00231 string m_proc_name; 00232 00234 string m_cmdline_name; 00235 00237 string m_port; 00238 00240 string m_default_config_file; 00241 00243 string m_config_file; 00244 00246 u_int m_log_size; 00247 00249 int m_instance; 00250 00252 string m_log_file; 00253 00255 string m_with_log_server; 00256 00259 string m_log_server; 00260 00262 long m_mask; 00263 00265 bool m_graceful_quit; 00266 00268 SigHandlers m_sig_dispatcher; 00269 00271 SIGPOLLHandler m_sig_poll; 00272 00274 Reactor m_reactor; 00275 00277 string m_version; 00278 00280 int m_revision; 00281 00283 string m_author; 00284 00286 const char* m_help_msg; 00287 00289 LogFlag m_log_flag; 00290 00292 string m_log_stdout; 00293 00295 string m_daemon; 00296 00298 string m_ommit_pidfile; 00299 00304 int m_log_level; 00305 00307 PidFileLock m_pidfile_lock; 00308 00310 string m_pidfile; 00311 00315 bool m_help_flag; 00316 00320 bool m_version_flag; 00321 00323 int m_exit_value; 00324 00325 private: 00327 GenServer (const GenServer&); 00328 GenServer& operator=(const GenServer&); 00329 00331 void init_internals (); 00332 }; 00333 00334 00335 inline void 00336 GenServer:: 00337 stop_service () 00338 { 00339 m_graceful_quit = true; 00340 m_reactor.deactivate (); 00341 } 00342 00343 inline void 00344 GenServer:: 00345 set_version (const string& release_, int revision_) 00346 { 00347 m_version = release_; 00348 m_revision = revision_; 00349 } 00350 00351 inline void 00352 GenServer:: 00353 set_author (const string& author_) 00354 { 00355 m_author = author_; 00356 } 00357 00358 inline string 00359 GenServer:: 00360 get_version () 00361 { 00362 std::ostringstream v; 00363 v << "Version: " << m_version << " Revision: " << m_revision << std::ends; 00364 return (v.str ()); 00365 } 00366 00367 inline void 00368 GenServer:: 00369 display_help () 00370 { 00371 std::cout << m_help_msg << '\n' 00372 << "Written by " << m_author << "\n" << std::endl; 00373 } 00374 00375 } // The end of namespase ASSA 00376 00377 00378 #endif /* GENSERVER_H */