# to the classic approach of using select(undef, undef, undef, ...).
# even though that one is not portable due to being implemented using
# select on Windows: https://perldoc.perl.org/perlport.html#select
-# On Windows it also just uses full-second sleep for waits >1 second.
+# Therefore it uses Win32::Sleep on Windows systems instead.
#
sub portable_sleep {
my ($seconds) = @_;
}
#######################################################################
-# pidkill kills the process with a given pid mercilessly andforcefully.
+# pidkill kills the process with a given pid mercilessly and forcefully.
#
sub pidkill {
my $pid = $_[0];
}
}
+#######################################################################
+# pidwait waits for the process with a given pid to be terminated.
+#
+sub pidwait {
+ my $pid = $_[0];
+ my $flags = $_[1];
+
+ # check if the process exists
+ if ($pid > 65536 && os_is_win()) {
+ if($flags == &WNOHANG) {
+ return pidexists($pid)?0:$pid;
+ }
+ while(pidexists($pid)) {
+ portable_sleep(0.01);
+ }
+ return $pid;
+ }
+
+ # wait on the process to terminate
+ return waitpid($pid, $flags);
+}
+
#######################################################################
# processexists checks if a process with the pid stored in the given
# pidfile exists and is alive. This will return 0 on any file related
# get rid of the certainly invalid pidfile
unlink($pidfile) if($pid == pidfromfile($pidfile));
# reap its dead children, if not done yet
- waitpid($pid, &WNOHANG);
+ pidwait($pid, &WNOHANG);
# negative return value means dead process
return -$pid;
}
print("RUN: Process with pid $pid already dead\n")
if($verbose);
# if possible reap its dead children
- waitpid($pid, &WNOHANG);
+ pidwait($pid, &WNOHANG);
push @reapchild, $pid;
}
}
if($verbose);
splice @signalled, $i, 1;
# if possible reap its dead children
- waitpid($pid, &WNOHANG);
+ pidwait($pid, &WNOHANG);
push @reapchild, $pid;
}
}
if($verbose);
pidkill($pid);
# if possible reap its dead children
- waitpid($pid, &WNOHANG);
+ pidwait($pid, &WNOHANG);
push @reapchild, $pid;
}
}
if(@reapchild) {
foreach my $pid (@reapchild) {
if($pid > 0) {
- waitpid($pid, 0);
+ pidwait($pid, 0);
}
}
}
printf("* kill pid for %s-%s => %d\n", $server,
($proto eq 'ftp')?'ctrl':'filt', $pid) if($verbose);
pidkill($pid);
- waitpid($pid, 0);
+ pidwait($pid, 0);
}
unlink($pidfile) if(-f $pidfile);
}
printf("* kill pid for %s-data => %d\n", $server,
$pid) if($verbose);
pidkill($pid);
- waitpid($pid, 0);
+ pidwait($pid, 0);
}
unlink($pidfile) if(-f $pidfile);
}