Skip to content

Commit

Permalink
exec: Don't leave zombies around if the child fails to exec()
Browse files Browse the repository at this point in the history
  • Loading branch information
tavianator committed Sep 12, 2018
1 parent fc76667 commit 727562e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,36 +355,37 @@ 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;
if (waitpid(pid, &wstatus, 0) < 0) {
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
Expand All @@ -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. */
Expand Down

0 comments on commit 727562e

Please sign in to comment.