radio/lib/utils/signalcatcher.cpp

60 lines
1.2 KiB
C++
Raw Permalink Normal View History

2018-08-04 21:46:25 +00:00
#include "signalcatcher.h"
#include <signal.h>
#include <sys/socket.h>
#include <unistd.h>
int W::SignalCatcher::sigintFd[2] = {0,0};
W::SignalCatcher::SignalCatcher(QCoreApplication *p_app, QObject *parent):
QObject(parent),
app(p_app)
{
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigintFd))
{
qFatal("Couldn't create INT socketpair");
}
if (setup_unix_signal_handlers() != 0)
{
qFatal("Couldn't install unix handlers");
}
snInt = new QSocketNotifier(sigintFd[1], QSocketNotifier::Read, this);
connect(snInt, SIGNAL(activated(int)), this, SLOT(handleSigInt()));
}
W::SignalCatcher::~SignalCatcher()
{}
void W::SignalCatcher::handleSigInt()
{
snInt->setEnabled(false);
char tmp;
::read(sigintFd[1], &tmp, sizeof(tmp));
app->quit();
snInt->setEnabled(true);
}
void W::SignalCatcher::intSignalHandler(int unused)
{
char a = 1;
::write(sigintFd[0], &a, sizeof(a));
}
int W::SignalCatcher::setup_unix_signal_handlers()
{
struct sigaction s_int;
s_int.sa_handler = SignalCatcher::intSignalHandler;
sigemptyset(&s_int.sa_mask);
s_int.sa_flags = 0;
s_int.sa_flags |= SA_RESTART;
if (sigaction(SIGINT, &s_int, 0) > 0)
return 1;
return 0;
}