libassa  3.5.1
Handlers.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // Handlers.h
4 //------------------------------------------------------------------------------
5 // Copyright (c) 2000,2005 by Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------
12 
13 #ifndef _Handlers_h
14 #define _Handlers_h
15 
16 #include <sys/types.h>
17 
18 #if !defined(WIN32)
19 # include <sys/wait.h>
20 #endif
21 
22 #include "assa/EventHandler.h"
23 #include "assa/Assure.h"
24 
25 namespace ASSA {
26 
27 #if !defined(WIN32)
28 
35 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__NetBSD__)
36 # define ASSAIOSIG SIGIO
37 #else
38 # define ASSAIOSIG SIGPOLL
39 #endif
40 
46 {
47 public:
49  SIGINTHandler();
50 
54  int handle_signal (int signum_);
55 
60  sig_atomic_t graceful_quit();
61 
63  void resetState();
64 
65 private:
67  sig_atomic_t m_graceful_quit;
68 };
69 
70 inline
73  : m_graceful_quit (0)
74 {
75  trace_with_mask("SIGINTHandler::SIGINTHandler", SIGHAND);
76 }
77 
78 inline int
80 handle_signal (int signum_)
81 {
82  trace_with_mask("SIGINTHandler::handle_signal", SIGHAND);
83 
84  if (signum_ == SIGINT) {
85  m_graceful_quit = 1;
86  return 0;
87  }
88  return -1;
89 }
90 
91 inline sig_atomic_t
94 {
95  return m_graceful_quit;
96 }
97 
98 inline void
100 resetState()
101 {
102  trace_with_mask("SIGINTHandler::resetState", SIGHAND);
103 
104  m_graceful_quit = 0;
105 }
106 
113 {
114 public:
118  trace_with_mask("SIGUSR1Handler::SIGUSR1Handler", SIGHAND);
119  }
120 
123  int handle_signal(int signum_) {
124  trace_with_mask("SIGUSR1Handler::handle_signal()", SIGHAND);
125 
126  if (signum_ == SIGUSR1) {
127  m_count++;
128  DL((TRACE, "signal count = %d\n", m_count));
129  return 0;
130  }
131  return -1;
132  }
133 
136  sig_atomic_t received_count() const { return m_count; }
137 
140  void resetState() { m_count = 0; }
141 
142 private:
144  sig_atomic_t m_count;
145 };
146 
147 
151 {
152 public:
156  trace_with_mask("SIGUSR2Handler::SIGUSR2Handler", SIGHAND);
157  }
158 
161  int handle_signal(int signum_) {
162  trace_with_mask("SIGUSR2Handler::handle_signal()", SIGHAND);
163 
164  if (signum_ == SIGUSR2) {
165  m_count++;
166  DL((TRACE, "signal count = %d\n", m_count));
167  return 0;
168  }
169  return -1;
170  }
171 
174  sig_atomic_t received_count() const { return m_count; }
175 
178  void resetState() { m_count = 0; }
179 
180 private:
182  sig_atomic_t m_count;
183 };
184 
188 {
189 public:
192  trace_with_mask("SIGCHLDHandler::SIGCHLDHandler", SIGHAND);
193  }
194 
197  int handle_signal(int signum_) {
198  trace_with_mask("SIGCHLDHandler::handle_signal", SIGHAND);
199 
200  if (signum_ == SIGCHLD && wait(NULL) != -1) {
201  m_child_exit_flag = 1;
202  return 0;
203  }
204  return -1;
205  }
208  sig_atomic_t child_exited() { return m_child_exit_flag; }
209 
213 
214 private:
216  sig_atomic_t m_child_exit_flag;
217 };
218 
222 {
223 public:
226  trace_with_mask("SIGALRMHandler::SIGALRMHandler", SIGHAND);
227  }
228 
231  int handle_signal(int signum_) {
232  trace_with_mask("SIGALRMHandler::handle_signal", SIGHAND);
233 
234  if (signum_ == SIGALRM) {
235  m_alarm_flag = 1; // notice that we have seen alarm
236  return 0;
237  }
238  return -1;
239  }
240 
242  sig_atomic_t alarmed () { return m_alarm_flag; }
243 
245  void resetState () { m_alarm_flag = 0; }
246 
247 private:
249  sig_atomic_t m_alarm_flag;
250 };
251 
252 
262 {
263 public:
267  trace_with_mask("SIGPOLLHandler", SIGHAND);
268  }
271  int handle_signal ( int signum_ ) {
272  trace_with_mask("SIGPOLLHandler::handle_signal", SIGHAND);
273 
274  return (signum_ == ASSAIOSIG) ? 0 : -1;
275  }
276 };
277 
278 #endif // !defined(WIN32)
279 
280 } // end namespace ASSA
281 
282 #endif
A collection of assert function wrappers.
An abstract interface for handling I/O events, timers, and such.
#define ASSAIOSIG
Definition: Handlers.h:38
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
EventHandler class.
Definition: EventHandler.h:103
Class SIGALRMHandler handles SIGALRM signal.
Definition: Handlers.h:222
SIGALRMHandler()
Constructor.
Definition: Handlers.h:225
sig_atomic_t alarmed()
Has alarm gone off?
Definition: Handlers.h:242
sig_atomic_t m_alarm_flag
Indicator whether alarm gone off yet.
Definition: Handlers.h:249
void resetState()
Reset internal state.
Definition: Handlers.h:245
int handle_signal(int signum_)
Receive ALRM signal.
Definition: Handlers.h:231
Class SIGCHLDHandler handles SIGCHLD signal.
Definition: Handlers.h:188
void resetState()
Reset child's exit state.
Definition: Handlers.h:212
int handle_signal(int signum_)
Receive CHLD signal.
Definition: Handlers.h:197
sig_atomic_t m_child_exit_flag
Child exit's state.
Definition: Handlers.h:216
sig_atomic_t child_exited()
Did child exit?
Definition: Handlers.h:208
SIGCHLDHandler()
Constructor.
Definition: Handlers.h:191
SIGINTHandler handles SIGINT signal.
Definition: Handlers.h:46
int handle_signal(int signum_)
Receive SIGINT signal.
Definition: Handlers.h:80
sig_atomic_t m_graceful_quit
flag that indicates whether signal was caught.
Definition: Handlers.h:67
SIGINTHandler()
constructor
Definition: Handlers.h:72
sig_atomic_t graceful_quit()
Indicates whether graceful quit signal has been raised by the signal handler.
Definition: Handlers.h:93
void resetState()
Reset state of the object to initial.
Definition: Handlers.h:100
Class SIGPOLLHandler handles SIGPOLL signal.
Definition: Handlers.h:262
SIGPOLLHandler()
Constructor.
Definition: Handlers.h:266
int handle_signal(int signum_)
Catch and absorb SIGPOLL signal.
Definition: Handlers.h:271
Class SIGUSR1Handler.
Definition: Handlers.h:113
void resetState()
Set received signals count back to 0.
Definition: Handlers.h:140
sig_atomic_t m_count
Received signals count.
Definition: Handlers.h:144
int handle_signal(int signum_)
Catch USR1 signal and increment count.
Definition: Handlers.h:123
sig_atomic_t received_count() const
Report count of received signals.
Definition: Handlers.h:136
SIGUSR1Handler()
Constructor.
Definition: Handlers.h:117
Class SIGUSR2Handler handles SIGUSR2 signal.
Definition: Handlers.h:151
sig_atomic_t received_count() const
Report count of received signals.
Definition: Handlers.h:174
int handle_signal(int signum_)
Catch USR2 signal and increment count.
Definition: Handlers.h:161
SIGUSR2Handler()
Constructor.
Definition: Handlers.h:155
sig_atomic_t m_count
Received signals count.
Definition: Handlers.h:182
void resetState()
Set received signals count back to 0.
Definition: Handlers.h:178
Definition: Acceptor.h:40
@ SIGHAND
Class SigHandler(s) messages
Definition: LogMask.h:38
@ TRACE
Function call trace
Definition: LogMask.h:26