From 8663869ce7d0e0aedbd05dcba47f5472f7ac8c2e Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Mon, 27 Jan 2014 22:17:45 +0000 Subject: [PATCH] Ensure we have a valid service exit time. Sometimes GetProcessTimes() will succeed but return 0 for the process exit time, leading check_parent() to conclude that the service had exited before a child process had started, and thus that the child process was not part of the service's tree. We now use initialise the service exit time to the current time before attempting to get the real exit time, and only override the value if it's valid. Thanks Czenda Czendov. --- process.cpp | 1 + service.cpp | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/process.cpp b/process.cpp index e96265d..c0b65df 100644 --- a/process.cpp +++ b/process.cpp @@ -23,6 +23,7 @@ int get_process_exit_time(HANDLE process_handle, FILETIME *ft) { return 1; } + if (! (exit_time.dwLowDateTime || exit_time.dwHighDateTime)) return 2; memmove(ft, &exit_time, sizeof(exit_time)); return 0; diff --git a/service.cpp b/service.cpp index 1477ba7..5a732b0 100644 --- a/service.cpp +++ b/service.cpp @@ -1680,15 +1680,18 @@ void CALLBACK end_service(void *arg, unsigned char why) { service->rotate_stdout_online = service->rotate_stderr_online = NSSM_ROTATE_OFFLINE; + /* Use now as a dummy exit time. */ + GetSystemTimeAsFileTime(&service->exit_time); + /* Check exit code */ unsigned long exitcode = 0; TCHAR code[16]; if (service->process_handle) { GetExitCodeProcess(service->process_handle, &exitcode); - if (exitcode == STILL_ACTIVE || get_process_exit_time(service->process_handle, &service->exit_time)) GetSystemTimeAsFileTime(&service->exit_time); + /* Check real exit time. */ + if (exitcode != STILL_ACTIVE) get_process_exit_time(service->process_handle, &service->exit_time); CloseHandle(service->process_handle); } - else GetSystemTimeAsFileTime(&service->exit_time); service->process_handle = 0; -- 2.20.1