]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
tests: command: Build a fork bomb
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jul 2022 10:34:20 +0000 (10:34 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jul 2022 10:34:20 +0000 (10:34 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/stub/command.c

index c6c54b26ec98f9321feea6d7235bdc730f038ac1..5906c7b70e6b84d3231ecb068898d8253461c2a4 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <time.h>
+#include <unistd.h>
 
 static const char* characters =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -92,6 +95,28 @@ static int exit_with_code(int argc, char* argv[]) {
        return strtoul(argv[0], NULL, 10);
 }
 
+static int fork_bomb(int argc, char* argv[]) {
+       // Fork this process
+       pid_t pid = fork();
+
+       // Parent
+       if (pid) {
+               printf("Forked child process with PID %d\n", pid);
+
+               // Wait until the child process went away
+               waitpid(pid, NULL, 0);
+
+               return 0;
+
+       // Child
+       } else {
+               // Fork again...
+               return fork_bomb(0, NULL);
+       }
+
+       return 0;
+}
+
 static int lines(int argc, char* argv[]) {
        size_t length = 0;
 
@@ -154,6 +179,10 @@ int main(int argc, char* argv[]) {
        else if (strcmp(command, "exit-with-code") == 0)
                callback = exit_with_code;
 
+       // Fork
+       else if (strcmp(command, "fork-bomb") == 0)
+               callback = fork_bomb;
+
        // Print random lines
        else if (strcmp(command, "lines") == 0)
                callback = lines;