From: Michael Tremer Date: Tue, 9 Aug 2022 16:32:48 +0000 (+0000) Subject: command: Return exit code of last child process X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e4a0565a843be819fdf7e159cc960e211670cf66;p=people%2Fstevee%2Fpakfire.git command: Return exit code of last child process Signed-off-by: Michael Tremer --- diff --git a/tests/stub/command.c b/tests/stub/command.c index 29a24559..3f07dbd0 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[]) {