libassa
3.5.0
|
00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // Handlers.h 00004 //------------------------------------------------------------------------------ 00005 // Copyright (c) 2000,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 00013 #ifndef _Handlers_h 00014 #define _Handlers_h 00015 00016 #include <sys/types.h> 00017 00018 #if !defined(WIN32) 00019 # include <sys/wait.h> 00020 #endif 00021 00022 #include "assa/EventHandler.h" 00023 #include "assa/Assure.h" 00024 00025 namespace ASSA { 00026 00027 #if !defined(WIN32) 00028 00035 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__NetBSD__) 00036 # define ASSAIOSIG SIGIO 00037 #else 00038 # define ASSAIOSIG SIGPOLL 00039 #endif 00040 00045 class SIGINTHandler : public EventHandler 00046 { 00047 public: 00049 SIGINTHandler(); 00050 00054 int handle_signal (int signum_); 00055 00060 sig_atomic_t graceful_quit(); 00061 00063 void resetState(); 00064 00065 private: 00067 sig_atomic_t m_graceful_quit; 00068 }; 00069 00070 inline 00071 SIGINTHandler:: 00072 SIGINTHandler () 00073 : m_graceful_quit (0) 00074 { 00075 trace_with_mask("SIGINTHandler::SIGINTHandler", SIGHAND); 00076 } 00077 00078 inline int 00079 SIGINTHandler:: 00080 handle_signal (int signum_) 00081 { 00082 trace_with_mask("SIGINTHandler::handle_signal", SIGHAND); 00083 00084 if (signum_ == SIGINT) { 00085 m_graceful_quit = 1; 00086 return 0; 00087 } 00088 return -1; 00089 } 00090 00091 inline sig_atomic_t 00092 SIGINTHandler:: 00093 graceful_quit () 00094 { 00095 return m_graceful_quit; 00096 } 00097 00098 inline void 00099 SIGINTHandler:: 00100 resetState() 00101 { 00102 trace_with_mask("SIGINTHandler::resetState", SIGHAND); 00103 00104 m_graceful_quit = 0; 00105 } 00106 00112 class SIGUSR1Handler : public EventHandler 00113 { 00114 public: 00117 SIGUSR1Handler() : m_count(0) { 00118 trace_with_mask("SIGUSR1Handler::SIGUSR1Handler", SIGHAND); 00119 } 00120 00123 int handle_signal(int signum_) { 00124 trace_with_mask("SIGUSR1Handler::handle_signal()", SIGHAND); 00125 00126 if (signum_ == SIGUSR1) { 00127 m_count++; 00128 DL((TRACE, "signal count = %d\n", m_count)); 00129 return 0; 00130 } 00131 return -1; 00132 } 00133 00136 sig_atomic_t received_count() const { return m_count; } 00137 00140 void resetState() { m_count = 0; } 00141 00142 private: 00144 sig_atomic_t m_count; 00145 }; 00146 00147 00150 class SIGUSR2Handler : public EventHandler 00151 { 00152 public: 00155 SIGUSR2Handler() : m_count(0) { 00156 trace_with_mask("SIGUSR2Handler::SIGUSR2Handler", SIGHAND); 00157 } 00158 00161 int handle_signal(int signum_) { 00162 trace_with_mask("SIGUSR2Handler::handle_signal()", SIGHAND); 00163 00164 if (signum_ == SIGUSR2) { 00165 m_count++; 00166 DL((TRACE, "signal count = %d\n", m_count)); 00167 return 0; 00168 } 00169 return -1; 00170 } 00171 00174 sig_atomic_t received_count() const { return m_count; } 00175 00178 void resetState() { m_count = 0; } 00179 00180 private: 00182 sig_atomic_t m_count; 00183 }; 00184 00187 class SIGCHLDHandler : public EventHandler 00188 { 00189 public: 00191 SIGCHLDHandler() : m_child_exit_flag(0) { 00192 trace_with_mask("SIGCHLDHandler::SIGCHLDHandler", SIGHAND); 00193 } 00194 00197 int handle_signal(int signum_) { 00198 trace_with_mask("SIGCHLDHandler::handle_signal", SIGHAND); 00199 00200 if (signum_ == SIGCHLD && wait(NULL) != -1) { 00201 m_child_exit_flag = 1; 00202 return 0; 00203 } 00204 return -1; 00205 } 00208 sig_atomic_t child_exited() { return m_child_exit_flag; } 00209 00212 void resetState() { m_child_exit_flag = 0; } 00213 00214 private: 00216 sig_atomic_t m_child_exit_flag; 00217 }; 00218 00221 class SIGALRMHandler : public EventHandler 00222 { 00223 public: 00225 SIGALRMHandler() : m_alarm_flag(0) { 00226 trace_with_mask("SIGALRMHandler::SIGALRMHandler", SIGHAND); 00227 } 00228 00231 int handle_signal(int signum_) { 00232 trace_with_mask("SIGALRMHandler::handle_signal", SIGHAND); 00233 00234 if (signum_ == SIGALRM) { 00235 m_alarm_flag = 1; // notice that we have seen alarm 00236 return 0; 00237 } 00238 return -1; 00239 } 00240 00242 sig_atomic_t alarmed () { return m_alarm_flag; } 00243 00245 void resetState () { m_alarm_flag = 0; } 00246 00247 private: 00249 sig_atomic_t m_alarm_flag; 00250 }; 00251 00252 00261 class SIGPOLLHandler : public EventHandler 00262 { 00263 public: 00266 SIGPOLLHandler () { 00267 trace_with_mask("SIGPOLLHandler", SIGHAND); 00268 } 00271 int handle_signal ( int signum_ ) { 00272 trace_with_mask("SIGPOLLHandler::handle_signal", SIGHAND); 00273 00274 return (signum_ == ASSAIOSIG) ? 0 : -1; 00275 } 00276 }; 00277 00278 #endif // !defined(WIN32) 00279 00280 } // end namespace ASSA 00281 00282 #endif