use strict;
use warnings;
use serverhelp qw(
servername_id
mainsockf_pidfilename
datasockf_pidfilename
);
sub pidfromfile {
my $pidfile = $_[0];
my $pid = 0;
if(-f $pidfile && -s $pidfile && open(PIDFH, "<$pidfile")) {
$pid = 0 + <PIDFH>;
close(PIDFH);
$pid = 0 unless($pid > 0);
}
return $pid;
}
sub pidexists {
my $pid = $_[0];
if($pid > 0) {
if(kill(0, $pid)) {
return $pid;
}
if($^O eq "msys") {
my $filter = "PID eq $pid";
my $result = `tasklist -fi \"$filter\" 2>nul`;
if(index($result, "$pid") != -1) {
return -$pid;
}
}
}
return 0;
}
sub pidterm {
my $pid = $_[0];
if($pid > 0) {
kill("TERM", $pid);
if($^O eq "msys") {
my $filter = "PID eq $pid";
my $result = `tasklist -fi \"$filter\" 2>nul`;
if(index($result, "$pid") != -1) {
system("taskkill -fi \"$filter\" >nul 2>&1");
}
}
}
}
sub pidkill {
my $pid = $_[0];
if($pid > 0) {
kill("KILL", $pid);
if($^O eq "msys") {
my $filter = "PID eq $pid";
my $result = `tasklist -fi \"$filter\" 2>nul`;
if(index($result, "$pid") != -1) {
system("taskkill -f -fi \"$filter\" >nul 2>&1");
system("tskill $pid >nul 2>&1");
}
}
}
}
sub processexists {
use POSIX ":sys_wait_h";
my $pidfile = $_[0];
my $pid = pidfromfile($pidfile);
if($pid > 0) {
if(pidexists($pid)) {
return $pid;
}
else {
unlink($pidfile) if($pid == pidfromfile($pidfile));
waitpid($pid, &WNOHANG);
return -$pid;
}
}
return 0;
}
sub killpid {
use POSIX ":sys_wait_h";
my ($verbose, $pidlist) = @_;
my @requested;
my @signalled;
my @reapchild;
return if(not defined($pidlist));
@requested = split(' ', $pidlist);
return if(not @requested);
if(scalar(@requested) > 2) {
@requested = sort({$a <=> $b} @requested);
}
for(my $i = scalar(@requested) - 2; $i >= 0; $i--) {
if($requested[$i] == $requested[$i+1]) {
splice @requested, $i+1, 1;
}
}
foreach my $tmp (@requested) {
chomp $tmp;
if($tmp =~ /^(\d+)$/) {
my $pid = $1;
if($pid > 0) {
if(pidexists($pid)) {
print("RUN: Process with pid $pid signalled to die\n")
if($verbose);
pidterm($pid);
push @signalled, $pid;
}
else {
print("RUN: Process with pid $pid already dead\n")
if($verbose);
waitpid($pid, &WNOHANG);
push @reapchild, $pid;
}
}
}
}
if(@signalled) {
my $twentieths = 5 * 20;
while($twentieths--) {
for(my $i = scalar(@signalled) - 1; $i >= 0; $i--) {
my $pid = $signalled[$i];
if(!pidexists($pid)) {
print("RUN: Process with pid $pid gracefully died\n")
if($verbose);
splice @signalled, $i, 1;
waitpid($pid, &WNOHANG);
push @reapchild, $pid;
}
}
last if(not scalar(@signalled));
select(undef, undef, undef, 0.05);
}
}
if(@signalled) {
foreach my $pid (@signalled) {
if($pid > 0) {
print("RUN: Process with pid $pid forced to die with SIGKILL\n")
if($verbose);
pidkill($pid);
waitpid($pid, &WNOHANG);
push @reapchild, $pid;
}
}
}
if(@reapchild) {
foreach my $pid (@reapchild) {
if($pid > 0) {
waitpid($pid, 0);
}
}
}
}
sub killsockfilters {
my ($proto, $ipvnum, $idnum, $verbose, $which) = @_;
my $server;
my $pidfile;
my $pid;
return if($proto !~ /^(ftp|imap|pop3|smtp)$/);
die "unsupported sockfilter: $which"
if($which && ($which !~ /^(main|data)$/));
$server = servername_id($proto, $ipvnum, $idnum) if($verbose);
if(!$which || ($which eq 'main')) {
$pidfile = mainsockf_pidfilename($proto, $ipvnum, $idnum);
$pid = processexists($pidfile);
if($pid > 0) {
printf("* kill pid for %s-%s => %d\n", $server,
($proto eq 'ftp')?'ctrl':'filt', $pid) if($verbose);
pidkill($pid);
waitpid($pid, 0);
}
unlink($pidfile) if(-f $pidfile);
}
return if($proto ne 'ftp');
if(!$which || ($which eq 'data')) {
$pidfile = datasockf_pidfilename($proto, $ipvnum, $idnum);
$pid = processexists($pidfile);
if($pid > 0) {
printf("* kill pid for %s-data => %d\n", $server,
$pid) if($verbose);
pidkill($pid);
waitpid($pid, 0);
}
unlink($pidfile) if(-f $pidfile);
}
}
sub killallsockfilters {
my $verbose = $_[0];
for my $proto (('ftp', 'imap', 'pop3', 'smtp')) {
for my $ipvnum (('4', '6')) {
for my $idnum (('1', '2')) {
killsockfilters($proto, $ipvnum, $idnum, $verbose);
}
}
}
}
sub set_advisor_read_lock {
my ($filename) = @_;
if(open(FILEH, ">$filename")) {
close(FILEH);
return;
}
printf "Error creating lock file $filename error: $!";
}
sub clear_advisor_read_lock {
my ($filename) = @_;
if(-f $filename) {
unlink($filename);
}
}
1;