lib/Mogura/Scanner/Command.pm

filter: Statementstmt 89.4% 76 / 85 Branchbran 67.8% 38 / 56 Conditioncond 56.2% 9 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 76.5% 134 / 175 CC 36 SCAR 27.7
CC
36
Cov
78%
run
26.2
_exec_child
13.8
_wait_for_exit
5.0
CRAP 15.9
« lib/Mogura/Scanner/Disk.pm ↑ Summary lib/Mogura/Scanner/Scan.pm »
1 package Mogura::Scanner::Command;
2 10 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 10 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use POSIX qw(setsid WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Time::HiRes qw(time sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 ▶sub run {
Subroutine run: called CC=18 SCAR=32.7
Pod undocumented
9 16 my ($self, $arguments, %options) = @_;
10  
11 # Callers supply an absolute executable and an argument array. No command
12 # is interpreted by a shell, including strings obtained from guest files.
13 16 ▶ die 'Invalid command' unless ref $arguments eq 'ARRAY' && $arguments->[0] =~ m{\A/};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arguments eq "ARRAY" and $arguments->[0] =~ m[\A/]u 0 16 16
Condition33%1/3
ref $arguments eq "ARRAY" and $arguments->[0] =~ m[\A/]u
!ll&&!rl&&r
0016
MC/DC0%0/2
ref $arguments eq "ARRAY" and $arguments->[0] =~ m[\A/]u
ref $arguments eq "ARRAY" $arguments->[0] =~ m[\A/]u
Truth table33%1/3
A and B
A= ref $arguments eq "ARRAY"B= $arguments->[0] =~ m[\A/]u
ABresult
0X0
100
111
14 16 ▶ pipe my $reader, my $writer or die "pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $reader, my $writer or die "pipe: $!" 16 0 16
15 16 ▶ defined $reader->blocking(0) or die "Set command output pipe to nonblocking: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $reader->blocking(0) or die "Set command output pipe to nonblocking: $!" 13 3 16
16 13 my $pid = fork;
17 13 ▶ die "fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 13 13
18  
19 13 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 3 10 13
20 3 close $reader;
21 3 eval { $self->_exec_child($arguments, $writer, \%options) };
22 0 print STDERR $@;
23 0 POSIX::_exit(127);
24 }
25  
26 10 close $writer;
27  
28 10 my $output = '';
29 10 my ($status, $failure);
30 10 my $succeeded = eval {
31 $status =
32 $self->_wait_for_exit($pid, $reader, \$output, $arguments->[0],
33 10 ▶ $options{timeout} // 30);
Condition100%2/2
$options{"timeout"} // 30
l!l
46
MC/DC100%1/1
$options{"timeout"} // 30
$options{"timeout"}
Truth table100%2/2
A
A= $options{"timeout"}
Aresult
11
00
34 6 1;
35 };
36 10 ▶ $failure = $@ || 'Command interrupted' unless $succeeded;
Branch: 1 branch
BranchTrueFalseTotal
unless $succeeded 4 6 10
Condition50%1/2
$@ || 'Command interrupted'
l!l
40
MC/DC0%0/1
$@ || 'Command interrupted'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
37  
38 10 ▶ if ($failure || !defined $status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failure or not defined $status) { } 4 6 10
Condition66%2/3
$failure or not defined $status
l!l&&r!l&&!r
406
MC/DC50%1/2
$failure or not defined $status
$failure not defined $status
Truth table66%2/3
A or not(B)
A= $failureB= not defined $status
ABresult
1X1
011
000
39 4 $self->_terminate_child($pid);
40 }
41 else {
42 # A successful CLI must not leave descendants running after its own
43 # process exits. These signals address the session created in the child.
44 6 kill 'TERM', -$pid;
45 6 kill 'KILL', -$pid;
46 }
47  
48 # The child may have written between the last nonblocking read and reap.
49 10 ▶ while (sysread($reader, my $buffer, 65536)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread $reader, my $buffer, 65536) 0 10 10
50 0 $output .= $buffer;
51 0 ▶ if (length($output) > 16 * 1024 * 1024) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $output > 16777216) 0 0 0
52 0 ▶ $failure ||= 'Command output exceeds 16 MiB';
Condition0%0/2
$failure ||= "Command output exceeds 16 MiB"
l!l
00
MC/DC0%0/1
$failure ||= "Command output exceeds 16 MiB"
$failure
Truth table0%0/2
A
A= $failure
Aresult
11
00
53 }
54 0 ▶ last if $failure;
Branch: 1 branch
BranchTrueFalseTotal
if $failure 0 0 0
55 }
56 10 close $reader;
57  
58 10 ▶ die $failure if $failure;
Branch: 1 branch
BranchTrueFalseTotal
if $failure 4 6 10
59 6 ▶ die "Command failed ($status): $arguments->[0]\n$output" if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 2 4 6
60 4 return $output;
61 }
62  
63 3 ▶sub _exec_child {
Subroutine _exec_child: called CC=12 SCAR=26.2
64 3 my ($self, $arguments, $writer, $options) = @_;
65  
66 # A separate process group lets timeout and stop handling terminate the
67 # CLI's descendants as well as its immediate process.
68 3 ▶ setsid() >= 0 or die "setsid: $!";
Branch: 1 branch
BranchTrueFalseTotal
setsid() >= 0 or die "setsid: $!" 3 0 3
69 3 ▶ my $directory = $options->{cwd} // '/';
Condition100%2/2
$options->{"cwd"} // "/"
l!l
12
MC/DC100%1/1
$options->{"cwd"} // "/"
$options->{"cwd"}
Truth table100%2/2
A
A= $options->{"cwd"}
Aresult
11
00
70 3 ▶ chdir($directory) or die "chdir: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $directory or die "chdir: $!" 3 0 3
71  
72 # Only Mogura's explicit environment reaches the CLI. HOME and cwd point
73 # outside the guest, so neither tool discovers guest-controlled settings.
74 %ENV = (
75 PATH => '/usr/sbin:/usr/bin:/sbin:/bin',
76 LANG => 'C.UTF-8',
77 HOME => $directory,
78 3 ▶ %{$options->{env} // {}},
Condition50%1/2
$options->{"env"} // {}
l!l
03
MC/DC0%0/1
$options->{"env"} // {}
$options->{"env"}
Truth table50%1/2
A
A= $options->{"env"}
Aresult
11
00
79 );
80 3 ▶ open STDIN, '<', '/dev/null' or die "stdin: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", "/dev/null" or die "stdin: $!" 3 0 3
81 3 ▶ if ($options->{stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'stdout'}) { } 1 2 3
82 1 ▶ open STDOUT, '>', $options->{stdout} or die "stdout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $options->{"stdout"} or die "stdout: $!" 1 0 1
83 }
84 else {
85 2 ▶ open STDOUT, '>&', $writer or die "stdout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $writer or die "stdout: $!" 2 0 2
86 }
87 3 ▶ if ($options->{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'log'}) { } 1 2 3
88 1 ▶ open STDERR, '>>', $options->{log} or die "stderr: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">>", $options->{"log"} or die "stderr: $!" 1 0 1
89 }
90 else {
91 2 ▶ open STDERR, '>&', $writer or die "stderr: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $writer or die "stderr: $!" 2 0 2
92 }
93 3 close $writer;
94  
95 3 exec {$arguments->[0]} @$arguments;
96 0 die "exec $arguments->[0]: $!";
97 }
98  
99 10 ▶sub _wait_for_exit {
Subroutine _wait_for_exit: called CC=5 SCAR=16.1
100 10 my ($self, $pid, $reader, $output, $executable, $timeout) = @_;
101  
102 10 my $deadline = time + $timeout;
103 10 while (1) {
104  
105 # Drain the pipe while the child is alive so verbose output cannot
106 # block the child before it gets a chance to exit.
107 69 ▶ while (sysread($reader, my $buffer, 65536)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread $reader, my $buffer, 65536) 3 69 72
108 3 $$output .= $buffer;
109 3 ▶ die 'Command output exceeds 16 MiB' if length($$output) > 16 * 1024 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
if length $$output > 16777216 0 3 3
110 }
111 69 ▶ return $? if waitpid($pid, WNOHANG) == $pid;
Branch: 1 branch
BranchTrueFalseTotal
if waitpid($pid, 1) == $pid 6 63 69
112 63 ▶ die "Command timed out: $executable" if time >= $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if &Mogura::Scanner::Command::time() >= $deadline 4 59 63
113 59 sleep 0.05;
114 }
115 }
116  
117 4 ▶sub _terminate_child {
Subroutine _terminate_child: called CC=4 SCAR=14.2
118 4 my ($self, $pid) = @_;
119  
120 4 kill 'TERM', -$pid;
121  
122 # A stop can arrive before setsid(), when no child process group exists.
123 4 kill 'TERM', $pid;
124  
125 4 my $deadline = time + 2;
126 4 my $reaped;
127 4 ▶ while (time < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Mogura::Scanner::Command::time() < $deadline) 8 0 8
128 8 ▶ if (waitpid($pid, WNOHANG) == $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($pid, 1) == $pid) 4 4 8
129 4 $reaped = 1;
130 4 last;
131 }
132 4 sleep 0.05;
133 }
134  
135 # Descendants may survive their parent's exit. Kill the group regardless,
136 # but never signal an already reaped PID that the OS could have reused.
137 4 kill 'KILL', -$pid;
138 4 ▶ unless ($reaped) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reaped) 0 4 4
139 0 kill 'KILL', $pid;
140 0 waitpid($pid, 0);
141 }
142 }
143  
144 10 1;
« lib/Mogura/Scanner/Disk.pm ↑ Summary lib/Mogura/Scanner/Scan.pm »