File: SigHandlersList.h

package info (click to toggle)
libassa 3.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,268 kB
  • sloc: cpp: 15,703; sh: 12,083; makefile: 379; perl: 51
file content (394 lines) | stat: -rw-r--r-- 9,031 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// -*- c++ -*-
//---------------------------------------------------------------------------
//                            SigHandlersList.h
//------------------------------------------------------------------------------
//  Copyright (c) 1997 by Vladislav Grinchenko
//
//  Permission to use, copy, modify, and distribute this software      
//  and its documentation for any purpose and without fee is hereby     
//  granted, provided that the above copyright notice appear in all     
//  copies.  The author makes no representations about the suitability  
//  of this software for any purpose.  It is provided "as is" without   
//  express or implied warranty.
//---------------------------------------------------------------------------

#ifndef _SigHandlersList_h
#define _SigHandlersList_h

#include <signal.h>
#include <errno.h>
#include <sys/time.h>	
#include <sys/types.h>

#include "assa/SigHandler.h"

#include <set>
using std::set;

namespace ASSA {
          
#if !defined(WIN32)

/** @file SigHandlersList.h 

    SigHandlersList is a Singleton class that maps signal number to a set of
	EventHandlers listening for the delivery of the signal.
*/

/** CFUNC_Handler  class.

    CFUNC_Handler is a wrapper around C signal handler function.
	It wraps C signal handler function into EventHandler interface.
*/

class CFUNC_Handler : public EventHandler
{
public:
	CFUNC_Handler (C_SIG_HANDLER csigh_);

	int           handle_signal (int signum_);
	C_SIG_HANDLER handler       () { return m_c_sig_hand; }
	
private:
	C_SIG_HANDLER m_c_sig_hand;
};

/** SigHandlersList class.


	SigHandlersList class is used by SigHandlers class to keep track of
	EventHandlers installed to be called on signal's delivery. It is sort of
	global process map of signal numbers into corresponding sets of 
	EventHandlers that are listening for signal delivery.
*/

class SigHandlersList 
{
public:
	typedef EventHandler* key_type;
	typedef EventHandler* data_type;

	struct CompSHL {
		bool operator () (const key_type c1_, const key_type c2_) const
		{
// This wouldn't fly on 64-bit machines, 'cause ptr size there is 8 bytes long
//			return int(c1_) < int(c2_);
//
			return (c1_ < c2_);
		}
	};

	typedef set< key_type, CompSHL > set_t;
	typedef set< key_type, CompSHL >::iterator iterator;

	/** Retrieve a pointer to the list of event handlers 
	    listening to signum_ signal delivery.
	*/
	static SigHandlersList* instance (int signum_);

	/// Destructor
	~SigHandlersList ();

	/// Is list empty
	bool empty () const;

	/// Size of the list
	size_t size () const;

	/** Add an event handler data_ to the list.
	    @return TRUE on success, FALSE on error.
	*/
	bool insert (data_type data_);

	/** Find and remove event handler  key_  from the list.
	 */
	void erase (const key_type key_);

	/** Remove an event handler pointed by iterator  it_ 
	    from the list.
	*/
	void erase (iterator it_);
	
	/** Empty event handlers' list.
	 */
	void erase ();
	
	/** Return an iterator pointing to the beginning of the list.
	 */
	iterator begin ();
	
	/** Return an iterator pointing to the end of the list.
	 */
	iterator end ();

	/** Find event handler by its pointer key_.
	    @return Iterator to the element.
	*/
	iterator find (const key_type key_);

	/** Save 3rd party C function handler to remember.
	    @param cfp_ New 3rd party C function handler. If it is NULL,
	    then seen_cfh flag is set to FALSE.
	    @return old 3rd party C function handler.
	*/
	CFUNC_Handler* cfunc_handler (CFUNC_Handler* cfp_);

	/** Retrieve pointer to 3rd party C function handler.
	    @return 3rd party C function handler.
	*/
	CFUNC_Handler* cfunc_handler () const;

	/** Indicate whether 3rd party C function handler was installed.
	    @param ft_ TRUE if was, FALSE if not.
	*/
	void seen_cfunc_handler (bool ft_);

	/** @return TRUE if we've seen 3rd party C function handler;
	    FALSE otherwise.
	*/
	bool seen_cfunc_handler () const;

protected:
	SigHandlersList ();		// Singleton
	SigHandlersList (const SigHandlersList& map_); // prohibit copying
	SigHandlersList& operator= (const SigHandlersList& map_);
	
public:
	/** Static map of signal numbers to SigHandlerLists. 
	 */
	static SigHandlersList* m_instance[NSIG];

private:
	/// Set of all event handlers registered for this signal.
	set_t*         m_set;

	/** If true this flag indicates that 3rd party event handler
	    has already been installed prior taking control by SigHandlers
	    manager.
	*/
	int            m_seen_cfh;  

