#!/usr/bin/perl 
#
# exorcist2.pl - A handy tool for testing disk and I/O-subsystem reliability
#
# Original concept by Michael Glad <glad@daimi.aau.dk>
# Version 2.0 by Martin Kasper Petersen <mkp@SunSITE.auc.dk>
#
# Usage: exorcist2.pl <processes> <dirs> <files> <filename>
#
# Example: exorcist2.pl 16 32 64 /boot/vmlinux.gz
#
# Creates 32 dirs with 64 copies of /boot/vmlinux.gz in each and forks 
# 16 processes to kill/recreate random files.
#

sub holy_water { 
  srand($$); 
  while(1) { 
    $victimdir = int(rand($dirs)); 
    $victimfile = int(rand($files));
    $file=sprintf("EXORCIST/dir-%02d/file-%02d", $victimdir, $victimfile); 
    print "Priest \#$$: Exorcising victim: $file\n";
    system("cp $file /dev/null"); 
    system("rm -f $file"); 
    system("cp $name $file"); 
  } 
}; 

$procs=$ARGV[0]; $dirs=$ARGV[1]; $files=$ARGV[2]; $name=$ARGV[3];

mkdir("EXORCIST", 0755);

for ($i=0 ; $i < $dirs ; $i++) {
  mkdir(sprintf("EXORCIST/dir-%02d", $i), 0755);
  
  for ($j=0 ; $j < $files ; $j++) {
    $file=sprintf("EXORCIST/dir-%02d/file-%02d", $i, $j);
    print "High priest preparing: $file\n";
    system("cp $name $file");
  };
};

print "High priest resigning. Let the exorcism begin...\n"; 

for ($k=0; $k < $procs ; $k++) { 
  $pid = fork; 
  if($pid == 0) { 
    &holy_water(); 
    exit(0); 
  } 
} 

sleep(1000000000); 

# EOF
