lib/Skilletor/Process.pm

filter: Statementstmt 92.3% 279 / 302 Branchbran 75.2% 137 / 182 Conditioncond 64.7% 46 / 71 MC/DCmcdc 48.7% 20 / 41 Subroutinesub 94.1% 32 / 34 Podpod 100% 2 / 2 totaltotal 81.6% 516 / 632 CC 132 SCAR 36.1
CC
132
Cov
83%
run_command
79.0
_run_win32
29.4
_win32_spawn
20.4
CRAP 36.9
« /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
BranchTrueFalseTotal
my $cmd = $args{"cmd"} or croak("Skilletor::Process->run_command needs cmd") 30400 0 30400
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
BranchTrueFalseTotal
if (defined $stdin) 91 30309 30400
38 91 $stdin = "$stdin";
39 91 ▶ utf8::downgrade( $stdin, 1 ) or croak __PACKAGE__.'->run_command: stdin must be bytes';
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($stdin, 1) or croak("Skilletor::Process->run_command: stdin must be bytes") 82 9 91
40 }
41 30391 ▶ if ( my $os = $args{win32} // ( WIN32 ? {} : undef ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $os = $args{"win32"} // undef) 29 30362 30391
Condition100%2/2
$args{"win32"} // undef
l!l
2930362
MC/DC100%1/1
$args{"win32"} // undef
$args{"win32"}
Truth table100%2/2
A
A= $args{"win32"}
Aresult
11
00
42 29 return _run_win32( $os, $file, \@argv, $stdin, %args );
43 }
44 30362 ▶ if ( defined $stdin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stdin) 78 30284 30362
45 78 ▶ pipe( $in_r, $in_w ) or croak 'pipe: '.$!;
Branch: 1 branch
BranchTrueFalseTotal
pipe $in_r, $in_w or croak('pipe: ' . $!) 78 0 78
46 }
47 30362 ▶ pipe( my $out_r, my $out_w ) or croak 'pipe: '.$!;
Branch: 1 branch
BranchTrueFalseTotal
pipe my $out_r, my $out_w or croak('pipe: ' . $!) 30362 0 30362
48 30362 ▶ pipe( my $err_r, my $err_w ) or croak 'pipe: '.$!;
Branch: 1 branch
BranchTrueFalseTotal
pipe my $err_r, my $err_w or croak('pipe: ' . $!) 30362 0 30362
49 30362 ▶ pipe( my $exec_r, my $exec_w ) or croak 'pipe: '.$!; # the child's exec errno, if any
Branch: 1 branch
BranchTrueFalseTotal
pipe my $exec_r, my $exec_w or croak('pipe: ' . $!) 30362 0 30362
50 30362 my $pid = fork;
51 30362 ▶ croak 'fork: '.$! unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 30362 30362
52 30362 ▶ if ( !$pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 632 29730 30362
53 632 close $_ for $out_r, $err_r, $exec_r;
54 632 ▶ if ($in_r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_r) { } 14 618 632
55 14 close $in_w;
56 14 ▶ open( STDIN, '<&', $in_r ) or POSIX::_exit(127);
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $in_r or POSIX::_exit(127) 14 0 14
57 }
58 else {
59 618 open( STDIN, '<', '/dev/null' );
60 }
61 632 ▶ open( STDOUT, '>&', $out_w ) or POSIX::_exit(127);
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $out_w or POSIX::_exit(127) 632 0 632
62 632 ▶ open( STDERR, '>&', $err_w ) or POSIX::_exit(127);
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $err_w or POSIX::_exit(127) 632 0 632
63 632 ▶ if ( defined $args{cwd} && length $args{cwd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"cwd"} and length $args{"cwd"}) 342 290 632
Condition66%2/3
defined $args{"cwd"} and length $args{"cwd"}
!ll&&!rl&&r
2900342
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"}
ABresult
0X0
100
111
64 342 ▶ unless ( chdir $args{cwd} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $args{"cwd"}) 0 342 342
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
BranchTrueFalseTotal
if $args{"env"} 620 12 632
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 // ""
l!l
6429666
MC/DC100%1/1
$stdin // ""
$stdin
Truth table100%2/2
A
A= $stdin
Aresult
11
00
80 29730 my $wsel = IO::Select->new;
81 29730 ▶ if ($in_w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_w) 64 29666 29730
82 64 close $in_r;
83 64 ▶ if ( length $pending ) { $in_w->blocking(0); $wsel->add($in_w) }
Branch: 1 branch
BranchTrueFalseTotal
if (length $pending) { } 57 7 64
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
BranchTrueFalseTotal
defined $args{'timeout_ms'} ? : 24733 4997 29730
90 29730 my $timed_out = 0;
91 29730 ▶ while ( $sel->count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sel->count) 52801 28546 81347
92 52801 ▶ my $wait = defined $deadline ? $deadline - Time::HiRes::time() : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $deadline ? : 42039 10762 52801
93 52801 ▶ if ( defined $wait && $wait <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wait and $wait <= 0) 1184 51617 52801
Condition100%3/3
defined $wait and $wait <= 0
!ll&&!rl&&r
10762408551184
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$wsel->count ? : 389 51228 51617
99 51617 ▶ for my $fh ( @{ $writable // [] } ) {
Condition100%2/2
$writable // []
l!l
504331184
MC/DC100%1/1
$writable // []
$writable
Truth table100%2/2
A
A= $writable
Aresult
11
00
100 284 my $n = syswrite $fh, $pending, 65536;
101 284 ▶ next if !defined $n && ( $!{EINTR} || $!{EAGAIN} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n and $!{"EINTR"} || $!{"EAGAIN"} 0 284 284
Condition16%1/6
not defined $n and $!{"EINTR"} || $!{"EAGAIN"}
!ll&&!rl&&r
28400
$!{"EINTR"} or $!{"EAGAIN"}
l!l&&r!l&&!r
000
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"}
ABCresult
0XX0
1000
11X1
1011
102 284 ▶ substr( $pending, 0, $n, '' ) if defined $n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $n 284 0 284
103 284 ▶ if ( !defined $n || !length $pending ) { # all written, or the child closed its stdin
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n and length $pending) 57 227 284
Condition66%2/3
defined $n and length $pending
!ll&&!rl&&r
057227
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
ABresult
0X0
100
111
104 57 $wsel->remove($fh);
105 57 close $fh;
106 }
107 }
108 51617 ▶ my @ready = @{ $readable // [] };
Condition100%2/2
$readable // []
l!l
504331184
MC/DC100%1/1
$readable // []
$readable
Truth table100%2/2
A
A= $readable
Aresult
11
00
109 51617 for my $fh (@ready) {
110 78404 my $n = sysread $fh, my $chunk, 65536;
111 78404 ▶ if ( !defined $n ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 78404 78404
112 0 ▶ next if $!{EINTR} || $!{EAGAIN};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} or $!{"EAGAIN"} 0 0 0
Condition0%0/3
$!{"EINTR"} or $!{"EAGAIN"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EINTR"} or $!{"EAGAIN"}
$!{"EINTR"} $!{"EAGAIN"}
Truth table0%0/3
A or B
A= $!{"EINTR"}B= $!{"EAGAIN"}
ABresult
1X1
011
000
113 0 $n = 0;
114 }
115 78404 ▶ if ( $n == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 57092 21312 78404
116 57092 $sel->remove($fh);
117 57092 next;
118 }
119 21312 ▶ $buf{ $fh == $out_r ? 'out' : 'err' } .= $chunk;
Branch: 1 branch
BranchTrueFalseTotal
$fh == $out_r ? : 11859 9453 21312
120 }
121 }
122 29730 ▶ if ($timed_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) 1184 28546 29730
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
BranchTrueFalseTotal
if ($sel->count and &Time::HiRes::time() < $grace) 1184 1184 2368
Condition66%2/3
$sel->count and &Time::HiRes::time() < $grace
!ll&&!rl&&r
118401184
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ($n) 2368 0 2368
129 0 ▶ $buf{ $fh == $out_r ? 'out' : 'err' } .= $chunk;
Branch: 1 branch
BranchTrueFalseTotal
$fh == $out_r ? : 0 0 0
130 }
131 }
132 1184 ▶ kill 'KILL', $pid if $sel->count;
Branch: 1 branch
BranchTrueFalseTotal
if $sel->count 0 1184 1184
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
BranchTrueFalseTotal
if (defined $msg and $msg =~ /\A(chdir|exec):(\d+)\z/) 12 29718 29730
Condition66%2/3
defined $msg and $msg =~ /\A(chdir|exec):(\d+)\z/
!ll&&!rl&&r
02971812
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/
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$status & 127 ? : 1184 28546 29730
$spawn_error ? : 12 29718 29730
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
BranchTrueFalseTotal
my $cmd = $args{"cmd"} or croak("Skilletor::Process->spawn_detached needs cmd") 14 0 14
164 14 my ( $file, @argv ) = @$cmd;
165 14 ▶ if ( my $os = $args{win32} // ( WIN32 ? {} : undef ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $os = $args{"win32"} // undef) 3 11 14
Condition100%2/2
$args{"win32"} // undef
l!l
311
MC/DC100%1/1
$args{"win32"} // undef
$args{"win32"}
Truth table100%2/2
A
A= $args{"win32"}
Aresult
11
00
166 3 ▶ my $detach = $os->{detach} // \&_win32_detach;
Condition50%1/2
$os->{"detach"} // \&Skilletor::Process::_win32_detach
l!l
30
MC/DC0%0/1
$os->{"detach"} // \&Skilletor::Process::_win32_detach
$os->{"detach"}
Truth table50%1/2
A
A= $os->{"detach"}
Aresult
11
00
167 3 ▶ my $program = _win32_program( $file, _env_value( $args{env} // \%ENV, 'PATH' ) );
Condition100%2/2
$args{"env"} // \%ENV
l!l
12
MC/DC100%1/1
$args{"env"} // \%ENV
$args{"env"}
Truth table100%2/2
A
A= $args{"env"}
Aresult
11
00
168 3 ▶ return 0 if !defined $program || $program =~ /\.(?:bat|cmd)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $program or $program =~ /\.(?:bat|cmd)\z/i 1 2 3
Condition66%2/3
not defined $program or $program =~ /\.(?:bat|cmd)\z/i
l!l&&r!l&&!r
102
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
_with_env($args{'env'}, sub { &$detach($program, $line); } ) ? : 1 1 2
171 }
172 11 my $pid = fork;
173 11 ▶ return 0 unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 11 11
174 11 ▶ if ( !$pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 2 9 11
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
BranchTrueFalseTotal
unless defined $grandchild 0 2 2
178 2 ▶ POSIX::_exit(0) if $grandchild; # the intermediate goes, the program runs on
Branch: 1 branch
BranchTrueFalseTotal
if $grandchild 0 2 2
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
BranchTrueFalseTotal
if $args{"env"} 2 0 2
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
BranchTrueFalseTotal
$? == 0 ? : 9 0 9
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
l!l
290
MC/DC0%0/1
$os->{"spawn"} // \&Skilletor::Process::_win32_spawn
$os->{"spawn"}
Truth table50%1/2
A
A= $os->{"spawn"}
Aresult
11
00
201 29 _std_fds();
202 29 my $in;
203 29 ▶ if ( defined $stdin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stdin) { } 4 25 29
204 4 $in = _tmpfile();
205 4 ▶ for ( my $off = 0; $off < length $stdin; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < length $stdin) 42 4 46
206 42 my $n = syswrite $in, $stdin, 65536, $off;
207 42 ▶ croak 'write: '.$! unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 42 42
208 42 $off += $n;
209 }
210 4 ▶ sysseek( $in, 0, 0 ) or croak 'seek: '.$!;
Branch: 1 branch
BranchTrueFalseTotal
sysseek $in, 0, 0 or croak('seek: ' . $!) 4 0 4
211 }
212 else {
213 25 ▶ open( $in, '<', File::Spec->devnull ) or croak 'open '.File::Spec->devnull.': '.$!;
Branch: 1 branch
BranchTrueFalseTotal
open $in, "<", "File::Spec"->devnull or croak('open ' . "File::Spec"->devnull . ': ' . $!) 25 0 25
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
BranchTrueFalseTotal
if (defined $args{"cwd"} and length $args{"cwd"}) 4 25 29
Condition66%2/3
defined $args{"cwd"} and length $args{"cwd"}
!ll&&!rl&&r
2504
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"}
ABresult
0X0
100
111
221 4 ▶ defined( $back = Cwd::getcwd() ) or croak 'getcwd: '.$!;
Branch: 1 branch
BranchTrueFalseTotal
defined($back = Cwd::getcwd()) or croak('getcwd: ' . $!) 4 0 4
222 4 ▶ chdir $args{cwd} or return _spawn_failed( chdir => 0 + $! );
Branch: 1 branch
BranchTrueFalseTotal
chdir $args{"cwd"} or return _spawn_failed("chdir", 0 + $!) 2 2 4
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
l!l
423
MC/DC100%1/1
$args{"env"} // \%ENV
$args{"env"}
Truth table100%2/2
A
A= $args{"env"}
Aresult
11
00
226 27 ▶ if ( !defined $program ) { $errno = Errno::ENOENT() }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $program) { } 2 25 27
elsif ($program =~ /\.(?:bat|cmd)\z/i) { } 2 23 25
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
BranchTrueFalseTotal
if defined $back 2 25 27
233 27 ▶ return _spawn_failed( exec => $errno // 0 ) unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 4 23 27
Condition50%1/2
$errno // 0
l!l
40
MC/DC0%0/1
$errno // 0
$errno
Truth table50%1/2
A
A= $errno
Aresult
11
00
234  
235 23 ▶ my $deadline = defined $args{timeout_ms} ? Time::HiRes::time() + $args{timeout_ms} / 1000 : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'timeout_ms'} ? : 2 21 23
236 23 my ( $timed_out, $nap ) = ( 0, 0.001 );
237 23 ▶ until ( waitpid $pid, POSIX::WNOHANG() ) { # 0 while it runs
Branch: 1 branch
BranchTrueFalseTotal
unless (waitpid $pid, 1) 1044 21 1065
238 1044 ▶ $ON_WAIT->() if $ON_WAIT;
Branch: 1 branch
BranchTrueFalseTotal
if $Skilletor::Process::ON_WAIT 270 774 1044
239 1044 ▶ my $left = defined $deadline ? $deadline - Time::HiRes::time() : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $deadline ? : 70 974 1044
240 1044 ▶ if ( defined $left && $left <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left and $left <= 0) 2 1042 1044
Condition100%3/3
defined $left and $left <= 0
!ll&&!rl&&r
974682
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
defined $left && $left < $nap ? : 2 1040 1042
Condition100%3/3
defined $left and $left < $nap
!ll&&!rl&&r
974662
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
ABresult
0X0
100
111
247 1042 ▶ $nap *= 2 if $nap < 0.016;
Branch: 1 branch
BranchTrueFalseTotal
if $nap < "0.016" 92 950 1042
248 }
249 23 my $status = $?;
250 23 close $in;
251 return {
252 23 ▶ code => ( $timed_out || ( $status & 127 ) ) ? undef : $status >> 8,
Branch: 2 branches
BranchTrueFalseTotal
$timed_out || $status & 127 ? : 2 21 23
$timed_out ? : 2 21 23
Condition66%2/3
$timed_out or $status & 127
l!l&&r!l&&!r
2021
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
open my $fh, "+>", undef or croak('tmpfile: ' . $!) 62 0 62
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
BranchTrueFalseTotal
sysseek $fh, 0, 0 or croak('seek: ' . $!) 46 0 46
276 46 my $buf = '';
277 46 ▶ 1 while sysread $fh, $buf, 65536, length $buf;
Branch: 1 branch
BranchTrueFalseTotal
sysread $fh, $buf, 65536, length $buf and do { '???' } 81 46 127
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
BranchTrueFalseTotal
unless $env 20 5 25
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
BranchTrueFalseTotal
if (defined $dup) 84 3 87
296 3 ▶ my $null = POSIX::open( File::Spec->devnull, POSIX::O_RDWR() ) // next;
Condition50%1/2
POSIX::open("File::Spec"->devnull, 2) // (next)
l!l
30
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)
Aresult
11
00
297 3 ▶ next if $null == $fd;
Branch: 1 branch
BranchTrueFalseTotal
if $null == $fd 3 0 3
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
BranchTrueFalseTotal
defined POSIX::dup2(fileno $handles->[$fd], $fd) or die 'dup2: ' . $! . "\n" 69 0 69
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)
l!l
690
$saved[$fd] // POSIX::open("File::Spec"->devnull, 2)
l!l&&r!l&&!r
6900
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)
ABresult
1X1
011
000
322 69 POSIX::dup2( $back, $fd );
323 69 POSIX::close($back);
324 }
325 23 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 23 23
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
BranchTrueFalseTotal
defined $key ? : 30 2 32
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
BranchTrueFalseTotal
unless defined $file and length $file 0 50 50
Condition33%1/3
defined $file and length $file
!ll&&!rl&&r
0050
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$name ne $file ? : 31 19 50
Condition50%1/2
$path // ""
l!l
190
MC/DC0%0/1
$path // ""
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
348 50 for my $base (@bases) {
349 48 ▶ for my $try ( ( $has_ext ? $base : () ), "$base.com", "$base.exe" ) {
Branch: 1 branch
BranchTrueFalseTotal
$has_ext ? : 26 22 48
350 64 ▶ return $try if -f $try;
Branch: 1 branch
BranchTrueFalseTotal
if -f $try 41 23 64
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
BranchTrueFalseTotal
unless length $arg 6 157 163
363 157 ▶ return $arg unless $arg =~ /[ \t\n\x0B\f\r"]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /[ \t\n\x0B\f\r"]/ 75 82 157
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
BranchTrueFalseTotal
defined &Win32::SetChildShowWindow ? : 0 0 0
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
BranchTrueFalseTotal
if (defined &Win32::SetChildShowWindow) 0 0 0
378 0 ▶ defined $shown ? Win32::SetChildShowWindow($shown) : Win32::SetChildShowWindow();
Branch: 1 branch
BranchTrueFalseTotal
defined $shown ? : 0 0 0
379 }
380 0 ▶ return $pid > 0 ? $pid : ( undef, $errno );
Branch: 1 branch
BranchTrueFalseTotal
$pid > 0 ? : 0 0 0
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
BranchTrueFalseTotal
Win32::Process::Create($process, $program, $line, 0, 134218240, '.') ? : 0 0 0
391 }
392  
393 693 1;
394  
395 __END__
« /usr/local/lib/perl5/site_perl/5.44.0/Sub/Defer.pm ↑ Summary /usr/local/lib/perl5/5.44.0/Pod/InputObjects.pm »