Clean termination on *nix systems
This commit is contained in:
parent
9df6b11a16
commit
2b5b5c22f2
3 changed files with 51 additions and 2 deletions
|
@ -67,17 +67,32 @@ 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()
|
||||
{
|
||||
// Kill process as requested
|
||||
if (process.state() == QProcess::Running) {
|
||||
process.kill();
|
||||
}
|
||||
}
|
||||
|
||||
void SMSubProcess::stopProcess()
|
||||
{
|
||||
// Terminate process as requested
|
||||
if (process.state() == QProcess::Running) {
|
||||
process.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
void SMSubProcess::writeInput(const QByteArray &input)
|
||||
{
|
||||
|
|
|
@ -33,6 +33,7 @@ private:
|
|||
QProcess process;
|
||||
|
||||
public slots:
|
||||
void aboutToQuit();
|
||||
void killProcess();
|
||||
void stopProcess();
|
||||
void writeInput(const QByteArray &input);
|
||||
|
|
33
main.cpp
33
main.cpp
|
@ -30,12 +30,44 @@
|
|||
#include "SMSubServer.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[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
a.setApplicationName("Server Manager Subprocess");
|
||||
a.setApplicationVersion("0.1");
|
||||
|
||||
#ifdef Q_OS_UNIX
|
||||
catchUnixSignals({SIGQUIT, SIGINT, SIGTERM, SIGHUP});
|
||||
#endif
|
||||
|
||||
QCommandLineParser commandLineParser;
|
||||
commandLineParser.addHelpOption();
|
||||
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(killRequested()), &subProcess, SLOT(killProcess()));
|
||||
QObject::connect(&subServer, SIGNAL(stopRequested()), &subProcess, SLOT(stopProcess()));
|
||||
QObject::connect(&a, SIGNAL(aboutToQuit()), &subProcess, SLOT(aboutToQuit()));
|
||||
|
||||
subProcess.start();
|
||||
|
||||
|
|
Loading…
Reference in a new issue