libdap++  Updated for version 3.11.7
SignalHandler.cc
Go to the documentation of this file.
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2002,2003 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // (c) COPYRIGHT URI/MIT 1994-2002
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31 
32 #include "config.h"
33 
34 static char rcsid[] not_used =
35  { "$Id: SignalHandler.cc 25112 2011-12-29 21:44:54Z jimg $"
36  };
37 
38 #include <cstdlib>
39 
40 #include <signal.h>
41 #include <pthread.h>
42 
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h> //for _exit
45 #endif
46 
47 #include "SignalHandler.h"
48 #include "util.h"
49 
50 namespace libdap {
51 
52 EventHandler *SignalHandler::d_signal_handlers[NSIG];
53 Sigfunc *SignalHandler::d_old_handlers[NSIG];
54 SignalHandler *SignalHandler::d_instance = 0;
55 
56 // instance_control is used to ensure that in a MT environment d_instance is
57 // correctly initialized.
58 static pthread_once_t instance_control = PTHREAD_ONCE_INIT;
59 
61 void
62 SignalHandler::initialize_instance()
63 {
64  // MT-Safe if called via pthread_once or similar
65  SignalHandler::d_instance = new SignalHandler;
66  atexit(SignalHandler::delete_instance);
67 }
68 
70 void
71 SignalHandler::delete_instance()
72 {
73  if (SignalHandler::d_instance) {
74  for (int i = 0; i < NSIG; ++i) {
75  // Fortify warns about a leak because the EventHandler objects
76  // are not deleted, but that's OK - this is a singleton and
77  // so the 'leak' is really just a constant amount of memory that
78  // gets used.
79  d_signal_handlers[i] = 0;
80  d_old_handlers[i] = 0;
81  }
82 
83  delete SignalHandler::d_instance;
84  SignalHandler::d_instance = 0;
85  }
86 }
87 
94 void
95 SignalHandler::dispatcher(int signum)
96 {
97  // Perform a sanity check...
98  if (SignalHandler::d_signal_handlers[signum] != 0)
99  // Dispatch the handler's hook method.
100  SignalHandler::d_signal_handlers[signum]->handle_signal(signum);
101 
102  Sigfunc *old_handler = SignalHandler::d_old_handlers[signum];
103  if (old_handler == SIG_IGN || old_handler == SIG_ERR)
104  return;
105  else if (old_handler == SIG_DFL) {
106  switch (signum) {
107 #if 0
108 #ifndef WIN32
109  case SIGHUP:
110  case SIGKILL:
111  case SIGUSR1:
112  case SIGUSR2:
113  case SIGPIPE:
114  case SIGALRM:
115 #endif
116  case SIGINT:
117  case SIGTERM: _exit(EXIT_FAILURE);
118 
119  // register_handler() should never allow any fiddling with
120  // signals other than those listed above.
121  default: abort();
122 #endif
123  // Calling _exit() or abort() is not a good thing for a library to be
124  // doing. This results in a warning from rpmlint
125  default:
126  throw Error("Signal handler operation on an unsupported signal.");
127  }
128  }
129  else
130  old_handler(signum);
131 }
132 
134 SignalHandler*
136 {
137  pthread_once(&instance_control, initialize_instance);
138 
139  return d_instance;
140 }
141 
154 EventHandler *
155 SignalHandler::register_handler(int signum, EventHandler *eh, bool override)
156 {
157  // Check first for improper use.
158  switch (signum) {
159 #ifndef WIN32
160  case SIGHUP:
161  case SIGKILL:
162  case SIGUSR1:
163  case SIGUSR2:
164  case SIGPIPE:
165  case SIGALRM:
166 #endif
167  case SIGINT:
168  case SIGTERM: break;
169 
170  default: throw InternalErr(__FILE__, __LINE__,
171  string("Call to register_handler with unsupported signal (")
172  + long_to_string(signum) + string(")."));
173  }
174 
175  // Save the old EventHandler
176  EventHandler *old_eh = SignalHandler::d_signal_handlers[signum];
177 
178  SignalHandler::d_signal_handlers[signum] = eh;
179 
180  // Register the dispatcher to handle this signal. See Stevens, Advanced
181  // Programming in the UNIX Environment, p.298.
182 #ifndef WIN32
183  struct sigaction sa;
184  sa.sa_handler = dispatcher;
185  sigemptyset(&sa.sa_mask);
186  sa.sa_flags = 0;
187 
188  // Try to suppress restarting system calls if we're handling an alarm.
189  // This lets alarms block I/O calls that would normally restart. 07/18/03
190  // jhrg
191  if (signum == SIGALRM) {
192 #ifdef SA_INTERUPT
193  sa.sa_flags |= SA_INTERUPT;
194 #endif
195  }
196  else {
197 #ifdef SA_RESTART
198  sa.sa_flags |= SA_RESTART;
199 #endif
200  }
201 
202  struct sigaction osa; // extract the old handler/action
203 
204  if (sigaction(signum, &sa, &osa) < 0)
205  throw InternalErr(__FILE__, __LINE__, "Could not register a signal handler.");
206 
207  // Take care of the case where this interface is used to register a
208  // handler more than once. We want to make sure that the dispatcher is
209  // not installed as the 'old handler' because that results in an infinite
210  // loop. 02/10/04 jhrg
211  if (override)
212  SignalHandler::d_old_handlers[signum] = SIG_IGN;
213  else if (osa.sa_handler != dispatcher)
214  SignalHandler::d_old_handlers[signum] = osa.sa_handler;
215 #endif
216 
217  return old_eh;
218 }
219 
223 EventHandler *
225 {
226  EventHandler *old_eh = SignalHandler::d_signal_handlers[signum];
227 
228  SignalHandler::d_signal_handlers[signum] = 0;
229 
230  return old_eh;
231 }
232 
233 } // namespace libdap