« /usr/local/lib/perl5/site_perl/5.44.0/Sub/Defer.pm
↑ Summary
/usr/local/lib/perl5/5.44.0/Pod/InputObjects.pm »
| 1 | package Skilletor::Process; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Run a program without a shell – cwd, environment, timeout, captured output | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 693 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 693 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 693 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 693 | ▶ | use Cwd (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 693 | ▶ | use Errno (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 693 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 693 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 693 | ▶ | use IO::Handle (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 693 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 693 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 693 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 693 | our $VERSION = '0.008000'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 693 | our @EXPORT_OK = qw( run_command spawn_detached ); | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 693 | ▶ | use constant WIN32 => $^O eq 'MSWin32'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # The creation flags of the detached spawn on Windows (WinBase.h). | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 693 | ▶ | use constant CREATE_NEW_PROCESS_GROUP => 0x0000_0200; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 693 | ▶ | use constant CREATE_NO_WINDOW => 0x0800_0000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # Called, without arguments, each time the Windows path looks whether its child has ended | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # (Skilletor::State's lock refreshes ride on it there). | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 693 | our $ON_WAIT; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 30400 | ▶ | sub run_command { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_command: called CC=59 SCAR=43.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 30400 | my ( %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 30400 | ▶ | my $cmd = $args{cmd} or croak __PACKAGE__.'->run_command needs cmd'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 30400 | my ( $file, @argv ) = @$cmd; | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 30400 | my ( $stdin, $in_r, $in_w ) = ( $args{stdin} ); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 30400 | ▶ | if ( defined $stdin ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 91 | $stdin = "$stdin"; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 91 | ▶ | utf8::downgrade( $stdin, 1 ) or croak __PACKAGE__.'->run_command: stdin must be bytes'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 30391 | ▶ | if ( my $os = $args{win32} // ( WIN32 ? {} : undef ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$args{"win32"} // undef
MC/DC100%1/1
$args{"win32"} // undef
$args{"win32"}
Truth table100%2/2
A
A= $args{"win32"}
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 29 | return _run_win32( $os, $file, \@argv, $stdin, %args ); | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 30362 | ▶ | if ( defined $stdin ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 78 | ▶ | pipe( $in_r, $in_w ) or croak 'pipe: '.$!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 30362 | ▶ | pipe( my $out_r, my $out_w ) or croak 'pipe: '.$!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 30362 | ▶ | pipe( my $err_r, my $err_w ) or croak 'pipe: '.$!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 30362 | ▶ | pipe( my $exec_r, my $exec_w ) or croak 'pipe: '.$!; # the child's exec errno, if any | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 30362 | my $pid = fork; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 30362 | ▶ | croak 'fork: '.$! unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 30362 | ▶ | if ( !$pid ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 632 | close $_ for $out_r, $err_r, $exec_r; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 632 | ▶ | if ($in_r) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 14 | close $in_w; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 14 | ▶ | open( STDIN, '<&', $in_r ) or POSIX::_exit(127); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 618 | open( STDIN, '<', '/dev/null' ); | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 632 | ▶ | open( STDOUT, '>&', $out_w ) or POSIX::_exit(127); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 632 | ▶ | open( STDERR, '>&', $err_w ) or POSIX::_exit(127); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 632 | ▶ | if ( defined $args{cwd} && length $args{cwd} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args{"cwd"} and length $args{"cwd"}
MC/DC50%1/2
defined $args{"cwd"} and length $args{"cwd"}
defined $args{"cwd"}
length $args{"cwd"}
Truth table66%2/3
A and B
A= defined $args{"cwd"}B= length $args{"cwd"}
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 342 | ▶ | unless ( chdir $args{cwd} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | syswrite $exec_w, 'chdir:'.( 0 + $! ); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | POSIX::_exit(127); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 632 | ▶ | %ENV = %{ $args{env} } if $args{env}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 693 | ▶ | { no warnings 'exec'; exec {$file} $file, @argv; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | syswrite $exec_w, 'exec:'.( 0 + $! ); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | POSIX::_exit(127); | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 29730 | close $_ for $out_w, $err_w, $exec_w; | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | # stdin goes in as the child takes it, between the reads: a child that writes a full pipe | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # before it reads stdin would otherwise wait on us while we wait on it. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 29730 | local $SIG{PIPE} = 'IGNORE'; # a child that exits without reading all of stdin | |||||||||||||||||||||||||||||||||||||||||
| 79 | 29730 | ▶ | my $pending = $stdin // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$stdin // ""
MC/DC100%1/1
$stdin // ""
$stdin
Truth table100%2/2
A
A= $stdin
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 29730 | my $wsel = IO::Select->new; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 29730 | ▶ | if ($in_w) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 64 | close $in_r; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 64 | ▶ | if ( length $pending ) { $in_w->blocking(0); $wsel->add($in_w) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 7 | else { close $in_w } | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 29730 | my %buf = ( out => '', err => '' ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 29730 | my $sel = IO::Select->new( $out_r, $err_r ); | |||||||||||||||||||||||||||||||||||||||||
| 89 | 29730 | ▶ | my $deadline = defined $args{timeout_ms} ? Time::HiRes::time() + $args{timeout_ms} / 1000 : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 29730 | my $timed_out = 0; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 29730 | ▶ | while ( $sel->count ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 52801 | ▶ | my $wait = defined $deadline ? $deadline - Time::HiRes::time() : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 52801 | ▶ | if ( defined $wait && $wait <= 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $wait and $wait <= 0
MC/DC100%2/2
defined $wait and $wait <= 0
defined $wait
$wait <= 0
Truth table100%3/3
A and B
A= defined $waitB= $wait <= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1184 | $timed_out = 1; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 1184 | kill 'TERM', $pid; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 1184 | last; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 51617 | ▶ | my ( $readable, $writable ) = IO::Select->select( $sel, ( $wsel->count ? $wsel : undef ), undef, $wait ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 51617 | ▶ | for my $fh ( @{ $writable // [] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$writable // []
MC/DC100%1/1
$writable // []
$writable
Truth table100%2/2
A
A= $writable
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 284 | my $n = syswrite $fh, $pending, 65536; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 284 | ▶ | next if !defined $n && ( $!{EINTR} || $!{EAGAIN} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
not defined $n and $!{"EINTR"} || $!{"EAGAIN"}
$!{"EINTR"} or $!{"EAGAIN"}
MC/DC0%0/3
not defined $n and $!{"EINTR"} || $!{"EAGAIN"}
not defined $n
$!{"EINTR"}
$!{"EAGAIN"}
Truth table25%1/4
not(A) and B or C
A= not defined $nB= $!{"EINTR"}C= $!{"EAGAIN"}
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 284 | ▶ | substr( $pending, 0, $n, '' ) if defined $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 284 | ▶ | if ( !defined $n || !length $pending ) { # all written, or the child closed its stdin | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $n and length $pending
MC/DC50%1/2
defined $n and length $pending
defined $n
length $pending
Truth table66%2/3
A and B
A= defined $nB= length $pending
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 57 | $wsel->remove($fh); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 57 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 51617 | ▶ | my @ready = @{ $readable // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$readable // []
MC/DC100%1/1
$readable // []
$readable
Truth table100%2/2
A
A= $readable
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 51617 | for my $fh (@ready) { | |||||||||||||||||||||||||||||||||||||||||
| 110 | 78404 | my $n = sysread $fh, my $chunk, 65536; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 78404 | ▶ | if ( !defined $n ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | next if $!{EINTR} || $!{EAGAIN}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$!{"EINTR"} or $!{"EAGAIN"}
MC/DC0%0/2
$!{"EINTR"} or $!{"EAGAIN"}
$!{"EINTR"}
$!{"EAGAIN"}
Truth table0%0/3
A or B
A= $!{"EINTR"}B= $!{"EAGAIN"}
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $n = 0; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 78404 | ▶ | if ( $n == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 57092 | $sel->remove($fh); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 57092 | next; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 21312 | ▶ | $buf{ $fh == $out_r ? 'out' : 'err' } .= $chunk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 29730 | ▶ | if ($timed_out) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | # drain what is left without waiting on a child that ignores TERM for long | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 1184 | my $grace = Time::HiRes::time() + 2; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 1184 | ▶ | while ( $sel->count && Time::HiRes::time() < $grace ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$sel->count and &Time::HiRes::time() < $grace
MC/DC50%1/2
$sel->count and &Time::HiRes::time() < $grace
$sel->count
&Time::HiRes::time() < $grace
Truth table66%2/3
A and B
A= $sel->countB= &Time::HiRes::time() < $grace
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 1184 | for my $fh ( $sel->can_read(0.1) ) { | |||||||||||||||||||||||||||||||||||||||||
| 127 | 2368 | my $n = sysread $fh, my $chunk, 65536; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 2368 | ▶ | if ( !$n ) { $sel->remove($fh); next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $buf{ $fh == $out_r ? 'out' : 'err' } .= $chunk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 1184 | ▶ | kill 'KILL', $pid if $sel->count; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 29730 | close $_ for $out_r, $err_r, $wsel->handles; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 29730 | waitpid $pid, 0; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 29730 | my $status = $?; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 29730 | my $spawn_error; | |||||||||||||||||||||||||||||||||||||||||
| 138 | { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 29730 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 29730 | my $msg = <$exec_r>; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 29730 | close $exec_r; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 29730 | ▶ | if ( defined $msg && $msg =~ /\A(chdir|exec):(\d+)\z/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $msg and $msg =~ /\A(chdir|exec):(\d+)\z/
MC/DC50%1/2
defined $msg and $msg =~ /\A(chdir|exec):(\d+)\z/
defined $msg
$msg =~ /\A(chdir|exec):(\d+)\z/
Truth table66%2/3
A and B
A= defined $msgB= $msg =~ /\A(chdir|exec):(\d+)\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 12 | local $! = $2; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 12 | $spawn_error = { stage => $1, errno => 0 + $2, message => "$!" }; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 148 | code => ( $status & 127 ) ? undef : $status >> 8, | ||||||||||||||||||||||||||||||||||||||||||
| 149 | signal => $status & 127, | ||||||||||||||||||||||||||||||||||||||||||
| 150 | stdout => $buf{out}, | ||||||||||||||||||||||||||||||||||||||||||
| 151 | stderr => $buf{err}, | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 29730 | ▶ | timed_out => $timed_out, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | ( $spawn_error ? ( spawn_error => $spawn_error ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | # Start a program and leave it running on its own: a session of its own (setsid, so a kill of the | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # caller's process group spares it), stdio on /dev/null, never waited for – the intermediate child | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # that starts it exits at once, so no zombie is left behind. Node's spawn with `detached` and | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # `stdio: "ignore"`, `unref()`ed. True when the program was started (its exec is not awaited). | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 14 | ▶ | sub spawn_detached { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spawn_detached: called CC=15 SCAR=27.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 14 | my ( %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 14 | ▶ | my $cmd = $args{cmd} or croak __PACKAGE__.'->spawn_detached needs cmd'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 14 | my ( $file, @argv ) = @$cmd; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 14 | ▶ | if ( my $os = $args{win32} // ( WIN32 ? {} : undef ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$args{"win32"} // undef
MC/DC100%1/1
$args{"win32"} // undef
$args{"win32"}
Truth table100%2/2
A
A= $args{"win32"}
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | ▶ | my $detach = $os->{detach} // \&_win32_detach; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$os->{"detach"} // \&Skilletor::Process::_win32_detach
MC/DC0%0/1
$os->{"detach"} // \&Skilletor::Process::_win32_detach
$os->{"detach"}
Truth table50%1/2
A
A= $os->{"detach"}
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | ▶ | my $program = _win32_program( $file, _env_value( $args{env} // \%ENV, 'PATH' ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{"env"} // \%ENV
MC/DC100%1/1
$args{"env"} // \%ENV
$args{"env"}
Truth table100%2/2
A
A= $args{"env"}
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | ▶ | return 0 if !defined $program || $program =~ /\.(?:bat|cmd)\z/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $program or $program =~ /\.(?:bat|cmd)\z/i
MC/DC50%1/2
not defined $program or $program =~ /\.(?:bat|cmd)\z/i
not defined $program
$program =~ /\.(?:bat|cmd)\z/i
Truth table66%2/3
not(A) or B
A= not defined $programB= $program =~ /\.(?:bat|cmd)\z/i
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 2 | my $line = join ' ', map { _win32_quote($_) } $file, @argv; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 2 | ▶ | return _with_env( $args{env}, sub { $detach->( $program, $line ) } ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 11 | my $pid = fork; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 11 | ▶ | return 0 unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 11 | ▶ | if ( !$pid ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 2 | POSIX::setsid(); | |||||||||||||||||||||||||||||||||||||||||
| 176 | 2 | my $grandchild = fork; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 2 | ▶ | POSIX::_exit(1) unless defined $grandchild; # no second fork: nothing started | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 2 | ▶ | POSIX::_exit(0) if $grandchild; # the intermediate goes, the program runs on | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 2 | open( STDIN, '<', '/dev/null' ); | |||||||||||||||||||||||||||||||||||||||||
| 180 | 2 | open( STDOUT, '>', '/dev/null' ); | |||||||||||||||||||||||||||||||||||||||||
| 181 | 2 | open( STDERR, '>', '/dev/null' ); | |||||||||||||||||||||||||||||||||||||||||
| 182 | 2 | ▶ | %ENV = %{ $args{env} } if $args{env}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 693 | ▶ | { no warnings 'exec'; exec {$file} $file, @argv; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | POSIX::_exit(127); | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 9 | waitpid $pid, 0; | |||||||||||||||||||||||||||||||||||||||||
| 187 | 9 | ▶ | return $? == 0 ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | #### Windows ########################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # No fork, no select() on pipes, no signals: the child's stdin, stdout and stderr are anonymous | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # temp files (on Windows deleted once their last handle closes), put on fds 0 to 2 for the moment | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # of a P_NOWAIT spawn; this process looks every few ms whether the child has ended, terminates it | ||||||||||||||||||||||||||||||||||||||||||
| 195 | # at the deadline, then reads what it wrote. What touches the system is in the primitives a test | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # replaces ($args{win32}): spawn and detach; the rest runs on any system. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 29 | ▶ | sub _run_win32 { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_win32: called CC=29 SCAR=33.8
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 29 | my ( $os, $file, $argv, $stdin, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 29 | ▶ | my $spawn = $os->{spawn} // \&_win32_spawn; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$os->{"spawn"} // \&Skilletor::Process::_win32_spawn
MC/DC0%0/1
$os->{"spawn"} // \&Skilletor::Process::_win32_spawn
$os->{"spawn"}
Truth table50%1/2
A
A= $os->{"spawn"}
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 29 | _std_fds(); | |||||||||||||||||||||||||||||||||||||||||
| 202 | 29 | my $in; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 29 | ▶ | if ( defined $stdin ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 4 | $in = _tmpfile(); | |||||||||||||||||||||||||||||||||||||||||
| 205 | 4 | ▶ | for ( my $off = 0; $off < length $stdin; ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 42 | my $n = syswrite $in, $stdin, 65536, $off; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 42 | ▶ | croak 'write: '.$! unless defined $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 42 | $off += $n; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 4 | ▶ | sysseek( $in, 0, 0 ) or croak 'seek: '.$!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 25 | ▶ | open( $in, '<', File::Spec->devnull ) or croak 'open '.File::Spec->devnull.': '.$!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 29 | my ( $out, $err ) = ( _tmpfile(), _tmpfile() ); | |||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | # The stages of the POSIX child, in its order: chdir, then the program – looked up with the | ||||||||||||||||||||||||||||||||||||||||||
| 218 | # child's PATH, from the child's cwd. | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 29 | my $back; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 29 | ▶ | if ( defined $args{cwd} && length $args{cwd} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args{"cwd"} and length $args{"cwd"}
MC/DC50%1/2
defined $args{"cwd"} and length $args{"cwd"}
defined $args{"cwd"}
length $args{"cwd"}
Truth table66%2/3
A and B
A= defined $args{"cwd"}B= length $args{"cwd"}
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 4 | ▶ | defined( $back = Cwd::getcwd() ) or croak 'getcwd: '.$!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 4 | ▶ | chdir $args{cwd} or return _spawn_failed( chdir => 0 + $! ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 27 | my ( $pid, $errno ); | |||||||||||||||||||||||||||||||||||||||||
| 225 | 27 | ▶ | my $program = _win32_program( $file, _env_value( $args{env} // \%ENV, 'PATH' ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{"env"} // \%ENV
MC/DC100%1/1
$args{"env"} // \%ENV
$args{"env"}
Truth table100%2/2
A
A= $args{"env"}
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 27 | ▶ | if ( !defined $program ) { $errno = Errno::ENOENT() } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 2 | elsif ( $program =~ /\.(?:bat|cmd)\z/i ) { $errno = Errno::EINVAL() } # never through cmd.exe, as Node | |||||||||||||||||||||||||||||||||||||||||
| 228 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 23 | my @line = map { _win32_quote($_) } $file, @$argv; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 23 | ▶ | ( $pid, $errno ) = _with_env( $args{env}, sub { _with_stdio( [ $in, $out, $err ], sub { $spawn->( $program, @line ) } ) } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 27 | ▶ | chdir $back if defined $back; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 27 | ▶ | return _spawn_failed( exec => $errno // 0 ) unless $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$errno // 0
MC/DC0%0/1
$errno // 0
$errno
Truth table50%1/2
A
A= $errno
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 23 | ▶ | my $deadline = defined $args{timeout_ms} ? Time::HiRes::time() + $args{timeout_ms} / 1000 : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 23 | my ( $timed_out, $nap ) = ( 0, 0.001 ); | |||||||||||||||||||||||||||||||||||||||||
| 237 | 23 | ▶ | until ( waitpid $pid, POSIX::WNOHANG() ) { # 0 while it runs | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 1044 | ▶ | $ON_WAIT->() if $ON_WAIT; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 1044 | ▶ | my $left = defined $deadline ? $deadline - Time::HiRes::time() : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 1044 | ▶ | if ( defined $left && $left <= 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $left and $left <= 0
MC/DC100%2/2
defined $left and $left <= 0
defined $left
$left <= 0
Truth table100%3/3
A and B
A= defined $leftB= $left <= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 2 | $timed_out = 1; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 2 | kill 'KILL', $pid; # TerminateProcess, what Node's timeout does there | |||||||||||||||||||||||||||||||||||||||||
| 243 | 2 | waitpid $pid, 0; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 1042 | ▶ | Time::HiRes::sleep( defined $left && $left < $nap ? $left : $nap ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $left and $left < $nap
MC/DC100%2/2
defined $left and $left < $nap
defined $left
$left < $nap
Truth table100%3/3
A and B
A= defined $leftB= $left < $nap
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 1042 | ▶ | $nap *= 2 if $nap < 0.016; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 23 | my $status = $?; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 23 | close $in; | |||||||||||||||||||||||||||||||||||||||||
| 251 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 23 | ▶ | code => ( $timed_out || ( $status & 127 ) ) ? undef : $status >> 8, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$timed_out or $status & 127
MC/DC50%1/2
$timed_out or $status & 127
$timed_out
$status & 127
Truth table66%2/3
A or B
A= $timed_outB= $status & 127
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | signal => $timed_out ? 15 : $status & 127, # a timeout as Node reports it there: SIGTERM | ||||||||||||||||||||||||||||||||||||||||||
| 254 | stdout => _drain($out), | ||||||||||||||||||||||||||||||||||||||||||
| 255 | stderr => _drain($err), | ||||||||||||||||||||||||||||||||||||||||||
| 256 | timed_out => $timed_out, | ||||||||||||||||||||||||||||||||||||||||||
| 257 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | # What the POSIX path returns for a child whose chdir or exec failed. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 6 | ▶ | sub _spawn_failed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_spawn_failed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 6 | my ( $stage, $errno ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 6 | local $! = $errno; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 6 | return { code => 127, signal => 0, stdout => '', stderr => '', timed_out => 0, spawn_error => { stage => $stage, errno => 0 + $errno, message => "$!" } }; | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 62 | ▶ | sub _tmpfile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tmpfile: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 62 | ▶ | open( my $fh, '+>', undef ) or croak 'tmpfile: '.$!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 62 | binmode $fh; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 62 | return $fh; | |||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 46 | ▶ | sub _drain { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_drain: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 46 | my ( $fh ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 46 | ▶ | sysseek( $fh, 0, 0 ) or croak 'seek: '.$!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 46 | my $buf = ''; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 46 | ▶ | 1 while sysread $fh, $buf, 65536, length $buf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 46 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 46 | return $buf; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | # Run $code with %$env as the whole environment, when one is given. | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 25 | ▶ | sub _with_env { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_with_env: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 25 | my ( $env, $code ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 25 | ▶ | return $code->() unless $env; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 5 | local %ENV = %$env; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 5 | return $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | # fds 0 to 2 open – on the null device where one is not (a process started without standard | ||||||||||||||||||||||||||||||||||||||||||
| 291 | # handles, as the detached one on Windows) – so no temp file takes the number of one. | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 29 | ▶ | sub _std_fds { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_std_fds: called CC=5 SCAR=17.0
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 29 | for my $fd ( 0 .. 2 ) { | |||||||||||||||||||||||||||||||||||||||||
| 294 | 87 | my $dup = POSIX::dup($fd); | |||||||||||||||||||||||||||||||||||||||||
| 295 | 87 | ▶ | if ( defined $dup ) { POSIX::close($dup); next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 3 | ▶ | my $null = POSIX::open( File::Spec->devnull, POSIX::O_RDWR() ) // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
POSIX::open("File::Spec"->devnull, 2) // (next)
MC/DC0%0/1
POSIX::open("File::Spec"->devnull, 2) // (next)
POSIX::open("File::Spec"->devnull, 2)
Truth table50%1/2
A
A= POSIX::open("File::Spec"->devnull, 2)
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 3 | ▶ | next if $null == $fd; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | POSIX::dup2( $null, $fd ); | |||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | POSIX::close($null); | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | # Run $code with fds 0, 1 and 2 on the given handles – what a child spawned meanwhile gets as its | ||||||||||||||||||||||||||||||||||||||||||
| 304 | # stdin, stdout and stderr (on Windows a dup2 onto them sets the standard handles) – then put back | ||||||||||||||||||||||||||||||||||||||||||
| 305 | # what they were. At the fd level, so STDIN, STDOUT and STDERR keep their PerlIO layers; flushed | ||||||||||||||||||||||||||||||||||||||||||
| 306 | # first, so nothing this process printed lands in the child's output. | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 23 | ▶ | sub _with_stdio { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_with_stdio: called CC=8 SCAR=21.3
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 23 | my ( $handles, $code ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 23 | $_->flush for \*STDOUT, \*STDERR; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 23 | my @saved = map { POSIX::dup($_) } 0 .. 2; | |||||||||||||||||||||||||||||||||||||||||
| 311 | 23 | my @r; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 23 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 313 | 23 | for my $fd ( 0 .. 2 ) { | |||||||||||||||||||||||||||||||||||||||||
| 314 | 69 | ▶ | defined POSIX::dup2( fileno( $handles->[$fd] ), $fd ) or die 'dup2: '.$!."\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 23 | @r = $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 317 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 318 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 23 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 320 | 23 | for my $fd ( reverse 0 .. 2 ) { | |||||||||||||||||||||||||||||||||||||||||
| 321 | 69 | ▶ | my $back = $saved[$fd] // POSIX::open( File::Spec->devnull, POSIX::O_RDWR() ) // next; | ||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($saved[$fd] // POSIX::open("File::Spec"->devnull, 2)) // (next)
$saved[$fd] // POSIX::open("File::Spec"->devnull, 2)
MC/DC0%0/2
($saved[$fd] // POSIX::open("File::Spec"->devnull, 2)) // (next)
$saved[$fd]
POSIX::open("File::Spec"->devnull, 2)
Truth table33%1/3
A // B
A= $saved[$fd]B= POSIX::open("File::Spec"->devnull, 2)
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 69 | POSIX::dup2( $back, $fd ); | |||||||||||||||||||||||||||||||||||||||||
| 323 | 69 | POSIX::close($back); | |||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 23 | ▶ | die $err unless $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 23 | return @r; | |||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | # A variable of %$env as Windows looks it up: its name in any case. | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 32 | ▶ | sub _env_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_env_value: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 32 | my ( $env, $name ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 332 | 32 | my ($key) = sort grep { uc($_) eq uc($name) } keys %$env; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 32 | ▶ | return defined $key ? $env->{$key} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | # The program a Windows spawn starts, found as libuv finds it for Node's spawn, but never in the | ||||||||||||||||||||||||||||||||||||||||||
| 337 | # working directory first: a name with a directory part there (relative to the cwd), a bare name | ||||||||||||||||||||||||||||||||||||||||||
| 338 | # in each PATH entry; in each place the name as it is when it has an extension, then with .com, | ||||||||||||||||||||||||||||||||||||||||||
| 339 | # then with .exe appended. undef when there is none. | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 50 | ▶ | sub _win32_program { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_win32_program: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 50 | my ( $file, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 50 | ▶ | return undef unless defined $file && length $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $file and length $file
MC/DC0%0/2
defined $file and length $file
defined $file
length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 50 | my ($name) = $file =~ m{([^/\\:]*)\z}; | |||||||||||||||||||||||||||||||||||||||||
| 344 | 50 | my $has_ext = $name =~ /\.(?=.)/s; | |||||||||||||||||||||||||||||||||||||||||
| 345 | my @bases = $name ne $file | ||||||||||||||||||||||||||||||||||||||||||
| 346 | ? ($file) | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 50 | ▶ | : map { File::Spec->catfile( $_, $file ) } grep { length } map { s/\A"(.*)"\z/$1/sr } split /;/, $path // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$path // ""
MC/DC0%0/1
$path // ""
$path
Truth table50%1/2
A
A= $path
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | 50 | for my $base (@bases) { | |||||||||||||||||||||||||||||||||||||||||
| 349 | 48 | ▶ | for my $try ( ( $has_ext ? $base : () ), "$base.com", "$base.exe" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | 64 | ▶ | return $try if -f $try; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||
| 353 | 9 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | # One argument of a Windows command line, so that the C runtime's parser (CommandLineToArgvW) | ||||||||||||||||||||||||||||||||||||||||||
| 357 | # reads it back as it is: libuv's quote_cmd_arg. As it is unless empty or holding whitespace or a | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # quote; else in quotes, a quote escaped, the backslashes before a quote or the end doubled. Any | ||||||||||||||||||||||||||||||||||||||||||
| 359 | # whitespace quotes, not only space and tab: Perl's spawn would quote it otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 163 | ▶ | sub _win32_quote { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_win32_quote: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 163 | my ( $arg ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 163 | ▶ | return '""' unless length $arg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 157 | ▶ | return $arg unless $arg =~ /[ \t\n\x0B\f\r"]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 364 | 82 | ( my $q = $arg ) =~ s/(\\*)"/$1$1\\"/g; | |||||||||||||||||||||||||||||||||||||||||
| 365 | 82 | $q =~ s/(\\+)\z/$1$1/; | |||||||||||||||||||||||||||||||||||||||||
| 366 | 82 | return qq{"$q"}; | |||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | # The spawn: Perl's own P_NOWAIT spawn of the program found (passing fds 0 to 2 as the standard | ||||||||||||||||||||||||||||||||||||||||||
| 370 | # handles); the arguments come quoted, so Perl's command line keeps them as they are. A console | ||||||||||||||||||||||||||||||||||||||||||
| 371 | # of its own – when this process has none to share – stays hidden. The pid, else undef and errno. | ||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | ▶ | sub _win32_spawn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_win32_spawn: not called CC=5 SCAR=30.1
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | my ( $program, @line ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | ▶ | my $shown = defined &Win32::SetChildShowWindow ? Win32::SetChildShowWindow(0) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | ▶ | my $pid = do { no warnings 'exec'; system { $program } 1, @line }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | my $errno = 0 + $!; | |||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | if ( defined &Win32::SetChildShowWindow ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | ▶ | defined $shown ? Win32::SetChildShowWindow($shown) : Win32::SetChildShowWindow(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | return $pid > 0 ? $pid : ( undef, $errno ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | # The detached spawn: no handle inherited – the hook's stdout must not stay open in a child that | ||||||||||||||||||||||||||||||||||||||||||
| 384 | # outlives it – in a process group of its own, with a console of its own that has no window, so | ||||||||||||||||||||||||||||||||||||||||||
| 385 | # neither it nor the programs it runs flash one. True when it was started. | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | ▶ | sub _win32_detach { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_win32_detach: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | my ( $program, $line ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | require Win32::Process; | |||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | my $process; | |||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | ▶ | return Win32::Process::Create( $process, $program, $line, 0, CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW, '.' ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | 693 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | __END__ | ||||||||||||||||||||||||||||||||||||||||||