From e4a0565a843be819fdf7e159cc960e211670cf66 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 9 Aug 2022 16:32:48 +0000 Subject: [PATCH] command: Return exit code of last child process Signed-off-by: Michael Tremer --- tests/stub/command.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/stub/command.c b/tests/stub/command.c index 29a245594..3f07dbd02 100644 --- a/tests/stub/command.c +++ b/tests/stub/command.c @@ -126,25 +126,31 @@ static int exit_with_code(int argc, char* argv[]) { } static int fork_bomb(int argc, char* argv[]) { + int status = 0; + // Fork this process pid_t pid = fork(); + // Catch any errors + if (pid < 0) { + fprintf(stderr, "Could not fork(): %m\n"); + exit(EXIT_FAILURE); + // Parent - if (pid) { + } else if (pid) { printf("Forked child process with PID %d\n", pid); // Wait until the child process went away - waitpid(pid, NULL, 0); + waitpid(pid, &status, 0); - return 0; + // Pass on the exit code + return WEXITSTATUS(status); // Child } else { // Fork again... return fork_bomb(0, NULL); } - - return 0; } static int lines(int argc, char* argv[]) { -- 2.47.3