Loading...
Searching...
No Matches
ASSA::SigHandlers Class Reference

`#include <SigHandlers.h>`

Inheritance diagram for ASSA::SigHandlers:

## Public Member Functions

virtual int | install (int signum_, EventHandler *new_hand_, SigAction *new_disp_=0, EventHandler **old_hand_=0, SigAction *old_disp_=0)
| Register EventHandler with dispatching system.
virtual int | remove (int signum_, EventHandler *eh_, SigAction *new_disp_=0, SigAction *old_disp_=0)
| Remove EventHandler from the list of registered handler for signum_.
Public Member Functions inherited from ASSA::SigHandler
virtual | ~SigHandler ()
| No-op virtual destructor.
EventHandler * | handler (int signum_, EventHandler *new_)
| Set new event handler for signal signum_ and return an existing one.
EventHandler * | handler (int signum_)
| Retrieve current event handler for signum_.

## Static Public Member Functions

static void | sighandlers_dispatcher (int signum_)
| A wrapper around static SigHandlers::dispatch().
Static Public Member Functions inherited from ASSA::SigHandler
static void | dispatch (int signum_)
| Here is the heart of SigHandler class.

## Static Private Member Functions

static void | dispatch (int signum_)
| The heart of SigHandlers class - this callback function is really registered with OS to catch all of the signals for which event handler has been installed.

## Additional Inherited Members

Protected Member Functions inherited from ASSA::SigHandler
int | in_range (int signum_)
| Check that signum_ is in valid range.

## Detailed Description

Definition at line 55 of file SigHandlers.h.

## Member Function Documentation

## dispatch()

void SigHandlers::dispatch | ( | int | signum_ | ) |

static private

The heart of SigHandlers class - this callback function is really registered with OS to catch all of the signals for which event handler has been installed.

Appropriate EventHandler(s) are then notified.

Definition at line 236 of file SigHandlers.cpp.

{
trace_with_mask("SigHandlers::dispatch", SIGHAND);
/*---
For every element in the set that holds all EventHandlers for
given signum, call its respective handle_signal() member function.
---*/
/*--- save errno ---*/
int errno_saved = errno;
SigHandlersList& handlist = *(SigHandlersList::instance(signum_));
SigHandlersList::iterator it;
EventHandler* ehp;
for (it=handlist.begin(); it != handlist.end(); it++) {
ehp = *it;
if (ehp->handle_signal (signum_) == -1) {
/*---
this event handler reported error when handling
signum - remove it from the set
---*/
handlist.erase (it);
}
}
/*--- restore errno ---*/
errno = errno_saved;
}
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
ASSA::EventHandler::handle_signal
virtual int handle_signal(int signum_)
Signal handler callback.
Definition EventHandler.h:204
ASSA::SigHandlersList::begin
iterator begin()
Return an iterator pointing to the beginning of the list.
Definition SigHandlersList.h:300
ASSA::SigHandlersList::erase
void erase(const key_type key_)
Find and remove event handler key_ from the list.
Definition SigHandlersList.h:270
ASSA::SigHandlersList::end
iterator end()
Return an iterator pointing to the end of the list.
Definition SigHandlersList.h:309
ASSA::SigHandlersList::instance
static SigHandlersList * instance(int signum_)
Retrieve a pointer to the list of event handlers listening to signum_ signal delivery.
Definition SigHandlersList.h:203
ASSA::SigHandlersList::iterator
set< key_type, CompSHL >::iterator iterator
Definition SigHandlersList.h:82
ASSA::SIGHAND
@ SIGHAND
Class SigHandler(s) messages.
Definition LogMask.h:38

References ASSA::SigHandlersList::begin(), ASSA::SigHandlersList::end(), ASSA::SigHandlersList::erase(), ASSA::EventHandler::handle_signal(), ASSA::SigHandlersList::instance(), ASSA::SIGHAND, and trace_with_mask.

Referenced by sighandlers_dispatcher().

## install()

int SigHandlers::install | ( | int | signum_,
|  | EventHandler * | new_hand_,
|  | SigAction * | new_disp_ = 0,
|  | EventHandler ** | old_hand_ = 0,
|  | SigAction * | old_disp_ = 0 )

virtual

Register EventHandler with dispatching system.

Parameters

  signum_ | (In ) Signal number.
  new_hand_ | (In ) Pointer to new event handler to install.
  new_disp_ | (In=0 ) New disposition to use to handle signal.
  old_hand_ | (Out=0) Placeholder for old event handler.
  old_disp_ | (Out=0) Placeholder for old disposition.

Returns
  0 on success; -1 on error.

Reimplemented from ASSA::SigHandler.

Definition at line 35 of file SigHandlers.cpp.

