From 201b641d6ef9560f0db11071d8104f5d0284e634 Mon Sep 17 00:00:00 2001 From: Heiko Voigt Date: Thu, 8 Oct 2009 22:08:19 +0200 Subject: [PATCH] introduce platform independent wait_for_process function Signed-off-by: Heiko Voigt --- common/exec.c | 20 ++++++++++---------- common/systeminfo.h | 4 ++++ explorer/systeminfo.c | 20 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/common/exec.c b/common/exec.c index 1825bd5..c1179d7 100644 --- a/common/exec.c +++ b/common/exec.c @@ -27,7 +27,8 @@ int exec_program(const char *working_directory, int argc = 0; pid_t pid; - DWORD status = 0; + int status = 0; + int ret; reporter *debug = QUIETMODE & flags ? debug_git : debug_git_mbox; @@ -86,18 +87,17 @@ int exec_program(const char *working_directory, close(fderr[1]); if (WAITMODE & flags) { - if (WAIT_OBJECT_0 == WaitForSingleObject((HANDLE)pid, - MAX_PROCESSING_TIME)) { - if (GetExitCodeProcess((HANDLE)pid, &status)) - debug_git("Exit code: %d", status); - else { - /* play safe, and return total failure */ - status = -1; - debug_git("[ERROR] GetExitCode failed (%d); " + ret = wait_for_process(pid, MAX_PROCESSING_TIME, + &status); + if (ret) { + if (ret < 0) { + debug_git("[ERROR] wait_for_process failed (%d); " "wd: %s; cmd: %s", - GetLastError(), + status, working_directory, argv[0]); + + status = -1; } if (output) { diff --git a/common/systeminfo.h b/common/systeminfo.h index fa2be95..62c9a7f 100644 --- a/common/systeminfo.h +++ b/common/systeminfo.h @@ -17,4 +17,8 @@ int is_directory(const char *path); pid_t fork_process(const char *cmd, const char **args, const char *wd); +/* returns 1 on success 0 on timeout and -1 on failure to get exit code + */ +int wait_for_process(pid_t pid, int max_time, int *errcode); + #endif /* SYSTEMINFO_H */ diff --git a/explorer/systeminfo.c b/explorer/systeminfo.c index 2c69472..4c98c79 100644 --- a/explorer/systeminfo.c +++ b/explorer/systeminfo.c @@ -166,3 +166,23 @@ pid_t fork_process(const char *cmd, const char **args, const char *wd) { return mingw_spawnvpe_cwd(cmd, args, env_for_git(), wd); } + +int wait_for_process(pid_t pid, int max_time, int *errcode) +{ + *errcode = 0; + DWORD status; + + if (WAIT_OBJECT_0 == WaitForSingleObject((HANDLE)pid, + max_time)) { + if (GetExitCodeProcess((HANDLE)pid, &status)) { + *errcode = status; + debug_git("Exit code: %d", status); + }else { + /* play safe, and return total failure */ + *errcode = GetLastError(); + return -1; + } + return 1; + } + return 0; +} -- 2.11.4.GIT