libassa  3.5.1
SigAction.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // SigAction.h
4 //------------------------------------------------------------------------------
5 // Copyright (c) 1997 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 _SigAction_h
14 #define _SigAction_h
15 
16 // System includes
17 //
18 #include <signal.h>
19 #include <errno.h>
20 
21 #include "assa/Assure.h"
22 #include "assa/SigSet.h"
23 
24 // some convenient typedefs
25 //
26 extern "C" {
27 typedef struct sigaction SIGACTION;
28 typedef void (*C_SIG_HANDLER)( int );
29 }
30 
31 namespace ASSA {
32 
33 #if !defined(WIN32)
34 
94 class SigAction
95 {
96 public:
100  SigAction();
101 
114  SigAction (C_SIG_HANDLER handler_,
115  SigSet* sig_mask_ = 0,
116  int flags_ = 0);
117 
133  SigAction (C_SIG_HANDLER handler_,
134  int signum_,
135  SigSet* sig_mask_ = 0,
136  int flags_ = 0);
137 
149  int register_action (int signum_, SigAction* oaction_ = 0);
150 
159  int restore_action (int signum_, SigAction& oaction_);
160 
167  int retrieve_action (int signum_);
168 
172  void action (SIGACTION * sa_);
173 
177  SIGACTION * action ();
178 
182  void flags (int new_flags_);
183 
187  int flags ();
188 
191  void mask (SigSet & mask_set_);
192 
195  SigSet mask ();
196 
199  void handler (C_SIG_HANDLER sha_);
200 
204 
209  operator SIGACTION *();
210 
211 private:
214 };
215 
216 //-------------------------------------------------------------------------
217 //------------------------Inline functions---------------------------------
218 //-------------------------------------------------------------------------
219 inline
221 SigAction ()
222 {
223  trace_with_mask("SigAction::SigAction", SIGACT);
224 
225  m_sa.sa_flags = 0;
226  sigemptyset(&m_sa.sa_mask);
227  *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) 0;
228 }
229 
230 inline
232 SigAction (C_SIG_HANDLER handler_,
233  SigSet* sig_mask_,
234  int flags_)
235 {
236  trace_with_mask("SigAction::SigAction(,,)", SIGACT);
237 
238  m_sa.sa_flags = flags_;
239  if (sig_mask_ == NULL) {
240  sigemptyset(&m_sa.sa_mask);
241  }
242  else {
243  /*---
244  here, suppose to do bitwise structure assignment,
245  but does it really do so?
246  = *sig_mask_
247  = *(sig_mask_.operator *())
248  = *(SigSet *tmp = &sig_mask_.m_sa) ????
249  ---*/
250  m_sa.sa_mask = **sig_mask_;
251  }
252  *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) handler_;
253 }
254 
255 inline
257 SigAction (C_SIG_HANDLER handler_,
258  int signum_,
259  SigSet* sig_mask_,
260  int flags_)
261 {
262  trace_with_mask("SigAction::SigAction(,,,)", SIGACT);
263 
264  m_sa.sa_flags = flags_;
265  if (sig_mask_ == NULL) {
266  sigemptyset(&m_sa.sa_mask);
267  }
268  else {
269  /*--- same problem as above... ---*/
270  m_sa.sa_mask = **sig_mask_;
271  }
272  *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) handler_;
273 
274  /*--- installing disposition... ---*/
275  sigaction (signum_, &m_sa, 0);
276 }
277 
278 inline void
280 action (SIGACTION* sa_)
281 {
282  trace_with_mask("SigAction::action", SIGACT);
283  m_sa = *sa_;
284 }
285 
286 inline SIGACTION *
288 action ()
289 {
290  trace_with_mask("SigAction::action", SIGACT);
291 
292  return &m_sa;
293 }
294 
295 inline void
297 flags (int new_flags_)
298 {
299  trace_with_mask("void SigAction::flags()", SIGACT);
300 
301  m_sa.sa_flags = new_flags_;
302 }
303 
304 inline int
306 flags ()
307 {
308  trace_with_mask("int SigAction::flags()", SIGACT);
309 
310  return m_sa.sa_flags;
311 }
312 
313 inline void
315 mask (SigSet & mask_set_)
316 {
317  trace_with_mask("void SigAction::mask()", SIGACT);
318 
319  m_sa.sa_mask = *mask_set_;
320 }
321 
322 inline SigSet
324 mask ()
325 {
326  trace_with_mask("SigSet SigAction::mask()", SIGACT);
327 
328  SigSet tmpset(&m_sa.sa_mask);
329  return tmpset;
330 }
331 
332 inline void
334 handler (C_SIG_HANDLER sha_)
335 {
336  trace_with_mask("void SigAction::handler()", SIGACT);
337 
338  *(C_SIG_HANDLER*) &m_sa.sa_handler = (C_SIG_HANDLER) sha_;
339 }
340 
341 inline C_SIG_HANDLER
343 handler ()
344 {
345  trace_with_mask("C_SIG_HANDLER SigAction::handler()", SIGACT);
346 
347  return (C_SIG_HANDLER) m_sa.sa_handler;
348 }
349 
350 inline
351 SigAction::operator SIGACTION * ()
352 {
353  trace_with_mask("SigAction::operator SIGACTION * ()", SIGACT);
354 
355  return &m_sa;
356 }
357 
358 inline int
360 register_action (int signum_, SigAction* oaction_)
361 {
362  trace_with_mask("SigAction::register_action()", SIGACT);
363 
364  /*--- place here recursive mutex lock to guard ... ---*/
365  struct sigaction *osa = oaction_ == 0 ? 0 : oaction_->action();
366  return sigaction(signum_, &m_sa, osa);
367 }
368 
369 inline int
371 restore_action (int signum_, SigAction& oaction_)
372 {
373  trace_with_mask("SigAction::restore_action()", SIGACT);
374 
375  m_sa = *oaction_.action();
376  return sigaction(signum_, &m_sa, 0);
377 }
378 
379 inline int
381 retrieve_action (int signum_)
382 {
383  trace_with_mask("SigAction::retrieve_action()", SIGACT);
384 
385  return sigaction(signum_, 0, &m_sa);
386 }
387 
388 #endif // !defined(WIN32)
389 
390 } // end namespace ASSA
391 
392 
393 #endif /* _SigAction_h */
A collection of assert function wrappers.
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
void(* C_SIG_HANDLER)(int)
Definition: SigAction.h:28
struct sigaction SIGACTION
Definition: SigAction.h:27
SigSet is a wrapper for UNIX sigset_t structure that provides all manipulators on this structure as w...
int restore_action(int signum_, SigAction &oaction_)
Change object's disposition to oaction_, and install it as current disposition for the signal signum_...
Definition: SigAction.h:371
SigAction()
Default constructor creates SigAction object with null-action.
Definition: SigAction.h:221
int retrieve_action(int signum_)
Retrieve current disposition for the signal signum_ into this object.
Definition: SigAction.h:381
C_SIG_HANDLER handler()
Retrieve current signal handler function.
Definition: SigAction.h:343
SigSet mask()
Retrieve current signal mask.
Definition: SigAction.h:324
void action(SIGACTION *sa_)
Set sigaction structure to sa_.
Definition: SigAction.h:280
SIGACTION m_sa
sigaction structure itself
Definition: SigAction.h:213
int flags()
Retrieve current flags.
Definition: SigAction.h:306
int register_action(int signum_, SigAction *oaction_=0)
Register this object as current disposition for signal signum_, and store old disposition in oaction_...
Definition: SigAction.h:360
SIGACTION * action()
Retrieve current sigaction.
Definition: SigAction.h:288
Definition: Acceptor.h:40
@ SIGACT
Class SigACtion messages
Definition: LogMask.h:48