/build/buildd/libassa-3.4.1/assa/Handlers.h

Go to the documentation of this file.
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 #include <sys/wait.h>
00018 
00019 #include "assa/EventHandler.h"
00020 #include "assa/Assure.h"
00021 
00022 namespace ASSA {
00023 
00030 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__NetBSD__)
00031 #   define ASSAIOSIG SIGIO
00032 #else
00033 #   define ASSAIOSIG SIGPOLL
00034 #endif
00035 
00040 class SIGINTHandler : public EventHandler
00041 {
00042 public:
00044     SIGINTHandler();
00045 
00049     int handle_signal (int signum_);
00050 
00055     sig_atomic_t graceful_quit();
00056 
00058     void resetState();
00059 
00060 private:
00062     sig_atomic_t m_graceful_quit;
00063 };
00064  
00065 inline
00066 SIGINTHandler::
00067 SIGINTHandler ()
00068     : m_graceful_quit (0)
00069 {
00070     trace_with_mask("SIGINTHandler::SIGINTHandler", SIGHAND);
00071 }
00072 
00073 inline int
00074 SIGINTHandler::
00075 handle_signal (int signum_)
00076 {
00077     trace_with_mask("SIGINTHandler::handle_signal", SIGHAND);
00078 
00079     if (signum_ == SIGINT) {
00080         m_graceful_quit = 1;
00081         return 0;
00082     }
00083     return -1;
00084 }
00085 
00086 inline sig_atomic_t
00087 SIGINTHandler::
00088 graceful_quit ()
00089 {
00090     return m_graceful_quit;
00091 }
00092 
00093 inline void
00094 SIGINTHandler::
00095 resetState()
00096 {
00097     trace_with_mask("SIGINTHandler::resetState", SIGHAND);
00098 
00099     m_graceful_quit = 0;
00100 }
00101 
00107 class SIGUSR1Handler : public EventHandler
00108 {
00109 public:
00112     SIGUSR1Handler() : m_count(0) {
00113         trace_with_mask("SIGUSR1Handler::SIGUSR1Handler", SIGHAND);
00114     }
00115     
00118     int handle_signal(int signum_) {
00119         trace_with_mask("SIGUSR1Handler::handle_signal()", SIGHAND);
00120         
00121         if (signum_ == SIGUSR1) { 
00122             m_count++; 
00123             DL((TRACE, "signal count = %d\n", m_count));
00124             return 0; 
00125         }
00126         return -1;
00127     }
00128     
00131     sig_atomic_t received_count() const { return m_count; }
00132     
00135     void resetState() { m_count = 0; }
00136 
00137 private:
00139     sig_atomic_t m_count;
00140 };
00141 
00142 
00145 class SIGUSR2Handler : public EventHandler
00146 {
00147 public:
00150     SIGUSR2Handler() : m_count(0) {
00151         trace_with_mask("SIGUSR2Handler::SIGUSR2Handler", SIGHAND);
00152     }
00153     
00156     int handle_signal(int signum_) {
00157         trace_with_mask("SIGUSR2Handler::handle_signal()", SIGHAND);
00158         
00159         if (signum_ == SIGUSR2) { 
00160             m_count++; 
00161             DL((TRACE, "signal count = %d\n", m_count));
00162             return 0; 
00163         }
00164         return -1;
00165     }
00166     
00169     sig_atomic_t received_count() const { return m_count; }
00170     
00173     void resetState() { m_count = 0; }
00174 
00175 private:
00177     sig_atomic_t m_count;
00178 };
00179 
00182 class SIGCHLDHandler : public EventHandler
00183 {
00184 public:
00186     SIGCHLDHandler() : m_child_exit_flag(0) {
00187         trace_with_mask("SIGCHLDHandler::SIGCHLDHandler", SIGHAND);
00188     }
00189 
00192     int handle_signal(int signum_) {
00193         trace_with_mask("SIGCHLDHandler::handle_signal", SIGHAND);
00194 
00195         if (signum_ == SIGCHLD && wait(NULL) != -1) { 
00196             m_child_exit_flag = 1;
00197             return 0;
00198         }
00199         return -1;
00200     }
00203     sig_atomic_t child_exited() { return m_child_exit_flag; }
00204 
00207     void resetState() { m_child_exit_flag = 0; }
00208 
00209 private:
00211     sig_atomic_t m_child_exit_flag;
00212 };
00213 
00216 class SIGALRMHandler : public EventHandler
00217 {
00218 public:
00220     SIGALRMHandler() : m_alarm_flag(0) {
00221         trace_with_mask("SIGALRMHandler::SIGALRMHandler", SIGHAND);
00222     }
00223 
00226     int handle_signal(int signum_) {
00227         trace_with_mask("SIGALRMHandler::handle_signal", SIGHAND);
00228 
00229         if (signum_ == SIGALRM) {
00230             m_alarm_flag = 1; // notice that we have seen alarm
00231             return 0;
00232         }
00233         return -1;
00234     }
00235 
00237     sig_atomic_t alarmed () { return m_alarm_flag; }
00238 
00240     void resetState () { m_alarm_flag = 0; }
00241 
00242 private:
00244     sig_atomic_t m_alarm_flag;
00245 };
00246 
00247 
00256 class SIGPOLLHandler : public EventHandler
00257 {
00258 public:
00261     SIGPOLLHandler () {
00262         trace_with_mask("SIGPOLLHandler", SIGHAND);
00263     }
00266     int handle_signal ( int signum_ ) { 
00267         trace_with_mask("SIGPOLLHandler::handle_signal", SIGHAND);
00268             
00269         return (signum_ == ASSAIOSIG) ? 0 : -1; 
00270     }
00271 };
00272 
00273 } // end namespace ASSA
00274 
00275 #endif

Generated on Wed Jun 21 01:42:50 2006 for libassa by  doxygen 1.4.6