Separate process wait and close

On Linux, waitpid() both waits for the process to terminate and reaps it
(closes its handle). On Windows, these actions are separated into
WaitForSingleObject() and CloseHandle().

Expose these actions separately, so that it is possible to send a signal
to a process while waiting for its termination without race condition.

This allows to wait for server termination normally, but kill the
process without race condition if it is not terminated after some delay.
This commit is contained in:
Romain Vimont
2021-01-03 17:06:08 +01:00
parent 821c175730
commit d580ee30f1
4 changed files with 64 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
#include "util/process.h"
#include <assert.h>
#include <sys/stat.h>
#include "config.h"
@@ -59,8 +60,8 @@ process_terminate(HANDLE handle) {
return TerminateProcess(handle, 1);
}
bool
process_wait(HANDLE handle, DWORD *exit_code) {
static bool
process_wait_internal(HANDLE handle, DWORD *exit_code, bool close) {
DWORD code;
if (WaitForSingleObject(handle, INFINITE) != WAIT_OBJECT_0
|| !GetExitCodeProcess(handle, &code)) {
@@ -70,10 +71,29 @@ process_wait(HANDLE handle, DWORD *exit_code) {
if (exit_code) {
*exit_code = code;
}
CloseHandle(handle);
if (close) {
CloseHandle(handle);
}
return !code;
}
bool
process_wait(HANDLE handle, DWORD *exit_code) {
return process_wait_internal(handle, exit_code, true);
}
bool
process_wait_noclose(HANDLE handle, DWORD *exit_code) {
return process_wait_internal(handle, exit_code, false);
}
void
process_close(HANDLE handle) {
bool closed = CloseHandle(handle);
assert(closed);
(void) closed;
}
char *
get_executable_path(void) {
HMODULE hModule = GetModuleHandleW(NULL);