Clean termination on *nix systems
This commit is contained in:
parent
9df6b11a16
commit
2b5b5c22f2
3 changed files with 51 additions and 2 deletions
|
@ -67,16 +67,31 @@ void SMSubProcess::processError(QProcess::ProcessError error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SMSubProcess::aboutToQuit()
|
||||||
|
{
|
||||||
|
// Main process terminated
|
||||||
|
if (process.state() == QProcess::Running) {
|
||||||
|
process.terminate();
|
||||||
|
if (!process.waitForFinished(60000)) {
|
||||||
|
QTextStream(stderr) << "Failed to terminate process!" << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SMSubProcess::killProcess()
|
void SMSubProcess::killProcess()
|
||||||
{
|
{
|
||||||
// Kill process as requested
|
// Kill process as requested
|
||||||
|
if (process.state() == QProcess::Running) {
|
||||||
process.kill();
|
process.kill();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubProcess::stopProcess()
|
void SMSubProcess::stopProcess()
|
||||||
{
|
{
|
||||||
// Terminate process as requested
|
// Terminate process as requested
|
||||||
|
if (process.state() == QProcess::Running) {
|
||||||
process.terminate();
|
process.terminate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SMSubProcess::writeInput(const QByteArray &input)
|
void SMSubProcess::writeInput(const QByteArray &input)
|
||||||
|
|
|
@ -33,6 +33,7 @@ private:
|
||||||
QProcess process;
|
QProcess process;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
void aboutToQuit();
|
||||||
void killProcess();
|
void killProcess();
|
||||||
void stopProcess();
|
void stopProcess();
|
||||||
void writeInput(const QByteArray &input);
|
void writeInput(const QByteArray &input);
|
||||||
|
|
33
main.cpp
33
main.cpp
|
@ -30,12 +30,44 @@
|
||||||
#include "SMSubServer.h"
|
#include "SMSubServer.h"
|
||||||
#include "smsub.h"
|
#include "smsub.h"
|
||||||
|
|
||||||
|
#ifdef Q_OS_UNIX
|
||||||
|
#include <initializer_list>
|
||||||
|
#include "signal.h"
|
||||||
|
#include "unistd.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef Q_OS_UNIX
|
||||||
|
void catchUnixSignals(std::initializer_list<int> quitSignals) {
|
||||||
|
auto handler = [](int sig) -> void {
|
||||||
|
QTextStream(stderr) << "Received Unix signal: " << sig << endl;
|
||||||
|
QCoreApplication::quit();
|
||||||
|
};
|
||||||
|
|
||||||
|
sigset_t blocking_mask;
|
||||||
|
sigemptyset(&blocking_mask);
|
||||||
|
for (auto sig : quitSignals)
|
||||||
|
sigaddset(&blocking_mask, sig);
|
||||||
|
|
||||||
|
struct sigaction sa;
|
||||||
|
sa.sa_handler = handler;
|
||||||
|
sa.sa_mask = blocking_mask;
|
||||||
|
sa.sa_flags = 0;
|
||||||
|
|
||||||
|
for (auto sig : quitSignals)
|
||||||
|
sigaction(sig, &sa, nullptr);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QCoreApplication a(argc, argv);
|
QCoreApplication a(argc, argv);
|
||||||
a.setApplicationName("Server Manager Subprocess");
|
a.setApplicationName("Server Manager Subprocess");
|
||||||
a.setApplicationVersion("0.1");
|
a.setApplicationVersion("0.1");
|
||||||
|
|
||||||
|
#ifdef Q_OS_UNIX
|
||||||
|
catchUnixSignals({SIGQUIT, SIGINT, SIGTERM, SIGHUP});
|
||||||
|
#endif
|
||||||
|
|
||||||
QCommandLineParser commandLineParser;
|
QCommandLineParser commandLineParser;
|
||||||
commandLineParser.addHelpOption();
|
commandLineParser.addHelpOption();
|
||||||
commandLineParser.addVersionOption();
|
commandLineParser.addVersionOption();
|
||||||
|
@ -148,6 +180,7 @@ int main(int argc, char *argv[])
|
||||||
QObject::connect(&subServer, SIGNAL(inputWritten(QByteArray)), &subProcess, SLOT(writeInput(QByteArray)));
|
QObject::connect(&subServer, SIGNAL(inputWritten(QByteArray)), &subProcess, SLOT(writeInput(QByteArray)));
|
||||||
QObject::connect(&subServer, SIGNAL(killRequested()), &subProcess, SLOT(killProcess()));
|
QObject::connect(&subServer, SIGNAL(killRequested()), &subProcess, SLOT(killProcess()));
|
||||||
QObject::connect(&subServer, SIGNAL(stopRequested()), &subProcess, SLOT(stopProcess()));
|
QObject::connect(&subServer, SIGNAL(stopRequested()), &subProcess, SLOT(stopProcess()));
|
||||||
|
QObject::connect(&a, SIGNAL(aboutToQuit()), &subProcess, SLOT(aboutToQuit()));
|
||||||
|
|
||||||
subProcess.start();
|
subProcess.start();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue