libassa 3.5.1
Loading...
Searching...
No Matches
SigHandler.cpp
Go to the documentation of this file.
1// -*- c++ -*-
2//---------------------------------------------------------------------------
3// SigHandler.cpp
4//---------------------------------------------------------------------------
5// Copyright (C) 1997-2002 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#include <signal.h>
13
14#include "assa/Assure.h"
15#include "assa/SigHandler.h"
16
17using namespace ASSA;
18
19#if !defined(WIN32)
20
21/*--- static variables ---*/
22
24
25int
27in_range (int signum_)
28{
29 trace_with_mask("SigHandler::in_range", SIGHAND);
30
31 if ( signum_ >= 1 && signum_ < NSIG) {
32 return 0;
33 }
34 else {
35 DL((SIGHAND,"signum_ %d is out of range [1;%d]\n", NSIG));
36 return -1;
37 }
38}
39
43{
44 trace_with_mask("SigHandler::handler(int, EH*)", SIGHAND);
45
46
47 if (in_range(signum_) == -1)
48 return 0;
49
52
53 return oh;
54}
55
58handler (int signum_)
59{
60 trace_with_mask("SigHandler::handler", SIGHAND);
61
62 if ( in_range (signum_) == -1 )
63 return 0;
64
66}
67
68int
72{
73 trace_with_mask("SigHandler::install", SIGHAND);
74
75 if (in_range (signum_) == -1)
76 return -1;
77
78 /*--- replace old Event Handler ptr with new one in my internal
79 dispatch table, returning the old one.
80 ---*/
82
83 /*--- if I am given place to store, save old handler ---*/
84 if (old_hand_ != 0)
85 *old_hand_ = eh;
86
87 /*--- retrieve old disposition ---*/
88 if (old_disp_ != 0) {
89 old_disp_->retrieve_action (signum_);
90 old_disp_->handler ((C_SIG_HANDLER) SIG_DFL);
91 }
92
93 /*--- if new disposition is NULL, use null action instead ---*/
95
96 if (new_disp_ == 0)
98
99 /*--- install my dispatcher ---*/
101
102 return new_disp_->register_action(signum_, old_disp_);
103}
104
105int
107remove (int signum_, EventHandler* /* eh_ */,
109{
110 trace_with_mask("SigHandler::remove", SIGHAND);
111
112 if (in_range(signum_) == -1)
113 return -1;
114 /*---
115 We need default disposition here if user forgot to give us one.
116 ---*/
118
119 if (new_disp_ == 0) {
120 new_disp_ = &sa;
121 }
122
124
125 return new_disp_->register_action (signum_, old_disp_);
126}
127
128void
130dispatch (int signum_)
131{
132 trace_with_mask("SigHandler::dispatch", SIGHAND);
133
134 /*--- save errno ---*/
135 int my_errno = errno;
136
138
139 if (eh != 0 && eh->handle_signal(signum_) == -1) {
140 /*---
141 we are in trouble, fall back to defaults
142 ---*/
145 defact.register_action(signum_);
146 }
147 /*--- restore errno ---*/
148 errno = my_errno;
149}
150
151#endif // !defined(WIN32)
152
A collection of assert function wrappers.
#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
void(* C_SIG_HANDLER)(int)
Definition SigAction.h:28
Class SigHandler is a UNIX signal handlers manager/dispatcher class.
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
EventHandler class.
static EventHandler * m_signal_handlers[NSIG]
Static array that stores one user-defined event handler pointer for every signal.
Definition SigHandler.h:127
virtual int remove(int signum_, EventHandler *eh_=0, SigAction *new_disp_=0, SigAction *old_disp_=0)
Remove EventHandler associated with signum_.
static void dispatch(int signum_)
Here is the heart of SigHandler class.
int in_range(int signum_)
Check that signum_ is in valid range.
EventHandler * handler(int signum_, EventHandler *new_)
Set new event handler for signal signum_ and return an existing one.
virtual int install(int signum_, EventHandler *new_hand_, SigAction *new_disp_=0, EventHandler **old_hand_=0, SigAction *old_disp_=0)
Add new signal handler and new disposition for the signal.
@ SIGHAND
Class SigHandler(s) messages
Definition LogMask.h:38