{
/*
Retrieve current signal disposition. If 3rd party handler has
already been istalled, make CFUNC_Handler out of it, and put it in
the list with id=0.
Add new_hand_ to the list. Has global sighandlers_dispatcher not
been installed yet, install it too.
*/
trace_with_mask("SigHandlers::install()", SIGHAND);
if (!in_range(signum_) == -1) {
EL((ASSAERR,"in_range (%s) failed\n",signum_));
return -1;
}
CFUNC_Handler* cfhp = NULL;
SigHandlersList* handlist = NULL;
handlist = SigHandlersList::instance(signum_);
/*--- Retrieve current signal disposition ---*/
SigAction cd;
cd.retrieve_action(signum_);
/*
Check whether 3rd party software has already installed
signal handler.
*/
if ( cd.handler() != (C_SIG_HANDLER) sighandlers_dispatcher &&
cd.handler() != SIG_IGN &&
cd.handler() != SIG_DFL )
{
/*
Looks like some other code got ahead of me and installed C-function
signal handler. Make a note of it.
Create EventHandler to hold 3rd party handler. This handler will be
deleted only by SigHandlers::remove (NULL), when application demanded
to remove all of the handlers.
*/
DL((SIGHAND,"Detected 3rd party \"C\" handler!\n"));
cfhp = new CFUNC_Handler (cd.handler ());
handlist->cfunc_handler (cfhp);
/*
Insert 3rd party handler in list of handlers
for this signal.
*/
DL((SIGHAND,"Adding 3rd party \"C\" handler\n"));
if ( handlist->insert (cfhp) == false ) {
EL((ASSAERR, "Failed to insert "\
"c_func_handler for signum %d\n", signum_));
delete (cfhp);
handlist->cfunc_handler (0);
return -1;
}
DL((SIGHAND,"Set size: %d\n", handlist->size () ));
}
/*--- Add new_hand_ to the list of handlers for signum_. ---*/
DL((SIGHAND,"Adding EventHandler to the list\n"));
if (handlist->insert (new_hand_) == false) {
/*---
I failed to install new handler and might have already
added 3rd party CFUNC_Handler to the list without altering
disposition - if that's true, clean up the list.
---*/
EL((ASSAERR,"failed to add new_hand_ to handlers list\n"));
if (handlist->seen_cfunc_handler () &&
handlist->size() == 1)
{
handlist->erase ();
handlist->cfunc_handler (0);
}
return -1;
}
DL((SIGHAND,"Set size: %d\n", handlist->size () ));
/*--- Has sighandlers_dispatcher been already installed? ---*/
if (cd.handler() == (C_SIG_HANDLER) sighandlers_dispatcher) {
return 0;
}
DL((SIGHAND,"Installing 'sighandlers_dispatcher'\n"));
/*
Installing new disposition; if user forgot to give me one
then default will be used.
*/
SigAction sa ((C_SIG_HANDLER) SIG_DFL);
if (new_disp_ == 0) {
new_disp_ = &sa;
}
new_disp_->handler ((C_SIG_HANDLER) sighandlers_dispatcher);
if (new_disp_->register_action (signum_, old_disp_) == -1) {
/*---
I failed to install sighandlers_dispatcher. Up to this
point, if application had conventional C handler installed,
it still remains active. Handlers list built so far is
meaningless - get rid of it. ---*/
EL((ASSAERR,"register_action() error\n"));
if (handlist->seen_cfunc_handler ()) {
handlist->erase ();
handlist->cfunc_handler (0);
delete cfhp;
}
handlist->erase (new_hand_);
return -1;
}
return 0;
}
EL
#define EL(X)
A macro for writing error message to the Logger.
Definition Logger.h:285
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
C_SIG_HANDLER
void(* C_SIG_HANDLER)(int)
Definition SigAction.h:28
ASSA::SigAction::handler
void handler(C_SIG_HANDLER sha_)
Set new signal handler to function pointer sha_.
Definition SigAction.h:334
ASSA::SigAction::retrieve_action
int retrieve_action(int signum_)
Retrieve current disposition for the signal signum_ into this object.
Definition SigAction.h:381
ASSA::SigAction::register_action
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
ASSA::SigHandler::in_range
int in_range(int signum_)
Check that signum_ is in valid range.
Definition SigHandler.cpp:27
ASSA::SigHandlersList::insert
bool insert(data_type data_)
Add an event handler data_ to the list.
Definition SigHandlersList.h:241
ASSA::SigHandlersList::cfunc_handler
CFUNC_Handler * cfunc_handler(CFUNC_Handler *cfp_)
Save 3rd party C function handler to remember.
Definition SigHandlersList.h:328
ASSA::SigHandlersList::size
size_t size() const
Size of the list.
Definition SigHandlersList.h:230
ASSA::SigHandlersList::seen_cfunc_handler
void seen_cfunc_handler(bool ft_)
Indicate whether 3rd party C function handler was installed.
Definition SigHandlersList.h:349
ASSA::SigHandlers::sighandlers_dispatcher
static void sighandlers_dispatcher(int signum_)
A wrapper around static SigHandlers::dispatch().
Definition SigHandlers.cpp:26
ASSA::ASSAERR
@ ASSAERR
ASSA and system errors.
Definition LogMask.h:34

