Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:17:05

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
0015 #include <DD4hep/Printout.h>
0016 #include <DD4hep/SignalHandler.h>
0017 
0018 #include <map>
0019 #include <string>
0020 #include <vector>
0021 #include <iostream>
0022 #include <unistd.h>
0023 #include <execinfo.h>
0024 
0025 using namespace dd4hep;
0026 
0027 using signal_handler_t = SignalHandler::signal_handler_t;
0028 
0029 namespace {
0030   static bool s_exit_handler_print  = true;
0031   static bool s_exit_handler_active = false;
0032   static bool s_exit_handler_backtrace = false;
0033   static bool s_exit_handler_sleep_on_fatal = false;
0034 
0035   template<class T> union func_cast   {
0036     void* ptr;
0037     T     fun;
0038     explicit func_cast(T t) { fun = t; }
0039     explicit func_cast(void* t) { ptr = t; }
0040   };
0041 }
0042 
0043 /**@class SignalHandler::implementation
0044  *
0045  * Small class to manipulate default signal handling
0046  *
0047  * \author M.Frank
0048  */
0049 class SignalHandler::implementation {
0050 protected:
0051   struct sig_entry_t  {
0052     void* user_context { nullptr };
0053     signal_handler_t user_handler { nullptr };
0054   };
0055   struct sig_handler_t  {
0056     std::string name { };
0057     struct sigaction old_action { };
0058     struct sigaction handler_action { };
0059     std::vector<sig_entry_t> user_handlers { };
0060   };
0061 
0062 public:
0063   typedef std::map<int, sig_handler_t> SigMap;
0064   SigMap  m_map;
0065 
0066 public:
0067   /// Default constructor
0068   implementation();
0069   /// Default destructor
0070   ~implementation();
0071   /// Singleton accessor
0072   static implementation& instance();
0073   /// Initialize the exit handler. Subscribe to default signals
0074   void init();
0075   /// Install handler for a single signal
0076   void install(int num, const std::string& name, struct sigaction& action);
0077   /// Subscribe to a given signal with a user context and a user handler. The context MUST be unique!
0078   int subscribe(int signum, void* user_context, signal_handler_t handler);
0079   /// Unsubscribe from a given signal with a user context identifier
0080   int unsubscribe(int signum, void* user_context);
0081   /// Create simple backtrace
0082   void back_trace(int /* signum */);
0083   /// Static handler callback for system signal handler
0084   static void handler(int signum, siginfo_t *info,void * );
0085 };
0086 
0087 /// Default constructor
0088 SignalHandler::implementation::implementation()  {
0089 }
0090 
0091 /// Default destructor
0092 SignalHandler::implementation::~implementation()  {
0093 }
0094 
0095 /// Singleton accessor
0096 SignalHandler::implementation& SignalHandler::implementation::instance()  {
0097   static std::unique_ptr<implementation> imp;
0098   if ( !imp )  {
0099     imp = std::make_unique<implementation>();
0100   }
0101   return *imp;
0102 }
0103 
0104 /// Initialize the exit handler. Subscribe to default signals
0105 void SignalHandler::implementation::init()  {
0106   struct sigaction new_action;
0107   sigemptyset(&new_action.sa_mask);
0108   new_action.sa_handler   = 0;
0109   new_action.sa_sigaction = handler;
0110   new_action.sa_flags     = SA_SIGINFO;
0111 
0112   install(SIGILL,  "SIGILL",  new_action);
0113   install(SIGINT,  "SIGINT",  new_action);
0114   install(SIGTERM, "SIGTERM", new_action);
0115   install(SIGHUP,  "SIGHUP",  new_action);
0116 
0117   install(SIGQUIT, "SIGQUIT", new_action);
0118   install(SIGBUS,  "SIGBUS",  new_action);
0119   install(SIGXCPU, "SIGXCPU", new_action);
0120   sigaddset(&new_action.sa_mask,SIGSEGV);
0121   sigaddset(&new_action.sa_mask,SIGABRT);
0122   sigaddset(&new_action.sa_mask,SIGFPE);
0123   install(SIGABRT, "SIGABRT", new_action);
0124   install(SIGFPE,  "SIGFPE",  new_action);
0125   install(SIGSEGV, "SIGSEGV", new_action);
0126 }
0127 
0128 /// Subscribe to a given signal with a user context and a user handler. The context MUST be unique!
0129 int SignalHandler::implementation::subscribe(int signum, void* user_context, signal_handler_t user_handler)   {
0130   if ( m_map.empty() )  {
0131     this->init();
0132   }
0133   auto ihandler = m_map.find(signum);
0134   if ( ihandler == m_map.end() )   {
0135     char text[32];
0136     struct sigaction new_action;
0137     sigemptyset(&new_action.sa_mask);
0138     new_action.sa_handler   = 0;
0139     new_action.sa_sigaction = SignalHandler::implementation::handler;
0140     new_action.sa_flags     = SA_SIGINFO;
0141     ::snprintf(text, sizeof(text),"%08X",signum);
0142     install(signum, text, new_action);
0143     ihandler = m_map.find(signum);
0144   }
0145   if ( ihandler != m_map.end() )   { // Should always be true
0146     sig_entry_t entry {user_context, user_handler};
0147     ihandler->second.user_handlers.emplace_back(entry);
0148     return 1;
0149   }
0150   return 0;
0151 }
0152 
0153 /// Unsubscribe from a given signal with a user context identifier
0154 int SignalHandler::implementation::unsubscribe(int signum, void* user_context)   {
0155   auto ihandler = m_map.find(signum);
0156   if ( ihandler != m_map.end() )   {
0157     auto & handlers = ihandler->second.user_handlers;
0158     for( auto it = handlers.begin(); it != handlers.end(); ++it )   {
0159       if ( it->user_context == user_context )   {
0160     handlers.erase(it);
0161     return 1;
0162       }
0163     }
0164   }
0165   return 0;
0166 }
0167 
0168 /// Create simple backtrace
0169 void SignalHandler::implementation::back_trace(int /* signum */) {
0170   if ( s_exit_handler_backtrace )   {
0171     void *bt[256];
0172     char text[512];
0173     int bt_size = ::backtrace(bt, sizeof(bt) / sizeof(void *));
0174     size_t len = ::snprintf(text, sizeof(text), "\n[INFO] (ExitSignalHandler) %s\n",
0175                 "---------------------- Backtrace ----------------------\n");
0176     text[sizeof(text)-2] = '\n';
0177     text[sizeof(text)-1] = 0;
0178     ::write(STDERR_FILENO, text, len);
0179     len = ::snprintf(text, sizeof(text), "[INFO] Number of elements in backtrace: %d\n", bt_size);
0180     text[sizeof(text)-2] = '\n';
0181     text[sizeof(text)-1] = 0;
0182     ::write(STDERR_FILENO, text, len);
0183     ::backtrace_symbols_fd(bt, bt_size, STDERR_FILENO);
0184     for (int i = 0; i < bt_size; i++) {
0185       len = ::snprintf(text,sizeof(text),"[INFO] (SignalHandler) %02d --> %p\n", i, bt[i]);
0186       text[sizeof(text)-2] = '\n';
0187       text[sizeof(text)-1] = 0;
0188       ::write(STDERR_FILENO, text, len);
0189     }
0190   }
0191 }
0192 
0193 /// Install handler for a single signal
0194 void SignalHandler::implementation::install(int num, const std::string& name, struct sigaction& action) {
0195   auto& action_entry = m_map[num];
0196   int res = ::sigaction (num, &action, &action_entry.old_action);
0197   if ( res != 0 ) {
0198     char text[512];
0199     auto len = ::snprintf(text,sizeof(text),"Failed to install exit handler for %s", name.c_str());
0200     text[sizeof(text)-2] = '\n';
0201     text[sizeof(text)-1] = 0;
0202     ::write(STDERR_FILENO, text, len);
0203     return;
0204   }
0205   action_entry.handler_action = action;
0206   action_entry.name = name;
0207 }
0208   
0209 /// Static handler callback for system signal handler
0210 void SignalHandler::implementation::handler(int signum, siginfo_t *info, void *ptr) {
0211   SigMap& m = instance().m_map;
0212   SigMap::iterator iter_handler = m.find(signum);
0213   s_exit_handler_active = true;
0214   if ( iter_handler != m.end() ) {
0215     auto hdlr = iter_handler->second.old_action.sa_handler;
0216     func_cast<void (*)(int)> dsc0(hdlr);
0217     func_cast<void (*)(int,siginfo_t*, void*)> dsc(dsc0.ptr);
0218 
0219     if ( s_exit_handler_print ) {{
0220     char text[512];
0221     size_t len = ::snprintf(text,sizeof(text),
0222                 "[FATAL] (SignalHandler) Handle signal: %d [%s] Old action:%p Mem:%p Code:%08X\n",
0223                 signum,iter_handler->second.name.c_str(),dsc.ptr,info->si_addr,info->si_code);
0224     text[sizeof(text)-2] = '\n';
0225     text[sizeof(text)-1] = 0;
0226     ::write(STDERR_FILENO,text,len);
0227     // Debugging hack, if enabled (default: NO)
0228     if ( s_exit_handler_sleep_on_fatal )  {
0229       bool _s_sleep = true;
0230       len = ::snprintf(text,sizeof(text),
0231                "[FATAL] (SignalHandler) Sleeping for debugging.... %s\n",
0232                _s_sleep ? "YES" : "NO");
0233       text[sizeof(text)-2] = '\n';
0234       text[sizeof(text)-1] = 0;
0235       ::write(STDERR_FILENO,text,len);
0236       while ( _s_sleep ) ::usleep(100000);
0237     }
0238       }
0239       if ( !iter_handler->second.user_handlers.empty() )    {
0240     auto& handlers = iter_handler->second.user_handlers;
0241     for( auto ih = handlers.rbegin(); ih != handlers.rend(); ++ih )   {
0242       if ( ih->user_handler )  {
0243         bool ret = (*(ih->user_handler))(ih->user_context, signum);
0244         if ( ret )   {
0245           return;
0246         }
0247         // Otherwise continue signal processing and eventually call default handlers
0248       }
0249       // No handler fired: call previously registered signal handler
0250       auto& entry = iter_handler->second.old_action;
0251       if ( entry.sa_handler )
0252         (*entry.sa_handler)(signum);
0253       else if ( entry.sa_sigaction )
0254         (*entry.sa_sigaction)(signum, info, ptr);
0255     }
0256       }
0257       if ( signum == SIGSEGV || signum == SIGBUS || signum == SIGILL || signum == SIGABRT )  {
0258     instance().back_trace(signum);
0259       }
0260       else if ( info->si_signo == SIGSEGV || info->si_signo == SIGBUS || info->si_signo == SIGILL || info->si_signo == SIGABRT )  {
0261     instance().back_trace(info->si_signo);
0262       }
0263     }
0264     if ( signum == SIGINT || signum == SIGHUP || signum == SIGFPE || signum == SIGPIPE ) {
0265       if ( dsc.fun && (dsc0.fun != SIG_IGN) )
0266     dsc.fun(signum, info, ptr);
0267       else if ( signum == SIGHUP )
0268     ::_exit(signum);
0269     }
0270     else if ( signum == SIGSEGV && hdlr && hdlr != SIG_IGN && hdlr != SIG_DFL ) {
0271       ::_exit(0);
0272     }
0273     else if ( hdlr && hdlr != SIG_IGN && dsc.fun )  {
0274       dsc.fun(signum, info, ptr);
0275     }
0276     else if ( hdlr == SIG_DFL ) {
0277       ::_exit(0);
0278     }
0279   }
0280   s_exit_handler_active = false;
0281 }
0282 
0283 /// Default constructor
0284 SignalHandler::SignalHandler()
0285 {
0286 }
0287 
0288 /// Default destructor
0289 SignalHandler::~SignalHandler()  {
0290 }
0291 
0292 /// (Re-)apply registered interrupt handlers to override potentially later registrations by other libraries
0293 void SignalHandler::applyHandlers()  {
0294   auto& imp = implementation::instance();
0295   struct sigaction old_action { };
0296   printout(INFO, "SignalHandler", "++ Re-apply signal handlers");
0297   for( const auto& e : imp.m_map )  {
0298     ::sigaction (e.first, &e.second.handler_action, &old_action);
0299     printout(DEBUG, "SignalHandler",
0300          "++ Re-apply signal handler for %-10s [%3ld entries]",
0301          e.second.name.c_str(), e.second.user_handlers.size());
0302   }
0303 }
0304 
0305 /// Install handler for any signal
0306 bool SignalHandler::registerHandler(int sig_num, void* param, signal_handler_t handler)  {
0307   return implementation::instance().subscribe(sig_num, param, handler) == 1;
0308 }