Add external qt-unix-signals library

This library, original taken from https://github.com/sijk/qt-unix-signals
(but a maintained fork exists at https://github.com/nyalldawson/qt-unix-signals),
handles unix signal watching using the Qt libraries.

It allows for detection of signals like SIGINT and SIGTERM,
and allows Qt applications to respond gracefully to these.

Included in external libraries for use in QGIS terminal
applications.
This commit is contained in:
Nyall Dawson 2019-03-01 09:46:46 +10:00
parent a33bf306f5
commit 620db06324
9 changed files with 478 additions and 1 deletions

42
external/qt-unix-signals/CMakeLists.txt vendored Normal file
View File

@ -0,0 +1,42 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3)
project(QTSignals)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(QTSignal_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "QTSignal Include Dir")
find_package(Qt5Core REQUIRED)
if (Qt5_POSITION_INDEPENDENT_CODE)
SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif(Qt5_POSITION_INDEPENDENT_CODE)
SET(CMAKE_CXX_STANDARD 11)
option(CMAKE_ENABLE "Enable automatic path configuration" ON)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
include_directories(
${Qt5Core_INCLUDE_DIRS})
add_library(QTSignal SHARED
sigwatch.cpp
)
target_link_libraries (QTSignal
${Qt5Core_LIBRARIES}
)
#option(WITH_EXAMPLE "Build example executable" ON)
#if (WITH_EXAMPLE)
# add_executable (sigwatch-demo example.cpp )
# target_link_libraries (sigwatch-demo
# QTSignal
# )
#endif(WITH_EXAMPLE)

22
external/qt-unix-signals/LICENCE vendored Normal file
View File

@ -0,0 +1,22 @@
Unix signal watcher for Qt.
Copyright (C) 2014 Simon Knopp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

98
external/qt-unix-signals/README.md vendored Normal file
View File

