summaryrefslogtreecommitdiffstats
path: root/src/lib/util/io/signal_set.cc
blob: b187df69d1cce3d301dac95217d1ef0f79ef25ae (plain)
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
// Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <util/io/signal_set.h>

#include <cerrno>
#include <list>

using namespace isc;
using namespace isc::util::io;

namespace {

/// @brief Returns a pointer to the global set of registered signals.
///
/// Multiple instances of @c SignalSet may use this pointer to access
/// and update the set.
///
/// @return Pointer to the global set of registered signals. This pointer
/// is always initialized and points to a valid object.
std::set<int>* getRegisteredSignals() {
    static std::set<int> registered_signals;
    return (&registered_signals);
}

/// @brief Returns a pointer to static collection of signals received.
///
/// Multiple instances of @c SignalSet may use this pointer to access
/// and update the queue of signals received.
///
/// @return Static collection of signals received. This pointer is always
/// initialized and points to a valid object.
std::list<int>* getSignalStates() {
    static std::list<int> states;
    return (&states);
}

/// @brief Internal signal handler for @c isc::util::io::SignalSet class.
///
/// This signal handler adds a signal number for which it is being
/// invoked to the queue of received signals. It prevents adding duplicated
/// signals. All duplicated signals are dropped. This prevents hammering
/// a process to invoke handlers (e.g. DHCP server reconfiguration), when
/// many the same signals are received one after another.
///
/// @param sig Signal number.
void internalHandler(int sig) {
    std::list<int>* states = getSignalStates();
    for (std::list<int>::const_iterator it = states->begin();
         it != states->end(); ++it) {
        if (sig == *it) {
            return;
        }
    }
    states->push_back(sig);
}

}

namespace isc {
namespace util {
namespace io {

SignalSet::SignalSet(const int sig0) {
    add(sig0);
}

SignalSet::SignalSet(const int sig0, const int sig1) {
    add(sig0);
    add(sig1);
}

SignalSet::SignalSet(const int sig0, const int sig1, const int sig2) {
    add(sig0);
    add(sig1);
    add(sig2);
}

SignalSet::~SignalSet() {
    // Set default signal handlers.
    try {
        clear();
    } catch (...) {
        // Not a good thing to throw from a destructor. in fact this should
        // not throw an exception because we just unregister the signals
        // that we have previously registered. So the signal codes are fine.
    }
}

void
SignalSet::add(const int sig) {
    insert(sig);
    struct sigaction sa;
    sa.sa_handler = internalHandler;
    if (sigaction(sig, &sa, 0) < 0) {
        erase(sig);
        isc_throw(SignalSetError, "failed to register a signal handler for"
                  " signal " << sig << ": " << strerror(errno));
    }
}

void
SignalSet::clear() {
    // Iterate over a copy of the registered signal set because the
    // remove function is erasing the elements and we don't want to
    // erase the elements we are iterating over. This would cause
    // a segfault.
    std::set<int> all_signals = local_signals_;
    for (std::set<int>::const_iterator it = all_signals.begin();
         it != all_signals.end(); ++it) {
        remove(*it);
    }
}

int
SignalSet::getNext() const {
    std::list<int>* states = getSignalStates();
    for (std::list<int>::iterator it = states->begin();
         it != states->end(); ++it) {
        if (local_signals_.find(*it) != local_signals_.end()) {
            return (*it);
        }
    }
    return (-1);
}

void
SignalSet::erase(const int sig) {
    if (local_signals_.find(sig) == local_signals_.end()) {
        isc_throw(SignalSetError, "failed to unregister signal " << sig
                  << " from a signal set: signal is not owned by the"
                  " signal set");
    }
    // Remove globally registered signal.
    getRegisteredSignals()->erase(sig);
    // Remove unhandled signals from the queue.
    for (std::list<int>::iterator it = getSignalStates()->begin();
         it != getSignalStates()->end(); ++it) {
        if (*it == sig) {
            it = getSignalStates()->erase(it);
        }
    }
    // Remove locally registered signal.
    local_signals_.erase(sig);
}

void
SignalSet::handleNext(SignalHandler signal_handler) {
    block();
    int signum = getNext();
    if (signum >= 0) {
        popNext();
        try {
            signal_handler(signum);
        } catch (...) {
            unblock();
            throw;
        }
    }
    unblock();
}

void
SignalSet::insert(const int sig) {
    std::set<int>* global_signals = getRegisteredSignals();
    if ((global_signals->find(sig) != global_signals->end()) ||
        (local_signals_.find(sig) != local_signals_.end())) {
        isc_throw(SignalSetError, "attempt to register a duplicate signal "
                  << sig);
    }
    global_signals->insert(sig);
    local_signals_.insert(sig);
}

void
SignalSet::maskSignals(const int mask) const {
    sigset_t new_set;
    for (std::set<int>::const_iterator it = getRegisteredSignals()->begin();
         it != getRegisteredSignals()->end(); ++it) {
        sigaddset(&new_set, *it);
    }
    sigprocmask(mask, &new_set, 0);
}

void
SignalSet::popNext() {
    std::list<int>* states = getSignalStates();
    for (std::list<int>::iterator it = states->begin();
         it != states->end(); ++it) {
        if (local_signals_.find(*it) != local_signals_.end()) {
            states->erase(it);
            return;
        }
    }
}

void
SignalSet::remove(const int sig) {
    // Unregister only if we own this signal.
    if (local_signals_.find(sig) != local_signals_.end()) {
        struct sigaction sa;
        sa.sa_handler = SIG_DFL;
        if (sigaction(sig, &sa, 0) < 0) {
            isc_throw(SignalSetError, "unable to restore original signal"
                      " handler for signal: " << sig);
        }
        erase(sig);
    } else {
        isc_throw(SignalSetError, "failed to unregister signal " << sig
                  << ": this signal is not owned by the signal set");
    }
}

} // end of isc::util::io
} // end of isc::util
} // end of isc