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 <assert.h> 00027 00028 #if !defined(WIN32) 00029 # include <sys/resource.h> /* getrlimit */ 00030 # include <syslog.h> 00031 #endif 00032 } 00033 00034 #include <iostream> 00035 #include <sstream> 00036 #include <string> 00037 #include <vector> 00038 00039 using std::string; 00040 using std::vector; 00041 00042 #include "assa/Assure.h" 00043 #include "assa/Handlers.h" 00044 #include "assa/SigHandlers.h" 00045 #include "assa/Fork.h" 00046 #include "assa/Reactor.h" 00047 #include "assa/CmdLineOpts.h" 00048 #include "assa/PidFileLock.h" 00049 00050 namespace ASSA { 00051 00059 class GenServer : 00060 public virtual EventHandler, 00061 public CmdLineOpts 00062 { 00063 public: 00066 enum LogFlag { 00067 KEEPLOG, 00070 RMLOG 00072 }; 00073 00074 public: 00078 GenServer (); 00079 00081 virtual ~GenServer() {} 00082 00101 virtual void init (int* argc, char* argv[], const char* help_info); 00102 00107 virtual int fini (void) { return 0; } 00108 00112 virtual int suspend (void) { return 0; } 00113 00117 virtual int resume (void) { return 0; } 00118 00122 virtual void init_service () =0; 00123 00128 virtual void process_events () =0; 00129 00135 virtual void fatal_signal_hook () { /*--- empty ---*/ } 00136 00140 int handle_signal (int signum_); 00141 00149 bool service_is_active () { return (!m_graceful_quit); } 00150 00154 void stop_service (); 00155 00161 void set_version (const string& release_, int revision_); 00162 00164 string get_version (); 00165 00167 void set_author (const string& author_); 00168 00176 void set_flags (LogFlag logf_) { m_log_flag = logf_; } 00177 00179 virtual void display_help (); 00180 00182 string get_proc_name () { return m_proc_name; } 00183 00187 void set_proc_name (string proc_name_) { m_proc_name = proc_name_; } 00188 00190 string get_cmdline_name () { return m_cmdline_name; } 00191 00198 string get_default_config_file () { return m_default_config_file; } 00199 00203 string get_config_file () { return m_config_file; } 00204 00206 string get_port () { return m_port; } 00207 00211 void set_port (string port_) { m_port = port_; } 00212 00213 #if !defined(WIN32) 00214 00216 SigHandlers& get_sig_manager () { return m_sig_dispatcher; } 00217 #endif 00218 00221 Reactor* get_reactor () { return &m_reactor; } 00222 00224 static bool become_daemon (); 00225 00227 int get_exit_value () const { return m_exit_value; } 00228 00229 protected: 00231 void set_exit_value (int v_) { m_exit_value = v_; } 00232 00233 protected: 00235 string m_proc_name; 00236 00238 string m_cmdline_name; 00239 00241 string m_port; 00242 00244 string m_default_config_file; 00245 00247 string m_config_file; 00248 00250 u_int m_log_size; 00251 00253 int m_instance; 00254 00256 string m_log_file; 00257 00259 string m_with_log_server; 00260 00263 string m_log_server; 00264 00266 long m_mask; 00267 00269 bool m_graceful_quit; 00270 00271 #if !defined(WIN32) 00272 00273 SigHandlers m_sig_dispatcher; 00274 00276 SIGPOLLHandler m_sig_poll; 00277 #endif 00278 00280 Reactor m_reactor; 00281 00283 string m_version; 00284 00286 int m_revision; 00287 00289 string m_author; 00290 00292 const char* m_help_msg; 00293 00295 LogFlag m_log_flag; 00296 00298 string m_log_stdout; 00299 00301 string m_daemon; 00302 00304 string m_ommit_pidfile; 00305 00310 int m_log_level; 00311 00313 PidFileLock m_pidfile_lock; 00314 00316 string m_pidfile; 00317 00321 bool m_help_flag; 00322 00326 bool m_version_flag; 00327 00329 int m_exit_value; 00330 00331 private: 00333 GenServer (const GenServer&); 00334 GenServer& operator=(const GenServer&); 00335 00337 void init_internals (); 00338 }; 00339 00340 00341 inline void 00342 GenServer:: 00343 stop_service () 00344 { 00345 m_graceful_quit = true; 00346 m_reactor.deactivate (); 00347 } 00348 00349 inline void 00350 GenServer:: 00351 set_version (const string& release_, int revision_) 00352 { 00353 m_version = release_; 00354 m_revision = revision_; 00355 } 00356 00357 inline void 00358 GenServer:: 00359 set_author (const string& author_) 00360 { 00361 m_author = author_; 00362 } 00363 00364 inline string 00365 GenServer:: 00366 get_version () 00367 { 00368 std::ostringstream v; 00369 v << "Version: " << m_version << " Revision: " << m_revision << std::ends; 00370 return (v.str ()); 00371 } 00372 00373 inline void 00374 GenServer:: 00375 display_help () 00376 { 00377 std::cout << m_help_msg << '\n' 00378 << "Written by " << m_author << "\n" << std::endl; 00379 } 00380 00381 } // The end of namespase ASSA 00382 00383 00384 #endif /* GENSERVER_H */