From 727562e99082e9ed808b5ff6e02b65b9cf5b65c3 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 12 Sep 2018 00:12:28 -0400 Subject: exec: Don't leave zombies around if the child fails to exec() --- exec.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'exec.c') diff --git a/exec.c b/exec.c index 87bb5e6..3cb05f4 100644 --- a/exec.c +++ b/exec.c @@ -355,12 +355,14 @@ static int bfs_exec_spawn(const struct bfs_exec *execbuf) { // Parent close(pipefd[1]); - int error; + int ret, error; ssize_t nbytes = read(pipefd[0], &error, sizeof(error)); close(pipefd[0]); if (nbytes == sizeof(error)) { - errno = error; - return -1; + ret = -1; + } else { + ret = 0; + error = 0; } int wstatus; @@ -368,23 +370,22 @@ static int bfs_exec_spawn(const struct bfs_exec *execbuf) { return -1; } - int ret = -1; - if (WIFEXITED(wstatus)) { int status = WEXITSTATUS(wstatus); - if (status == EXIT_SUCCESS) { - ret = 0; - } else { + if (status != EXIT_SUCCESS) { bfs_exec_debug(execbuf, "Command '%s' failed with status %d\n", execbuf->argv[0], status); + ret = -1; } } else if (WIFSIGNALED(wstatus)) { int sig = WTERMSIG(wstatus); bfs_exec_debug(execbuf, "Command '%s' terminated by signal %d\n", execbuf->argv[0], sig); + ret = -1; } else { bfs_exec_debug(execbuf, "Command '%s' terminated abnormally\n", execbuf->argv[0]); + ret = -1; } - errno = 0; + errno = error; return ret; } else { // Child @@ -407,7 +408,6 @@ static int bfs_exec_spawn(const struct bfs_exec *execbuf) { close(pipefd[1]); _Exit(EXIT_FAILURE); } - } /** exec() a command for a single file. */ -- cgit v1.2.3