$SIG{INT} = \&ctrlc; #$SIG{CHLD} = \&ripZombie; foreach(@ARGV){ my $arg=$_; unless(defined($arg)){ die("$!\n") if(help()); exit 0; } if($arg eq '-e' || $arg eq '--erase'){ die("$!\n") if(erase()); } elsif($arg eq '-i' || $arg eq '--info'){ die("$!\n") if(versionInfos()); } elsif($arg eq '-c' || $arg eq '--create'){ die("$!\n") if(createData()); } elsif($arg eq '-p' || $arg eq '--proceed'){ die("$!\n") if(continueDataCreation()); } elsif($arg eq '-v' || $arg eq '--verify'){ die("$!\n") if(verifyData()); } elsif($arg eq '-s' || $arg eq '--search'){ die("$!\n") if(searchExtInfos()); } elsif($arg eq '-h' || $arg eq '--help'){ die("$!\n") if(help()); } elsif($arg eq '-x' || $arg eq '--extract'){ die("$!\n") if(fallbackExtractResults()); } else{ die("$!\n") if(help()); } } sub createData{ ### Set default sig-handler for ctrl+c ### $SIG{INT} = "DEFAULT"; ... my @a_childs=(); ... ### Set custom sig handler for ctrl+c ### $SIG{INT} = \&ctrlc; ### Begin forking regarding amount of instances ### for(my $i=0; $i<$AMOUNT_INSTANCES; $i++){ my $child=fork(); if($child){ ### parent ### push(@a_childs, $child); } elsif($child == 0){ ### child ### ... ### Build BerkeleyDB Environment ### my $envChild = new BerkeleyDB::Env( -Home => "$WORKDIR" , -Flags => DB_CREATE| DB_INIT_CDB | DB_INIT_MPOOL) or die "FAILURE: Cannot open environment: $BerkeleyDB::Error\n"; ### Tie Hash ### our $DB_RESULT = tie %H_RESULT, 'BerkeleyDB::Hash', -Filename => "$FILE_RESULT_DB", -Flags => DB_CREATE, -Env => $envChild or die "FAILURE: Cannot open database: $BerkeleyDB::Error\n"; ### Tie Hash for IP-pref-pairs ### my $db_prefChild = tie %H_PREF, 'BerkeleyDB::Hash', -Filename => "$FILE_PREF_DB", -Flags => DB_CREATE, -Env => $envChild or die "FAILURE: Cannot open database: $BerkeleyDB::Error\n"; ### only copies some files to certain positions ### system("..."); ### Query responsible RIR and extract information ### while(keys(%H_PREF) > 0){ ### Lock hash ### my $lock=$db_prefChild->cds_lock(); ### extract random key and mark it ### my $key=""; my $value=""; do{ $key=(keys %H_PREF)[rand keys %H_PREF]; $value=$H_PREF{$key}; }while($value =~ m/^inuse\_/); $H_PREF{$key}="inuse\_$value"; ### Unlock hash ### $lock->cds_unlock(); ### this funktion does only some regex work and system-calls, at begin and end the var set by sig handler is checked ### handlePref($key,$value); ### Check if SIG{INT} (CTRL+C) was pushed ### if($CHECK_CTRL == 1 ){ $lock=$db_prefChild->cds_lock(); $H_PREF{$key}=$value; $lock->cds_unlock(); last; } delete($H_PREF{$key}); } warn("$!\n") if(cleanUpChild($childPid)); ### Clean Up BerkeleyDB Env and untie ## undef $DB_RESULT; untie %H_RESULT; undef $db_prefChild; untie %H_PREF; exit 0; } else{ return 1, warn("FAILURE: Could not fork! $!\n"); } } ### Wait/catch all childs ### foreach(@a_childs){ while(){ my $pid=waitpid(-1, WNOHANG); last if($pid <= 0); } print "Child ended successfully (PID: $_)!\n"; } #foreach(@a_childs){ # wait(); # waitpid($_,0); #} ... return 0; } ### sig handler for ctrl-c ### sub ctrlc{ $SIG{INT} = \&ctrlc; ### Set global check variable to 1 ### $CHECK_CTRL=1; print("! Please be patient, programm is existing but will end all current queries and this could take some minutes !\n"); return 0; } ### reaper but not in use ### sub ripZombie{ my $pid; while ((my $pid = waitpid(-1, WNOHANG)) > 0) { ### If you want to do sth with your reaped child pids ### ; } $SIG{CHLD} = \&ripZombie; }