From 974be684768d2c337a277e70ab44efc04cd1e4b9 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Thu, 10 Sep 2009 10:27:00 +0200 Subject: tests/webserver: rework locking of pid file. Michael --- tests/scripts/webserver.pl | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/tests/scripts/webserver.pl b/tests/scripts/webserver.pl index ac029bd..99f6c40 100755 --- a/tests/scripts/webserver.pl +++ b/tests/scripts/webserver.pl @@ -171,15 +171,37 @@ sub reopen_logs() { } sub get_pid_lock() { - open LOCKFILE, "> $pid_file" or - die "Error opening pid-file $pid_file: $!"; + # first make sure the file exists + open(LOCKFILE_W, ">> $pid_file") or + die "Error opening pid file '$pid_file' for writing: $!"; + + # open for reading and try to lock: + open(LOCKFILE, "< $pid_file") or + die "Error opening pid file '$pid_file' for reading: $!"; unless (flock(LOCKFILE, LOCK_EX|LOCK_NB)) { - my $other_pid = qx(cat $pid_file); - print "Webserver is already running (pid $other_pid)"; + print "pid file '$pid_file' is already locked.\n"; + my $other_pid = ; + if (!defined($other_pid)) { + print "Error reading from pid file.\n"; + } else { + chomp($other_pid); + if (!$other_pid) { + print "pid file is empty.\n"; + } else { + print "Webserver is already running with pid '$other_pid'.\n"; + } + } + close LOCKFILE; exit(0); } - print LOCKFILE "$$"; + # now re-open for recreating the file and write our pid + close(LOCKFILE_W); + open(LOCKFILE_W, "> $pid_file") or + die "Error opening pid file '$pid_file' for writing: $!"; + LOCKFILE_W->autoflush(1); + print LOCKFILE_W "$$"; + close(LOCKFILE_W); } sub release_pid_lock() { -- cgit v1.2.3