DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
SignalHandler.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Printout.h>
16 #include <DD4hep/SignalHandler.h>
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 #include <iostream>
22 #include <unistd.h>
23 #include <execinfo.h>
24 
25 using namespace dd4hep;
26 
28 
29 namespace {
30  static bool s_exit_handler_print = true;
31  static bool s_exit_handler_active = false;
32  static bool s_exit_handler_backtrace = false;
33  static bool s_exit_handler_sleep_on_fatal = false;
34 
35  template<class T> union func_cast {
36  void* ptr;
37  T fun;
38  explicit func_cast(T t) { fun = t; }
39  explicit func_cast(void* t) { ptr = t; }
40  };
41 }
42 
50 protected:
51  struct sig_entry_t {
52  void* user_context { nullptr };
54  };
55  struct sig_handler_t {
56  std::string name { };
57  struct sigaction old_action { };
58  struct sigaction handler_action { };
59  std::vector<sig_entry_t> user_handlers { };
60  };
61 
62 public:
63  typedef std::map<int, sig_handler_t> SigMap;
65 
66 public:
70  ~implementation();
72  static implementation& instance();
74  void init();
76  void install(int num, const std::string& name, struct sigaction& action);
78  int subscribe(int signum, void* user_context, signal_handler_t handler);
80  int unsubscribe(int signum, void* user_context);
82  void back_trace(int /* signum */);
84  static void handler(int signum, siginfo_t *info,void * );
85 };
86 
89 }
90 
93 }
94 
97  static std::unique_ptr<implementation> imp;
98  if ( !imp ) {
99  imp = std::make_unique<implementation>();
100  }
101  return *imp;
102 }
103 
106  struct sigaction new_action;
107  sigemptyset(&new_action.sa_mask);
108  new_action.sa_handler = 0;
109  new_action.sa_sigaction = handler;
110  new_action.sa_flags = SA_SIGINFO;
111 
112  install(SIGILL, "SIGILL", new_action);
113  install(SIGINT, "SIGINT", new_action);
114  install(SIGTERM, "SIGTERM", new_action);
115  install(SIGHUP, "SIGHUP", new_action);
116 
117  install(SIGQUIT, "SIGQUIT", new_action);
118  install(SIGBUS, "SIGBUS", new_action);
119  install(SIGXCPU, "SIGXCPU", new_action);
120  sigaddset(&new_action.sa_mask,SIGSEGV);
121  sigaddset(&new_action.sa_mask,SIGABRT);
122  sigaddset(&new_action.sa_mask,SIGFPE);
123  install(SIGABRT, "SIGABRT", new_action);
124  install(SIGFPE, "SIGFPE", new_action);
125  install(SIGSEGV, "SIGSEGV", new_action);
126 }
127 
129 int SignalHandler::implementation::subscribe(int signum, void* user_context, signal_handler_t user_handler) {
130  if ( m_map.empty() ) {
131  this->init();
132  }
133  auto ihandler = m_map.find(signum);
134  if ( ihandler == m_map.end() ) {
135  char text[32];
136  struct sigaction new_action;
137  sigemptyset(&new_action.sa_mask);
138  new_action.sa_handler = 0;
139  new_action.sa_sigaction = SignalHandler::implementation::handler;
140  new_action.sa_flags = SA_SIGINFO;
141  ::snprintf(text, sizeof(text),"%08X",signum);
142  install(signum, text, new_action);
143  ihandler = m_map.find(signum);
144  }
145  if ( ihandler != m_map.end() ) { // Should always be true
146  sig_entry_t entry {user_context, user_handler};
147  ihandler->second.user_handlers.emplace_back(entry);
148  return 1;
149  }
150  return 0;
151 }
152 
154 int SignalHandler::implementation::unsubscribe(int signum, void* user_context) {
155  auto ihandler = m_map.find(signum);
156  if ( ihandler != m_map.end() ) {
157  auto & handlers = ihandler->second.user_handlers;
158  for( auto it = handlers.begin(); it != handlers.end(); ++it ) {
159  if ( it->user_context == user_context ) {
160  handlers.erase(it);
161  return 1;
162  }
163  }
164  }
165  return 0;
166 }
167 
170  if ( s_exit_handler_backtrace ) {
171  void *bt[256];
172  char text[512];
173  int bt_size = ::backtrace(bt, sizeof(bt) / sizeof(void *));
174  size_t len = ::snprintf(text, sizeof(text), "\n[INFO] (ExitSignalHandler) %s\n",
175  "---------------------- Backtrace ----------------------\n");
176  text[sizeof(text)-2] = '\n';
177  text[sizeof(text)-1] = 0;
178  ::write(STDERR_FILENO, text, len);
179  len = ::snprintf(text, sizeof(text), "[INFO] Number of elements in backtrace: %d\n", bt_size);
180  text[sizeof(text)-2] = '\n';
181  text[sizeof(text)-1] = 0;
182  ::write(STDERR_FILENO, text, len);
183  ::backtrace_symbols_fd(bt, bt_size, STDERR_FILENO);
184  for (int i = 0; i < bt_size; i++) {
185  len = ::snprintf(text,sizeof(text),"[INFO] (SignalHandler) %02d --> %p\n", i, bt[i]);
186  text[sizeof(text)-2] = '\n';
187  text[sizeof(text)-1] = 0;
188  ::write(STDERR_FILENO, text, len);
189  }
190  }
191 }
192 
194 void SignalHandler::implementation::install(int num, const std::string& name, struct sigaction& action) {
195  auto& action_entry = m_map[num];
196  int res = ::sigaction (num, &action, &action_entry.old_action);
197  if ( res != 0 ) {
198  char text[512];
199  auto len = ::snprintf(text,sizeof(text),"Failed to install exit handler for %s", name.c_str());
200  text[sizeof(text)-2] = '\n';
201  text[sizeof(text)-1] = 0;
202  ::write(STDERR_FILENO, text, len);
203  return;
204  }
205  action_entry.handler_action = action;
206  action_entry.name = name;
207 }
208 
210 void SignalHandler::implementation::handler(int signum, siginfo_t *info, void *ptr) {
211  SigMap& m = instance().m_map;
212  SigMap::iterator iter_handler = m.find(signum);
213  s_exit_handler_active = true;
214  if ( iter_handler != m.end() ) {
215  auto hdlr = iter_handler->second.old_action.sa_handler;
216  func_cast<void (*)(int)> dsc0(hdlr);
217  func_cast<void (*)(int,siginfo_t*, void*)> dsc(dsc0.ptr);
218 
219  if ( s_exit_handler_print ) {{
220  char text[512];
221  size_t len = ::snprintf(text,sizeof(text),
222  "[FATAL] (SignalHandler) Handle signal: %d [%s] Old action:%p Mem:%p Code:%08X\n",
223  signum,iter_handler->second.name.c_str(),dsc.ptr,info->si_addr,info->si_code);
224  text[sizeof(text)-2] = '\n';
225  text[sizeof(text)-1] = 0;
226  ::write(STDERR_FILENO,text,len);
227  // Debugging hack, if enabled (default: NO)
228  if ( s_exit_handler_sleep_on_fatal ) {
229  bool _s_sleep = true;
230  len = ::snprintf(text,sizeof(text),
231  "[FATAL] (SignalHandler) Sleeping for debugging.... %s\n",
232  _s_sleep ? "YES" : "NO");
233  text[sizeof(text)-2] = '\n';
234  text[sizeof(text)-1] = 0;
235  ::write(STDERR_FILENO,text,len);
236  while ( _s_sleep ) ::usleep(100000);
237  }
238  }
239  if ( !iter_handler->second.user_handlers.empty() ) {
240  auto& handlers = iter_handler->second.user_handlers;
241  for( auto ih = handlers.rbegin(); ih != handlers.rend(); ++ih ) {
242  if ( ih->user_handler ) {
243  bool ret = (*(ih->user_handler))(ih->user_context, signum);
244  if ( ret ) {
245  return;
246  }
247  // Otherwise continue signal processing and eventually call default handlers
248  }
249  // No handler fired: call previously registered signal handler
250  auto& entry = iter_handler->second.old_action;
251  if ( entry.sa_handler )
252  (*entry.sa_handler)(signum);
253  else if ( entry.sa_sigaction )
254  (*entry.sa_sigaction)(signum, info, ptr);
255  }
256  }
257  if ( signum == SIGSEGV || signum == SIGBUS || signum == SIGILL || signum == SIGABRT ) {
258  instance().back_trace(signum);
259  }
260  else if ( info->si_signo == SIGSEGV || info->si_signo == SIGBUS || info->si_signo == SIGILL || info->si_signo == SIGABRT ) {
261  instance().back_trace(info->si_signo);
262  }
263  }
264  if ( signum == SIGINT || signum == SIGHUP || signum == SIGFPE || signum == SIGPIPE ) {
265  if ( dsc.fun && (dsc0.fun != SIG_IGN) )
266  dsc.fun(signum, info, ptr);
267  else if ( signum == SIGHUP )
268  ::_exit(signum);
269  }
270  else if ( signum == SIGSEGV && hdlr && hdlr != SIG_IGN && hdlr != SIG_DFL ) {
271  ::_exit(0);
272  }
273  else if ( hdlr && hdlr != SIG_IGN && dsc.fun ) {
274  dsc.fun(signum, info, ptr);
275  }
276  else if ( hdlr == SIG_DFL ) {
277  ::_exit(0);
278  }
279  }
280  s_exit_handler_active = false;
281 }
282 
285 {
286 }
287 
290 }
291 
294  auto& imp = implementation::instance();
295  struct sigaction old_action { };
296  printout(INFO, "SignalHandler", "++ Re-apply signal handlers");
297  for( const auto& e : imp.m_map ) {
298  ::sigaction (e.first, &e.second.handler_action, &old_action);
299  printout(DEBUG, "SignalHandler",
300  "++ Re-apply signal handler for %-10s [%3ld entries]",
301  e.second.name.c_str(), e.second.user_handlers.size());
302  }
303 }
304 
306 bool SignalHandler::registerHandler(int sig_num, void* param, signal_handler_t handler) {
307  return implementation::instance().subscribe(sig_num, param, handler) == 1;
308 }
dd4hep::SignalHandler::applyHandlers
void applyHandlers()
(Re-)apply registered interrupt handlers to override potentially later registrations by other librari...
SignalHandler::implementation::unsubscribe
int unsubscribe(int signum, void *user_context)
Unsubscribe from a given signal with a user context identifier.
Definition: SignalHandler.cpp:154
SignalHandler::implementation
Definition: SignalHandler.cpp:49
SignalHandler::implementation::sig_entry_t::user_handler
signal_handler_t user_handler
Definition: SignalHandler.cpp:53
SignalHandler::implementation::SigMap
std::map< int, sig_handler_t > SigMap
Definition: SignalHandler.cpp:63
dd4hep::info
std::size_t info(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:65
dd4hep::SignalHandler::signal_handler_t
bool(* signal_handler_t)(void *user_context, int signal)
User signal handler definition.
Definition: SignalHandler.h:32
SignalHandler::implementation::sig_handler_t::name
std::string name
Definition: SignalHandler.cpp:56
SignalHandler::implementation::sig_entry_t
Definition: SignalHandler.cpp:51
SignalHandler::implementation::handler
static void handler(int signum, siginfo_t *info, void *)
Static handler callback for system signal handler.
Definition: SignalHandler.cpp:210
dd4hep::SignalHandler::SignalHandler
SignalHandler()
Default constructor.
SignalHandler::implementation::m_map
SigMap m_map
Definition: SignalHandler.cpp:64
SignalHandler::implementation::instance
static implementation & instance()
Singleton accessor.
Definition: SignalHandler.cpp:96
SignalHandler::implementation::back_trace
void back_trace(int)
Create simple backtrace.
Definition: SignalHandler.cpp:169
dd4hep::SignalHandler::~SignalHandler
virtual ~SignalHandler()
Default destructor.
SignalHandler::implementation::sig_handler_t::handler_action
Definition: SignalHandler.cpp:58
SignalHandler::implementation::subscribe
int subscribe(int signum, void *user_context, signal_handler_t handler)
Subscribe to a given signal with a user context and a user handler. The context MUST be unique!
Definition: SignalHandler.cpp:129
SignalHandler::implementation::install
void install(int num, const std::string &name, struct sigaction &action)
Install handler for a single signal.
Definition: SignalHandler.cpp:194
SignalHandler::implementation::~implementation
~implementation()
Default destructor.
Definition: SignalHandler.cpp:92
SignalHandler::implementation::implementation
implementation()
Default constructor.
Definition: SignalHandler.cpp:88
SignalHandler::implementation::sig_handler_t::old_action
Definition: SignalHandler.cpp:57
SignalHandler::implementation::sig_handler_t::user_handlers
std::vector< sig_entry_t > user_handlers
Definition: SignalHandler.cpp:59
SignalHandler::implementation::sig_entry_t::user_context
void * user_context
Definition: SignalHandler.cpp:52
SignalHandler.h
dd4hep::SignalHandler::registerHandler
bool registerHandler(int sig_num, void *param, signal_handler_t handler)
Specialized handler for any signal.
SignalHandler::implementation::sig_handler_t
Definition: SignalHandler.cpp:55
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
SignalHandler::implementation::init
void init()
Initialize the exit handler. Subscribe to default signals.
Definition: SignalHandler.cpp:105
signal_handler_t
SignalHandler::signal_handler_t signal_handler_t
Definition: SignalHandler.cpp:27
Printout.h