SignalHandler.cc

Go to the documentation of this file.
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 16088 2007-03-28 21:42:19Z jimg $"
00036     };
00037 
00038 #include <signal.h>
00039 
00040 #ifndef WIN32
00041 #include <unistd.h>
00042 #endif
00043 
00044 #include <pthread.h>
00045 
00046 #include "SignalHandler.h"
00047 #include "util.h"
00048 
00049 EventHandler *SignalHandler::d_signal_handlers[NSIG];
00050 Sigfunc *SignalHandler::d_old_handlers[NSIG];
00051 SignalHandler *SignalHandler::d_instance = 0;
00052 
00053 // instance_control is used to ensure that in a MT environment d_instance is
00054 // correctly initialized.
00055 static pthread_once_t instance_control = PTHREAD_ONCE_INIT;
00056 
00058 void
00059 SignalHandler::initialize_instance()
00060 {
00061     // MT-Safe if called via pthread_once or similar
00062     SignalHandler::d_instance = new SignalHandler;
00063     atexit(SignalHandler::delete_instance);
00064 }
00065 
00067 void
00068 SignalHandler::delete_instance()
00069 {
00070     if (SignalHandler::d_instance) {
00071         for (int i = 0; i < NSIG; ++i) {
00072             d_signal_handlers[i] = 0;
00073             d_old_handlers[i] = 0;
00074         }
00075 
00076         delete SignalHandler::d_instance;
00077         SignalHandler::d_instance = 0;
00078     }
00079 }
00080 
00087 void
00088 SignalHandler::dispatcher(int signum)
00089 {
00090     // Perform a sanity check...
00091     if (SignalHandler::d_signal_handlers[signum] != 0)
00092         // Dispatch the handler's hook method.
00093         SignalHandler::d_signal_handlers[signum]->handle_signal(signum);
00094 
00095     Sigfunc *old_handler = SignalHandler::d_old_handlers[signum];
00096     if (old_handler == SIG_IGN || old_handler == SIG_ERR)
00097         return;
00098     else if (old_handler == SIG_DFL) {
00099         switch (signum) {
00100 #ifndef WIN32
00101         case SIGHUP:
00102         case SIGKILL:
00103         case SIGUSR1:
00104         case SIGUSR2:
00105         case SIGPIPE:
00106         case SIGALRM:
00107 #endif
00108         case SIGINT:
00109         case SIGTERM: _exit(EXIT_FAILURE);
00110 
00111             // register_handler() should never allow any fiddling with
00112             // signals other than those listed above.
00113         default: abort();
00114         }
00115     }
00116     else
00117         old_handler(signum);
00118 }
00119 
00121 SignalHandler*
00122 SignalHandler::instance()
00123 {
00124     pthread_once(&instance_control, initialize_instance);
00125 
00126     return d_instance;
00127 }
00128 
00141 EventHandler *
00142 SignalHandler::register_handler(int signum, EventHandler *eh, bool override)
00143 {
00144     // Check first for improper use.
00145     switch (signum) {
00146 #ifndef WIN32
00147     case SIGHUP:
00148     case SIGKILL:
00149     case SIGUSR1:
00150     case SIGUSR2:
00151     case SIGPIPE:
00152     case SIGALRM:
00153 #endif
00154     case SIGINT:
00155     case SIGTERM: break;
00156 
00157     default: throw InternalErr(__FILE__, __LINE__,
00158                                    string("Call to register_handler with unsupported signal (")
00159                                    + long_to_string(signum) + string(")."));
00160     }
00161 
00162     // Save the old EventHandler
00163     EventHandler *old_eh = SignalHandler::d_signal_handlers[signum];
00164 
00165     SignalHandler::d_signal_handlers[signum] = eh;
00166 
00167     // Register the dispatcher to handle this signal. See Stevens, Advanced
00168     // Programming in the UNIX Environment, p.298.
00169 #ifndef WIN32
00170     struct sigaction sa;
00171     sa.sa_handler = dispatcher;
00172     sigemptyset(&sa.sa_mask);
00173     sa.sa_flags = 0;
00174 
00175     // Try to suppress restarting system calls if we're handling an alarm.
00176     // This lets alarms block I/O calls that would normally restart. 07/18/03
00177     // jhrg
00178     if (signum == SIGALRM) {
00179 #ifdef SA_INTERUPT
00180         sa.sa_flags |= SA_INTERUPT;
00181 #endif
00182     }
00183     else {
00184 #ifdef SA_RESTART
00185         sa.sa_flags |= SA_RESTART;
00186 #endif
00187     }
00188 
00189     struct sigaction osa; // extract the old handler/action
00190 
00191     if (sigaction(signum, &sa, &osa) < 0)
00192         throw InternalErr(__FILE__, __LINE__, "Could not register a signal handler.");
00193 
00194     // Take care of the case where this interface is used to register a
00195     // handler more than once. We want to make sure that the dispatcher is
00196     // not installed as the 'old handler' because that results in an infinite
00197     // loop. 02/10/04 jhrg
00198     if (override)
00199         SignalHandler::d_old_handlers[signum] = SIG_IGN;
00200     else if (osa.sa_handler != dispatcher)
00201         SignalHandler::d_old_handlers[signum] = osa.sa_handler;
00202 #endif
00203 
00204     return old_eh;
00205 }
00206 
00210 EventHandler *
00211 SignalHandler::remove_handler(int signum)
00212 {
00213     EventHandler *old_eh = SignalHandler::d_signal_handlers[signum];
00214 
00215     SignalHandler::d_signal_handlers[signum] = 0;
00216 
00217     return old_eh;
00218 }

Generated on Wed Jun 27 12:56:39 2007 for libdap++ by  doxygen 1.4.7