aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYang Tse <yangsita@gmail.com>2009-11-30 13:48:51 +0000
committerYang Tse <yangsita@gmail.com>2009-11-30 13:48:51 +0000
commita72ce23f16ddb521bfae45519a1f6ce26a9ade08 (patch)
treec4b707d19c66e43545223e79605218cb31077ecf
parentbfae1bd9998688ead5f6c3e3e6a6766ab2ad8f76 (diff)
- In order to better reflect that the returned pid is extracted from the
given file, serverpid sub is renamed to pidfromfile. In addition it is enhanced to make sure that it always returns zero unless a numerical positive value is returned. - To better reflect that only process existance is actually checked, checkserver sub is renamed to processexists. In addition it is enhanced making it remove the given pid file when the extracted pid is no longer alive.
-rw-r--r--tests/ftp.pm54
-rw-r--r--tests/ftpserver.pl2
-rwxr-xr-xtests/runtests.pl14
3 files changed, 44 insertions, 26 deletions
diff --git a/tests/ftp.pm b/tests/ftp.pm
index 421fee1a6..eed64903e 100644
--- a/tests/ftp.pm
+++ b/tests/ftp.pm
@@ -5,7 +5,7 @@
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
-# Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
+# Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
@@ -22,30 +22,48 @@
###########################################################################
#######################################################################
-# Return the pid of the server as found in the given pid file
+# pidfromfile returns the pid stored in the given pidfile. The value
+# of the returned pid will never be a negative value. It will be zero
+# on any file related error or if a pid can not be extracted from the
+# given file.
#
-sub serverpid {
- my $PIDFILE = $_[0];
- open(PFILE, "<$PIDFILE");
- my $PID=0+<PFILE>;
- close(PFILE);
- return $PID;
+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;
}
#######################################################################
-# Check the given test server if it is still alive.
+# 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
+# error or if a pid can not be extracted from the given file. When a
+# process with the same pid as the one extracted from the given file
+# is currently alive this returns that positive pid. Otherwise, when
+# the process is not alive, will return the negative value of the pid.
#
-sub checkserver {
- my ($pidfile)=@_;
- my $pid=0;
-
- # check for pidfile
- if ( -f $pidfile ) {
- $pid=serverpid($pidfile);
- if ($pid ne "" && kill(0, $pid)) {
+sub processexists {
+# use POSIX ":sys_wait_h";
+ my $pidfile = $_[0];
+
+ # fetch pid from pidfile
+ my $pid = pidfromfile($pidfile);
+
+ if($pid > 0) {
+ # verify if currently alive
+ if(kill(0, $pid)) {
return $pid;
}
else {
+ # reap it if this has not already been done
+ # waitpid($pid, &WNOHANG);
+ # get rid of the certainly invalid pidfile
+ unlink($pidfile) if($pid == pidfromfile($pidfile));
return -$pid; # negative means dead process
}
}
@@ -60,7 +78,7 @@ sub ftpkillslave {
my $base;
for $base (('filt', 'data')) {
my $f = ".sock$base$id$ext.pid";
- my $pid = checkserver($f);
+ my $pid = processexists($f);
if($pid > 0) {
printf ("* kill pid for %s => %d\n", "ftp-$base$id$ext", $pid) if($verbose);
kill (9, $pid); # die!
diff --git a/tests/ftpserver.pl b/tests/ftpserver.pl
index ddbc57430..c1b53cdfc 100644
--- a/tests/ftpserver.pl
+++ b/tests/ftpserver.pl
@@ -577,7 +577,7 @@ sub PASV_command {
my $pasvport;
my $pidf=".sockdata$ftpdnum$ext.pid";
- my $prev = checkserver($pidf);
+ my $prev = processexists($pidf);
if($prev > 0) {
print "kill existing server: $prev\n" if($verbose);
kill(9, $prev);
diff --git a/tests/runtests.pl b/tests/runtests.pl
index be20a63ab..834c86652 100755
--- a/tests/runtests.pl
+++ b/tests/runtests.pl
@@ -828,7 +828,7 @@ sub runhttpserver {
return (0,0);
}
- my $pid = checkserver($pidfile);
+ my $pid = processexists($pidfile);
if($pid > 0) {
stopserver($pid);
}
@@ -898,7 +898,7 @@ sub runhttpsserver {
return (0,0);
}
- my $pid = checkserver($pidfile);
+ my $pid = processexists($pidfile);
if($pid > 0) {
# kill previous stunnel!
stopserver($pid);
@@ -968,7 +968,7 @@ sub runftpserver {
return (0,0);
}
- my $pid = checkserver($pidfile);
+ my $pid = processexists($pidfile);
if($pid > 0) {
stopserver($pid);
}
@@ -1045,7 +1045,7 @@ sub runftpsserver {
return (0,0);
}
- my $pid = checkserver($pidfile);
+ my $pid = processexists($pidfile);
if($pid > 0) {
# kill previous stunnel!
stopserver($pid);
@@ -1114,7 +1114,7 @@ sub runtftpserver {
return (0,0);
}
- my $pid = checkserver($pidfile);
+ my $pid = processexists($pidfile);
if($pid > 0) {
stopserver($pid);
}
@@ -1179,7 +1179,7 @@ sub runsshserver {
return (0,0);
}
- my $pid = checkserver($pidfile);
+ my $pid = processexists($pidfile);
if($pid > 0) {
stopserver($pid);
}
@@ -1254,7 +1254,7 @@ sub runsocksserver {
return (0,0);
}
- my $pid = checkserver($pidfile);
+ my $pid = processexists($pidfile);
if($pid > 0) {
stopserver($pid);
}