From 2d246d25637edc69902a8ebe0ac2d639ca523cfb Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 13 Jul 2017 13:34:50 -0700 Subject: [PATCH] Implement shutdown with parent for Mac and Linux --- libraries/shared/src/SharedUtil.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/libraries/shared/src/SharedUtil.cpp b/libraries/shared/src/SharedUtil.cpp index 58b8aead45..99bdfc4d90 100644 --- a/libraries/shared/src/SharedUtil.cpp +++ b/libraries/shared/src/SharedUtil.cpp @@ -47,6 +47,7 @@ extern "C" FILE * __cdecl __iob_func(void) { #include #include #include +#include #include #include #include @@ -1077,14 +1078,20 @@ void setMaxCores(uint8_t maxCores) { #endif } -#ifdef Q_OS_WIN -VOID CALLBACK parentDiedCallback(PVOID lpParameter, BOOLEAN timerOrWaitFired) { - if (!timerOrWaitFired && qApp) { +void quitWithParentProcess() { + if (qApp) { qDebug() << "Parent process died, quitting"; qApp->quit(); } } +#ifdef Q_OS_WIN +VOID CALLBACK parentDiedCallback(PVOID lpParameter, BOOLEAN timerOrWaitFired) { + if (!timerOrWaitFired) { + quitWithParentProcess(); + } +} + void watchParentProcess(int parentPID) { DWORD processID = parentPID; HANDLE procHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processID); @@ -1092,8 +1099,17 @@ void watchParentProcess(int parentPID) { HANDLE newHandle; RegisterWaitForSingleObject(&newHandle, procHandle, parentDiedCallback, NULL, INFINITE, WT_EXECUTEONLYONCE); } -#else +#elif defined(Q_OS_MAC) || defined(Q_OS_LINUX) void watchParentProcess(int parentPID) { - qWarning() << "Parent PID option not implemented on this plateform"; + auto timer = new QTimer(qApp); + timer->setInterval(MSECS_PER_SECOND); + QObject::connect(timer, &QTimer::timeout, qApp, [parentPID]() { + auto ppid = getppid(); + if (parentPID != ppid) { + // If the PPID changed, then that means our parent process died. + quitWithParentProcess(); + } + }); + timer->start(); } -#endif // Q_OS_WIN \ No newline at end of file +#endif