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
|
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
// (c) COPYRIGHT URI/MIT 1994-2002
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
#include "config.h"
#include <csignal>
#include "SignalHandler.h"
#include "util.h"
namespace libdap {
std::vector<EventHandler *> SignalHandler::d_signal_handlers{NSIG, nullptr};
std::vector<Sigfunc *> SignalHandler::d_old_handlers{NSIG, nullptr};
/** This private method is the adapter between the C-style interface of the
signal subsystem and C++'s method interface. This uses the lookup table
to find an instance of EventHandler and calls that instance's
handle_signal method.
It then looks for an 'old handler' (one that was set before and was/is not
a child of EvenHandler) and calls it.
@param signum The number of the signal. */
void SignalHandler::dispatcher(int signum) {
// Perform a sanity check...
if (d_signal_handlers[signum] != nullptr)
// Dispatch the handler's hook method.
d_signal_handlers[signum]->handle_signal(signum);
Sigfunc *old_handler = SignalHandler::d_old_handlers[signum];
if (old_handler == SIG_IGN || old_handler == SIG_ERR)
return;
else if (old_handler == SIG_DFL)
throw Error(internal_error, "Signal handler operation on an unsupported signal.");
else
old_handler(signum);
}
/** Get a pointer to the single instance of SignalHandler. */
SignalHandler *SignalHandler::instance() {
static SignalHandler instance;
return &instance;
}
/** Register an event handler. By default run any previously registered
action/handler such as those installed using \c sigaction(). For signals
such as SIGALRM (the alarm signal) this may not be what you want; see the
\e override parameter. See also the class description.
@param signum Bind the event handler to this signal number. Limited to
those signals that, according to POSIX.1, cause process termination.
@param eh A pointer to the EventHandler for \c signum.
@param ignore_by_default If \c true, do not run the default handler/action.
Instead run \e eh and then treat the signal as if the original action was
SIG_IGN. Default is false.
@return A pointer to the old EventHandler or null. */
EventHandler *SignalHandler::register_handler(int signum, EventHandler *eh, bool ignore_by_default) {
// Check first for improper use.
switch (signum) {
#ifndef WIN32
case SIGHUP:
case SIGKILL:
case SIGUSR1:
case SIGUSR2:
case SIGPIPE:
case SIGALRM:
#endif
case SIGINT:
case SIGTERM:
break;
default:
throw InternalErr(__FILE__, __LINE__,
string("Call to register_handler with unsupported signal (") + long_to_string(signum) +
string(")."));
}
// Save the old EventHandler
EventHandler *old_eh = SignalHandler::d_signal_handlers[signum];
SignalHandler::d_signal_handlers[signum] = eh;
// Register the dispatcher to handle this signal. See Stevens, Advanced
// Programming in the UNIX Environment, p.298.
#ifndef WIN32
struct sigaction sa {};
sa.sa_handler = dispatcher;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
// Try to suppress restarting system calls if we're handling an alarm.
// This lets alarms block I/O calls that would normally restart. 07/18/03
// jhrg
if (signum == SIGALRM) {
#ifdef SA_INTERUPT
sa.sa_flags |= SA_INTERUPT;
#endif
} else {
#ifdef SA_RESTART
sa.sa_flags |= SA_RESTART;
#endif
}
struct sigaction old_sa {}; // extract the old handler/action
if (sigaction(signum, &sa, &old_sa) < 0)
throw InternalErr(__FILE__, __LINE__, "Could not register a signal handler.");
// Take care of the case where this interface is used to register a
// handler more than once. We want to make sure that the dispatcher is
// not installed as the 'old handler' because that results in an infinite
// loop. 02/10/04 jhrg
if (ignore_by_default)
SignalHandler::d_old_handlers[signum] = SIG_IGN;
else if (old_sa.sa_handler != &dispatcher)
SignalHandler::d_old_handlers[signum] = old_sa.sa_handler;
#endif // ndef WIN32
return old_eh;
}
/** Remove the event handler.
@param signum The signal number of the handler to remove.
@return The old event handler */
EventHandler *SignalHandler::remove_handler(int signum) {
EventHandler *old_eh = SignalHandler::d_signal_handlers[signum];
SignalHandler::d_signal_handlers[signum] = nullptr;
return old_eh;
}
} // namespace libdap
|