References ASSA::ASSAERR, ASSA::SigHandlersList::cfunc_handler(), DL, EL, ASSA::SigHandlersList::erase(), ASSA::SigAction::handler(), ASSA::SigHandler::in_range(), ASSA::SigHandlersList::insert(), ASSA::SigHandlersList::instance(), ASSA::SigAction::register_action(), ASSA::SigAction::retrieve_action(), ASSA::SigHandlersList::seen_cfunc_handler(), ASSA::SIGHAND, sighandlers_dispatcher(), ASSA::SigHandlersList::size(), and trace_with_mask.

## remove()

int SigHandlers::remove | ( | int | signum_,
|  | EventHandler * | eh_,
|  | SigAction * | new_disp_ = 0,
|  | SigAction * | old_disp_ = 0 )

virtual

Remove EventHandler from the list of registered handler for signum_.

If eh_ is NULL, then all EventHandler(s) will be removed from the list, and object will go back to passive mode in which no signal handling is done via SigHandlers class dispatcher. If new_disp_ is omitted, SIG_DFL will be used instead.

Parameters

  signum_ | (In ) Signal number.
  eh_ | (In ) Event handler to remove.
  new_disp_ | (In=0 ) New disposition to use to handle signal.
  old_disp_ | (Out=0) Placeholder for old disposition.

Returns
  0 on success; -1 on error.

Reimplemented from ASSA::SigHandler.

Definition at line 166 of file SigHandlers.cpp.

{
trace_with_mask("SigHandlers::remove()", SIGHAND);
if (in_range (signum_)) {
EL((ASSAERR, "singum_ %d is out of range\n", signum_));
return -1;
}
CFUNC_Handler* Cfhp = NULL; // pointer to C-function event handler
EventHandler* ehp = NULL; // pointer to current event handler
SigHandlersList& handlist = *(SigHandlersList::instance(signum_));
if (eh_ == NULL) {
DL((SIGHAND,"Erasing the entire set\n"));
/*--- Erase an entire list. ---*/
handlist.erase ();
DL((SIGHAND,"Set size: %d\n", handlist.size ()));
}
else {
/*
Note: I cannot do erasure in the same loop for following reason:
According to Stroustrup (Section 17.4.1.7):
"After erase(), the iterator cannot be used again because
the element to which it pointed is no longer there."
According to STL Tutorial and Ref. Guide:
"The erase function invalidates all iterators to all
positions past the point of erasure."
That's why here we first take care of id recycling and heap memory
deallocation, and only then clean() the map all at once.
*/
SigHandlersList::iterator it;
if ((it = handlist.find (eh_)) != handlist.end ()) {
DL((SIGHAND,"Removing EventHandler\n"));
ehp = (*it);
handlist.erase (it);
}
DL((SIGHAND,"Set size: %d\n", handlist.size () ));
}
/*--- If set is not empty, we're done ---*/
if (handlist.size ()) return 0;
/* If map was emptied out, install new disposition
with the 3rd party "C" function handler, if we had it.
*/
SigAction null_sa;
if (new_disp_ == 0) new_disp_ = &null_sa;
DL((SIGHAND,"Handlers List is empty\n"));
if (handlist.seen_cfunc_handler ()) {
/*--- Put 3rd party handler into disposition ---*/
DL((SIGHAND,"Reinstalling \"C\" handler\n"));
Cfhp = handlist.cfunc_handler (0);
new_disp_->handler (Cfhp->handler ());
delete Cfhp;
}
/*--- Install new disposition ---*/
return new_disp_->register_action (signum_, old_disp_);
}
ASSA::CFUNC_Handler::handler
C_SIG_HANDLER handler()
Definition SigHandlersList.h:50
ASSA::SigHandlersList::find
iterator find(const key_type key_)
Find event handler by its pointer key_.
Definition SigHandlersList.h:318

References ASSA::ASSAERR, ASSA::SigHandlersList::cfunc_handler(), DL, EL, ASSA::SigHandlersList::end(), ASSA::SigHandlersList::erase(), ASSA::SigHandlersList::find(), ASSA::CFUNC_Handler::handler(), ASSA::SigAction::handler(), ASSA::SigHandler::in_range(), ASSA::SigHandlersList::instance(), ASSA::SigAction::register_action(), ASSA::SigHandlersList::seen_cfunc_handler(), ASSA::SIGHAND, ASSA::SigHandlersList::size(), and trace_with_mask.

## sighandlers_dispatcher()

void SigHandlers::sighandlers_dispatcher | ( | int | signum_ | ) |

static

A wrapper around static SigHandlers::dispatch().

It is needed for the purpose of differentiation it with other signal handlers that might be installed by user's code.

Parameters

  signum_ | Dispatch event handlers for this signal.

Definition at line 25 of file SigHandlers.cpp.

{
trace_with_mask("SigHandlers::sighandlers_dispatch", SIGHAND);
DL((SIGHAND,"==> Received signal # %d\n", signum_));
dispatch (signum_);
}
ASSA::SigHandlers::dispatch
static void dispatch(int signum_)
The heart of SigHandlers class - this callback function is really registered with OS to catch all of ...
Definition SigHandlers.cpp:237

References dispatch(), DL, ASSA::SIGHAND, and trace_with_mask.

Referenced by install().

The documentation for this class was generated from the following files:

- SigHandlers.h
- SigHandlers.cpp