Fork/Child.pm

filter: Statementstmt 83.8% 176 / 210 Branchbran 58.5% 89 / 152 Conditioncond 41.6% 10 / 24 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 75.0% 27 / 36 Podpod 100% 9 / 9 totaltotal 70.4% 314 / 446 CC 86 SCAR 23.0
CC
86
Cov
71%
_cancel
26.5
stdin_write
16.4
_on_timeout_kill
12.0
CRAP 10.0
« Fork.pm ↑ Summary Fork/Exit.pm »
1 package Linux::Event::Fork::Child;
2 40 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 40 our $VERSION = '0.007';
7  
8 40 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Errno qw(EAGAIN EINTR EPIPE);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 ▶use Linux::Event::Fork::Exit ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 ▶sub _new ($class, %args) {
Subroutine _new: called CC=6 SCAR=18.2
16 my $self = bless {
17 loop => delete $args{loop},
18 pid => delete $args{pid},
19  
20 tag => delete $args{tag},
21 data => delete $args{data},
22  
23 out_r => delete $args{out_r},
24 err_r => delete $args{err_r},
25 in_w => delete $args{in_w},
26  
27 on_stdout => delete $args{on_stdout},
28 on_stderr => delete $args{on_stderr},
29 on_exit => delete $args{on_exit},
30  
31 timeout_id => undef,
32 timeout_kill_id => undef,
33 on_timeout => delete $args{on_timeout},
34 timeout => delete $args{timeout},
35 timeout_kill => delete $args{timeout_kill},
36 timed_out => 0,
37  
38 capture_stdout => delete $args{capture_stdout},
39 capture_stderr => delete $args{capture_stderr},
40  
41 w_out => undef,
42 w_err => undef,
43 w_in => undef,
44 sub_pid => undef,
45  
46 saw_exit => 0,
47 exit => undef,
48  
49 eof_out => 1,
50 eof_err => 1,
51  
52 in_buf => '',
53 in_off => 0,
54 in_close_when_empty => 0,
55  
56 _canceled => 0,
57  
58 managed_by_fork => delete $args{managed_by_fork},
59 56 }, $class;
60  
61 56 ▶ croak "loop missing" if !$self->{loop};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"loop"} 0 56 56
62 56 ▶ croak "pid missing" if !$self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pid"} 0 56 56
63  
64 56 ▶ croak "unknown args: " . join(", ", sort keys %args) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 56 56
65  
66 56 ▶ $self->{eof_out} = $self->{out_r} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'out_r'} ? : 7 49 56
67 56 ▶ $self->{eof_err} = $self->{err_r} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'err_r'} ? : 4 52 56
68  
69 56 return $self;
70 }
71  
72 0 ▶sub exit ($self) { return $self->{exit} }
Subroutine exit: not called CC=1 SCAR=6.9
Pod documented
73 0 ▶sub pid ($self) { return $self->{pid} }
Subroutine pid: not called CC=1 SCAR=6.9
Pod documented
74 0 ▶sub loop ($self) { return $self->{loop} }
Subroutine loop: not called CC=1 SCAR=6.9
Pod documented
75 7 ▶sub tag ($self) { return $self->{tag} }
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
76 0 ▶sub data ($self) { return $self->{data} }
Subroutine data: not called CC=1 SCAR=6.9
Pod documented
77 0 ▶sub is_running ($self) { return $self->{saw_exit} ? 0 : 1 }
Subroutine is_running: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self->{'saw_exit'} ? : 0 0 0
78  
79 4 ▶sub kill ($self, $sig = 'TERM') {
Subroutine kill: called CC=4 SCAR=14.5
Pod documented
80 4 ▶ return 0 if !$self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pid"} 0 4 4
81 4 my $ok = kill($sig, $self->{pid});
82 4 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 4 0 4
83 }
84  
85 ▶sub stdin_write ($self, $bytes) {
Subroutine stdin_write: called CC=10 SCAR=28.0
Pod documented
86 88 ▶ my $fh = $self->{in_w} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->{"in_w"} or return 0 0 88 88
87 88 ▶ return 0 if !defined $bytes || $bytes eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bytes or $bytes eq "" 0 88 88
Condition33%1/3
not defined $bytes or $bytes eq ""
l!l&&r!l&&!r
0088
MC/DC0%0/2
not defined $bytes or $bytes eq ""
not defined $bytes $bytes eq ""
Truth table33%1/3
not(A) or B
A= not defined $bytesB= $bytes eq ""
ABresult
1X1
011
000
88 88 ▶ return 0 if $self->{in_close_when_empty};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"in_close_when_empty"} 0 88 88
89  
90 88 $self->{in_buf} .= $bytes;
91  
92 88 my $wrote = $self->_drain_stdin;
93  
94 88 ▶ if (!$self->{_canceled} && $self->{in_w} && length($self->{in_buf}) > $self->{in_off}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_canceled"} and $self->{"in_w"} and length $self->{"in_buf"} > $self->{"in_off"}) 88 0 88
Condition33%2/6
not $self->{"_canceled"} and $self->{"in_w"} and length $self->{"in_buf"} > $self->{"in_off"}
!ll&&!rl&&r
0088
not $self->{"_canceled"} and $self->{"in_w"}
!ll&&!rl&&r
0088
MC/DC0%0/3
not $self->{"_canceled"} and $self->{"in_w"} and length $self->{"in_buf"} > $self->{"in_off"}
not $self->{"_canceled"} $self->{"in_w"} length $self->{"in_buf"} > $self->{"in_off"}
Truth table25%1/4
not(A) and B and C
A= not $self->{"_canceled"}B= $self->{"in_w"}C= length $self->{"in_buf"} > $self->{"in_off"}
ABCresult
0XX0
10X0
1100
1111
95 88 ▶ if (my $w = $self->{w_in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = $self->{"w_in"}) 88 0 88
96 88 ▶ $w->enable_write if $w->can('enable_write');
Branch: 1 branch
BranchTrueFalseTotal
if $w->can("enable_write") 88 0 88
97 }
98 }
99  
100 88 return $wrote;
101 }
102  
103 ▶sub close_stdin ($self) {
Subroutine close_stdin: called CC=6 SCAR=20.5
Pod documented
104 2 ▶ return 1 if $self->{_canceled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_canceled"} 0 2 2
105  
106 2 ▶ my $fh = $self->{in_w} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->{"in_w"} or return 1 0 2 2
107  
108 2 ▶ if (length($self->{in_buf}) <= $self->{in_off}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"in_buf"} <= $self->{"in_off"}) 0 2 2
109 0 $self->_close_stdin_now;
110 0 return 1;
111 }
112  
113 2 $self->{in_close_when_empty} = 1;
114  
115 2 ▶ if (my $w = $self->{w_in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = $self->{"w_in"}) 2 0 2
116 2 ▶ $w->enable_write if $w->can('enable_write');
Branch: 1 branch
BranchTrueFalseTotal
if $w->can("enable_write") 2 0 2
117 }
118  
119 2 return 1;
120 }
121  
122 ▶sub _close_stdin_now ($self) {
Subroutine _close_stdin_now: called CC=3 SCAR=11.1
123 2 ▶ if (my $w = delete $self->{w_in}) { $w->cancel }
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"w_in"}) 2 0 2
124 2 ▶ if (my $fh = delete $self->{in_w}) { close($fh) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = delete $self->{"in_w"}) 2 0 2
125  
126 2 $self->{in_buf} = '';
127 2 $self->{in_off} = 0;
128 2 $self->{in_close_when_empty} = 0;
129  
130 2 return;
131 }
132  
133 ▶sub _cancel($self) {
Subroutine _cancel: called CC=14 SCAR=32.8
134 33 ▶ return 0 if $self->{_canceled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_canceled"} 0 33 33
135 33 $self->{_canceled} = 1;
136  
137 # If max_children is managing capacity, keep exit observation alive until we
138 # actually see the exit status. Otherwise the queue could stall forever.
139 33 ▶ my $keep_exit = $self->{managed_by_fork} && !$self->{saw_exit} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'managed_by_fork'} && !$self->{'saw_exit'} ? : 0 33 33
Condition33%1/3
$self->{"managed_by_fork"} and not $self->{"saw_exit"}
!ll&&!rl&&r
3300
MC/DC0%0/2
$self->{"managed_by_fork"} and not $self->{"saw_exit"}
$self->{"managed_by_fork"} not $self->{"saw_exit"}
Truth table33%1/3
A and not(B)
A= $self->{"managed_by_fork"}B= not $self->{"saw_exit"}
ABresult
0X0
100
111
140  
141 33 ▶ if (!$keep_exit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keep_exit) 33 0 33
142 33 ▶ if (defined(my $tid = delete $self->{timeout_id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tid = delete $self->{"timeout_id"})) 4 29 33
143 4 $self->{loop}->cancel($tid);
144 }
145 33 ▶ if (defined(my $tid = delete $self->{timeout_kill_id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tid = delete $self->{"timeout_kill_id"})) 0 33 33
146 0 $self->{loop}->cancel($tid);
147 }
148 33 ▶ if (my $sub = delete $self->{sub_pid}) { $sub->cancel }
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = delete $self->{"sub_pid"}) 33 0 33
149 }
150  
151 33 ▶ if (my $w = delete $self->{w_out}) { $w->cancel }
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"w_out"}) 0 33 33
152 33 ▶ if (my $w = delete $self->{w_err}) { $w->cancel }
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"w_err"}) 0 33 33
153 33 ▶ if (my $w = delete $self->{w_in}) { $w->cancel }
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{"w_in"}) 0 33 33
154  
155 33 ▶ if (my $fh = delete $self->{out_r}) { close($fh) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = delete $self->{"out_r"}) 0 33 33
156 33 ▶ if (my $fh = delete $self->{err_r}) { close($fh) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = delete $self->{"err_r"}) 0 33 33
157 33 ▶ if (my $fh = delete $self->{in_w}) { close($fh) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = delete $self->{"in_w"}) 0 33 33
158  
159 33 return 1;
160 }
161  
162 ▶sub _arm ($self) {
Subroutine _arm: called CC=8 SCAR=21.7
163 56 my $loop = $self->{loop};
164  
165 56 ▶ if ($self->{out_r}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'out_r'}) { } 7 49 56
166 7 $self->{eof_out} = 0;
167 $self->{w_out} = $loop->watch($self->{out_r},
168 14 ▶ read => sub ($loop, $fh, $w) { $self->_drain_stream('out') },
Subroutine __ANON__: called CC=1 SCAR=0.0
169 0 ▶ error => sub ($loop, $fh, $w) { $self->_drain_stream('out') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 7 );
171 } else {
172 49 $self->{eof_out} = 1;
173 }
174  
175 56 ▶ if ($self->{err_r}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'err_r'}) { } 4 52 56
176 4 $self->{eof_err} = 0;
177 $self->{w_err} = $loop->watch($self->{err_r},
178 7 ▶ read => sub ($loop, $fh, $w) { $self->_drain_stream('err') },
Subroutine __ANON__: called CC=1 SCAR=0.0
179 0 ▶ error => sub ($loop, $fh, $w) { $self->_drain_stream('err') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 4 );
181 } else {
182 52 $self->{eof_err} = 1;
183 }
184  
185 56 ▶ if ($self->{in_w}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_w"}) 2 54 56
186 $self->{w_in} = $loop->watch($self->{in_w},
187 8 ▶ write => sub ($loop, $fh, $w) { $self->_on_stdin_writable($w) },
Subroutine __ANON__: called CC=1 SCAR=0.0
188 1 ▶ error => sub ($loop, $fh, $w) { $self->_on_stdin_writable($w) },
Subroutine __ANON__: called CC=1 SCAR=0.0
189 2 );
190  
191 2 ▶ if (length($self->{in_buf}) <= $self->{in_off}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"in_buf"} <= $self->{"in_off"}) 2 0 2
192 2 ▶ $self->{w_in}->disable_write if $self->{w_in}->can('disable_write');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"w_in"}->can("disable_write") 2 0 2
193 }
194 }
195  
196 ▶ $self->{sub_pid} = $loop->pid($self->{pid}, sub ($loop, $pid2, $status, $ud) {
Subroutine __ANON__: called CC=3 SCAR=17.3
197 43 $ud->_on_exit($status);
198  
199 # Race hardening: if the child exits before pidfd observation is armed,
200 # pid() may never deliver a callback. Perform a non-blocking waitpid check.
201 33 ▶ if (!$self->{saw_exit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"saw_exit"}) 0 33 33
202 0 my $got = POSIX::waitpid($self->{pid}, POSIX::WNOHANG());
203 0 ▶ if ($got == $self->{pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got == $self->{"pid"}) 0 0 0
204 0 $self->_on_exit($?);
205 }
206 }
207 56 }, data => $self);
208  
209 56 ▶ if (defined $self->{timeout} && $self->{timeout} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"timeout"} and $self->{"timeout"} > 0) 4 52 56
Condition66%2/3
defined $self->{"timeout"} and $self->{"timeout"} > 0
!ll&&!rl&&r
5204
MC/DC50%1/2
defined $self->{"timeout"} and $self->{"timeout"} > 0
defined $self->{"timeout"} $self->{"timeout"} > 0
Truth table66%2/3
A and B
A= defined $self->{"timeout"}B= $self->{"timeout"} > 0
ABresult
0X0
100
111
210 4 my $secs = 0 + $self->{timeout};
211 4 ▶ $self->{timeout_id} = $loop->after($secs, sub ($loop) { $self->_on_timeout });
Subroutine __ANON__: called CC=1 SCAR=0.0
212 }
213  
214 56 return;
215 }
216  
217 ▶sub _on_timeout ($self) {
Subroutine _on_timeout: called CC=7 SCAR=23.7
218 4 ▶ return if $self->{_canceled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_canceled"} 0 4 4
219 4 ▶ return if $self->{saw_exit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"saw_exit"} 0 4 4
220 4 ▶ return if $self->{timed_out};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"timed_out"} 0 4 4
221  
222 4 $self->{timed_out} = 1;
223  
224 4 ▶ if (my $cb = $self->{on_timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"on_timeout"}) 4 0 4
225 4 $cb->($self);
226 }
227  
228 4 $self->kill('TERM');
229  
230 4 ▶ if (defined $self->{timeout_kill} && $self->{timeout_kill} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"timeout_kill"} and $self->{"timeout_kill"} > 0) 0 4 4
Condition33%1/3
defined $self->{"timeout_kill"} and $self->{"timeout_kill"} > 0
!ll&&!rl&&r
400
MC/DC0%0/2
defined $self->{"timeout_kill"} and $self->{"timeout_kill"} > 0
defined $self->{"timeout_kill"} $self->{"timeout_kill"} > 0
Truth table33%1/3
A and B
A= defined $self->{"timeout_kill"}B= $self->{"timeout_kill"} > 0
ABresult
0X0
100
111
231 0 my $secs = 0 + $self->{timeout_kill};
232 0 my $loop = $self->{loop};
233 0 ▶ $self->{timeout_kill_id} = $loop->after($secs, sub ($loop) { $self->_on_timeout_kill });
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 }
235 4 return;
236 }
237  
238 ▶sub _on_timeout_kill ($self) {
Subroutine _on_timeout_kill: not called CC=3 SCAR=24.8
239 0 ▶ return if $self->{_canceled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_canceled"} 0 0 0
240 0 ▶ return if $self->{saw_exit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"saw_exit"} 0 0 0
241  
242 # Hard stop: the child ignored/handled TERM.
243 0 $self->kill('KILL');
244 0 return;
245 }
246  
247 ▶sub _on_stdin_writable ($self, $w) {
Subroutine _on_stdin_writable: called CC=8 SCAR=24.0
248 9 ▶ return if $self->{_canceled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_canceled"} 0 9 9
249 9 ▶ return if !$self->{in_w};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"in_w"} 0 9 9
250  
251 9 $self->_drain_stdin;
252  
253 9 ▶ if ($self->{in_w} && length($self->{in_buf}) <= $self->{in_off}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_w"} and length $self->{"in_buf"} <= $self->{"in_off"}) 1 8 9
Condition100%3/3
$self->{"in_w"} and length $self->{"in_buf"} <= $self->{"in_off"}
!ll&&!rl&&r
171
MC/DC100%2/2
$self->{"in_w"} and length $self->{"in_buf"} <= $self->{"in_off"}
$self->{"in_w"} length $self->{"in_buf"} <= $self->{"in_off"}
Truth table100%3/3
A and B
A= $self->{"in_w"}B= length $self->{"in_buf"} <= $self->{"in_off"}
ABresult
0X0
100
111
254 1 ▶ if ($self->{in_close_when_empty}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_close_when_empty"}) 1 0 1
255 1 $self->_close_stdin_now;
256 1 return;
257 }
258 0 ▶ $w->disable_write if $w && $w->can('disable_write');
Branch: 1 branch
BranchTrueFalseTotal
if $w and $w->can("disable_write") 0 0 0
Condition0%0/3
$w and $w->can("disable_write")
!ll&&!rl&&r
000
MC/DC0%0/2
$w and $w->can("disable_write")
$w $w->can("disable_write")
Truth table0%0/3
A and B
A= $wB= $w->can("disable_write")
ABresult
0X0
100
111
259 }
260  
261 8 return;
262 }
263  
264 ▶sub _drain_stdin ($self) {
Subroutine _drain_stdin: called CC=10 SCAR=23.2
265 97 ▶ my $fh = $self->{in_w} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->{"in_w"} or return 0 0 97 97
266  
267 97 my $buf = $self->{in_buf};
268 97 my $off = $self->{in_off};
269 97 my $len = length($buf);
270  
271 97 my $wrote_total = 0;
272  
273 97 local $SIG{PIPE} = 'IGNORE';
274  
275 97 ▶ while ($off < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < $len) 113 1 114
276 113 my $w = syswrite($fh, $buf, $len - $off, $off);
277 113 ▶ if (!defined $w) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $w) 96 17 113
278 96 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 96 96
279 96 ▶ last if $! == EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 95 1 96
280 1 ▶ if ($! == EPIPE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 32) 1 0 1
281 1 $self->_close_stdin_now;
282 1 return $wrote_total;
283 }
284 0 $self->_close_stdin_now;
285 0 return $wrote_total;
286 }
287 17 ▶ last if $w == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $w == 0 0 17 17
288 17 $off += $w;
289 17 $wrote_total += $w;
290 }
291  
292 96 $self->{in_off} = $off;
293  
294 96 ▶ if ($off >= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off >= $len) { } 1 95 96
295 1 $self->{in_buf} = '';
296 1 $self->{in_off} = 0;
297 } else {
298 95 ▶ if ($off > 65536) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off > 65536) 7 88 95
299 7 substr($buf, 0, $off, '');
300 7 $self->{in_buf} = $buf;
301 7 $self->{in_off} = 0;
302 }
303 }
304  
305 96 return $wrote_total;
306 }
307  
308 ▶sub _on_exit ($self, $status) {
Subroutine _on_exit: called CC=2 SCAR=7.0
309 43 ▶ return if $self->{saw_exit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"saw_exit"} 0 43 43
310 43 $self->{saw_exit} = 1;
311 43 $self->{exit} = Linux::Event::Fork::Exit->new($self->{pid}, $status);
312  
313 43 $self->_maybe_finish;
314 33 return;
315 }
316  
317 ▶sub _drain_stream ($self, $which) {
Subroutine _drain_stream: called CC=10 SCAR=24.0
318 21 ▶ return if $self->{_canceled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_canceled"} 0 21 21
319  
320 21 ▶ my ($fh_key, $w_key, $cb_key, $eof_key) =
Branch: 1 branch
BranchTrueFalseTotal
$which eq 'out' ? : 14 7 21
321 $which eq 'out'
322 ? ('out_r','w_out','on_stdout','eof_out')
323 : ('err_r','w_err','on_stderr','eof_err');
324  
325 21 ▶ my $fh = $self->{$fh_key} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->{$fh_key} or do { $self->{$eof_key} = 1; $self->_maybe_finish; return } 0 21 21
326 0 $self->{$eof_key} = 1;
327 0 $self->_maybe_finish;
328 0 return;
329 };
330  
331 21 my $cb = $self->{$cb_key};
332  
333 21 while (1) {
334 36 my $buf = '';
335 36 my $n = sysread($fh, $buf, 8192);
336  
337 36 ▶ if (!defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 10 26 36
338 10 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 10 10
339 10 ▶ last if $! == EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 10 0 10
340 0 $n = 0;
341 }
342  
343 26 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 11 15 26
344 11 ▶ if (my $w = delete $self->{$w_key}) { $w->cancel }
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = delete $self->{$w_key}) 11 0 11
345 11 close($fh);
346 11 $self->{$fh_key} = undef;
347  
348 11 $self->{$eof_key} = 1;
349 11 $self->_maybe_finish;
350 11 last;
351 }
352  
353 15 ▶ $cb->($self, $buf) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 9 6 15
354 }
355  
356 21 return;
357 }
358  
359 ▶sub _maybe_finish ($self) {
Subroutine _maybe_finish: called CC=5 SCAR=16.4
360 54 ▶ return if !$self->{saw_exit};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"saw_exit"} 11 43 54
361 43 ▶ return if !$self->{eof_out};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"eof_out"} 0 43 43
362 43 ▶ return if !$self->{eof_err};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"eof_err"} 0 43 43
363  
364 43 ▶ if (my $cb = $self->{on_exit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"on_exit"}) 43 0 43
365 43 $cb->($self, $self->{exit});
366 }
367  
368 # Now safe to fully tear down everything, even for managed children.
369 33 $self->{_canceled} = 0; # allow cancel() to run full teardown path
370 33 $self->{managed_by_fork} = undef;
371 33 $self->_cancel;
372  
373 33 return;
374 }
375  
376 40 1;
377  
378 __END__
« Fork.pm ↑ Summary Fork/Exit.pm »