Skip to content

Commit

Permalink
src/unixsignals.cpp: fix signal handler
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HED-jzignego committed Feb 24, 2022
1 parent 45f260b commit 8f84502
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/unixsignals.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <signal.h>
#include <QApplication>
#include <QDebug>
#include <csignal>
#include "unixsignals.h"

SocketPair UnixSignals::sockPair;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8f84502

Please sign in to comment.