diff --git a/bin/pt-archiver b/bin/pt-archiver index 9f5ed755..6e78dcc4 100755 --- a/bin/pt-archiver +++ b/bin/pt-archiver @@ -2980,8 +2980,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -3058,6 +3063,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-config-diff b/bin/pt-config-diff index 5a8f7896..8483a298 100755 --- a/bin/pt-config-diff +++ b/bin/pt-config-diff @@ -1704,8 +1704,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -1782,6 +1787,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-deadlock-logger b/bin/pt-deadlock-logger index 092fcac3..ca5a1889 100755 --- a/bin/pt-deadlock-logger +++ b/bin/pt-deadlock-logger @@ -2306,8 +2306,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2384,6 +2389,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-duplicate-key-checker b/bin/pt-duplicate-key-checker index 1d3f9866..1709fd0e 100755 --- a/bin/pt-duplicate-key-checker +++ b/bin/pt-duplicate-key-checker @@ -2596,8 +2596,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2674,6 +2679,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-fifo-split b/bin/pt-fifo-split index 1f33b3e9..c8df319f 100755 --- a/bin/pt-fifo-split +++ b/bin/pt-fifo-split @@ -1144,8 +1144,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -1222,6 +1227,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-find b/bin/pt-find index b7d562b8..c2194cca 100755 --- a/bin/pt-find +++ b/bin/pt-find @@ -2070,8 +2070,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2148,6 +2153,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-fk-error-logger b/bin/pt-fk-error-logger index 42a3719f..9262df22 100755 --- a/bin/pt-fk-error-logger +++ b/bin/pt-fk-error-logger @@ -1662,8 +1662,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -1740,6 +1745,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-heartbeat b/bin/pt-heartbeat index c281e30c..f4c8ee20 100755 --- a/bin/pt-heartbeat +++ b/bin/pt-heartbeat @@ -2278,8 +2278,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2356,6 +2361,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-kill b/bin/pt-kill index 8fecada9..93e29ef1 100755 --- a/bin/pt-kill +++ b/bin/pt-kill @@ -2000,8 +2000,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2078,6 +2083,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-log-player b/bin/pt-log-player index 45906734..ab8e38e4 100755 --- a/bin/pt-log-player +++ b/bin/pt-log-player @@ -2477,8 +2477,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2555,6 +2560,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-online-schema-change b/bin/pt-online-schema-change index 770260ea..3820e6de 100755 --- a/bin/pt-online-schema-change +++ b/bin/pt-online-schema-change @@ -2200,8 +2200,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2278,6 +2283,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-query-advisor b/bin/pt-query-advisor index 770324b2..5291ce41 100755 --- a/bin/pt-query-advisor +++ b/bin/pt-query-advisor @@ -3177,8 +3177,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -3255,6 +3260,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-query-digest b/bin/pt-query-digest index 2c3fe96e..6af0830c 100755 --- a/bin/pt-query-digest +++ b/bin/pt-query-digest @@ -8752,8 +8752,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -8830,6 +8835,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-show-grants b/bin/pt-show-grants index c3420870..69a76f3e 100755 --- a/bin/pt-show-grants +++ b/bin/pt-show-grants @@ -1518,8 +1518,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -1596,6 +1601,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-slave-delay b/bin/pt-slave-delay index 6d7921bd..1ec41047 100755 --- a/bin/pt-slave-delay +++ b/bin/pt-slave-delay @@ -1993,8 +1993,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2071,6 +2076,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-slave-find b/bin/pt-slave-find index e6868b13..9536cb1f 100755 --- a/bin/pt-slave-find +++ b/bin/pt-slave-find @@ -2706,8 +2706,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2784,6 +2789,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-slave-restart b/bin/pt-slave-restart index ee47ba94..796dd1e6 100755 --- a/bin/pt-slave-restart +++ b/bin/pt-slave-restart @@ -3042,8 +3042,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -3120,6 +3125,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-table-checksum b/bin/pt-table-checksum index bad2d08e..b33cc455 100755 --- a/bin/pt-table-checksum +++ b/bin/pt-table-checksum @@ -6438,8 +6438,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -6516,6 +6521,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-table-sync b/bin/pt-table-sync index 972fde5f..77b4bd73 100755 --- a/bin/pt-table-sync +++ b/bin/pt-table-sync @@ -7286,8 +7286,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -7364,6 +7369,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-table-usage b/bin/pt-table-usage index 8646ae02..c1d1f5bb 100755 --- a/bin/pt-table-usage +++ b/bin/pt-table-usage @@ -4931,8 +4931,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -5009,6 +5014,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-trend b/bin/pt-trend index 10f1946f..2151ce82 100755 --- a/bin/pt-trend +++ b/bin/pt-trend @@ -1148,8 +1148,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -1226,6 +1231,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-upgrade b/bin/pt-upgrade index 42e8042e..157da959 100755 --- a/bin/pt-upgrade +++ b/bin/pt-upgrade @@ -4390,8 +4390,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -4468,6 +4473,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-variable-advisor b/bin/pt-variable-advisor index a78295a9..5cf35c0c 100755 --- a/bin/pt-variable-advisor +++ b/bin/pt-variable-advisor @@ -2191,8 +2191,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2269,6 +2274,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/bin/pt-visual-explain b/bin/pt-visual-explain index 817b0cab..8d7b451d 100755 --- a/bin/pt-visual-explain +++ b/bin/pt-visual-explain @@ -2192,8 +2192,13 @@ sub check_PID_file { PTDEBUG && _d('Checking PID file', $PID_file); if ( $PID_file && -f $PID_file ) { my $pid; - eval { chomp($pid = `cat $PID_file`); }; - die "Cannot cat $PID_file: $OS_ERROR" if $EVAL_ERROR; + eval { + chomp($pid = (slurp_file($PID_file) || '')); + }; + if ( $EVAL_ERROR ) { + 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; @@ -2270,6 +2275,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/lib/Daemon.pm b/lib/Daemon.pm index acc9e1fb..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 = `cat $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; @@ -221,6 +228,13 @@ sub DESTROY { return; } +sub slurp_file { + my ($file) = @_; + return unless $file; + open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; + return do { local $/; <$fh> }; +} + sub _d { my ($package, undef, $line) = caller 0; @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; } diff --git a/t/lib/Daemon.t b/t/lib/Daemon.t index edecd297..1240a4cd 100644 --- a/t/lib/Daemon.t +++ b/t/lib/Daemon.t @@ -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); @@ -256,7 +256,7 @@ ok( 'Dies if PID file already exists for non-daemon' ); - `rm -rf /tmp/d2.pid`; + diag(`rm -rf /tmp/d2.pid >/dev/null`); } # ############################################################################# @@ -264,4 +264,3 @@ ok( # ############################################################################# rm_tmp_files(); done_testing; -exit;