File indexing completed on 2025-01-18 10:17:33
0001
0002
0003
0004
0005 #include "JSignalHandler.h"
0006
0007 #include <JANA/JApplication.h>
0008 #include <JANA/Engine/JExecutionEngine.h>
0009 #include <JANA/Utils/JBacktrace.h>
0010
0011 #include <unistd.h>
0012 #include <csignal>
0013
0014
0015
0016 namespace JSignalHandler {
0017
0018 JApplication* g_app;
0019 JLogger* g_logger;
0020
0021
0022
0023
0024
0025
0026
0027 void handle_sigint(int) {
0028 if (g_app->IsInitialized()) {
0029 g_app->GetService<JExecutionEngine>()->HandleSIGINT();
0030 }
0031 else {
0032 exit(-2);
0033 }
0034 }
0035
0036 void handle_usr1(int) {
0037 if (g_app->IsInitialized()) {
0038 g_app->GetService<JExecutionEngine>()->HandleSIGUSR1();
0039 }
0040 }
0041
0042 void handle_usr2(int) {
0043 if (g_app->IsInitialized()) {
0044 g_app->GetService<JExecutionEngine>()->HandleSIGUSR2();
0045 }
0046 }
0047
0048 void handle_tstp(int) {
0049 if (g_app->IsInitialized()) {
0050 g_app->GetService<JExecutionEngine>()->HandleSIGTSTP();
0051 }
0052 }
0053
0054 void handle_sigsegv(int , siginfo_t* , void* ) {
0055 LOG_FATAL(*g_logger) << "Segfault detected!" << LOG_END;
0056 JBacktrace backtrace;
0057 backtrace.Capture(3);
0058 LOG_FATAL(*g_logger) << "Hard exit due to segmentation fault! Backtrace:\n\n" << backtrace.ToString() << LOG_END;
0059 _exit(static_cast<int>(JApplication::ExitCode::Segfault));
0060
0061 }
0062
0063
0064
0065 void register_handlers(JApplication* app) {
0066 assert (app != nullptr);
0067 g_app = app;
0068 g_logger = &default_cout_logger;
0069 *g_logger = app->GetJParameterManager()->GetLogger("jana");
0070
0071
0072
0073
0074
0075
0076
0077
0078 JBacktrace backtrace;
0079 backtrace.Capture();
0080
0081
0082 struct sigaction sSignalAction;
0083 sSignalAction.sa_sigaction = handle_sigsegv;
0084 sSignalAction.sa_flags = SA_RESTART | SA_SIGINFO;
0085
0086
0087 sigemptyset(&sSignalAction.sa_mask);
0088 sigaction(SIGSEGV, &sSignalAction, nullptr);
0089
0090 LOG_WARN(*g_logger) << "Setting signal handler USR1. Use to write status info to the named pipe." << LOG_END;
0091 signal(SIGUSR1, handle_usr1);
0092 signal(SIGUSR2, handle_usr2);
0093 signal(SIGTSTP, handle_tstp);
0094 LOG_WARN(*g_logger) << "Setting signal handler SIGINT (Ctrl-C). Use a single SIGINT to enter the Inspector, or multiple SIGINTs for an immediate shutdown." << LOG_END;
0095 signal(SIGINT, handle_sigint);
0096 }
0097
0098
0099 };
0100