3 #include <QSocketNotifier>
4 #include <QCoreApplication>
12 #include <sys/types.h>
15 #include <sys/socket.h>
29 static const std::array<
const int, 6
38 SIGINT, SIGTERM, SIGSEGV, SIGABRT, SIGFPE, SIGILL,
50 std::array<std::string,SIG_STR_COUNT>
sig_str;
57 sig_str[sig] = qPrintable(QString(
"Handling %1\n").arg(name));
62 for (
size_t i = 0; i <
sig_str.size(); i++)
63 sig_str_init(i, qPrintable(QString(
"Signal %1").arg(i)));
78 stack.ss_size = SIGSTKSZ;
81 if (sigaltstack(&stack,
nullptr) == -1)
83 std::cerr <<
"Couldn't create signal stack!" << std::endl;
88 if (::socketpair(AF_UNIX, SOCK_STREAM, 0,
s_sigFd.data()))
90 std::cerr <<
"Couldn't create socketpair" << std::endl;
120 int signum = it.key();
121 signal(signum, SIG_DFL);
153 const char *signame = strsignal(signum);
154 QString signal_name = signame ?
155 QString(signame) : QString(
"Unknown(%1)").arg(signum);
157 bool sa_handler_already_set =
false;
160 sa_handler_already_set =
m_sigMap.contains(signum);
161 if (
m_sigMap.value(signum,
nullptr) && (handler !=
nullptr))
163 LOG(VB_GENERAL, LOG_WARNING,
164 QString(
"Warning %1 signal handler overridden")
170 if (!sa_handler_already_set)
172 struct sigaction sa {};
174 sigemptyset(&sa.sa_mask);
175 sa.sa_flags = SA_RESTART | SA_SIGINFO;
177 sa.sa_flags |= SA_ONSTACK;
181 sigaction(signum, &sa,
nullptr);
184 LOG(VB_GENERAL, LOG_INFO, QString(
"Setup %1 handler").arg(signal_name));
198 [[maybe_unused]]
void *context)
204 signalInfo.m_code = 0;
205 signalInfo.m_pid = 0;
206 signalInfo.m_uid = 0;
207 signalInfo.m_value = 0;
209 signalInfo.m_code = (
info ?
info->si_code : 0);
210 signalInfo.m_pid = (
info ? (int)
info->si_pid : 0);
211 signalInfo.m_uid = (
info ? (int)
info->si_uid : 0);
212 signalInfo.m_value = (
info ?
info->si_value.sival_int : 0);
218 char *buffer = (
char *)&signalInfo;
248 signal(signum, SIG_DFL);
262 if (signum <
static_cast<int>(
sig_str.size()))
273 signal(signum, SIG_DFL);
285 bool infoComplete = (ret ==
sizeof(
SignalInfo));
286 int signum = (infoComplete ? signalInfo.m_signum : 0);
290 QString signame = strsignal(signum);
291 if (signame.isEmpty())
292 signame =
"Unknown Signal";
293 LOG(VB_GENERAL, LOG_CRIT,
294 QString(
"Received %1: Code %2, PID %3, UID %4, Value 0x%5")
295 .arg(signame) .arg(signalInfo.m_code) .arg(signalInfo.m_pid)
296 .arg(signalInfo.m_uid) .arg(signalInfo.m_value,8,16,QChar(
'0')));
300 bool allowNullHandler =
false;
303 if (signum == SIGRTMIN)
308 allowNullHandler =
true;
310 #endif // Q_OS_DARWIN
317 handler =
m_sigMap.value(signum,
nullptr);
323 QCoreApplication::exit(0);
336 handler =
m_sigMap.value(signum,
nullptr);
342 else if (!allowNullHandler)
344 LOG(VB_GENERAL, LOG_CRIT, QString(
"Received unexpected signal %1")