diff --git a/lib/Daemon.pm b/lib/Daemon.pm index 28664693..75acaf78 100644 --- a/lib/Daemon.pm +++ b/lib/Daemon.pm @@ -125,8 +125,15 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = slurp_file($PID_file)); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + # Be safe and die if we can't check that a process is + # or is not already running. + die "The PID file $PID_file already exists but it cannot be read: " + . $EVAL_ERROR; + } PTDEBUG && _d('PID file exists; it contains PID', $pid); if ( $pid ) { my $pid_is_alive = kill 0, $pid; @@ -223,7 +230,8 @@ sub DESTROY { sub slurp_file { my ($file) = @_; - open my $fh, "<", $file or die "Couldn't slurp file: $!"; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; return do { local $/; <$fh> }; } diff --git a/t/lib/Daemon.t b/t/lib/Daemon.t index 4e2f2ca2..2fbd0f5b 100644 --- a/t/lib/Daemon.t +++ b/t/lib/Daemon.t @@ -9,7 +9,7 @@ BEGIN { use strict; use warnings FATAL => 'all'; use English qw(-no_match_vars); -use Test::More tests => 22; +use Test::More; use Time::HiRes qw(sleep); use File::Temp qw( tempfile ); use Daemon; @@ -106,8 +106,8 @@ unlike( # ########################################################################## rm_tmp_files(); SKIP: { - skip 'No /proc', 1 unless -d '/proc'; - skip 'No fd in /proc', 1 unless -l "/proc/$PID/0" || -l "/proc/$PID/fd/0"; + skip 'No /proc', 2 unless -d '/proc'; + skip 'No fd in /proc', 2 unless -l "/proc/$PID/0" || -l "/proc/$PID/fd/0"; system("$cmd 5 --daemonize --pid $pid_file --log $log_file"); PerconaTest::wait_for_files($pid_file); @@ -253,11 +253,11 @@ ok( 'Dies if PID file already exists for non-daemon' ); - `rm -rf /tmp/d2.pid`; + diag(`rm -rf /tmp/d2.pid >/dev/null`); } # ############################################################################# # Done. # ############################################################################# rm_tmp_files(); -exit; +done_testing;