@ -0,0 +1,98 @@
Unix Signal Watcher For Qt
==========================
Author: Simon Knopp
Licence: [MIT](http://opensource.org/licenses/MIT)
## Summary
When writing a Qt application, as with any application, it is sometimes useful
to handle Unix signals. Of course, Qt already incorporates the notion of
signals, so it would be nice if Unix signals could be mapped to Qt signals. Then
we could write handlers for Unix signals and connect them up in the same way as
normal Qt slots.
The class described below does just this. It is heavily based on [this
example](http://qt-project.org/doc/qt-5.0/qtdoc/unix-signals.html) in the Qt
documentation, but it encapsulates that functionality in a generic re-usable
class.
## Interface
The interface is simple: you call `watchForSignal()` with the signals you're
interested in, and `connect()` your handlers to `SIGNAL(unixSignal(int))`.
``` c++
class UnixSignalWatcher : public QObject
{
Q_OBJECT
public:
explicit UnixSignalWatcher(QObject *parent = 0);
~UnixSignalWatcher();
void watchForSignal(int signal);
signals:
void unixSignal(int signal);
};
```
## Example usage
Let's look at an example program.
``` c++
#include <QCoreApplication>
#include <QDebug>
#include "sigwatch.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
qDebug() << "Hello from process" << QCoreApplication::applicationPid();
UnixSignalWatcher sigwatch;
sigwatch.watchForSignal(SIGINT);
sigwatch.watchForSignal(SIGTERM);
QObject::connect(&sigwatch, SIGNAL(unixSignal(int)), &app, SLOT(quit()));
int exitcode = app.exec();
qDebug() << "Goodbye";
return exitcode;
}
```
This simply registers signal handlers for `SIGINT` and `SIGTERM` and then idles
forever. If you run it (`qmake && make && ./sigwatch-demo`) you'll see a
greeting and the pid of the process:
Hello from process 6811
Press `^C` to send `SIGINT`. The `UnixSignalWatcher` will handle the signal,
which in turn is connected to `QCoreApplication::quit()`, so the event loop
exits and the farewell message is printed.
^CCaught signal: Interrupt
Goodbye
Similarly, you could use `kill` to send `SIGTERM`.
$ ./sigwatch-demo &
Hello from process 6848
$ kill 6848
Caught signal: Terminated
Goodbye
If you send a signal that does not have a handler, though, you won't see the
farewell message. For instance:
$ ./sigwatch-demo
Hello from process 6906
$ kill -SIGABRT 6906
Aborted (core dumped)
## Compatibility
Tested with Qt 4.6 and 5.2 on Linux.

19
external/qt-unix-signals/example.cpp vendored Normal file
View File

@ -0,0 +1,19 @@
#include <QCoreApplication>
#include <QDebug>
#include "sigwatch.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
qDebug() << "Hello from process" << QCoreApplication::applicationPid();
UnixSignalWatcher sigwatch;
sigwatch.watchForSignal(SIGINT);
sigwatch.watchForSignal(SIGTERM);
QObject::connect(&sigwatch, SIGNAL(unixSignal(int)), &app, SLOT(quit()));
int exitcode = app.exec();
qDebug() << "Goodbye";
return exitcode;
}

42
external/qt-unix-signals/qtsignal.h vendored Normal file
View File

@ -0,0 +1,42 @@
#ifndef QTSIGNAL_EXPORT_H
#define QTSIGNAL_EXPORT_H
#ifdef QTSIGNAL_STATIC_DEFINE
# define QTSIGNAL_EXPORT
# define QTSIGNAL_NO_EXPORT
#else
# ifndef QTSIGNAL_EXPORT
# ifdef QTSignal_EXPORTS
/* We are building this library */
# define QTSIGNAL_EXPORT __attribute__((visibility("default")))
# else
/* We are using this library */
# define QTSIGNAL_EXPORT __attribute__((visibility("default")))
# endif
# endif
# ifndef QTSIGNAL_NO_EXPORT
# define QTSIGNAL_NO_EXPORT __attribute__((visibility("hidden")))
# endif
#endif
#ifndef QTSIGNAL_DEPRECATED
# define QTSIGNAL_DEPRECATED __attribute__ ((__deprecated__))
#endif
#ifndef QTSIGNAL_DEPRECATED_EXPORT
# define QTSIGNAL_DEPRECATED_EXPORT QTSIGNAL_EXPORT QTSIGNAL_DEPRECATED
#endif
#ifndef QTSIGNAL_DEPRECATED_NO_EXPORT
# define QTSIGNAL_DEPRECATED_NO_EXPORT QTSIGNAL_NO_EXPORT QTSIGNAL_DEPRECATED
#endif
#if 0 /* DEFINE_NO_DEPRECATED */
# ifndef QTSIGNAL_NO_DEPRECATED
# define QTSIGNAL_NO_DEPRECATED
# endif
#endif
#endif /* QTSIGNAL_EXPORT_H */

180
external/qt-unix-signals/sigwatch.cpp vendored Normal file
View File

@ -0,0 +1,180 @@
/*
* Unix signal watcher for Qt.
*
* Copyright (C) 2014 Simon Knopp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <QMap>
#include <QSocketNotifier>
#include <QtDebug>
#include "sigwatch.h"
#include <memory>
#ifdef Q_OS_WIN
#define HAS_UNIX_SIGNALS 0
#else
#define HAS_UNIX_SIGNALS 1
#endif
#if HAS_UNIX_SIGNALS
# include <csignal>
# include <sys/socket.h>
# include <unistd.h>
# include <cerrno>
#endif
/*!
* \brief The UnixSignalWatcherPrivate class implements the back-end signal
* handling for the UnixSignalWatcher.
*
* \see http://qt-project.org/doc/qt-5.0/qtdoc/unix-signals.html
*/
class UnixSignalWatcherPrivate : public QObject
{
UnixSignalWatcher * const q_ptr = nullptr;
Q_DECLARE_PUBLIC(UnixSignalWatcher)
public:
UnixSignalWatcherPrivate(UnixSignalWatcher *q);
~UnixSignalWatcherPrivate() override;
void watchForSignal(int signal);
static void signalHandler(int signal);
void _q_onNotify(int sockfd);
private:
static int sockpair[2];
std::unique_ptr< QSocketNotifier > notifier;
QList<int> watchedSignals;
};
int UnixSignalWatcherPrivate::sockpair[2];
UnixSignalWatcherPrivate::UnixSignalWatcherPrivate(UnixSignalWatcher *q) :
q_ptr(q)
{
#if HAS_UNIX_SIGNALS
// Create socket pair
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair)) {
qDebug() << "UnixSignalWatcher: socketpair: " << ::strerror(errno);
return;
}
#endif
// Create a notifier for the read end of the pair
notifier.reset( new QSocketNotifier(sockpair[1], QSocketNotifier::Read) );
QObject::connect(notifier.get(), SIGNAL(activated(int)), q, SLOT(_q_onNotify(int)));
notifier->setEnabled(true);
}
UnixSignalWatcherPrivate::~UnixSignalWatcherPrivate() = default;
/*!
* Registers a handler for the given Unix \a signal. The handler will write to
* a socket pair, the other end of which is connected to a QSocketNotifier.
* This provides a way to break out of the asynchronous context from which the
* signal handler is called and back into the Qt event loop.
*/
void UnixSignalWatcherPrivate::watchForSignal(int signal)
{
if (watchedSignals.contains(signal)) {
qDebug() << "Already watching for signal" << signal;
return;
}
#if HAS_UNIX_SIGNALS
// Register a sigaction which will write to the socket pair
struct sigaction sigact;
sigact.sa_handler = UnixSignalWatcherPrivate::signalHandler;
sigact.sa_flags = 0;
::sigemptyset(&sigact.sa_mask);
sigact.sa_flags |= SA_RESTART;
if (::sigaction(signal, &sigact, nullptr)) {
qDebug() << "UnixSignalWatcher: sigaction: " << ::strerror(errno);
return;
}
#endif
watchedSignals.append(signal);
}
/*!
* Called when a Unix \a signal is received. Write to the socket to wake up the
* QSocketNotifier.
*/
void UnixSignalWatcherPrivate::signalHandler(int signal)
{
(void)::write(sockpair[0], &signal, sizeof(signal));
}
/*!
* Called when the signal handler has written to the socket pair. Emits the Unix
* signal as a Qt signal.
*/
void UnixSignalWatcherPrivate::_q_onNotify(int sockfd)
{
Q_Q(UnixSignalWatcher);
int signal = 0;
(void)::read(sockfd, &signal, sizeof(signal));
qDebug() << "Caught signal:" << ::strsignal(signal);
emit q->unixSignal(signal);
}
/*!
* Create a new UnixSignalWatcher as a child of the given \a parent.
*/
UnixSignalWatcher::UnixSignalWatcher(QObject *parent) :
QObject(parent),
d_ptr(new UnixSignalWatcherPrivate(this))
{
}
UnixSignalWatcher::~UnixSignalWatcher() = default;
/*!
* Register a signal handler for the given \a signal.
*
* After calling this method you can \c connect() to the unixSignal() Qt signal
* to be notified when the Unix signal is received.
*/
void UnixSignalWatcher::watchForSignal(int signal)
{
Q_D(UnixSignalWatcher);
d->watchForSignal(signal);
}
/*!
* \fn void UnixSignalWatcher::unixSignal(int signal)
* Emitted when the given Unix \a signal is received.
*
* watchForSignal() must be called for each Unix signal that you want to receive
* via the unixSignal() Qt signal. If a watcher is watching multiple signals,
* unixSignal() will be emitted whenever *any* of the watched Unix signals are
* received, and the \a signal argument can be inspected to find out which one
* was actually received.
*/
#include "moc_sigwatch.cpp"

