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)));
77 stack.ss_size = SIGSTKSZ;
80 if (sigaltstack(&stack,
nullptr) == -1)
82 std::cerr <<
"Couldn't create signal stack!" << std::endl;
87 if (::socketpair(AF_UNIX, SOCK_STREAM, 0,
s_sigFd.data()))
89 std::cerr <<
"Couldn't create socketpair" << std::endl;
119 int signum = it.key();
120 signal(signum, SIG_DFL);
151 const char *signame = strsignal(signum);
152 QString signal_name = signame ?
153 QString(signame) : QString(
"Unknown(%1)").arg(signum);
155 bool sa_handler_already_set =
false;
158 sa_handler_already_set =
m_sigMap.contains(signum);
159 if (
m_sigMap.value(signum,
nullptr) && (handler !=
nullptr))
161 LOG(VB_GENERAL, LOG_WARNING,
162 QString(
"Warning %1 signal handler overridden")
168 if (!sa_handler_already_set)
170 struct sigaction sa {};
172 sigemptyset(&sa.sa_mask);
173 sa.sa_flags = SA_RESTART | SA_SIGINFO;
175 sa.sa_flags |= SA_ONSTACK;
179 sigaction(signum, &sa,
nullptr);
182 LOG(VB_GENERAL, LOG_INFO, QString(
"Setup %1 handler").arg(signal_name));
205 signalInfo.m_code = 0;
206 signalInfo.m_pid = 0;
207 signalInfo.m_uid = 0;
208 signalInfo.m_value = 0;
210 signalInfo.m_code = (info ? info->si_code : 0);
211 signalInfo.m_pid = (info ? (int)info->si_pid : 0);
212 signalInfo.m_uid = (info ? (int)info->si_uid : 0);
213 signalInfo.m_value = (info ? info->si_value.sival_int : 0);
219 char *buffer = (
char *)&signalInfo;
249 signal(signum, SIG_DFL);
263 if (signum <
static_cast<int>(
sig_str.size()))
274 signal(signum, SIG_DFL);
286 bool infoComplete = (ret ==
sizeof(
SignalInfo));
287 int signum = (infoComplete ? signalInfo.m_signum : 0);
291 QString signame = strsignal(signum);
292 if (signame.isEmpty())
293 signame =
"Unknown Signal";
294 LOG(VB_GENERAL, LOG_CRIT,
295 QString(
"Received %1: Code %2, PID %3, UID %4, Value 0x%5")
296 .arg(signame) .arg(signalInfo.m_code) .arg(signalInfo.m_pid)
297 .arg(signalInfo.m_uid) .arg(signalInfo.m_value,8,16,QChar(
'0')));
301 bool allowNullHandler =
false;
304 if (signum == SIGRTMIN)
309 allowNullHandler =
true;
311 #endif // Q_OS_DARWIN
318 handler =
m_sigMap.value(signum,
nullptr);
324 QCoreApplication::exit(0);
337 handler =
m_sigMap.value(signum,
nullptr);
343 else if (!allowNullHandler)
345 LOG(VB_GENERAL, LOG_CRIT, QString(
"Received unexpected signal %1")