From 789f21ccb3f7b41e8f708a1072b059c713d288b6 Mon Sep 17 00:00:00 2001 From: Quantum Date: Thu, 14 Jan 2021 20:03:38 -0500 Subject: [PATCH] [host] windows: handle defined exit codes in service The service should now react to host application exit codes. For the exit codes that demands it, the service will exit instead of restarting the host. --- host/platform/Windows/src/service.c | 37 +++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/host/platform/Windows/src/service.c b/host/platform/Windows/src/service.c index 61c4f3c9..1940ac5a 100644 --- a/host/platform/Windows/src/service.c +++ b/host/platform/Windows/src/service.c @@ -19,6 +19,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "interface/platform.h" #include "common/ivshmem.h" +#include "service.h" #include #include @@ -690,19 +691,47 @@ VOID WINAPI SvcMain(DWORD dwArgc, LPTSTR *lpszArgv) { case WAIT_OBJECT_0: goto stopped; + case WAIT_OBJECT_0 + 1: { + service.running = false; + DWORD code; - if (GetExitCodeProcess(service.process, &code)) - doLog("Host application exited with code 0x%lx\n", code); - else + if (!GetExitCodeProcess(service.process, &code)) doLog("Failed to GetExitCodeProcess (0x%lx)\n", GetLastError()); + else + { + doLog("Host application exited with code 0x%lx\n", code); + switch (code) + { + case LG_HOST_EXIT_USER: + doLog("Host application exited due to user action\n"); + goto stopped; + + case LG_HOST_EXIT_CAPTURE: + doLog("Host application exited due to capture error; restarting\n"); + break; + + case LG_HOST_EXIT_KILLED: + doLog("Host application was killed; restarting\n"); + break; + + case LG_HOST_EXIT_FAILED: + doLog("Host application failed to start; will not restart\n"); + goto stopped; + + default: + doLog("Host application failed due to unknown error; restarting\n"); + break; + } + } // avoid restarting too often if (GetTickCount64() - launchTime < 1000) Sleep(1); break; } + case WAIT_FAILED: doLog("Failed to WaitForMultipleObjects (0x%lx)\n", GetLastError()); } @@ -712,7 +741,7 @@ VOID WINAPI SvcMain(DWORD dwArgc, LPTSTR *lpszArgv) if (service.running) { doLog("Terminating the host application\n"); - if (TerminateProcess(service.process, 0)) + if (TerminateProcess(service.process, LG_HOST_EXIT_KILLED)) { while(WaitForSingleObject(service.process, INFINITE) != WAIT_OBJECT_0) {} doLog("Host application terminated\n");