2013-03-31 17:31:51 +08:00
|
|
|
/*
|
2013-03-31 17:45:44 +08:00
|
|
|
* This file is part of the PulseView project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Adam Reichold
|
|
|
|
*
|
|
|
|
* 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 2 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2013-03-31 17:31:51 +08:00
|
|
|
|
2014-11-22 16:53:27 +08:00
|
|
|
#include "signalhandler.hpp"
|
2013-03-31 17:31:51 +08:00
|
|
|
|
2013-04-23 04:43:00 +08:00
|
|
|
#include <assert.h>
|
2013-03-31 17:31:51 +08:00
|
|
|
#include <signal.h>
|
2013-04-23 04:43:00 +08:00
|
|
|
#include <stdlib.h>
|
2013-03-31 17:31:51 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-04-23 04:43:00 +08:00
|
|
|
#include <QDebug>
|
2013-03-31 17:31:51 +08:00
|
|
|
#include <QSocketNotifier>
|
|
|
|
|
2014-11-22 16:46:22 +08:00
|
|
|
int SignalHandler::sockets_[2];
|
2013-03-31 17:31:51 +08:00
|
|
|
|
2013-03-31 17:45:44 +08:00
|
|
|
bool SignalHandler::prepare_signals()
|
2013-03-31 17:31:51 +08:00
|
|
|
{
|
2015-07-26 00:05:11 +08:00
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets_) != 0)
|
2013-03-31 17:45:44 +08:00
|
|
|
return false;
|
2013-03-31 17:31:51 +08:00
|
|
|
|
2013-03-31 17:45:44 +08:00
|
|
|
struct sigaction sig_action;
|
2013-03-31 17:31:51 +08:00
|
|
|
|
2013-03-31 17:45:44 +08:00
|
|
|
sig_action.sa_handler = SignalHandler::handle_signals;
|
|
|
|
sigemptyset(&sig_action.sa_mask);
|
|
|
|
sig_action.sa_flags = SA_RESTART;
|
2013-03-31 17:31:51 +08:00
|
|
|
|
2015-07-26 00:05:11 +08:00
|
|
|
if (sigaction(SIGINT, &sig_action, 0) != 0 ||
|
2013-03-31 17:45:44 +08:00
|
|
|
sigaction(SIGTERM, &sig_action, 0) != 0) {
|
2014-11-22 16:46:22 +08:00
|
|
|
close(sockets_[0]);
|
|
|
|
close(sockets_[1]);
|
2013-03-31 17:45:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-31 17:31:51 +08:00
|
|
|
|
2013-03-31 17:45:44 +08:00
|
|
|
return true;
|
2013-03-31 17:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SignalHandler::SignalHandler(QObject* parent) : QObject(parent),
|
2014-11-22 16:46:22 +08:00
|
|
|
socket_notifier_(0)
|
2013-03-31 17:31:51 +08:00
|
|
|
{
|
2014-11-22 16:46:22 +08:00
|
|
|
socket_notifier_ = new QSocketNotifier(sockets_[1],
|
2013-03-31 17:45:44 +08:00
|
|
|
QSocketNotifier::Read, this);
|
2014-11-22 16:46:22 +08:00
|
|
|
connect(socket_notifier_, SIGNAL(activated(int)),
|
2013-03-31 17:45:44 +08:00
|
|
|
SLOT(on_socket_notifier_activated()));
|
2013-03-31 17:31:51 +08:00
|
|
|
}
|
|
|
|
|
2013-03-31 17:45:44 +08:00
|
|
|
void SignalHandler::on_socket_notifier_activated()
|
2013-03-31 17:31:51 +08:00
|
|
|
{
|
2014-11-22 16:46:22 +08:00
|
|
|
socket_notifier_->setEnabled(false);
|
2013-03-31 17:45:44 +08:00
|
|
|
|
|
|
|
int sig_number;
|
2015-07-26 00:05:11 +08:00
|
|
|
if (read(sockets_[1], &sig_number, sizeof(int)) != sizeof(int)) {
|
2013-04-23 04:43:00 +08:00
|
|
|
qDebug() << "Failed to catch signal";
|
|
|
|
abort();
|
|
|
|
}
|
2013-03-31 17:45:44 +08:00
|
|
|
|
|
|
|
switch(sig_number)
|
|
|
|
{
|
|
|
|
case SIGINT:
|
2014-07-20 02:15:55 +08:00
|
|
|
Q_EMIT int_received();
|
2013-03-31 17:45:44 +08:00
|
|
|
break;
|
|
|
|
case SIGTERM:
|
2014-07-20 02:15:55 +08:00
|
|
|
Q_EMIT term_received();
|
2013-03-31 17:45:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-22 16:46:22 +08:00
|
|
|
socket_notifier_->setEnabled(true);
|
2013-03-31 17:31:51 +08:00
|
|
|
}
|
|
|
|
|
2013-03-31 17:45:44 +08:00
|
|
|
void SignalHandler::handle_signals(int sig_number)
|
2013-03-31 17:31:51 +08:00
|
|
|
{
|
2015-07-26 00:05:11 +08:00
|
|
|
if (write(sockets_[0], &sig_number, sizeof(int)) != sizeof(int)) {
|
2013-04-23 04:43:00 +08:00
|
|
|
// Failed to handle signal
|
|
|
|
abort();
|
|
|
|
}
|
2013-03-31 17:31:51 +08:00
|
|
|
}
|