| 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
Condition33%1/3
ref $arguments eq "ARRAY" and $arguments->[0] =~ m[\A/]u
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
| |||||||||||||||||||||||||||||
| 14 | 16 | ▶ | pipe my $reader, my $writer or die "pipe: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 16 | ▶ | defined $reader->blocking(0) or die "Set command output pipe to nonblocking: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 13 | my $pid = fork; | |||||||||||||||||||||||||||
| 17 | 13 | ▶ | die "fork: $!" unless defined $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 13 | ▶ | if ($pid == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$options{"timeout"} // 30
$options{"timeout"}
Truth table100%2/2
A
A= $options{"timeout"}
| |||||||||||||||||||||||||||||
| 34 | 6 | 1; | |||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||
| 36 | 10 | ▶ | $failure = $@ || 'Command interrupted' unless $succeeded; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$@ || 'Command interrupted'
MC/DC0%0/1
$@ || 'Command interrupted'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 10 | ▶ | if ($failure || !defined $status) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$failure or not defined $status
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 50 | 0 | $output .= $buffer; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if (length($output) > 16 * 1024 * 1024) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $failure ||= 'Command output exceeds 16 MiB'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$failure ||= "Command output exceeds 16 MiB"
MC/DC0%0/1
$failure ||= "Command output exceeds 16 MiB"
$failure
Truth table0%0/2
A
A= $failure
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | last if $failure; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 10 | close $reader; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 10 | ▶ | die $failure if $failure; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 6 | ▶ | die "Command failed ($status): $arguments->[0]\n$output" if $status; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | 3 | ▶ | my $directory = $options->{cwd} // '/'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"cwd"} // "/"
MC/DC100%1/1
$options->{"cwd"} // "/"
$options->{"cwd"}
Truth table100%2/2
A
A= $options->{"cwd"}
| |||||||||||||||||||||||||||||
| 70 | 3 | ▶ | chdir($directory) or die "chdir: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"} // {}
MC/DC0%0/1
$options->{"env"} // {}
$options->{"env"}
Truth table50%1/2
A
A= $options->{"env"}
| |||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | 3 | ▶ | open STDIN, '<', '/dev/null' or die "stdin: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 3 | ▶ | if ($options->{stdout}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | open STDOUT, '>', $options->{stdout} or die "stdout: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||
| 85 | 2 | ▶ | open STDOUT, '>&', $writer or die "stdout: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 3 | ▶ | if ($options->{log}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | open STDERR, '>>', $options->{log} or die "stderr: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||
| 91 | 2 | ▶ | open STDERR, '>&', $writer or die "stderr: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 67 | ▶ | while (sysread($reader, my $buffer, 65536)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 3 | $$output .= $buffer; | |||||||||||||||||||||||||||
| 109 | 3 | ▶ | die 'Command output exceeds 16 MiB' if length($$output) > 16 * 1024 * 1024; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 67 | ▶ | return $? if waitpid($pid, WNOHANG) == $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 61 | ▶ | die "Command timed out: $executable" if time >= $deadline; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 57 | 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
| |||||||||||||||||||||||||||||
| 128 | 8 | ▶ | if (waitpid($pid, WNOHANG) == $pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 139 | 0 | kill 'KILL', $pid; | |||||||||||||||||||||||||||
| 140 | 0 | waitpid($pid, 0); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 10 | 1; | |||||||||||||||||||||||||||