cabana: properly handle unix signals (#28657)

old-commit-hash: fe91ea1139a5ff3f832a73f09bbdd77a78533acc
This commit is contained in:
Dean Lee
2023-06-24 06:27:51 +08:00
committed by GitHub
parent 215819c218
commit d1c19dfda9
3 changed files with 57 additions and 0 deletions
+38
View File
@@ -1,7 +1,11 @@
#include "tools/cabana/util.h"
#include <array>
#include <csignal>
#include <sys/socket.h>
#include <unistd.h>
#include <QDebug>
#include <QColor>
#include <QFontDatabase>
#include <QLocale>
@@ -145,6 +149,40 @@ void TabBar::closeTabClicked() {
}
}
// UnixSignalHandler
UnixSignalHandler::UnixSignalHandler(QObject *parent) : QObject(nullptr) {
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sig_fd)) {
qFatal("Couldn't create TERM socketpair");
}
sn = new QSocketNotifier(sig_fd[1], QSocketNotifier::Read, this);
connect(sn, SIGNAL(activated(QSocketDescriptor)), this, SLOT(handleSigTerm()));
std::signal(SIGINT, signalHandler);
std::signal(SIGTERM, UnixSignalHandler::signalHandler);
}
UnixSignalHandler::~UnixSignalHandler() {
::close(sig_fd[0]);
::close(sig_fd[1]);
}
void UnixSignalHandler::signalHandler(int s) {
::write(sig_fd[0], &s, sizeof(s));
}
void UnixSignalHandler::handleSigTerm() {
sn->setEnabled(false);
int tmp;
::read(sig_fd[1], &tmp, sizeof(tmp));
printf("\nexiting...\n");
qApp->closeAllWindows();
qApp->exit();
}
// NameValidator
NameValidator::NameValidator(QObject *parent) : QRegExpValidator(QRegExp("^(\\w+)"), parent) {}
QValidator::State NameValidator::validate(QString &input, int &pos) const {