| 1 | package MCP::Run::Bash; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 77 | our $VERSION = '0.107'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 77 | ▶ | use Mojo::Base 'MCP::Run', -signatures; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: MCP server that executes commands via bash | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 77 | ▶ | use IPC::Open3; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 77 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 77 | ▶ | use Symbol 'gensym'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 77 | ▶ | use POSIX ':sys_wait_h'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 77 | ▶ | use Errno 'EINTR'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 77 | ▶ | use Scalar::Util 'looks_like_number'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # Grace period (seconds) between SIGTERM and SIGKILL when a command times | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # out, plus the poll interval used while waiting for the child to go away. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # Without the escalation a child that ignores SIGTERM blocks the server for | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # as long as it feels like running. | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 77 | ▶ | use constant TERM_GRACE => 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 77 | ▶ | use constant REAP_POLL => 0.05; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # Last resort when neither the per-call timeout nor the server default is a | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # usable positive number. Mirrors the MCP::Run timeout attribute default: a | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # command must never run unbounded, that would pin the server indefinitely. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 77 | ▶ | use constant FALLBACK_TIMEOUT => 30; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # alarm() hands its argument to a C unsigned int, and 2**31-1 is the largest | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # value that arrives intact. Above it alarm() either warns about a negative | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # argument or - from 2**32 on - quietly sets no alarm at all, which is the | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # unbounded command all over again, only without a trace. Measured, not | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # assumed; Inf gets here too, since it passes for a positive number. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 77 | ▶ | use constant MAX_TIMEOUT => 2**31 - 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 77 | has name => 'mcp-run-bash'; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 77 | has version => $VERSION; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 796 | ▶ | sub execute ($self, $command, $working_directory, $timeout) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=25 SCAR=33.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 796 | $timeout = $self->_effective_timeout($timeout); | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 796 | my ($stdout, $stderr) = ('', ''); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 796 | my ($exit_code, $error); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 796 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 47 | 796 | my $err = gensym; | |||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | # The child reports a failed chdir through this pipe. Perl sets | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # close-on-exec on it, so a successful exec closes the write end and the | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # parent reads end-of-file; a failure leaves the reason behind. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 796 | ▶ | pipe(my $chdir_r, my $chdir_w) or die "pipe: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # open3 with '-' forks without exec'ing and returns 0 in the child, which | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # lets the child put itself into its own process group *before* exec. | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # Doing that from the parent loses a race: open3 only returns once the | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # child has exec'd, and setpgid() on an already exec'd child fails with | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # EACCES, leaving the command in our own group where it cannot be | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # signalled as a group. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 796 | my $pid = open3(my $in, my $out, $err, '-'); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 796 | ▶ | unless ($pid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 55 | eval { setpgrp(0, 0) }; # best effort: not available everywhere | |||||||||||||||||||||||||||||||||||||||||
| 63 | 55 | close $chdir_r; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # chdir here, not "cd DIR && COMMAND" in front of the command: the | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # prefix binds only to the first command of a list, so a command after | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # ';', a newline or '&' ran in the wrong directory, and when the cd | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # failed the rest of the list ran anyway, in the caller's. A directory | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # that cannot be entered stops here, before the command exists. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 55 | ▶ | if (defined $working_directory && length $working_directory | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $working_directory and length $working_directory and not chdir $working_directory
defined $working_directory and length $working_directory
MC/DC0%0/3
defined $working_directory and length $working_directory and not chdir $working_directory
defined $working_directory
length $working_directory
not chdir $working_directory
Truth table75%3/4
A and B and not(C)
A= defined $working_directoryB= length $working_directoryC= not chdir $working_directory
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | && !chdir $working_directory) { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | syswrite $chdir_w, "cannot change to working directory '$working_directory': $!"; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | POSIX::_exit(1); | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 55 | ▶ | exec 'bash', '-c', $command | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | or print STDERR "mcp-run-bash: exec of bash failed: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | POSIX::_exit(127); # must never return into the parent program | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 741 | close $in; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 741 | close $chdir_w; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 741 | my $select = IO::Select->new($out, $err); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 741 | my $timed_out = 0; | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | # The command has its own process group now, so a group kill aimed at us | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # no longer reaches it. Take the termination signals over for the | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # duration of the command, or a cancelled run would leave the command | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # behind after we are gone. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # Taking a signal over no longer stops at a signal the embedding program | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # has taken over itself. The command is stopped either way - a command | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # that outlives the run leaves work behind that nobody asked for - and | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # the signal is handed on afterwards, to the program's own handler, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # chained once the command is gone. How the program shuts down is its | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # business; that the command does not outlive the run is ours. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # A signal the program ignores is left alone, program and command | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # alike: it asked for the signal to have no effect, and the command | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # inherits that disposition across the exec. | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 741 | my @forward = grep { !_signal_ignored($_) } qw(HUP INT TERM); | |||||||||||||||||||||||||||||||||||||||||
| 101 | 741 | my %previous = map { $_ => $SIG{$_} } @forward; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 741 | my $child_status; | |||||||||||||||||||||||||||||||||||||||||
| 103 | local @SIG{@forward} = map { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 741 | my $signal = $_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | # The timeout alarm must not abort the cleanup: a die from here | ||||||||||||||||||||||||||||||||||||||||||
| 107 | # would skip the chaining and swallow the signal the program was | ||||||||||||||||||||||||||||||||||||||||||
| 108 | # sent. The command is dead after this anyway, so the timeout has | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # nothing left to guard. | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | local $SIG{ALRM} = sub { }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # $? is global: a waitpid in here overwrites the status the main | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # flow is about to read. Keep it, and put it back unless this | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # handler reaped the child itself. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | my $saved = $?; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | my $status = _terminate($pid); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | if (defined $status) { $child_status = $status } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | else { $? = $saved } | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | _chain_signal($signal, $previous{$signal}); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 2222 | }; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } @forward; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 741 | ▶ | local $SIG{ALRM} = sub { $timed_out = 1; die "alarm\n" }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 741 | alarm($timeout); | |||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 741 | my $status; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 741 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 129 | 741 | ▶ | while ($select->count) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 1104 | my @ready = $select->can_read; | |||||||||||||||||||||||||||||||||||||||||
| 131 | # can_read comes back empty when a signal interrupted the select, not | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # only at end of output. Looping on the handle count instead of on | ||||||||||||||||||||||||||||||||||||||||||
| 133 | # can_read keeps that from cutting the output short; a select that | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # keeps failing is bounded by the alarm above. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 932 | ▶ | next unless @ready; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 930 | for my $fh (@ready) { | |||||||||||||||||||||||||||||||||||||||||
| 137 | 1499 | my $buf; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 1499 | my $bytes = sysread($fh, $buf, 4096); | |||||||||||||||||||||||||||||||||||||||||
| 139 | 1499 | ▶ | if (!defined $bytes) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | # A signal interrupting the read is not end of output; treating | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # EINTR as EOF would silently truncate what the command wrote. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | next if $! == EINTR; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | $select->remove($fh); | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 1499 | ▶ | if (!$bytes) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 1138 | $select->remove($fh); | |||||||||||||||||||||||||||||||||||||||||
| 148 | 1138 | next; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 361 | ▶ | if ($fh == $out) { $stdout .= $buf } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 31 | else { $stderr .= $buf } | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | # Reaped under the alarm on purpose: a command that closes its output | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # and keeps running - 'exec >/dev/null 2>&1; sleep 300' - reaches this | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # point immediately, and an unguarded waitpid would let it outrun its | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # timeout by as much as it liked. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 569 | my $reaped = waitpid($pid, 0); | |||||||||||||||||||||||||||||||||||||||||
| 160 | 552 | ▶ | $status = $reaped > 0 ? $? : $child_status; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | # A signal handler may have reaped the child already, in which case | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # the wait above finds nothing and the status was recorded there. If | ||||||||||||||||||||||||||||||||||||||||||
| 164 | # nobody reaped it at all, the wait status is all there is - and it | ||||||||||||||||||||||||||||||||||||||||||
| 165 | # says the child was not ours. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 552 | ▶ | $status = $? unless defined $status; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 741 | alarm(0); | |||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 741 | ▶ | if ($timed_out) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 189 | _terminate($pid); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 189 | $exit_code = 124; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 189 | $error = "Command timed out after ${timeout}s"; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | elsif (defined $status) { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # A signalled child carries the signal number in the low byte and zero | ||||||||||||||||||||||||||||||||||||||||||
| 178 | # in the high byte, so $? >> 8 alone would report success. Use the | ||||||||||||||||||||||||||||||||||||||||||
| 179 | # shell convention instead. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 552 | ▶ | $exit_code = ($status & 127) ? 128 + ($status & 127) : $status >> 8; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 741 | close $out; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 741 | close $err; | |||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | # Nothing is written on success (the descriptor closes on exec), so a | ||||||||||||||||||||||||||||||||||||||||||
| 187 | # message here means the command never started. | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 741 | my $chdir_error = ''; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 741 | ▶ | unless ($timed_out) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 552 | my $got = sysread($chdir_r, $chdir_error, 4096); | |||||||||||||||||||||||||||||||||||||||||
| 191 | 552 | ▶ | $chdir_error = '' unless $got; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 741 | close $chdir_r; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 741 | ▶ | if (length $chdir_error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 60 | $exit_code = 1; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 60 | $error = $chdir_error; | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 741 | ▶ | if ($@ && !defined $exit_code) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ and not defined $exit_code
MC/DC0%0/2
$@ and not defined $exit_code
$@
not defined $exit_code
Truth table33%1/3
A and not(B)
A= $@B= not defined $exit_code
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | $exit_code = 1; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $error = "$@"; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | chomp $error; | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 741 | chomp $stdout; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 741 | chomp $stderr; | |||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 741 | ▶ | return { exit_code => $exit_code, stdout => $stdout, stderr => $stderr, (defined $error ? (error => $error) : ()) }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | # Stop a timed out, cancelled or interrupted command: SIGTERM the group, then | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # SIGKILL it if the grace period passes without the child being reaped. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | # Returns the child's status if this call reaped it, and undef if the child | ||||||||||||||||||||||||||||||||||||||||||
| 216 | # was never there to reap - only the reaper gets to see the status. | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 190 | ▶ | sub _terminate ($pid) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_terminate: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 190 | _signal_group($pid, 'TERM'); | |||||||||||||||||||||||||||||||||||||||||
| 219 | 190 | my $status = _reap_within_grace($pid); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 190 | ▶ | return $status if defined $status; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 18 | _signal_group($pid, 'KILL'); | |||||||||||||||||||||||||||||||||||||||||
| 222 | 18 | my $reaped = waitpid($pid, 0); | |||||||||||||||||||||||||||||||||||||||||
| 223 | 18 | ▶ | return $reaped > 0 ? $? : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | # Hand the signal on to whoever was going to receive it. A handler of the | ||||||||||||||||||||||||||||||||||||||||||
| 227 | # embedding program is called, with the signal name, the way Perl itself | ||||||||||||||||||||||||||||||||||||||||||
| 228 | # would call it; a default disposition is re-raised, so the signal keeps the | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # effect it usually has. | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 1 | ▶ | sub _chain_signal ($signal, $previous) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_chain_signal: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | ▶ | if (ref $previous) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | $previous->($signal); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | $SIG{$signal} = 'DEFAULT'; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | kill $signal, $$; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | # True while the embedding program has the signal ignored. Such a signal is | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # left alone: the program asked for the signal to have no effect on it, and | ||||||||||||||||||||||||||||||||||||||||||
| 241 | # the command inherits the disposition across the exec, so taking the signal | ||||||||||||||||||||||||||||||||||||||||||
| 242 | # over would only make program and command disagree about it. | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 2223 | ▶ | sub _signal_ignored ($signal) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_signal_ignored: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 2223 | my $handler = $SIG{$signal}; | |||||||||||||||||||||||||||||||||||||||||
| 245 | 2223 | ▶ | return defined $handler && !ref $handler && $handler eq 'IGNORE'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
defined $handler && !ref($handler) && $handler eq 'IGNORE'
defined $handler and not ref $handler
MC/DC66%2/3
defined $handler && !ref($handler) && $handler eq 'IGNORE'
defined $handler
not ref $handler
$handler eq 'IGNORE'
Truth table75%3/4
A and not(B) && C
A= defined $handlerB= not ref $handlerC= $handler eq 'IGNORE'
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | # Signal the command's whole process group, falling back to the bare child | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # when the group kill fails (setpgrp unsupported, or the child already gone). | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 208 | ▶ | sub _signal_group ($pid, $signal) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_signal_group: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 208 | ▶ | return kill($signal, -$pid) || kill($signal, $pid); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
kill($signal, -$pid) || kill($signal, $pid)
MC/DC0%0/2
kill($signal, -$pid) || kill($signal, $pid)
kill($signal, -$pid)
kill($signal, $pid)
Truth table33%1/3
A || B
A= kill($signal, -$pid)B= kill($signal, $pid)
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | # Wait for the child to go away, but no longer than the grace period. Returns | ||||||||||||||||||||||||||||||||||||||||||
| 255 | # the child's status if it was reaped here, and undef while it is still | ||||||||||||||||||||||||||||||||||||||||||
| 256 | # running. | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 190 | ▶ | sub _reap_within_grace ($pid) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reap_within_grace: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 190 | my $waited = 0; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 190 | ▶ | while ($waited < TERM_GRACE) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 1064 | my $reaped = waitpid($pid, WNOHANG); | |||||||||||||||||||||||||||||||||||||||||
| 261 | 1064 | ▶ | return $? if $reaped > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 892 | select undef, undef, undef, REAP_POLL; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 892 | $waited += REAP_POLL; | |||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 18 | my $reaped = waitpid($pid, WNOHANG); | |||||||||||||||||||||||||||||||||||||||||
| 266 | 18 | ▶ | return $reaped > 0 ? $? : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | # Timeouts alarm() cannot act on fall back to the server default, and then to | ||||||||||||||||||||||||||||||||||||||||||
| 270 | # FALLBACK_TIMEOUT, rather than disabling the alarm. | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 923 | ▶ | sub _effective_timeout ($self, $timeout) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_effective_timeout: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 923 | my ($usable) = grep { _usable_timeout($_) } $timeout, $self->timeout, FALLBACK_TIMEOUT; | |||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | # alarm() counts whole seconds, so half a second would become alarm(0) - | ||||||||||||||||||||||||||||||||||||||||||
| 275 | # switching the timeout off instead of firing at once. Round that up, and | ||||||||||||||||||||||||||||||||||||||||||
| 276 | # truncate the rest the way alarm() would, so the timeout reported back is | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # the one that actually ran. | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 923 | ▶ | return $usable < 1 ? 1 : int($usable); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 2769 | ▶ | sub _usable_timeout ($value) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usable_timeout: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 2769 | ▶ | return 0 unless defined $value && looks_like_number($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and &looks_like_number($value)
MC/DC100%2/2
defined $value and &looks_like_number($value)
defined $value
&looks_like_number($value)
Truth table100%3/3
A and B
A= defined $valueB= &looks_like_number($value)
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 2730 | ▶ | return 0 if $value != $value; # NaN | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 2710 | ▶ | return 0 unless $value > 0 && $value <= MAX_TIMEOUT; # Inf fails this too | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value > 0 and $value <= 2147483647
MC/DC100%2/2
$value > 0 and $value <= 2147483647
$value > 0
$value <= 2147483647
Truth table100%3/3
A and B
A= $value > 0B= $value <= 2147483647
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 2514 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 77 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | __END__ | ||||||||||||||||||||||||||||||||||||||||||