	/** Pointer to the 3rd party signal handler in the set
	*/
	CFUNC_Handler*  m_cfhp;
};

//-------------------------------------------------------------------------
//----------------------- SigHandlersList Inlines -------------------------
//-------------------------------------------------------------------------

inline
SigHandlersList::
SigHandlersList ()
	: m_seen_cfh (false), m_cfhp (NULL)
{
	trace_with_mask("SigHandlersList::SigHandlersList", SIGHAND);

	m_set = new set_t;
}

inline
SigHandlersList::
~SigHandlersList ()
{
	trace_with_mask("SigHandlersList::~SigHandlersList", SIGHAND);

	erase ();
	delete m_set;
	m_set = NULL;
}

inline  SigHandlersList*
SigHandlersList::
instance (int signum_)
{
	trace_with_mask("SigHandlersList::instance", SIGHAND);

	DL((APP, "m_instance[%d] = 0x%x\n", signum_,
	    SigHandlersList::m_instance[signum_]));

	if (SigHandlersList::m_instance[signum_] == 0) {
		DL((APP, "new SigHandlersList allocated\n"));
		SigHandlersList::m_instance[signum_] = new SigHandlersList();
	}
	return SigHandlersList::m_instance[signum_];
}

inline bool
SigHandlersList::
empty () const
{
	trace_with_mask("SigHandlersList::empty", SIGHAND);

	// true if map is empty, false otherwise

	return m_set->empty ();
}

inline size_t
SigHandlersList::
size () const
{
	trace_with_mask("SigHandlersList::size", SIGHAND);

	// return number of elements in the map

	return m_set->size ();
}

inline bool
SigHandlersList::
insert (data_type eh_)
{
	trace_with_mask("SigHandlersList::insert", SIGHAND);

	/*---
	  Insert 'eh_' into the set. set::insert() returns a 'pair' object.

	  If the set doesn't contain an element that matches 'eh_', insert a 
	  copy of 'eh_' and returns a 'pair' whose first element is an
	  iterator positioned at the new element and second element is
	  'true'.

	  If the set already contains an element that matches 'eh_', returns
	  a pair whose first element is an iterator positioned at the
	  existing element and second element is false!
	  ---*/

	set_t::const_iterator it = m_set->find (eh_);

	/*--- Not in the set ---*/
	if (it == m_set->end ()) { 
		return (m_set->insert (eh_)).second;
	}
	/*--- Already in the set ---*/
	return true;
}

inline void
SigHandlersList::
erase (const key_type key_)
{
	// return number of erased elements
	trace_with_mask("SigHandlersList::erase(key_)", SIGHAND);
	
	m_set->erase (key_);
}

inline void
SigHandlersList::
erase ()
{
	// empty the map
	trace_with_mask("SigHandlersList::erase(void)", SIGHAND);

	m_set->erase (m_set->begin(), m_set->end());
}

inline void
SigHandlersList::
erase(iterator it_)
{
	// erase element pointed by iterator
	trace_with_mask("SigHandlersList::erase(it_)", SIGHAND);

	m_set->erase(it_);
}

inline SigHandlersList::iterator
SigHandlersList::
begin ()
{
	trace_with_mask("SigHandlersList::begin()", SIGHAND);

	return m_set->begin ();
}

inline SigHandlersList::iterator
SigHandlersList::
end ()
{
	trace_with_mask("SigHandlersList::end", SIGHAND);

	return m_set->end ();
}

inline SigHandlersList::iterator
SigHandlersList::
find (const key_type key_)
{
	trace_with_mask("SigHandlersList::find", SIGHAND);

	return m_set->find (key_);
}


inline CFUNC_Handler*
SigHandlersList::
cfunc_handler (CFUNC_Handler* cfhp_)
{
	trace_with_mask("SigHandlersList::cfunc_handler", SIGHAND);
	
	CFUNC_Handler* old_cfhp = m_cfhp;
	m_cfhp = cfhp_;
	m_seen_cfh = cfhp_ == NULL ? false : true;
	return old_cfhp;
}

inline CFUNC_Handler*
SigHandlersList::
cfunc_handler () const
{
	trace_with_mask("SigHandlersList::cfunc_handler", SIGHAND);
	
	return m_cfhp;
}

inline void
SigHandlersList::
seen_cfunc_handler (bool ft_) 
{
	trace_with_mask("SigHandlersList::seen_cfunc_handler", SIGHAND);

	m_seen_cfh = ft_;
}

inline bool
SigHandlersList::
seen_cfunc_handler () const
{
	trace_with_mask("SigHandlersList::seen_cfunc_handler", SIGHAND);

	return m_seen_cfh;
}

//-------------------------------------------------------------------------
//------------------------ CFUNC_Handler Inlines --------------------------
//-------------------------------------------------------------------------

inline
CFUNC_Handler::
CFUNC_Handler (C_SIG_HANDLER csigh_)
	: m_c_sig_hand (csigh_)
{
	trace_with_mask("CFUNC_Handler::CFUNC_Handler", SIGHAND);
}

inline int
CFUNC_Handler::
handle_signal (int signum_)
{
	trace_with_mask("CFUNC_Handler::handle_signal", SIGHAND);

	if (m_c_sig_hand) {
		(*m_c_sig_hand)(signum_);
	}
	return 1;
}

#endif // !defined(WIN32)

} // end namespace ASSA

#endif /* _SigHandlersList_h */