Skip to content

Commit

Permalink
try another way to clean up child processes of pingsweep
Browse files Browse the repository at this point in the history
  • Loading branch information
ollyg committed Apr 30, 2024
1 parent 59ba4a6 commit 7022afd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions Build.PL
Expand Up @@ -80,6 +80,7 @@ Module::Build->new(
'Plack::Middleware::Headers' => '0',
'Plack::Middleware::ReverseProxy' => '0.15',
'Pod::Usage' => 0,
'Proc::ProcessTable' => '0.636',
'Regexp::Common' => 2017060201,
'Regexp::Common::net::CIDR' => 0,
'Role::Tiny' => '1.002005',
Expand Down
18 changes: 18 additions & 0 deletions lib/App/Netdisco/Worker/Plugin/PingSweep.pm
Expand Up @@ -10,6 +10,7 @@ use Time::HiRes;
use Sys::SigAction 'timeout_call';
use Net::Ping;
use Net::Ping::External;
use Proc::ProcessTable;
use NetAddr::IP qw/:rfc3021 :lower/;

register_worker({ phase => 'main' }, sub {
Expand All @@ -35,13 +36,30 @@ register_worker({ phase => 'main' }, sub {
};

$SIG{CHLD} = 'IGNORE';
debug sprintf 'I am PID %s', $$;

foreach my $idx (0 .. $net->num()) {
my $addr = $net->nth($idx) or next;
my $host = $addr->addr;

if (timeout_call($timeout, $pinger, $host)) {
debug sprintf 'pinged %s and timed out', $host;
my $t = Proc::ProcessTable->new;
foreach my $p ( @{$t->table} ) {
if ($p->ppid() and $p->ppid() == $$) {
my $pid = $p->pid();

foreach my $c ( @{$t->table} ) {
if ($c->ppid() and $c->ppid() == $pid) {
debug sprintf 'killing fork %s (%s) of %s', $c->pid(), $c->cmndline(), $p->pid();
kill 1, $c->pid();
}
}

debug sprintf 'killing fork %s (%s) of %s', $p->pid(), $p->cmndline(), $$;
kill 1, $p->pid();
}
}
next;
}

Expand Down

0 comments on commit 7022afd

Please sign in to comment.