]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
command: Return exit code of last child process
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Aug 2022 16:32:48 +0000 (16:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Aug 2022 16:32:48 +0000 (16:32 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/stub/command.c

index 29a2455941d527e394770def611c4f3162015530..3f07dbd02163aa9c8b1200c8ee455d432fabb11e 100644 (file)
@@ -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[]) {