1 // $Id: forkbomb.c,v 1.2 2003/12/26 00:48:26 uid68581 Exp $
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on tests/forkbomb.cc by Jacques Gelinas
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
28 typedef enum {MODE_SLEEP,MODE_LOOP,MODE_FORK, MODE_FORKSHELL} MODE;
31 static void forkbomb_userfork (MODE mode)
35 fprintf (stderr,"Fork failed (%s)\n",strerror(errno));
37 if (mode == MODE_SLEEP){
39 }else if (mode == MODE_LOOP){
42 }else if (mode == MODE_FORKSHELL){
43 system ("/bin/false");
50 int main (int argc, char *argv[])
53 fprintf (stderr,"formboom N M mode\n"
54 "where N is the number of process to start\n"
55 "and M is the number of user to start\n"
56 "Each user will try to start N process\n"
59 " sleep: Each process sleeps for 20 seconds and exits\n"
60 " loop: Each process loops forever\n"
61 " fork: Each process exits immediatly and is restarted\n"
63 " forkshell: Each process runs /bin/false in a shell and\n"
64 " exits, then the parent start a new one\n"
69 if (strcmp(argv[3],"sleep")==0){
71 }else if (strcmp(argv[3],"loop")==0){
73 }else if (strcmp(argv[3],"fork")==0){
75 }else if (strcmp(argv[3],"forkshell")==0){
76 mode = MODE_FORKSHELL;
78 fprintf (stderr,"Invalid mode\n");
81 for (i=0; i<atoi(argv[2]); i++){
83 if (setuid (i+1)==-1){
84 fprintf (stderr,"Can't setuid to uid %d (%s)\n",i+1
88 for (j=0; j<atoi(argv[1]); j++){
89 forkbomb_userfork (mode);
91 if (mode == MODE_FORK || mode == MODE_FORKSHELL){
92 // Ok, all processes are started, in MODE_FORK
93 // we create a new one all the time
95 while (wait(&status)!=-1) forkbomb_userfork(mode);
101 system ("ps ax | wc -l");
102 printf ("All the process are running now\n");
103 printf ("Exit to end all processes\n");
105 system ("killall forkbomb");