2019-08-14 14:54:46 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-04-02 21:58:43 +00:00
|
|
|
#include "signalcatcher.h"
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
int SignalCatcher::sigintFd[2] = {0,0};
|
|
|
|
|
|
|
|
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);
|
2019-11-03 18:46:40 +00:00
|
|
|
connect(snInt, &QSocketNotifier::activated, this, &SignalCatcher::handleSigInt);
|
2019-04-02 21:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SignalCatcher::~SignalCatcher()
|
|
|
|
{}
|
|
|
|
|
|
|
|
void SignalCatcher::handleSigInt()
|
|
|
|
{
|
|
|
|
snInt->setEnabled(false);
|
|
|
|
char tmp;
|
2020-07-29 20:26:56 +00:00
|
|
|
ssize_t s = ::read(sigintFd[1], &tmp, sizeof(tmp));
|
2019-04-02 21:58:43 +00:00
|
|
|
|
|
|
|
app->quit();
|
|
|
|
|
|
|
|
snInt->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignalCatcher::intSignalHandler(int unused)
|
|
|
|
{
|
|
|
|
char a = 1;
|
2020-07-29 20:26:56 +00:00
|
|
|
ssize_t s = ::write(sigintFd[0], &a, sizeof(a));
|
2019-04-02 21:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int 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;
|
|
|
|
}
|