From: Michael Tremer Date: Thu, 21 Jul 2022 10:34:20 +0000 (+0000) Subject: tests: command: Build a fork bomb X-Git-Tag: 0.9.28~655 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=88d74057e0361e768bb3677b841eb0d2d93f2c5d;p=pakfire.git tests: command: Build a fork bomb Signed-off-by: Michael Tremer --- diff --git a/tests/stub/command.c b/tests/stub/command.c index c6c54b26e..5906c7b70 100644 --- a/tests/stub/command.c +++ b/tests/stub/command.c @@ -21,7 +21,10 @@ #include #include #include +#include +#include #include +#include 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;