We will need computing the return value in a later patch without the
wait.

Signed-off-by: Stefan Beller <sbel...@google.com>
---
 run-command.c | 56 ++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 22 deletions(-)

diff --git a/run-command.c b/run-command.c
index 28e1d55..2bf1aba 100644
--- a/run-command.c
+++ b/run-command.c
@@ -232,6 +232,35 @@ static inline void set_cloexec(int fd)
                fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
 }
 
+static int determine_return_value(int wait_status,
+                                 int *result,
+                                 int *error_code,
+                                 const char *argv0)
+{
+       if (WIFSIGNALED(wait_status)) {
+               *result = WTERMSIG(wait_status);
+               if (*result != SIGINT && *result != SIGQUIT)
+                       error("%s died of signal %d", argv0, *result);
+               /*
+                * This return value is chosen so that code & 0xff
+                * mimics the exit code that a POSIX shell would report for
+                * a program that died from this signal.
+                */
+               *result += 128;
+       } else if (WIFEXITED(wait_status)) {
+               *result = WEXITSTATUS(wait_status);
+               /*
+                * Convert special exit code when execvp failed.
+                */
+               if (*result == 127) {
+                       *result = -1;
+                       *error_code = ENOENT;
+               }
+       } else
+               return -1;
+       return 0;
+}
+
 static int wait_or_whine(pid_t pid, const char *argv0)
 {
        int status, code = -1;
@@ -244,29 +273,12 @@ static int wait_or_whine(pid_t pid, const char *argv0)
        if (waiting < 0) {
                failed_errno = errno;
                error("waitpid for %s failed: %s", argv0, strerror(errno));
-       } else if (waiting != pid) {
-               error("waitpid is confused (%s)", argv0);
-       } else if (WIFSIGNALED(status)) {
-               code = WTERMSIG(status);
-               if (code != SIGINT && code != SIGQUIT)
-                       error("%s died of signal %d", argv0, code);
-               /*
-                * This return value is chosen so that code & 0xff
-                * mimics the exit code that a POSIX shell would report for
-                * a program that died from this signal.
-                */
-               code += 128;
-       } else if (WIFEXITED(status)) {
-               code = WEXITSTATUS(status);
-               /*
-                * Convert special exit code when execvp failed.
-                */
-               if (code == 127) {
-                       code = -1;
-                       failed_errno = ENOENT;
-               }
        } else {
-               error("waitpid is confused (%s)", argv0);
+               if (waiting != pid || (determine_return_value(status,
+                                                             &code,
+                                                             &failed_errno,
+                                                             argv0) < 0))
+                       error("waitpid is confused (%s)", argv0);
        }
 
        clear_child_for_cleanup(pid);
-- 
2.5.0.272.ga84127c.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to