diff options
author | Michael Adam <obnox@samba.org> | 2009-09-10 10:27:00 +0200 |
---|---|---|
committer | Michael Adam <obnox@samba.org> | 2009-09-10 11:08:17 +0200 |
commit | 974be684768d2c337a277e70ab44efc04cd1e4b9 (patch) | |
tree | a2bdfbd66795ff099b0e51f9c04fbeb8d9d22345 | |
parent | 449af292dc48b89e266c2a74c8110947c3db2ae2 (diff) |
tests/webserver: rework locking of pid file.
Michael
-rwxr-xr-x | tests/scripts/webserver.pl | 32 |
1 files 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 = <LOCKFILE>; + 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() { |