64
external/qt-unix-signals/sigwatch.h vendored Normal file
View File

@ -0,0 +1,64 @@
/*
* Unix signal watcher for Qt.
*
* Copyright (C) 2014 Simon Knopp
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SIGWATCH_H
#define SIGWATCH_H
#include "qtsignal.h"
#include <QObject>
#include <csignal>
class UnixSignalWatcherPrivate;
#if Q_OS_WIN
const int SIGINT = 2;
const int SIGTERM = 15;
#endif
/*!
* \brief The UnixSignalWatcher class converts Unix signals to Qt signals.
*
* To watch for a given signal, e.g. \c SIGINT, call \c watchForSignal(SIGINT)
* and \c connect() your handler to unixSignal().
*/
class QTSIGNAL_EXPORT UnixSignalWatcher : public QObject
{
Q_OBJECT
public:
explicit UnixSignalWatcher(QObject *parent = nullptr);
~UnixSignalWatcher();
void watchForSignal(int signal);
signals:
void unixSignal(int signal);
private:
UnixSignalWatcherPrivate * const d_ptr = nullptr;
Q_DECLARE_PRIVATE(UnixSignalWatcher)
Q_PRIVATE_SLOT(d_func(), void _q_onNotify(int))
};
#endif // SIGWATCH_H

10
external/qt-unix-signals/sigwatch.pro vendored Normal file
View File

@ -0,0 +1,10 @@
TEMPLATE = app
QT = core
TARGET = sigwatch-demo
SOURCES += example.cpp \
sigwatch.cpp
HEADERS += sigwatch.h

View File

@ -89,7 +89,7 @@ astyleit() {
for f in "$@"; do
case "$f" in
src/plugins/grass/qtermwidget/*|external/o2/*|external/astyle/*|external/kdbush/*|external/wintoast/*|external/qt3dextra-headers/*|python/ext-libs/*|ui_*.py|*.astyle|tests/testdata/*|editors/*)
src/plugins/grass/qtermwidget/*|external/o2/*|external/qt-unix-signals/*|external/astyle/*|external/kdbush/*|external/wintoast/*|external/qt3dextra-headers/*|python/ext-libs/*|ui_*.py|*.astyle|tests/testdata/*|editors/*)
echo -ne "$f skipped $elcr"
continue
;;