1*11be35a1SLionel Sambuc // Copyright 2010 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include "utils/signals/programmer.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <signal.h>
33*11be35a1SLionel Sambuc }
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include <cerrno>
36*11be35a1SLionel Sambuc
37*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
38*11be35a1SLionel Sambuc #include "utils/logging/macros.hpp"
39*11be35a1SLionel Sambuc #include "utils/sanity.hpp"
40*11be35a1SLionel Sambuc #include "utils/signals/exceptions.hpp"
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc namespace utils {
44*11be35a1SLionel Sambuc namespace signals {
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc
47*11be35a1SLionel Sambuc /// Internal implementation for the signals::programmer class.
48*11be35a1SLionel Sambuc struct programmer::impl {
49*11be35a1SLionel Sambuc /// The number of the signal managed by this programmer.
50*11be35a1SLionel Sambuc int signo;
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc /// Whether the signal is currently programmed by us or not.
53*11be35a1SLionel Sambuc bool programmed;
54*11be35a1SLionel Sambuc
55*11be35a1SLionel Sambuc /// The signal handler that we replaced; to be restored on unprogramming.
56*11be35a1SLionel Sambuc struct ::sigaction old_sa;
57*11be35a1SLionel Sambuc
58*11be35a1SLionel Sambuc /// Initializes the internal implementation of the programmer.
59*11be35a1SLionel Sambuc ///
60*11be35a1SLionel Sambuc /// \param signo_ The signal number.
implutils::signals::programmer::impl61*11be35a1SLionel Sambuc impl(const int signo_) :
62*11be35a1SLionel Sambuc signo(signo_),
63*11be35a1SLionel Sambuc programmed(false)
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc }
66*11be35a1SLionel Sambuc };
67*11be35a1SLionel Sambuc
68*11be35a1SLionel Sambuc
69*11be35a1SLionel Sambuc } // namespace signals
70*11be35a1SLionel Sambuc } // namespace utils
71*11be35a1SLionel Sambuc
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc namespace signals = utils::signals;
74*11be35a1SLionel Sambuc
75*11be35a1SLionel Sambuc
76*11be35a1SLionel Sambuc /// Programs a signal handler.
77*11be35a1SLionel Sambuc ///
78*11be35a1SLionel Sambuc /// \param signo The signal for which to install the handler.
79*11be35a1SLionel Sambuc /// \param handler The handler to install.
80*11be35a1SLionel Sambuc ///
81*11be35a1SLionel Sambuc /// \throw signals::system_error If there is an error programming the signal.
programmer(const int signo,const handler_type handler)82*11be35a1SLionel Sambuc signals::programmer::programmer(const int signo, const handler_type handler) :
83*11be35a1SLionel Sambuc _pimpl(new impl(signo))
84*11be35a1SLionel Sambuc {
85*11be35a1SLionel Sambuc struct ::sigaction sa;
86*11be35a1SLionel Sambuc sa.sa_handler = handler;
87*11be35a1SLionel Sambuc sigemptyset(&sa.sa_mask);
88*11be35a1SLionel Sambuc sa.sa_flags = SA_RESTART;
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc if (::sigaction(_pimpl->signo, &sa, &_pimpl->old_sa) == -1) {
91*11be35a1SLionel Sambuc const int original_errno = errno;
92*11be35a1SLionel Sambuc throw system_error(F("Could not install handler for signal %s") %
93*11be35a1SLionel Sambuc _pimpl->signo, original_errno);
94*11be35a1SLionel Sambuc } else
95*11be35a1SLionel Sambuc _pimpl->programmed = true;
96*11be35a1SLionel Sambuc }
97*11be35a1SLionel Sambuc
98*11be35a1SLionel Sambuc
99*11be35a1SLionel Sambuc /// Destructor; unprograms the signal handler if still programmed.
100*11be35a1SLionel Sambuc ///
101*11be35a1SLionel Sambuc /// Given that this is a destructor and it can't report errors back to the
102*11be35a1SLionel Sambuc /// caller, the caller must attempt to call unprogram() on its own.
~programmer(void)103*11be35a1SLionel Sambuc signals::programmer::~programmer(void)
104*11be35a1SLionel Sambuc {
105*11be35a1SLionel Sambuc if (_pimpl->programmed) {
106*11be35a1SLionel Sambuc LW("Destroying still-programmed signals::programmer object");
107*11be35a1SLionel Sambuc try {
108*11be35a1SLionel Sambuc unprogram();
109*11be35a1SLionel Sambuc } catch (const system_error& e) {
110*11be35a1SLionel Sambuc UNREACHABLE;
111*11be35a1SLionel Sambuc }
112*11be35a1SLionel Sambuc }
113*11be35a1SLionel Sambuc }
114*11be35a1SLionel Sambuc
115*11be35a1SLionel Sambuc
116*11be35a1SLionel Sambuc /// Unprograms the signal handler.
117*11be35a1SLionel Sambuc ///
118*11be35a1SLionel Sambuc /// \pre The signal handler is programmed (i.e. this can only be called once).
119*11be35a1SLionel Sambuc ///
120*11be35a1SLionel Sambuc /// \throw system_error If unprogramming the signal failed. If this happens,
121*11be35a1SLionel Sambuc /// the signal is left programmed, this object forgets about the signal and
122*11be35a1SLionel Sambuc /// therefore there is no way to restore the original handler.
123*11be35a1SLionel Sambuc void
unprogram(void)124*11be35a1SLionel Sambuc signals::programmer::unprogram(void)
125*11be35a1SLionel Sambuc {
126*11be35a1SLionel Sambuc PRE(_pimpl->programmed);
127*11be35a1SLionel Sambuc
128*11be35a1SLionel Sambuc // If we fail, we don't want the destructor to attempt to unprogram the
129*11be35a1SLionel Sambuc // handler again, as it would result in a crash.
130*11be35a1SLionel Sambuc _pimpl->programmed = false;
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc if (::sigaction(_pimpl->signo, &_pimpl->old_sa, NULL) == -1) {
133*11be35a1SLionel Sambuc const int original_errno = errno;
134*11be35a1SLionel Sambuc throw system_error(F("Could not reset handler for signal %s") %
135*11be35a1SLionel Sambuc _pimpl->signo, original_errno);
136*11be35a1SLionel Sambuc }
137*11be35a1SLionel Sambuc }
138