File indexing completed on 2025-01-30 09:17:05
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
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
0044
0045
0046
0047
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
0068 implementation();
0069
0070 ~implementation();
0071
0072 static implementation& instance();
0073
0074 void init();
0075
0076 void install(int num, const std::string& name, struct sigaction& action);
0077
0078 int subscribe(int signum, void* user_context, signal_handler_t handler);
0079
0080 int unsubscribe(int signum, void* user_context);
0081
0082 void back_trace(int );
0083
0084 static void handler(int signum, siginfo_t *info,void * );
0085 };
0086
0087
0088 SignalHandler::implementation::implementation() {
0089 }
0090
0091
0092 SignalHandler::implementation::~implementation() {
0093 }
0094
0095
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
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
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() ) {
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
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
0169 void SignalHandler::implementation::back_trace(int ) {
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
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
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
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
0248 }
0249
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
0284 SignalHandler::SignalHandler()
0285 {
0286 }
0287
0288
0289 SignalHandler::~SignalHandler() {
0290 }
0291
0292
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
0306 bool SignalHandler::registerHandler(int sig_num, void* param, signal_handler_t handler) {
0307 return implementation::instance().subscribe(sig_num, param, handler) == 1;
0308 }