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
|
#include "../config.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( int argc, char * argv[] )
{
// Create a "fork bomb", but with finite scope.
// argv[1] should be the depth
// argv[2]='r' means that we should recurse (creating 2^argv[1] processes, instead of just argv[1])
if( argc<2 )
return 1;
int depth=0;
int target=atoi(argv[1]);
bool recurse=argc>=3 && argv[2][0]=='r';
int num_children=0;
int serial=1;
int origserial=1;
bool stop=false;
while( !stop && depth<target ) {
pid_t child=fork();
depth++;
if( recurse )
serial*=2;
if( child<0 ) {
fprintf(stderr, "Fork failed for process %d (child %d): %s\n", getpid(), serial, strerror(errno) );
break;
} else if( child==0 ) {
// We are the child
serial++;
origserial=serial;
num_children=0;
} else {
num_children++;
if( !recurse ) {
stop=true;
}
}
}
printf("Pid %d is child %d running with uid %d\n", getpid(), origserial, getuid());
for( int i=0; i<num_children; ++i ) {
int status;
pid_t child=wait(&status);
printf("Child %d terminated\n", child);
}
return 0;
}
|