| 1 | package Unix::PID::Tiny; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.95'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | my ( $self, $args_hr ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | my %DEFAULTS = ( | |||||||||||||||||||||||||||||||||||||||||
| 12 | 'keep_open' => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 'check_proc_open_fds' => 0 | ||||||||||||||||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | $args_hr ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args_hr ||= {}
MC/DC100%1/1
$args_hr ||= {}
$args_hr
Truth table100%2/2
A
A= $args_hr
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | %{$args_hr} = ( %DEFAULTS, %{$args_hr} ); | |||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | $args_hr->{'minimum_pid'} = 11 if !exists $args_hr->{'minimum_pid'} || $args_hr->{'minimum_pid'} !~ m{\A\d+\z}ms; # this does what one assumes m{^\d+$} would do | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args_hr->{"minimum_pid"} and $args_hr->{"minimum_pid"} =~ /\A\d+\z/ms
MC/DC50%1/2
exists $args_hr->{"minimum_pid"} and $args_hr->{"minimum_pid"} =~ /\A\d+\z/ms
exists $args_hr->{"minimum_pid"}
$args_hr->{"minimum_pid"} =~ /\A\d+\z/ms
Truth table66%2/3
A and B
A= exists $args_hr->{"minimum_pid"}B= $args_hr->{"minimum_pid"} =~ /\A\d+\z/ms
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | if ( defined $args_hr->{'ps_path'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | $args_hr->{'ps_path'} .= '/' if $args_hr->{'ps_path'} !~ m{/$}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | if ( !-d $args_hr->{'ps_path'} || !-x "$args_hr->{'ps_path'}ps" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-d $args_hr->{"ps_path"} and -x qq[$args_hr->{"ps_path"}ps]
MC/DC0%0/2
-d $args_hr->{"ps_path"} and -x qq[$args_hr->{"ps_path"}ps]
-d $args_hr->{"ps_path"}
-x qq[$args_hr->{"ps_path"}ps]
Truth table0%0/3
A and B
A= -d $args_hr->{"ps_path"}B= -x qq[$args_hr->{"ps_path"}ps]
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | $args_hr->{'ps_path'} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | $args_hr->{'ps_path'} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | return bless { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 'ps_path' => $args_hr->{'ps_path'}, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 'minimum_pid' => $args_hr->{'minimum_pid'}, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 'keep_open' => $args_hr->{'keep_open'}, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | 'check_proc_open_fds' => $args_hr->{'check_proc_open_fds'}, | |||||||||||||||||||||||||||||||||||||||||
| 35 | 'open_handles' => [] | ||||||||||||||||||||||||||||||||||||||||||
| 36 | }, $self; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub kill { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kill: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my ( $self, $pid, $give_kill_a_chance ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $give_kill_a_chance = int $give_kill_a_chance if defined $give_kill_a_chance; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | $pid = int $pid; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $min = int $self->{'minimum_pid'}; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ( $pid < $min ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | # prevent bad args from killing the process group (IE '0') | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # or general low level ones | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | warn "kill() called with integer value less than $min"; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | # CORE::kill 0, $pid : may be false but still running, see `perldoc -f kill` | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ( $self->is_pid_running($pid) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | # RC from CORE::kill is not a boolean of if the PID was killed or not, only that it was signaled | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # so it is not an indicator of "success" in killing $pid | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | _kill( 15, $pid ); # TERM | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | _kill( 2, $pid ); # INT | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | _kill( 1, $pid ); # HUP | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | _kill( 9, $pid ); # KILL | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # give kill() some time to take effect? | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($give_kill_a_chance) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | sleep($give_kill_a_chance); | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | return if $self->is_pid_running($pid); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub is_pid_running { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_pid_running: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my ( $self, $check_pid ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $check_pid = int $check_pid; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return if !$check_pid || $check_pid < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $check_pid or $check_pid < 0
MC/DC0%0/2
not $check_pid or $check_pid < 0
not $check_pid
$check_pid < 0
Truth table0%0/3
not(A) or B
A= not $check_pidB= $check_pid < 0
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | return 1 if $> == 0 && _kill( 0, $check_pid ); # if we are superuser we can avoid the the system call. For details see `perldoc -f kill` | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$> == 0 and _kill(0, $check_pid)
MC/DC0%0/2
$> == 0 and _kill(0, $check_pid)
$> == 0
_kill(0, $check_pid)
Truth table0%0/3
A and B
A= $> == 0B= _kill(0, $check_pid)
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # If the proc filesystem is available, it's a good test. If not, continue on to system call | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return 1 if -e "/proc/$$" && -r "/proc/$$" && -r "/proc/$check_pid"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
-e "/proc/${$}" and -r "/proc/${$}" and -r "/proc/$check_pid"
-e "/proc/${$}" and -r "/proc/${$}"
MC/DC0%0/3
-e "/proc/${$}" and -r "/proc/${$}" and -r "/proc/$check_pid"
-e "/proc/${$}"
-r "/proc/${$}"
-r "/proc/$check_pid"
Truth table0%0/4
A and B and C
A= -e "/proc/${$}"B= -r "/proc/${$}"C= -r "/proc/$check_pid"
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # even if we are superuser, go ahead and call ps just in case CORE::kill 0's false RC was erroneous | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my @outp = $self->_raw_ps( 'u', '-p', $check_pid ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | chomp @outp; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | return 1 if defined $outp[1]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub pid_info_hash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pid_info_hash: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my ( $self, $pid ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $pid = int $pid; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | return if !$pid || $pid < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $pid or $pid < 0
MC/DC0%0/2
not $pid or $pid < 0
not $pid
$pid < 0
Truth table0%0/3
not(A) or B
A= not $pidB= $pid < 0
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my @outp = $self->_raw_ps( 'u', '-p', $pid ); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | chomp @outp; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my %info; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | @info{ split( /\s+/, $outp[0], 11 ) } = split( /\s+/, $outp[1], 11 ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | return wantarray ? %info : \%info; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _raw_ps { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_raw_ps: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my ( $self, @ps_args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $psargs = join( ' ', @ps_args ); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my @res = `$self->{'ps_path'}ps $psargs`; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return wantarray ? @res : join '', @res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub get_pid_from_pidfile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_pid_from_pidfile: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my ( $self, $pid_file ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | # if this function is ever changed to use $self as a hash object, update pid_file() to not do a class method call | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | return 0 if !-e $pid_file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | open my $pid_fh, '<', $pid_file or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | chomp( my $pid = <$pid_fh> ); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | close $pid_fh; | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | return int( abs($pid) ); | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub _sets_match { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sets_match: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my ( $left, $right ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $count = scalar @{$left}; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return 0 unless scalar @{$right} == $count; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | for ( my $i = 0; $i < $count; $i++ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | return 0 unless $left->[$i] eq $right->[$i]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub is_pidfile_running { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_pidfile_running: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my ( $self, $pid_file, $since ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my $pid = $self->get_pid_from_pidfile($pid_file) || return; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->get_pid_from_pidfile($pid_file) || (return)
MC/DC0%0/1
$self->get_pid_from_pidfile($pid_file) || (return)
$self->get_pid_from_pidfile($pid_file)
Truth table0%0/2
A
A= $self->get_pid_from_pidfile($pid_file)
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my @pidfile_st = stat $pid_file or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ( defined $since ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | return if $pidfile_st[9] < $since; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if ( $self->{'check_proc_open_fds'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $dir = "/proc/$pid/fd"; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my $found = 0; | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | opendir my $dh, $dir or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | while ( my $dirent = readdir $dh ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | next if $dirent eq '.' || $dirent eq '..'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$dirent eq "." or $dirent eq ".."
MC/DC0%0/2
$dirent eq "." or $dirent eq ".."
$dirent eq "."
$dirent eq ".."
Truth table0%0/3
A or B
A= $dirent eq "."B= $dirent eq ".."
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my $path = "$dir/$dirent"; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my $dest = readlink $path or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | my @st = stat $dest or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ( _sets_match( [ @pidfile_st[ 0, 1 ] ], [ @st[ 0, 1 ] ] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | $found = 1; | |||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | closedir $dh; | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | return unless $found; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | return unless $self->is_pid_running($pid); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | return $pid; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub pid_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pid_file: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my ( $self, $pid_file, $newpid, $retry_conf ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | $newpid = $$ if !$newpid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | my $rc = $self->pid_file_no_unlink( $pid_file, $newpid, $retry_conf ); | |||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if ( $rc && $newpid == $$ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$rc and $newpid == $$
MC/DC0%0/2
$rc and $newpid == $$
$rc
$newpid == $$
Truth table0%0/3
A and B
A= $rcB= $newpid == $$
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | # prevent forked childrens' END from killing parent's pid files | ||||||||||||||||||||||||||||||||||||||||||
| 184 | # 'unlink_end_use_current_pid_only' is undocumented as this may change, feedback welcome! | ||||||||||||||||||||||||||||||||||||||||||
| 185 | # 'carp_unlink_end' undocumented as it is only meant for testing (rt57462, use Test::Carp to test END behavior) | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if ( $self->{'unlink_end_use_current_pid_only'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | eval 'END { unlink $pid_file if $$ eq ' . $$ . '}'; ## no critic qw(ProhibitStringyEval) | |||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if ( $self->{'carp_unlink_end'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | # eval 'END { require Carp;Carp::carp("[info] $$ !unlink $pid_file (current pid check)") if $$ ne ' . $$ . '}'; ## no critic qw(ProhibitStringyEval) | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | eval 'END { require Carp;Carp::carp("[info] $$ unlink $pid_file (current pid check)") if $$ eq ' . $$ . '}'; ## no critic qw(ProhibitStringyEval) | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | eval 'END { unlink $pid_file if Unix::PID::Tiny->get_pid_from_pidfile($pid_file) eq $$ }'; ## no critic qw(ProhibitStringyEval) | |||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if ( $self->{'carp_unlink_end'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | # eval 'END { require Carp;Carp::carp("[info] $$ !unlink $pid_file (pid file check)") if Unix::PID::Tiny->get_pid_from_pidfile($pid_file) ne $$ }'; ## no critic qw(ProhibitStringyEval) | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | eval 'END { require Carp;Carp::carp("[info] $$ unlink $pid_file (pid file check)") if Unix::PID::Tiny->get_pid_from_pidfile($pid_file) eq $$ }'; ## no critic qw(ProhibitStringyEval) | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | return 1 if defined $rc && $rc == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $rc and $rc == 1
MC/DC0%0/2
defined $rc and $rc == 1
defined $rc
$rc == 1
Truth table0%0/3
A and B
A= defined $rcB= $rc == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | return 0 if defined $rc && $rc == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $rc and $rc == 0
MC/DC0%0/2
defined $rc and $rc == 0
defined $rc
$rc == 0
Truth table0%0/3
A and B
A= defined $rcB= $rc == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 2 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | # more intuitively named alias | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 2 | *pid_file_no_cleanup = \&pid_file_no_unlink; | |||||||||||||||||||||||||||||||||||||||||
| 213 | 2 | ▶ | use warnings 'once'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | sub pid_file_no_unlink { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pid_file_no_unlink: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | my ( $self, $pid_file, $newpid, $retry_conf ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | $newpid = $$ if !$newpid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | if ( ref($retry_conf) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | $retry_conf->[0] = int( abs( $retry_conf->[0] ) ); | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | for my $idx ( 1 .. scalar( @{$retry_conf} ) - 1 ) { | |||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | next if ref $retry_conf->[$idx] eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | $retry_conf->[$idx] = int( abs( $retry_conf->[$idx] ) ); | |||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | elsif ( ref($retry_conf) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | $retry_conf->{'num_of_passes'} ||= 3; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$retry_conf->{"num_of_passes"} ||= 3
MC/DC0%0/1
$retry_conf->{"num_of_passes"} ||= 3
$retry_conf->{"num_of_passes"}
Truth table0%0/2
A
A= $retry_conf->{"num_of_passes"}
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | $retry_conf->{'passes_config'} ||= [ 1, 2 ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$retry_conf->{"passes_config"} ||= [1, 2]
MC/DC0%0/1
$retry_conf->{"passes_config"} ||= [1, 2]
$retry_conf->{"passes_config"}
Truth table0%0/2
A
A= $retry_conf->{"passes_config"}
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | $retry_conf = [ int( $retry_conf->{'num_of_passes'} ), @{ $retry_conf->{'passes_config'} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | $retry_conf = [ 3, 1, 2 ]; | |||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | my $passes = 0; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | require Fcntl; | |||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | EXISTS: | |||||||||||||||||||||||||||||||||||||||||
| 239 | $passes++; | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | ▶ | if ( -e $pid_file ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | my $curpid = $self->get_pid_from_pidfile($pid_file); | |||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | # TODO: narrow even more the race condition where $curpid stops running and a new PID is put in | ||||||||||||||||||||||||||||||||||||||||||
| 244 | # the file between when we pull in $curpid above and check to see if it is running/unlink below | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | return 1 if int $curpid == $$ && $newpid == $$; # already setup | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
int $curpid == $$ and $newpid == $$
MC/DC0%0/2
int $curpid == $$ and $newpid == $$
int $curpid == $$
$newpid == $$
Truth table0%0/3
A and B
A= int $curpid == $$B= $newpid == $$
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | return if int $curpid == $$; # can't change it while $$ is alive | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | return if $self->is_pid_running( int $curpid ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | unlink $pid_file; # must be a stale PID file, so try to remove it for sysopen() | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | # write only if it does not exist: | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | my $pid_fh = _sysopen($pid_file); | |||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | if ( !$pid_fh ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | return 0 if $passes >= $retry_conf->[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | if ( ref( $retry_conf->[$passes] ) eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | $retry_conf->[$passes]->( $self, $pid_file, $passes ); | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | sleep( $retry_conf->[$passes] ) if $retry_conf->[$passes]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | goto EXISTS; | |||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | syswrite( $pid_fh, int( abs($newpid) ) ); | |||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | if ( $self->{'keep_open'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | push @{ $self->{'open_handles'} }, $pid_fh; | |||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | close $pid_fh; | |||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | sub _sysopen { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sysopen: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | my ($pid_file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | sysopen( my $pid_fh, $pid_file, Fcntl::O_WRONLY() | Fcntl::O_EXCL() | Fcntl::O_CREAT() ) || return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | return $pid_fh; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | sub _kill { ## no critic(RequireArgUnpacking | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_kill: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | return CORE::kill(@_); # goto &CORE::kill; is problematic | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | __END__ | ||||||||||||||||||||||||||||||||||||||||||