libdap++ Updated for version 3.8.2
|
00001 00002 // -*- mode: c++; c-basic-offset:4 -*- 00003 00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data 00005 // Access Protocol. 00006 00007 // Copyright (c) 2002,2003 OPeNDAP, Inc. 00008 // Author: James Gallagher <jgallagher@opendap.org> 00009 // 00010 // This library is free software; you can redistribute it and/or 00011 // modify it under the terms of the GNU Lesser General Public 00012 // License as published by the Free Software Foundation; either 00013 // version 2.1 of the License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 // 00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112. 00025 00026 // (c) COPYRIGHT URI/MIT 1994-2002 00027 // Please read the full copyright statement in the file COPYRIGHT_URI. 00028 // 00029 // Authors: 00030 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu> 00031 00032 #include "config.h" 00033 00034 static char rcsid[] not_used = 00035 { "$Id: SignalHandler.cc 22703 2010-05-11 18:10:01Z jimg $" 00036 }; 00037 00038 #include <cstdlib> 00039 00040 #include <signal.h> 00041 #include <pthread.h> 00042 00043 #ifdef HAVE_UNISTD_H 00044 #include <unistd.h> //for _exit 00045 #endif 00046 00047 #include "SignalHandler.h" 00048 #include "util.h" 00049 00050 namespace libdap { 00051 00052 EventHandler *SignalHandler::d_signal_handlers[NSIG]; 00053 Sigfunc *SignalHandler::d_old_handlers[NSIG]; 00054 SignalHandler *SignalHandler::d_instance = 0; 00055 00056 // instance_control is used to ensure that in a MT environment d_instance is 00057 // correctly initialized. 00058 static pthread_once_t instance_control = PTHREAD_ONCE_INIT; 00059 00061 void 00062 SignalHandler::initialize_instance() 00063 { 00064 // MT-Safe if called via pthread_once or similar 00065 SignalHandler::d_instance = new SignalHandler; 00066 atexit(SignalHandler::delete_instance); 00067 } 00068 00070 void 00071 SignalHandler::delete_instance() 00072 { 00073 if (SignalHandler::d_instance) { 00074 for (int i = 0; i < NSIG; ++i) { 00075 // Fortify warns about a leak because the EventHandler objects 00076 // are not deleted, but that's OK - this is a singleton and 00077 // so the 'leak' is really just a constant amount of memory that 00078 // gets used. 00079 d_signal_handlers[i] = 0; 00080 d_old_handlers[i] = 0; 00081 } 00082 00083 delete SignalHandler::d_instance; 00084 SignalHandler::d_instance = 0; 00085 } 00086 } 00087 00094 void 00095 SignalHandler::dispatcher(int signum) 00096 { 00097 // Perform a sanity check... 00098 if (SignalHandler::d_signal_handlers[signum] != 0) 00099 // Dispatch the handler's hook method. 00100 SignalHandler::d_signal_handlers[signum]->handle_signal(signum); 00101 00102 Sigfunc *old_handler = SignalHandler::d_old_handlers[signum]; 00103 if (old_handler == SIG_IGN || old_handler == SIG_ERR) 00104 return; 00105 else if (old_handler == SIG_DFL) { 00106 switch (signum) { 00107 #ifndef WIN32 00108 case SIGHUP: 00109 case SIGKILL: 00110 case SIGUSR1: 00111 case SIGUSR2: 00112 case SIGPIPE: 00113 case SIGALRM: 00114 #endif 00115 case SIGINT: 00116 case SIGTERM: _exit(EXIT_FAILURE); 00117 00118 // register_handler() should never allow any fiddling with 00119 // signals other than those listed above. 00120 default: abort(); 00121 } 00122 } 00123 else 00124 old_handler(signum); 00125 } 00126 00128 SignalHandler* 00129 SignalHandler::instance() 00130 { 00131 pthread_once(&instance_control, initialize_instance); 00132 00133 return d_instance; 00134 } 00135 00148 EventHandler * 00149 SignalHandler::register_handler(int signum, EventHandler *eh, bool override) 00150 { 00151 // Check first for improper use. 00152 switch (signum) { 00153 #ifndef WIN32 00154 case SIGHUP: 00155 case SIGKILL: 00156 case SIGUSR1: 00157 case SIGUSR2: 00158 case SIGPIPE: 00159 case SIGALRM: 00160 #endif 00161 case SIGINT: 00162 case SIGTERM: break; 00163 00164 default: throw InternalErr(__FILE__, __LINE__, 00165 string("Call to register_handler with unsupported signal (") 00166 + long_to_string(signum) + string(").")); 00167 } 00168 00169 // Save the old EventHandler 00170 EventHandler *old_eh = SignalHandler::d_signal_handlers[signum]; 00171 00172 SignalHandler::d_signal_handlers[signum] = eh; 00173 00174 // Register the dispatcher to handle this signal. See Stevens, Advanced 00175 // Programming in the UNIX Environment, p.298. 00176 #ifndef WIN32 00177 struct sigaction sa; 00178 sa.sa_handler = dispatcher; 00179 sigemptyset(&sa.sa_mask); 00180 sa.sa_flags = 0; 00181 00182 // Try to suppress restarting system calls if we're handling an alarm. 00183 // This lets alarms block I/O calls that would normally restart. 07/18/03 00184 // jhrg 00185 if (signum == SIGALRM) { 00186 #ifdef SA_INTERUPT 00187 sa.sa_flags |= SA_INTERUPT; 00188 #endif 00189 } 00190 else { 00191 #ifdef SA_RESTART 00192 sa.sa_flags |= SA_RESTART; 00193 #endif 00194 } 00195 00196 struct sigaction osa; // extract the old handler/action 00197 00198 if (sigaction(signum, &sa, &osa) < 0) 00199 throw InternalErr(__FILE__, __LINE__, "Could not register a signal handler."); 00200 00201 // Take care of the case where this interface is used to register a 00202 // handler more than once. We want to make sure that the dispatcher is 00203 // not installed as the 'old handler' because that results in an infinite 00204 // loop. 02/10/04 jhrg 00205 if (override) 00206 SignalHandler::d_old_handlers[signum] = SIG_IGN; 00207 else if (osa.sa_handler != dispatcher) 00208 SignalHandler::d_old_handlers[signum] = osa.sa_handler; 00209 #endif 00210 00211 return old_eh; 00212 } 00213 00217 EventHandler * 00218 SignalHandler::remove_handler(int signum) 00219 { 00220 EventHandler *old_eh = SignalHandler::d_signal_handlers[signum]; 00221 00222 SignalHandler::d_signal_handlers[signum] = 0; 00223 00224 return old_eh; 00225 } 00226 00227 } // namespace libdap