blib/lib/System/Command/Reaper.pm

filter: Statementstmt 63.0% 82 / 130 Branchbran 47.9% 23 / 48 Conditioncond 36.0% 31 / 86 MC/DCmcdc 14.2% 6 / 42 Subroutinesub 68.0% 17 / 25 Podpod 100% 6 / 6 totaltotal 48.9% 165 / 337 CC 53 SCAR 31.4
CC
53
Cov
52%
_reap
40.4
_reap
40.4
close
32.7
CRAP 23.2
↑ Summary blib/lib/System/Command.pm »
1 package System::Command::Reaper;
2 41 $System::Command::Reaper::VERSION = '1.122';
3 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 41 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 41 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 41 ▶use Scalar::Util qw( weaken reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 41 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 41 ▶use constant MSWin32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 41 ▶use constant HANDLES => qw( stdin stdout stderr );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 41 ▶use constant STATUS => qw( exit signal core );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 41 for my $attr ( HANDLES ) {
17 41 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 0 ▶ *$attr = sub { return $_[0]{$attr} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
19 }
20 41 for my $attr ( STATUS ) {
21 41 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 0 ▶ *$attr = sub { $_[0]->is_terminated(); return $_[0]{$attr} };
Subroutine __ANON__: not called CC=1 SCAR=2.6
23 }
24  
25 99 ▶sub new {
Subroutine new: called CC=3 SCAR=15.0
Pod documented
26 0 ▶ my ($class, $command, $o) = @_;
Subroutine new: not called CC=3 SCAR=15.0
Pod documented
27 0 ▶ $o ||= {};
Condition25%1/4
$o ||= {}
l!l
990
$o ||= {}
l!l
00
MC/DC0%0/1
$o ||= {}
$o
MC/DC0%0/1
$o ||= {}
$o
Truth table50%1/2
A
A= $o
Aresult
11
00
Truth table0%0/2
A
A= $o
Aresult
11
00
28 0 my $self = bless { %$o, command => $command }, $class;
29  
30 # copy/weaken the important keys
31 0 @{$self}{ pid => HANDLES } = @{$command}{ pid => HANDLES };
32 0 weaken $self->{$_} for ( command => HANDLES );
33  
34 0 return $self;
35 }
36  
37 # this is necessary, because kill(0,pid) is misimplemented in perl core
38 my $_is_alive = MSWin32
39 0 ▶ ? sub { return `tasklist /FO CSV /NH /fi "PID eq $_[0]"` =~ /^"/ }
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 333 ▶ : sub { return kill 0, $_[0]; };
Subroutine __ANON__: called CC=1 SCAR=0.0
41  
42 333 ▶sub is_terminated {
Subroutine is_terminated: called CC=3 SCAR=14.2
Pod documented
43 0 ▶ my ($self) = @_;
Subroutine is_terminated: not called CC=3 SCAR=14.2
Pod documented
44 0 my $pid = $self->{pid};
45  
46 # Zed's dead, baby. Zed's dead.
47 0 ▶ return $pid if !$_is_alive->($pid) and exists $self->{exit};
Branch: 2 branches
BranchTrueFalseTotal
if not &$_is_alive($pid) and exists $self->{"exit"} 266 67 333
if not &$_is_alive($pid) and exists $self->{"exit"} 0 0 0
Condition50%3/6
not &$_is_alive($pid) and exists $self->{"exit"}
!ll&&!rl&&r
643266
not &$_is_alive($pid) and exists $self->{"exit"}
!ll&&!rl&&r
000
MC/DC100%2/2
not &$_is_alive($pid) and exists $self->{"exit"}
not &$_is_alive($pid) exists $self->{"exit"}
MC/DC0%0/2
not &$_is_alive($pid) and exists $self->{"exit"}
not &$_is_alive($pid) exists $self->{"exit"}
Truth table100%3/3
not(A) and B
A= not &$_is_alive($pid)B= exists $self->{"exit"}
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not &$_is_alive($pid)B= exists $self->{"exit"}
ABresult
0X0
100
111
48  
49 # If that is a re-animated body, we're gonna have to kill it.
50 0 return $self->_reap(WNOHANG);
51 }
52  
53 166 ▶sub _reap {
Subroutine _reap: called CC=13 SCAR=37.0
54 0 ▶ my ( $self, $flags ) = @_;
Subroutine _reap: not called CC=13 SCAR=37.0
55  
56 0 ▶ $flags = 0 if ! defined $flags;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $flags 99 67 166
unless defined $flags 0 0 0
57  
58 0 my $pid = $self->{pid};
59  
60 # REPENT/THE END IS/EXTREMELY/FUCKING/NIGH
61 0 ▶ if ( !exists $self->{exit} and my $reaped = waitpid( $pid, $flags ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $self->{"exit"} and my $reaped = waitpid($pid, $flags)) 99 67 166
if (not exists $self->{"exit"} and my $reaped = waitpid($pid, $flags)) 0 0 0
Condition50%3/6
not exists $self->{"exit"} and my $reaped = waitpid($pid, $flags)
!ll&&!rl&&r
283999
not exists $self->{"exit"} and my $reaped = waitpid($pid, $flags)
!ll&&!rl&&r
000
MC/DC100%2/2
not exists $self->{"exit"} and my $reaped = waitpid($pid, $flags)
not exists $self->{"exit"} my $reaped = waitpid($pid, $flags)
MC/DC0%0/2
not exists $self->{"exit"} and my $reaped = waitpid($pid, $flags)
not exists $self->{"exit"} my $reaped = waitpid($pid, $flags)
Truth table100%3/3
not(A) and B
A= not exists $self->{"exit"}B= my $reaped = waitpid($pid, $flags)
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not exists $self->{"exit"}B= my $reaped = waitpid($pid, $flags)
ABresult
0X0
100
111
62  
63 # Well, it's a puzzle because, technically, you're not alive.
64 0 my $zed = $reaped == $pid;
65 0 ▶ carp "Child process already reaped, check for a SIGCHLD handler"
Branch: 2 branches
BranchTrueFalseTotal
if 5 94 99
if 0 0 0
Condition40%4/10
not $zed || $System::Command::QUIET and
!ll
945
$zed || $System::Command::QUIET
l!l&&r!l&&!r
9405
not $zed || $System::Command::QUIET and
!ll
00
$zed || $System::Command::QUIET
l!l&&r!l&&!r
000
MC/DC50%1/2
not $zed || $System::Command::QUIET and
$zed $System::Command::QUIET
MC/DC0%0/2
not $zed || $System::Command::QUIET and
$zed $System::Command::QUIET
Truth table66%2/3
not(A || B)
A= $zedB= $System::Command::QUIET
ABresult
1X0
010
001
Truth table0%0/3
not(A || B)
A= $zedB= $System::Command::QUIET
ABresult
1X0
010
001
66 if !$zed && !$System::Command::QUIET && !MSWin32;
67  
68 # What do you think? "Zombie Kill of the Week"?
69 0 ▶ @{$self}{ STATUS() }
Branch: 2 branches
BranchTrueFalseTotal
$zed ? : 94 5 99
$zed ? : 0 0 0
70 = $zed
71 ? ( $? >> 8, $? & 127, $? & 128 )
72 : ( -1, -1, -1 );
73  
74 # Who died and made you fucking king of the zombies?
75 0 ▶ if ( defined( my $cmd = $self->{command} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $cmd = $self->{"command"})) 69 30 99
if (defined(my $cmd = $self->{"command"})) 0 0 0
76 0 @{$cmd}{ STATUS() } = @{$self}{ STATUS() };
77  
78 # I know you're here, because I can smell your brains.
79 0 my $o = $cmd->{options};
80 defined reftype( $o->{$_} )
81 and reftype( $o->{$_} ) eq 'SCALAR'
82 0 and ${ $o->{$_} } = $self->{$_}
83 0 ▶ for STATUS();
Branch: 2 branches
BranchTrueFalseTotal
defined builtin::reftype($o->{$_}) and builtin::reftype($o->{$_}) eq "SCALAR" and ${$$o{$_};} = $self->{$_} 21 186 207
defined builtin::reftype($o->{$_}) and builtin::reftype($o->{$_}) eq "SCALAR" and ${$$o{$_};} = $self->{$_} 0 0 0
Condition33%2/6
defined builtin::reftype($o->{$_}) and builtin::reftype($o->{$_}) eq "SCALAR"
!ll&&!rl&&r
186021
defined builtin::reftype($o->{$_}) and builtin::reftype($o->{$_}) eq "SCALAR"
!ll&&!rl&&r
000
MC/DC50%1/2
defined builtin::reftype($o->{$_}) and builtin::reftype($o->{$_}) eq "SCALAR"
defined builtin::reftype($o->{$_}) builtin::reftype($o->{$_}) eq "SCALAR"
MC/DC0%0/2
defined builtin::reftype($o->{$_}) and builtin::reftype($o->{$_}) eq "SCALAR"
defined builtin::reftype($o->{$_}) builtin::reftype($o->{$_}) eq "SCALAR"
Truth table66%2/3
A and B
A= defined builtin::reftype($o->{$_})B= builtin::reftype($o->{$_}) eq "SCALAR"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined builtin::reftype($o->{$_})B= builtin::reftype($o->{$_}) eq "SCALAR"
ABresult
0X0
100
111
84 }
85  
86 # I think it's safe to assume it isn't a zombie.
87 0 print { $self->{th} } "System::Command xit[$pid]: ",
88 join( ', ', map "$_: $self->{$_}", STATUS() ), "\n"
89 0 ▶ if $self->{trace};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"trace"} 0 99 99
if $self->{"trace"} 0 0 0
90  
91 0 return $reaped; # It's dead, Jim!
92 }
93  
94 # Look! It's moving. It's alive. It's alive...
95 0 return;
96 }
97  
98 99 ▶sub close {
Subroutine close: called CC=10 SCAR=34.9
Pod documented
99 0 ▶ my ($self) = @_;
Subroutine close: not called CC=10 SCAR=34.9
Pod documented
100  
101 # close all pipes
102 0 my ( $in, $out, $err ) = @{$self}{qw( stdin stdout stderr )};
103 0 ▶ $in and $in->opened and $in->close || carp "error closing stdin: $!";
Branch: 2 branches
BranchTrueFalseTotal
$in and $in->opened and $in->close || carp("error closing stdin: $!") 64 35 99
$in and $in->opened and $in->close || carp("error closing stdin: $!") 0 0 0
Condition33%6/18
$in && $in->opened && ($in->close || carp("error closing stdin: $!"))
!ll&&!rl&&r
35640
$in and $in->opened
!ll&&!rl&&r
30564
$in->close or carp("error closing stdin: $!")
l!l&&r!l&&!r
6400
$in && $in->opened && ($in->close || carp("error closing stdin: $!"))
!ll&&!rl&&r
000
$in and $in->opened
!ll&&!rl&&r
000
$in->close or carp("error closing stdin: $!")
l!l&&r!l&&!r
000
MC/DC0%0/4
$in && $in->opened && ($in->close || carp("error closing stdin: $!"))
$in $in->opened $in->close carp("error closing stdin: $!")
MC/DC0%0/4
$in && $in->opened && ($in->close || carp("error closing stdin: $!"))
$in $in->opened $in->close carp("error closing stdin: $!")
Truth table40%2/5
A and B && C or D
A= $inB= $in->openedC= $in->closeD= carp("error closing stdin: $!")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
Truth table0%0/5
A and B && C or D
A= $inB= $in->openedC= $in->closeD= carp("error closing stdin: $!")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
104 0 ▶ $out and $out->opened and $out->close || carp "error closing stdout: $!";
Branch: 2 branches
BranchTrueFalseTotal
$out and $out->opened and $out->close || carp("error closing stdout: $!") 67 32 99
$out and $out->opened and $out->close || carp("error closing stdout: $!") 0 0 0
Condition33%6/18
$out && $out->opened && ($out->close || carp("error closing stdout: $!"))
!ll&&!rl&&r
32670
$out and $out->opened
!ll&&!rl&&r
30267
$out->close or carp("error closing stdout: $!")
l!l&&r!l&&!r
6700
$out && $out->opened && ($out->close || carp("error closing stdout: $!"))
!ll&&!rl&&r
000
$out and $out->opened
!ll&&!rl&&r
000
$out->close or carp("error closing stdout: $!")
l!l&&r!l&&!r
000
MC/DC0%0/4
$out && $out->opened && ($out->close || carp("error closing stdout: $!"))
$out $out->opened $out->close carp("error closing stdout: $!")
MC/DC0%0/4
$out && $out->opened && ($out->close || carp("error closing stdout: $!"))
$out $out->opened $out->close carp("error closing stdout: $!")
Truth table40%2/5
A and B && C or D
A= $outB= $out->openedC= $out->closeD= carp("error closing stdout: $!")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
Truth table0%0/5
A and B && C or D
A= $outB= $out->openedC= $out->closeD= carp("error closing stdout: $!")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
105 0 ▶ $err and $err->opened and $err->close || carp "error closing stderr: $!";
Branch: 2 branches
BranchTrueFalseTotal
$err and $err->opened and $err->close || carp("error closing stderr: $!") 67 32 99
$err and $err->opened and $err->close || carp("error closing stderr: $!") 0 0 0
Condition33%6/18
$err && $err->opened && ($err->close || carp("error closing stderr: $!"))
!ll&&!rl&&r
32670
$err and $err->opened
!ll&&!rl&&r
30267
$err->close or carp("error closing stderr: $!")
l!l&&r!l&&!r
6700
$err && $err->opened && ($err->close || carp("error closing stderr: $!"))
!ll&&!rl&&r
000
$err and $err->opened
!ll&&!rl&&r
000
$err->close or carp("error closing stderr: $!")
l!l&&r!l&&!r
000
MC/DC0%0/4
$err && $err->opened && ($err->close || carp("error closing stderr: $!"))
$err $err->opened $err->close carp("error closing stderr: $!")
MC/DC0%0/4
$err && $err->opened && ($err->close || carp("error closing stderr: $!"))
$err $err->opened $err->close carp("error closing stderr: $!")
Truth table40%2/5
A and B && C or D
A= $errB= $err->openedC= $err->closeD= carp("error closing stderr: $!")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
Truth table0%0/5
A and B && C or D
A= $errB= $err->openedC= $err->closeD= carp("error closing stderr: $!")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
106  
107 # and wait for the child (if any)
108 0 $self->_reap();
109  
110 0 return $self;
111 }
112  
113 97 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=9.2
114 0 ▶ my ($self) = @_;
Subroutine DESTROY: not called CC=2 SCAR=9.2
115 0 local $?;
116 0 local $!;
117 0 ▶ $self->close if !exists $self->{exit};
Branch: 2 branches
BranchTrueFalseTotal
if not exists $self->{"exit"} 30 67 97
if not exists $self->{"exit"} 0 0 0
118 }
119  
120 41 1;
121  
122 __END__
↑ Summary blib/lib/System/Command.pm »