X-Git-Url: http://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=signalhandler.cpp;fp=signalhandler.cpp;h=a3ef6b82e8755c7d579da5e46f2f199395138acf;hp=0000000000000000000000000000000000000000;hb=7a255aa9c51ebb41bb629f6d327560dcfa18433f;hpb=ad1d8e2b737106c3210706a8ee8c537dbd438bfb diff --git a/signalhandler.cpp b/signalhandler.cpp new file mode 100644 index 00000000..a3ef6b82 --- /dev/null +++ b/signalhandler.cpp @@ -0,0 +1,94 @@ +/* + +Copyright 2013 Adam Reichold + +This file is part of qpdfview. + +qpdfview 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. + +qpdfview 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 qpdfview. If not, see . + +*/ + +#include "signalhandler.h" + +#include +#include +#include + +#include + +int SignalHandler::s_sockets[2]; + +bool SignalHandler::prepareSignals() +{ + if(socketpair(AF_UNIX, SOCK_STREAM, 0, s_sockets) != 0) + { + return false; + } + + struct sigaction sigAction; + + sigAction.sa_handler = SignalHandler::handleSignals; + sigemptyset(&sigAction.sa_mask); + sigAction.sa_flags = SA_RESTART; + + if(sigaction(SIGINT, &sigAction, 0) != 0) + { + close(s_sockets[0]); + close(s_sockets[1]); + + return false; + } + + if(sigaction(SIGTERM, &sigAction, 0) != 0) + { + close(s_sockets[0]); + close(s_sockets[1]); + + return false; + } + + return true; +} + +SignalHandler::SignalHandler(QObject* parent) : QObject(parent), + m_socketNotifier(0) +{ + m_socketNotifier = new QSocketNotifier(s_sockets[1], QSocketNotifier::Read, this); + connect(m_socketNotifier, SIGNAL(activated(int)), SLOT(on_socketNotifier_activated())); +} + +void SignalHandler::on_socketNotifier_activated() +{ + m_socketNotifier->setEnabled(false); + + int sigNumber; + read(s_sockets[1], &sigNumber, sizeof(int)); + + switch(sigNumber) + { + case SIGINT: + emit sigIntReceived(); + break; + case SIGTERM: + emit sigTermReceived(); + break; + } + + m_socketNotifier->setEnabled(true); +} + +void SignalHandler::handleSignals(int sigNumber) +{ + write(s_sockets[0], &sigNumber, sizeof(int)); +}