From 8f845029b6f663f2b2b3cecec1ded5590d49775c Mon Sep 17 00:00:00 2001 From: Jeff Zignego Date: Thu, 24 Feb 2022 12:16:08 -0600 Subject: [PATCH] src/unixsignals.cpp: fix signal handler Fix two issues in the signal handler function: * Use `volatile std::sig_atomic_t` for the variable that will be written to the socket. See https://en.cppreference.com/w/cpp/utility/program/sig_atomic_t and https://en.cppreference.com/w/cpp/utility/program/signal * Don't try to print anything to the log or the console in the signal handler. We were calling `qDebug()` which seems convenient for debugging the signal handler, however, `qDebug()` calls `gettimeofday()`, which is not signal safe. The list of signal safe functions is here: https://pubs.opengroup.org/onlinepubs/000095399/functions/xsh_chap02_04.html#tag_02_04_01 which notably does _not_ include `gettimeofday()`. Unfortunately there is virtually no way to log something safely from inside a signal handler itself. We can log stuff once we've passed it along via Qt Signals and Slots, but not inside the OS signal handler itself. Also see https://stackoverflow.com/questions/53155166/is-gettimeofday-async-signal-safe-and-can-it-cause-deadlock-if-used-in-signal and https://man7.org/linux/man-pages/man7/signal-safety.7.html --- src/unixsignals.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/unixsignals.cpp b/src/unixsignals.cpp index cb211da..13bed56 100644 --- a/src/unixsignals.cpp +++ b/src/unixsignals.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include #include "unixsignals.h" SocketPair UnixSignals::sockPair; @@ -107,9 +107,8 @@ void UnixSignals::stop() void UnixSignals::signalHandler(int number) { - char tmp = number; - qDebug() << "UnixSignals::signalHandler -- pass signal" << number; - UnixSignals::sockPair.input()->write(&tmp, sizeof(tmp)); + volatile std::sig_atomic_t tmp = number; + UnixSignals::sockPair.input()->write((char* )&tmp, sizeof(tmp)); } void UnixSignals::handleSig(QByteArray data)