Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,4 @@ if(BUILD_PYTHON_BINDINGS AND BINDINGS_SRC)
)
endif()

# ===============================================================================================
# ===============================================================================================
4 changes: 4 additions & 0 deletions Include/CircusApplication.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#pragma once
#include <QApplication>

#include "sigwatch.h"
namespace spqr {

class CircusApplication : public QApplication {
public:
CircusApplication(int& argc, char** argv);
~CircusApplication();

private:
UnixSignalWatcher sigwatch;
};

} // namespace spqr
2 changes: 1 addition & 1 deletion Include/Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ class Sensor {

private:
mutable std::shared_mutex mtx_;
};
};
58 changes: 58 additions & 0 deletions Include/sigwatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 <signal.h>

#include <QObject>

class UnixSignalWatcherPrivate;

/*!
* \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 UnixSignalWatcher : public QObject {
Q_OBJECT
public:
explicit UnixSignalWatcher(QObject *parent = 0);
~UnixSignalWatcher();

void watchForSignal(int signal);

signals:
void unixSignal(int signal);

private:
UnixSignalWatcherPrivate *const d_ptr;
Q_DECLARE_PRIVATE(UnixSignalWatcher)
Q_PRIVATE_SLOT(d_func(), void _q_onNotify(int))
};

#endif // SIGWATCH_H
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This instructs pixi to create the environment specified in [pixi.toml](pixi.toml
### Run
The pixi manifest declares a task named `main` that runs the python entrypoint (see [pixi.toml](pixi.toml) tasks). Run
```
pixi run main
pixi run circus-main
```

### Links
Expand Down
6 changes: 3 additions & 3 deletions Resources/config/framework_config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
image: test_image
image: booster-env-base
volumes:
- "/home/daniaffch/Dev/spqrbooster2026/src/SimBridge/bridge/build:/app/BridgeSubscriber"
- "/home/daniaffch/Dev/spqrbooster2026/src/SimBridge/fake_framework/build:/app/fake_framework"
- "/home/ubuntu/Robocup/spqrbooster2026/src/SimBridge/bridge/build:/app/BridgeSubscriber"
- "/home/ubuntu/Robocup/spqrbooster2026/src/SimBridge/fake_framework/build:/app/fake_framework"
13 changes: 0 additions & 13 deletions Src/AppWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
namespace spqr {

AppWindow::AppWindow(int& argc, char** argv) {
std::signal(SIGTERM, signalHandler);
std::signal(SIGINT, signalHandler);
std::signal(SIGSEGV, signalHandler);
std::signal(SIGABRT, signalHandler);

resize(spqr::initialWindowWidth, spqr::initialWindowHeight);
setWindowTitle(spqr::appName);

Expand Down Expand Up @@ -86,14 +81,6 @@ void AppWindow::loadScene(const QString& yamlFile) {
}
}

void AppWindow::signalHandler(int signal) {
TeamManager::instance().clear();
RobotManager::instance().stopCommunicationServer();

std::signal(signal, SIG_DFL);
std::raise(signal);
}

AppWindow::~AppWindow() {
if (sim != nullptr && sim->isRunning())
sim->stop();
Expand Down
15 changes: 15 additions & 0 deletions Src/CircusApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

#include "curl/curl.h"

void installsigwatch(UnixSignalWatcher* sigwatch) {
sigwatch->watchForSignal(SIGABRT);
sigwatch->watchForSignal(SIGFPE);
sigwatch->watchForSignal(SIGILL);
sigwatch->watchForSignal(SIGINT);
sigwatch->watchForSignal(SIGSEGV);
sigwatch->watchForSignal(SIGTERM);
sigwatch->watchForSignal(SIGQUIT);
sigwatch->watchForSignal(SIGHUP);
sigwatch->watchForSignal(SIGSYS);
}

namespace spqr {
CircusApplication::CircusApplication(int& argc, char** argv) : QApplication(argc, argv) {
QSurfaceFormat format;
Expand All @@ -17,6 +29,9 @@ CircusApplication::CircusApplication(int& argc, char** argv) : QApplication(argc
format.setProfile(QSurfaceFormat::CompatibilityProfile);
QSurfaceFormat::setDefaultFormat(format);
curl_global_init(CURL_GLOBAL_DEFAULT);

installsigwatch(&sigwatch);
QObject::connect(&sigwatch, SIGNAL(unixSignal(int)), this, SLOT(quit()));
}

CircusApplication::~CircusApplication() {
Expand Down
42 changes: 41 additions & 1 deletion Src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@

#include <linux/prctl.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <unistd.h>

#include <QSurfaceFormat>
#include <csignal>
#include <iostream>

#include "AppWindow.h"
#include "CircusApplication.h"
#include "Constants.h"

int main(int argc, char** argv) {
int runCircusApp(int argc, char** argv) {
spqr::CircusApplication app(argc, argv);
app.setApplicationName(spqr::appName);

Expand All @@ -14,3 +22,35 @@ int main(int argc, char** argv) {

return app.exec();
}

int main(int argc, char** argv) {
pid_t pid = fork();

if (pid < 0) {
perror("fork");
}

if (pid == 0) {
prctl(PR_SET_PDEATHSIG, SIGHUP);
return runCircusApp(argc, argv);
} else {
// ignore all signal sent to him except of the uncatchable ones
sigset_t mask;
sigfillset(&mask);
sigprocmask(SIG_SETMASK, &mask, NULL);

int status;
waitpid(pid, &status, 0);

FILE* dockerList = popen("docker ps -aq", "r");
char buffer[128];

if (fgets(buffer, sizeof(buffer), dockerList) != nullptr) {
std::cout << "Dockers still running: forcing their closure.\n";
}

pclose(dockerList);
system("docker rm -f $(docker ps -aq) > /dev/null 2>&1");
return WEXITSTATUS(status);
}
}
163 changes: 163 additions & 0 deletions Src/sigwatch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* 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 "sigwatch.h"

#include <errno.h>
#include <sys/socket.h>
#include <unistd.h>

#include <QDebug>
#include <QMap>
#include <QSocketNotifier>

/*!
* \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;
Q_DECLARE_PUBLIC(UnixSignalWatcher)

public:
UnixSignalWatcherPrivate(UnixSignalWatcher *q);
~UnixSignalWatcherPrivate();

void watchForSignal(int signal);
static void signalHandler(int signal);

void _q_onNotify(int sockfd);

private:
static int sockpair[2];
QSocketNotifier *notifier;
QList<int> watchedSignals;
};

int UnixSignalWatcherPrivate::sockpair[2];

UnixSignalWatcherPrivate::UnixSignalWatcherPrivate(UnixSignalWatcher *q) : q_ptr(q) {
// Create socket pair
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair)) {
qDebug() << "UnixSignalWatcher: socketpair: " << ::strerror(errno);
return;
}

// Create a notifier for the read end of the pair
notifier = new QSocketNotifier(sockpair[1], QSocketNotifier::Read);
QObject::connect(notifier, SIGNAL(activated(int)), q, SLOT(_q_onNotify(int)));
notifier->setEnabled(true);
}

UnixSignalWatcherPrivate::~UnixSignalWatcherPrivate() {
delete notifier;
}

/*!
* 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;
}

// 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, NULL)) {
qDebug() << "UnixSignalWatcher: sigaction: " << ::strerror(errno);
return;
}

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) {
ssize_t nBytes = ::write(sockpair[0], &signal, sizeof(signal));
Q_UNUSED(nBytes);
}

/*!
* 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;
ssize_t nBytes = ::read(sockfd, &signal, sizeof(signal));
Q_UNUSED(nBytes);
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)) {}

/*!
* Destroy this UnixSignalWatcher.
*/
UnixSignalWatcher::~UnixSignalWatcher() {
delete d_ptr;
}

/*!
* 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"
8 changes: 8 additions & 0 deletions Src/sigwatch.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
TEMPLATE = app
QT = core

TARGET = sigwatch-demo

SOURCES += sigwatch.cpp

HEADERS += sigwatch.h
2 changes: 1 addition & 1 deletion dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ RUN apt-get update && apt-get install -y \
libtinyxml2-dev && \
rm -rf /var/lib/apt/lists/*

CMD ["bash", "-c", "/app/fake_framework/fake_framework & /app/BridgeSubscriber/BridgeSubscriber"]
CMD ["bash", "-c", "/app/fake_framework/fake_framework & /app/BridgeSubscriber/BridgeSubscriber"]
Loading