aboutsummaryrefslogtreecommitdiff
path: root/user/usr/bin/tests/forkbomb.c
blob: 543990c8ada33e52b6e520c60059625a4364dd2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* TODO add options for different ways of forkbombing
   (kind of low priority but would be fun) */

int main(int argc, char **argv)
{
    int n = 1;
    pid_t pid;

    open("/dev/tty0", O_RDONLY, 0);
    open("/dev/tty0", O_WRONLY, 0);
    printf("Forking up a storm!\n");
    printf("If this runs for 10 minutes without crashing, then you ");
    printf("probably aren't \nleaking resources\n");

    if (fork())
    {
        for (;;)
        {
            printf("Fork number : %d\n", n);
            if ((pid = fork()))
            {
                if (pid == -1)
                {
                    printf("Fork %d failed. Forkbomb stopping.\n", n);
                    exit(1);
                }
                int status;
                sched_yield();
                wait(&status);
                if (status != 0)
                {
                    printf("Test failed. Child exit with status %d\n", status);
                    exit(1);
                }
            }
            else
            {
                int a = 0;
                sched_yield();
                exit(0);
            }
            n++;
        }
    }

#if 0
// Old forkbomb 
    if (!fork())
    {
        for (;;)
        {
            printf("I am fork number %d\n", n);
            if ((pid = fork()))
            {
                if (-1 != pid)
                {
                    exit(0);
                }
                else
                {
                    printf(
                        "%d-th fork failed. "
                        "forkbomb stopping.",
                        n);
                    exit(1);
                }
            }
            ++n;
        }
    }
    else
    {
        int status;
        while (wait(&status) > 0)
            ;
    }
#endif
    return 0;
}