147 lines
4 KiB
C++
147 lines
4 KiB
C++
|
/******************************************************************************\
|
||
|
* Copyright (c) 2020
|
||
|
*
|
||
|
* Author(s):
|
||
|
* Peter L Jones
|
||
|
*
|
||
|
******************************************************************************
|
||
|
*
|
||
|
* 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.,
|
||
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
*
|
||
|
\******************************************************************************/
|
||
|
|
||
|
#include "signalhandler.h"
|
||
|
|
||
|
class CSignalHandlerSingleton : public CSignalHandler {
|
||
|
public:
|
||
|
inline CSignalHandlerSingleton() : CSignalHandler() {}
|
||
|
};
|
||
|
Q_GLOBAL_STATIC ( CSignalHandlerSingleton, singleton )
|
||
|
|
||
|
CSignalHandler::CSignalHandler() : pSignalBase ( CSignalBase::withSignalHandler ( this ) ) {}
|
||
|
|
||
|
CSignalHandler::~CSignalHandler() = default;
|
||
|
|
||
|
CSignalHandler* CSignalHandler::getSingletonP() { return singleton; }
|
||
|
|
||
|
bool CSignalHandler::emitSignal ( int sigNum )
|
||
|
{
|
||
|
return QMetaObject::invokeMethod( singleton, "ShutdownSignal", Qt::QueuedConnection, Q_ARG( int, sigNum ) );
|
||
|
}
|
||
|
|
||
|
#ifndef _WIN32
|
||
|
void CSignalHandler::OnSocketNotify( int socket )
|
||
|
{
|
||
|
int sigNum;
|
||
|
if ( ::read ( socket, &sigNum, sizeof ( int ) ) == sizeof ( int ) )
|
||
|
{
|
||
|
emitSignal ( sigNum );
|
||
|
}
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
// ----------------------------------------------------------
|
||
|
|
||
|
CSignalBase::CSignalBase ( CSignalHandler* pSignalHandler ) :
|
||
|
pSignalHandler ( pSignalHandler )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
CSignalBase::~CSignalBase() = default;
|
||
|
|
||
|
CSignalBase* CSignalBase::withSignalHandler ( CSignalHandler* pSignalHandler )
|
||
|
{
|
||
|
#ifdef _WIN32
|
||
|
return new CSignalWin ( pSignalHandler );
|
||
|
#else
|
||
|
return new CSignalUnix ( pSignalHandler );
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
#ifdef _WIN32
|
||
|
CSignalWin::CSignalWin ( CSignalHandler* nPSignalHandler ) :
|
||
|
CSignalBase ( nPSignalHandler ),
|
||
|
lock ( QReadWriteLock::Recursive )
|
||
|
{
|
||
|
SetConsoleCtrlHandler ( signalHandler, true );
|
||
|
}
|
||
|
|
||
|
CSignalWin::~CSignalWin() {
|
||
|
SetConsoleCtrlHandler ( signalHandler, false );
|
||
|
}
|
||
|
|
||
|
QReadWriteLock* CSignalWin::getLock() const
|
||
|
{
|
||
|
return &lock;
|
||
|
}
|
||
|
|
||
|
BOOL WINAPI CSignalWin::signalHandler ( _In_ DWORD sigNum )
|
||
|
{
|
||
|
auto self = getSelf<CSignalWin>();
|
||
|
QReadLocker lock ( self->getLock() );
|
||
|
return self->pSignalHandler->emitSignal ( static_cast<int>( sigNum ) );
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
int CSignalUnix::socketPair[2];
|
||
|
|
||
|
CSignalUnix::CSignalUnix ( CSignalHandler* nPSignalHandler ) :
|
||
|
CSignalBase ( nPSignalHandler )
|
||
|
{
|
||
|
if ( ::socketpair ( AF_UNIX, SOCK_STREAM, 0, socketPair ) == 0 )
|
||
|
{
|
||
|
socketNotifier = new QSocketNotifier ( socketPair[ 1 ], QSocketNotifier::Read );
|
||
|
|
||
|
QObject::connect ( socketNotifier, &QSocketNotifier::activated, nPSignalHandler, &CSignalHandler::OnSocketNotify );
|
||
|
|
||
|
socketNotifier->setEnabled ( true );
|
||
|
|
||
|
setSignalHandled ( SIGINT, true );
|
||
|
setSignalHandled ( SIGTERM, true );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
CSignalUnix::~CSignalUnix() {
|
||
|
setSignalHandled ( SIGINT, false );
|
||
|
setSignalHandled ( SIGTERM, false );
|
||
|
}
|
||
|
|
||
|
QReadWriteLock* CSignalUnix::getLock() const { return nullptr; }
|
||
|
|
||
|
bool CSignalUnix::setSignalHandled ( int sigNum, bool state )
|
||
|
{
|
||
|
struct sigaction sa;
|
||
|
sigemptyset ( &sa.sa_mask );
|
||
|
|
||
|
if ( state )
|
||
|
{
|
||
|
sa.sa_handler = CSignalUnix::signalHandler;
|
||
|
sa.sa_flags |= SA_RESTART;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
sa.sa_handler = SIG_DFL;
|
||
|
}
|
||
|
|
||
|
return ::sigaction ( sigNum, &sa, nullptr ) == 0;
|
||
|
}
|
||
|
|
||
|
void CSignalUnix::signalHandler ( int sigNum )
|
||
|
{
|
||
|
const auto res = ::write ( socketPair[ 0 ], &sigNum, sizeof ( int ) );
|
||
|
Q_UNUSED ( res );
|
||
|
}
|
||
|
#endif
|