ReadWriteFork.pm

filter: Statementstmt 87.9% 190 / 216 Branchbran 66.6% 60 / 90 Conditioncond 45.8% 39 / 85 MC/DCmcdc 26.0% 12 / 46 Subroutinesub 88.8% 40 / 45 Podpod 100% 8 / 8 totaltotal 71.2% 349 / 490 CC 81 SCAR 34.5
CC
81
Cov
74%
_start_child
120.9
_error
42.0
kill
12.0
CRAP 31.7
↑ Summary ReadWriteFork/SIGCHLD.pm »
1 package Mojo::IOLoop::ReadWriteFork;
2 26 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 26 ▶use Errno qw(EAGAIN ECONNRESET EINTR EPIPE EWOULDBLOCK EIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use IO::Pty;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶use Mojo::Asset::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 ▶use Mojo::IOLoop::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 26 ▶use Mojo::IOLoop::ReadWriteFork::SIGCHLD;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use Mojo::Util qw(term_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 26 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 26 ▶use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;
Subroutine BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{'MOJO_CHUNK_SIZE'} || 131072
l!l
125
MC/DC100%1/1
$ENV{'MOJO_CHUNK_SIZE'} || 131072
$ENV{'MOJO_CHUNK_SIZE'}
Truth table100%2/2
A
A= $ENV{'MOJO_CHUNK_SIZE'}
Aresult
11
00
16 26 ▶use constant DEBUG => $ENV{MOJO_READWRITEFORK_DEBUG} && 1;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_READWRITEFORK_DEBUG'} && 1
!ll
260
MC/DC0%0/1
$ENV{'MOJO_READWRITEFORK_DEBUG'} && 1
$ENV{'MOJO_READWRITEFORK_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_READWRITEFORK_DEBUG'}
Aresult
00
11
17  
18 26 our $VERSION = '2.02';
19  
20 our @SAFE_SIG
21 26 = grep { !m!^(NUM\d+|__[A-Z0-9]+__|ALL|CATCHALL|DEFER|HOLD|IGNORE|MAX|PAUSE|RTMAX|RTMIN|SEGV|SETS)$! } keys %SIG;
22  
23 26 my $SIGCHLD = Mojo::IOLoop::ReadWriteFork::SIGCHLD->singleton;
24  
25 13 ▶has conduit => sub { +{type => 'pipe'} };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 8 ▶sub pid { shift->{pid} || 0; }
Subroutine pid: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"pid"} or 0 4 4 8
27 40 ▶has ioloop => sub { Mojo::IOLoop->singleton }, weak => 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 6 ▶sub close {
Subroutine close: called CC=9 SCAR=22.6
Pod documented
30 6 my $self = shift;
31 6 ▶ my $fh = delete $self->{stdin_write} or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = delete $self->{"stdin_write"} or return $self 6 0 6
32  
33 6 ▶ if (blessed $fh and $fh->isa('IO::Pty')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($fh) and $fh->isa("IO::Pty")) 2 4 6
Condition66%2/3
builtin::blessed($fh) and $fh->isa("IO::Pty")
!ll&&!rl&&r
402
MC/DC50%1/2
builtin::blessed($fh) and $fh->isa("IO::Pty")
builtin::blessed($fh) $fh->isa("IO::Pty")
Truth table66%2/3
A and B
A= builtin::blessed($fh)B= $fh->isa("IO::Pty")
ABresult
0X0
100
111
34 2 for my $name (qw(pty stdout)) {
35 4 ▶ my $stream = $self->{stream}{$name} && $self->ioloop->stream($self->{stream}{$name});
Condition66%2/3
$self->{'stream'}{$name} && $self->ioloop->stream($self->{'stream'}{$name})
!ll&&!rl&&r
103
MC/DC50%1/2
$self->{'stream'}{$name} && $self->ioloop->stream($self->{'stream'}{$name})
$self->{'stream'}{$name} $self->ioloop->stream($self->{'stream'}{$name})
Truth table66%2/3
A && B
A= $self->{'stream'}{$name}B= $self->ioloop->stream($self->{'stream'}{$name})
ABresult
0X0
100
111
36 4 ▶ $stream->close if $stream and $stream->handle eq $fh;
Branch: 1 branch
BranchTrueFalseTotal
if $stream and $stream->handle eq $fh 2 2 4
Condition100%3/3
$stream and $stream->handle eq $fh
!ll&&!rl&&r
112
MC/DC100%2/2
$stream and $stream->handle eq $fh
$stream $stream->handle eq $fh
Truth table100%3/3
A and B
A= $streamB= $stream->handle eq $fh
ABresult
0X0
100
111
37 }
38 }
39  
40 6 ▶ if (!$fh->close) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->close) 0 6 6
41 0 $self->emit(error => $!);
42 }
43  
44 6 return $self;
45 }
46  
47 36 ▶sub run {
Subroutine run: called CC=2 SCAR=6.9
Pod documented
48 36 ▶ my $args = ref $_[-1] eq 'HASH' ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 23 13 36
49 36 my ($self, $program, @program_args) = @_;
50 36 return $self->start({%$args, program => $program, program_args => \@program_args});
51 }
52  
53 1 ▶sub run_and_capture_p {
Subroutine run_and_capture_p: called CC=2 SCAR=6.9
Pod documented
54 1 my $self = shift;
55 1 my $asset = Mojo::Asset::Memory->new(auto_upgrade => 1);
56 1 ▶ my $read_event = $self->conduit->{stdout} ? 'stdout' : 'read';
Branch: 1 branch
BranchTrueFalseTotal
$self->conduit->{'stdout'} ? : 0 1 1
57 1 ▶ my $read_cb = $self->on($read_event => sub { $asset->add_chunk($_[1]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
58 1 ▶ $asset->once(upgrade => sub { $asset = $_[1]; $self->emit(asset => $asset) });
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 ▶ return $self->emit(asset => $asset)->run_p(@_)->then(sub {$asset})
Subroutine __ANON__: called CC=1 SCAR=0.0
60 1 ▶ ->finally(sub { $self->unsubscribe($read_event => $read_cb) });
Subroutine __ANON__: called CC=1 SCAR=0.0
61 }
62  
63 13 ▶sub run_p {
Subroutine run_p: called CC=1 SCAR=0.1
Pod documented
64 13 my $self = shift;
65 13 my $p = Mojo::Promise->new;
66 13 my @cb;
67 0 ▶ push @cb, $self->once(error => sub { shift->unsubscribe(finish => $cb[1]); $p->reject(@_) });
Subroutine __ANON__: not called CC=1 SCAR=2.6
68 13 ▶ push @cb, $self->once(finish => sub { shift->unsubscribe(error => $cb[0]); $p->resolve(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
69 13 $self->run(@_);
70 13 return $p;
71 }
72  
73 42 ▶sub start {
Subroutine start: called CC=7 SCAR=19.6
Pod documented
74 42 my $self = shift;
75 42 ▶ my $args = ref $_[0] ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 38 4 42
76 42 my $conduit = $self->conduit;
77  
78 42 ▶ $args->{$_} //= $conduit->{$_} for keys %$conduit;
Condition33%1/3
$args->{$_} //= $conduit->{$_}
l!l&&r!l&&!r
0450
MC/DC0%0/2
$args->{$_} //= $conduit->{$_}
$args->{$_} $conduit->{$_}
Truth table33%1/3
A //= B
A= $args->{$_}B= $conduit->{$_}
ABresult
1X1
011
000
79 42 ▶ $args->{conduit} ||= delete $args->{type};
Condition66%2/3
$args->{"conduit"} ||= delete $args->{"type"}
l!l&&r!l&&!r
2400
MC/DC0%0/2
$args->{"conduit"} ||= delete $args->{"type"}
$args->{"conduit"} delete $args->{"type"}
Truth table66%2/3
A ||= B
A= $args->{"conduit"}B= delete $args->{"type"}
ABresult
1X1
011
000
80 42 ▶ $args->{env} ||= {%ENV};
Condition100%2/2
$args->{"env"} ||= {%ENV}
l!l
2319
MC/DC100%1/1
$args->{"env"} ||= {%ENV}
$args->{"env"}
Truth table100%2/2
A
A= $args->{"env"}
Aresult
11
00
81 42 $self->{errno} = 0;
82 42 ▶ $args->{program} or die 'program is required input';
Branch: 1 branch
BranchTrueFalseTotal
$args->{"program"} or die "program is required input" 40 2 42
83 40 ▶ $self->ioloop->next_tick(sub { $self->_start($args) });
Subroutine __ANON__: called CC=1 SCAR=0.0
84 40 return $self;
85 }
86  
87 40 ▶sub _start {
Subroutine _start: called CC=9 SCAR=22.4
88 40 my ($self, $args) = @_;
89 40 my %fh;
90  
91 40 ▶ if ($args->{conduit} eq 'pipe') {
Branch: 3 branches
BranchTrueFalseTotal
if ($args->{'conduit'} eq 'pipe') { } 13 27 40
elsif ($args->{'conduit'} eq 'pty') { } 26 1 27
elsif ($args->{'conduit'} eq 'pty3') { } 1 0 1
92 13 @fh{qw(stdin_read stdin_write)} = $self->_pipe;
93 13 @fh{qw(stdout_read stdout_write)} = $self->_pipe;
94 }
95 elsif ($args->{conduit} eq 'pty') {
96 26 $fh{stdin_write} = $fh{stdout_read} = IO::Pty->new;
97 }
98 elsif ($args->{conduit} eq 'pty3') {
99 1 ▶ $args->{$_} //= 1 for qw(stdin stdout stderr);
Condition50%1/2
$args->{$_} //= 1
l!l
03
MC/DC0%0/1
$args->{$_} //= 1
$args->{$_}
Truth table50%1/2
A
A= $args->{$_}
Aresult
11
00
100 1 @fh{stdin_write} = IO::Pty->new;
101 1 @fh{qw(stdout_read stdout_write)} = $self->_pipe;
102 }
103 else {
104 0 warn "[RWF] Invalid conduit ($args->{conduit})\n" if DEBUG;
105 0 return $self->emit(error => "Invalid conduit ($args->{conduit})");
106 }
107  
108 40 ▶ @fh{qw(stderr_read stderr_write)} = $self->_pipe if $args->{stderr};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"stderr"} 3 37 40
109  
110 40 $self->emit(before_fork => \%fh); # LEGACY
111 40 $self->emit(prepare => \%fh);
112  
113 40 ▶ return $self->emit(error => "Couldn't fork ($!)") unless defined($self->{pid} = fork);
Branch: 1 branch
BranchTrueFalseTotal
unless defined($self->{"pid"} = fork) 0 40 40
114 40 ▶ return $self->{pid} ? $self->_start_parent($args, \%fh) : $self->_start_child($args, \%fh);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'pid'} ? : 36 4 40
115 }
116  
117 4 ▶sub _start_child {
Subroutine _start_child: called CC=27 SCAR=48.0
118 4 my ($self, $args, $fh) = @_;
119  
120 4 ▶ if (my $pty = blessed $fh->{stdin_write} && $fh->{stdin_write}->isa('IO::Pty') && $fh->{stdin_write}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pty = builtin::blessed($fh->{"stdin_write"}) && $fh->{"stdin_write"}->isa("IO::Pty") && $fh->{"stdin_write"}) 2 2 4
Condition66%4/6
builtin::blessed($fh->{'stdin_write'}) && $fh->{'stdin_write'}->isa('IO::Pty') && $fh->{'stdin_write'}
!ll&&!rl&&r
202
builtin::blessed($fh->{"stdin_write"}) and $fh->{"stdin_write"}->isa("IO::Pty")
!ll&&!rl&&r
202
MC/DC33%1/3
builtin::blessed($fh->{'stdin_write'}) && $fh->{'stdin_write'}->isa('IO::Pty') && $fh->{'stdin_write'}
builtin::blessed($fh->{"stdin_write"}) $fh->{"stdin_write"}->isa("IO::Pty") $fh->{'stdin_write'}
Truth table50%2/4
A and B && C
A= builtin::blessed($fh->{"stdin_write"})B= $fh->{"stdin_write"}->isa("IO::Pty")C= $fh->{'stdin_write'}
ABCresult
0XX0
10X0
1100
1111
121 2 $pty->make_slave_controlling_terminal;
122 2 $fh->{stdin_read} = $pty->slave;
123 2 ▶ $fh->{stdin_read}->set_raw if $args->{raw};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"raw"} 0 2 2
124 2 ▶ $fh->{stdin_read}->clone_winsize_from($args->{clone_winsize_from}) if $args->{clone_winsize_from};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"clone_winsize_from"} 0 2 2
125 2 ▶ $fh->{stdout_write} ||= $fh->{stdin_read};
Condition33%1/3
$fh->{"stdout_write"} ||= $fh->{"stdin_read"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$fh->{"stdout_write"} ||= $fh->{"stdin_read"}
$fh->{"stdout_write"} $fh->{"stdin_read"}
Truth table33%1/3
A ||= B
A= $fh->{"stdout_write"}B= $fh->{"stdin_read"}
ABresult
1X1
011
000
126 }
127  
128 4 ▶ my $stdout_no = ($args->{stdout} // 1) && fileno($fh->{stdout_write});
Condition40%2/5
($args->{'stdout'} // 1) && fileno $fh->{'stdout_write'}
!ll&&!rl&&r
004
$args->{"stdout"} // 1
l!l
04
MC/DC0%0/2
($args->{'stdout'} // 1) && fileno $fh->{'stdout_write'}
$args->{"stdout"} fileno $fh->{'stdout_write'}
Truth table0%0/3
A && B
A= $args->{"stdout"}B= fileno $fh->{'stdout_write'}
ABresult
0X0
100
111
129 4 ▶ my $stderr_no = ($args->{stderr} // 1) && fileno($fh->{stderr_write} || $fh->{stdout_write});
Condition37%3/8
($args->{'stderr'} // 1) && fileno($fh->{'stderr_write'} || $fh->{'stdout_write'})
!ll&&!rl&&r
004
$args->{"stderr"} // 1
l!l
04
$fh->{'stderr_write'} || $fh->{'stdout_write'}
l!l&&r!l&&!r
040
MC/DC0%0/3
($args->{'stderr'} // 1) && fileno($fh->{'stderr_write'} || $fh->{'stdout_write'})
$args->{"stderr"} $fh->{'stderr_write'} $fh->{'stdout_write'}
Truth table0%0/4
A && B || C
A= $args->{"stderr"}B= $fh->{'stderr_write'}C= $fh->{'stdout_write'}
ABCresult
0XX0
1000
11X1
1011
130 4 ▶ open STDIN, '<&' . fileno($fh->{stdin_read}) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, '<&' . fileno($fh->{"stdin_read"}) or die $! 4 0 4
131 4 ▶ open STDOUT, '>&' . $stdout_no or die $! if $stdout_no;
Branch: 1 branch
BranchTrueFalseTotal
if $stdout_no 4 0 4
Condition40%2/5
$stdout_no && (open(STDOUT, '>&' . $stdout_no) || die($!))
!ll
040
open STDOUT, '>&' . $stdout_no or die $!
l!l
40
MC/DC0%0/2
$stdout_no && (open(STDOUT, '>&' . $stdout_no) || die($!))
$stdout_no open STDOUT, '>&' . $stdout_no
Truth table0%0/3
A && B
A= $stdout_noB= open STDOUT, '>&' . $stdout_no
ABresult
0X0
100
111
132 4 ▶ open STDERR, '>&' . $stderr_no or die $! if $stderr_no;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr_no 4 0 4
Condition40%2/5
$stderr_no && (open(STDERR, '>&' . $stderr_no) || die($!))
!ll
040
open STDERR, '>&' . $stderr_no or die $!
l!l
40
MC/DC0%0/2
$stderr_no && (open(STDERR, '>&' . $stderr_no) || die($!))
$stderr_no open STDERR, '>&' . $stderr_no
Truth table0%0/3
A && B
A= $stderr_noB= open STDERR, '>&' . $stderr_no
ABresult
0X0
100
111
133 4 ▶ $stdout_no ? STDOUT->autoflush(1) : STDOUT->close;
Branch: 1 branch
BranchTrueFalseTotal
$stdout_no ? : 4 0 4
134 4 ▶ $stderr_no ? STDERR->autoflush(1) : STDERR->close;
Branch: 1 branch
BranchTrueFalseTotal
$stderr_no ? : 4 0 4
135  
136 4 $fh->{stdin_write}->close;
137 4 $fh->{stdout_read}->close;
138 4 ▶ $fh->{stderr_read}->close if $fh->{stderr_read};
Branch: 1 branch
BranchTrueFalseTotal
if $fh->{"stderr_read"} 0 4 4
139  
140 4 %ENV = %{$args->{env}};
141  
142 4 my $errno;
143 4 ▶ if (ref $args->{program} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'program'} eq 'CODE') { } 0 4 4
144 0 $! = 0;
145 0 @SIG{@SAFE_SIG} = ('DEFAULT') x @SAFE_SIG;
146 0 ▶ eval { $args->{program}->(@{$args->{program_args} || []}); };
Condition0%0/2
$args->{'program_args'} || []
l!l
00
MC/DC0%0/1
$args->{'program_args'} || []
$args->{'program_args'}
Truth table0%0/2
A
A= $args->{'program_args'}
Aresult
11
00
147 0 ▶ $errno = $@ ? 255 : $!;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
148 0 ▶ print STDERR $@ if length $@;
Branch: 1 branch
BranchTrueFalseTotal
if length $@ 0 0 0
149 }
150 else {
151 4 ▶ exec $args->{program}, @{$args->{program_args} || []};
Condition50%1/2
$args->{'program_args'} || []
l!l
40
MC/DC0%0/1
$args->{'program_args'} || []
$args->{'program_args'}
Truth table50%1/2
A
A= $args->{'program_args'}
Aresult
11
00
152 }
153  
154 0 ▶ eval { POSIX::_exit($errno // $!); };
Condition0%0/3
$errno // $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$errno // $!
$errno $!
Truth table0%0/3
A // B
A= $errnoB= $!
ABresult
1X1
011
000
155 0 ▶ die($errno // $!);
Condition0%0/3
$errno // $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$errno // $!
$errno $!
Truth table0%0/3
A // B
A= $errnoB= $!
ABresult
1X1
011
000
156 }
157  
158 36 ▶sub _start_parent {
Subroutine _start_parent: called CC=7 SCAR=19.5
159 36 my ($self, $args, $fh) = @_;
160  
161 36 $self->_d("Forked $args->{program} @{$args->{program_args} || []}") if DEBUG;
162 36 @$self{qw(stdin_write stdout_read stderr_read)} = @$fh{qw(stdin_write stdout_read stderr_read)};
163 36 @$self{qw(wait_eof wait_sigchld)} = (1, 1);
164  
165 36 ▶ $fh->{stdin_write}->close_slave if blessed $fh->{stdin_write} and $fh->{stdin_write}->isa('IO::Pty');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($fh->{"stdin_write"}) and $fh->{"stdin_write"}->isa("IO::Pty") 25 11 36
Condition66%2/3
builtin::blessed($fh->{"stdin_write"}) and $fh->{"stdin_write"}->isa("IO::Pty")
!ll&&!rl&&r
11025
MC/DC50%1/2
builtin::blessed($fh->{"stdin_write"}) and $fh->{"stdin_write"}->isa("IO::Pty")
builtin::blessed($fh->{"stdin_write"}) $fh->{"stdin_write"}->isa("IO::Pty")
Truth table66%2/3
A and B
A= builtin::blessed($fh->{"stdin_write"})B= $fh->{"stdin_write"}->isa("IO::Pty")
ABresult
0X0
100
111
166 36 ▶ $self->{stream}{pty} = $self->_stream(pty => $fh->{stdin_write}) if $args->{conduit} eq 'pty3';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"conduit"} eq "pty3" 1 35 36
167 36 ▶ $self->{stream}{stderr} = $self->_stream(stderr => $fh->{stderr_read}) if $fh->{stderr_read};
Branch: 1 branch
BranchTrueFalseTotal
if $fh->{"stderr_read"} 3 33 36
168 36 ▶ $self->{stream}{stdout} = $self->_stream(stdout => $fh->{stdout_read}) if !$fh->{stderr_read} or $args->{stdout};
Branch: 1 branch
BranchTrueFalseTotal
if not $fh->{"stderr_read"} or $args->{"stdout"} 35 1 36
Condition100%3/3
not $fh->{"stderr_read"} or $args->{"stdout"}
l!l&&r!l&&!r
3321
MC/DC100%2/2
not $fh->{"stderr_read"} or $args->{"stdout"}
not $fh->{"stderr_read"} $args->{"stdout"}
Truth table100%3/3
not(A) or B
A= not $fh->{"stderr_read"}B= $args->{"stdout"}
ABresult
1X1
011
000
169  
170 36 ▶ $SIGCHLD->waitpid($self->{pid} => sub { $self->_sigchld(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
171 36 $self->emit('fork'); # LEGACY
172 36 $self->emit('spawn');
173 36 $self->_write;
174 }
175  
176 6 ▶sub write {
Subroutine write: called CC=3 SCAR=11.0
Pod documented
177 6 my ($self, $chunk, $cb) = @_;
178 6 ▶ $self->once(drain => $cb) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 3 3 6
179 6 $self->{stdin_buffer} .= $chunk;
180 6 ▶ $self->_write if $self->{stdin_write};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stdin_write"} 2 4 6
181 6 return $self;
182 }
183  
184 0 ▶sub kill {
Subroutine kill: not called CC=3 SCAR=24.8
Pod documented
185 0 my $self = shift;
186 0 ▶ my $signal = shift // 15;
Condition0%0/2
shift() // 15
l!l
00
MC/DC0%0/1
shift() // 15
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
187 0 ▶ return undef unless my $pid = $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
unless my $pid = $self->{"pid"} 0 0 0
188 0 $self->_d("kill $signal $pid") if DEBUG;
189 0 return kill $signal, $pid;
190 }
191  
192 0 ▶sub _error {
Subroutine _error: not called CC=6 SCAR=37.4
193 0 ▶ return if $! == EAGAIN || $! == EINTR || $! == EWOULDBLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 or $! == 4 or $! == 11 0 0 0
Condition0%0/6
$! == 11 or $! == 4 or $! == 11
l!l&&r!l&&!r
000
$! == 11 or $! == 4
l!l&&r!l&&!r
000
MC/DC0%0/3
$! == 11 or $! == 4 or $! == 11
$! == 11 $! == 4 $! == 11
Truth table0%0/4
A or B or C
A= $! == 11B= $! == 4C= $! == 11
ABCresult
1XX1
01X1
0011
0000
194 0 ▶ return $_[0]->kill if $! == ECONNRESET || $! == EPIPE;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 104 or $! == 32 0 0 0
Condition0%0/3
$! == 104 or $! == 32
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 104 or $! == 32
$! == 104 $! == 32
Truth table0%0/3
A or B
A= $! == 104B= $! == 32
ABresult
1X1
011
000
195 0 return $_[0]->emit(error => $!)->kill;
196 }
197  
198 0 ▶sub _d { warn "-- [$_[0]->{pid}] $_[1]\n" }
Subroutine _d: not called CC=1 SCAR=6.9
199  
200 73 ▶sub _maybe_terminate {
Subroutine _maybe_terminate: called CC=6 SCAR=17.9
201 73 my ($self, $pending_event) = @_;
202 73 delete $self->{$pending_event};
203 73 ▶ return if $self->{wait_eof} or $self->{wait_sigchld};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"wait_eof"} or $self->{"wait_sigchld"} 38 35 73
Condition100%3/3
$self->{"wait_eof"} or $self->{"wait_sigchld"}
l!l&&r!l&&!r
13735
MC/DC100%2/2
$self->{"wait_eof"} or $self->{"wait_sigchld"}
$self->{"wait_eof"} $self->{"wait_sigchld"}
Truth table100%3/3
A or B
A= $self->{"wait_eof"}B= $self->{"wait_sigchld"}
ABresult
1X1
011
000
204  
205 35 delete $self->{stdin_write};
206 35 delete $self->{stdout_read};
207 35 delete $self->{stderr_read};
208  
209 35 my @errors;
210 35 for my $cb (@{$self->subscribers('close')}, @{$self->subscribers('finish')}) {
211 41 ▶ push @errors, $@ unless eval { $self->$cb(@$self{qw(exit_value signal)}); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->$cb(@$self{"exit_value", "signal"}); 1 } } 1 40 41
212 }
213  
214 35 $self->emit(error => $_) for @errors;
215 }
216  
217 30 ▶sub _pipe {
Subroutine _pipe: called CC=2 SCAR=7.0
218 30 my $self = shift;
219 30 ▶ pipe my $read, my $write or return $self->emit(error => "pipe: $!");
Branch: 1 branch
BranchTrueFalseTotal
pipe my $read, my $write or return $self->emit("error", "pipe: $!") 30 0 30
220 30 $write->autoflush(1);
221 30 return $read, $write;
222 }
223  
224 35 ▶sub _sigchld {
Subroutine _sigchld: called CC=1 SCAR=0.0
225 35 my ($self, $status, $pid) = @_;
226 35 my ($exit_value, $signal) = ($status >> 8, $status & 127);
227 35 $self->_d("Exit exit_value=$exit_value, signal=$signal") if DEBUG;
228 35 @$self{qw(exit_value signal)} = ($exit_value, $signal);
229 35 $self->_maybe_terminate('wait_sigchld');
230 }
231  
232 39 ▶sub _stream {
Subroutine _stream: called CC=2 SCAR=6.9
233 39 my ($self, $conduit, $handle) = @_;
234 39 my $stream = Mojo::IOLoop::Stream->new($handle)->timeout(0);
235  
236 39 ▶ my $event_name = $self->{stderr_read} ? $conduit : 'read';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'stderr_read'} ? : 6 33 39
237 53 ▶ my $read_cb = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
238 53 $self->_d(sprintf ">>> RWF:%s ($event_name)\n%s", uc $conduit, term_escape $_[1]) if DEBUG;
239 53 $self->emit($event_name => $_[1]);
240 39 };
241  
242 39 ▶ $stream->on(error => sub { $! != EIO && $self->emit(error => "Read error: $_[1]") });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$! != 5 and $self->emit("error", "Read error: $_[1]") 0 22 22
243 39 ▶ $stream->on(close => sub { $self->_maybe_terminate('wait_eof') });
Subroutine __ANON__: called CC=1 SCAR=0.0
244 39 $stream->on(read => $read_cb);
245  
246 39 return $self->ioloop->stream($stream);
247 }
248  
249 38 ▶sub _write {
Subroutine _write: called CC=4 SCAR=15.0
250 38 my $self = shift;
251 38 ▶ return unless length $self->{stdin_buffer};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"stdin_buffer"} 32 6 38
252  
253 6 my $stdin_write = $self->{stdin_write};
254 6 my $written = $stdin_write->syswrite($self->{stdin_buffer});
255 6 ▶ return $self->_error unless defined $written;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $written 0 6 6
256  
257 6 my $chunk = substr $self->{stdin_buffer}, 0, $written, '';
258 6 $self->_d(sprintf "<<< RWF:STDIN\n%s", term_escape $chunk) if DEBUG;
259  
260 6 ▶ if (length $self->{stdin_buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'stdin_buffer'}) { } 0 6 6
261  
262 # This is one ugly hack because it does not seem like IO::Pty play
263 # nice with Mojo::Reactor(::EV) ->io(...) and ->watch(...)
264 0 ▶ $self->ioloop->timer(0.01 => sub { $self and $self->_write });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_write 0 0 0
265 }
266 else {
267 6 $self->emit('drain');
268 }
269 }
270  
271 26 1;
272  
273 =encoding utf8
274  
275 =head1 NAME
276  
277 Mojo::IOLoop::ReadWriteFork - Fork a process and read/write from it
278  
279 =head1 VERSION
280  
281 2.02
282  
283 =head1 SYNOPSIS
284  
285 use Mojo::Base -strict, -signatures;
286 my $rwf = Mojo::IOLoop::ReadWriteFork->new;
287  
288 # Emitted if something terrible happens
289 $rwf->on(error => sub ($rwf, $error) { warn $error });
290  
291 # Emitted when the child completes
292 $rwf->on(finish => sub ($rwf, $exit_value, $signal) { Mojo::IOLoop->stop; });
293  
294 # Emitted when the child prints to STDOUT or STDERR
295 $rwf->on(read => sub ($rwf, $buf) { print qq(Child process sent us "$buf") });
296  
297 # Need to set "conduit" for bash, ssh, and other programs that require a pty
298 $rwf->conduit({type => 'pty'});
299  
300 # Start the application
301 $rwf->run('bash', -c => q(echo $YIKES foo bar baz));
302  
303 # Using promises
304 $rwf->on(read => sub ($rwf, $buf) { ... });
305 $rwf->run_p('bash', -c => q(echo $YIKES foo bar baz))->wait;
306  
307 See also
308 L<https://github.com/jhthorsen/mojo-ioloop-readwritefork/tree/master/examples/tail.pl>
309 for an example usage from a L<Mojo::Controller>.
310  
311 =head1 DESCRIPTION
312  
313 L<Mojo::IOLoop::ReadWriteFork> enable you to fork a child process and L</read>
314 and L</write> data to. You can also L<send signals|/kill> to the child and see
315 when the process ends. The child process can be an external program (bash,
316 telnet, ffmpeg, ...) or a CODE block running perl.
317  
318 =head2 Conduits
319  
320 L<Mojo::IOLoop::ReadWriteFork> can write to STDIN or a L<IO::Pty> object, and
321 read from STDOUT or STDERR, depending on the "type" given to L</conduit>.
322  
323 Here is an overview of the different conduits:
324  
325 =over 2
326  
327 =item * pipe
328  
329 The "pipe" type will create a STDIN and a STDOUT conduit using a plain pipe.
330 Passing in C<stderr> will also create a seperate pipe for STDERR.
331  
332 $rwf->conduit({type => 'pipe'});
333 $rwf->conduit({type => 'pipe', stderr => 1});
334 $rwf->write('some data'); # write to STDIN
335 $rwf->on(read => sub { ... }); # STDOUT and STDERR
336 $rwf->on(stdout => sub { ... }); # STDOUT
337 $rwf->on(stderr => sub { ... }); # STDERR
338  
339 This is useful if you want to run a program like "cat" that simply read/write
340 from STDIN, STDERR or STDOUT.
341  
342 =item * pty
343  
344 The "pty" type will create a STDIN and a STDOUT conduit using L<IO::Pty>.
345 Passing in "stderr" will also create a seperate pipe for STDERR.
346  
347 $rwf->conduit({type => 'pty'});
348 $rwf->conduit({type => 'pty', stderr => 1});
349 $rwf->write('some data'); # write to STDIN
350 $rwf->on(read => sub { ... }); # STDOUT and STDERR
351 $rwf->on(stdout => sub { ... }); # STDOUT
352 $rwf->on(stderr => sub { ... }); # STDERR
353  
354 The difference between "pipe" and "pty" is that a L<IO::Pty> object will be
355 used for STDIN and STDOUT instead of a plain pipe. In addition, it is possible
356 to pass in C<clone_winsize_from> and C<raw>:
357  
358 $rwf->conduit({type => 'pty', clone_winsize_from => \*STDOUT, raw => 1});
359  
360 This is useful if you want to run "bash" or another program that requires a
361 pseudo terminal.
362  
363 =item * pty3
364  
365 The "pty3" type will create a STDIN, a STDOUT, a STDERR and a PTY conduit.
366  
367 $rwf->conduit({type => 'pty3'});
368 $rwf->write('some data'); # write to STDIN/PTY
369 $rwf->on(pty => sub { ... }); # PTY
370 $rwf->on(stdout => sub { ... }); # STDOUT
371 $rwf->on(stderr => sub { ... }); # STDERR
372  
373 The difference between "pty" and "pty3" is that there will be a different
374 L</read> event for bytes coming from the pseudo PTY. This type also supports
375 "clone_winsize_from" and "raw".
376  
377 $rwf->conduit({type => 'pty3', clone_winsize_from => \*STDOUT, raw => 1});
378  
379 This is useful if you want to run "ssh" or another program that sends password
380 prompts (or other output) on the PTY channel. See
381 L<https://github.com/jhthorsen/mojo-ioloop-readwritefork/tree/master/examples/sshpass>
382 for an example application.
383  
384 =back
385  
386 =head1 EVENTS
387  
388 =head2 asset
389  
390 $rwf->on(asset => sub ($rwf, $asset) { ... });
391  
392 Emitted at least once when calling L</run_and_capture_p>. C<$asset> can be
393 either a L<Mojo::Asset::Memory> or L<Mojo::Asset::File> object.
394  
395 $rwf->on(asset => sub ($rwf, $asset) {
396 # $asset->auto_upgrade(1) is set by default
397 $asset->max_memory_size(1) if $asset->can('max_memory_size');
398 });
399  
400 =head2 drain
401  
402 $rwf->on(drain => sub ($rwf) { ... });
403  
404 Emitted when the buffer has been written to the sub process.
405  
406 =head2 error
407  
408 $rwf->on(error => sub ($rwf, $str) { ... });
409  
410 Emitted when when the there is an issue with creating, writing or reading
411 from the child process.
412  
413 =head2 finish
414  
415 $rwf->on(finish => sub ($rwf, $exit_value, $signal) { ... });
416  
417 Emitted when the child process exit.
418  
419 =head2 pty
420  
421 $rwf->on(pty => sub ($rwf, $buf) { ... });
422  
423 Emitted when the child has written a chunk of data to a pty and L</conduit> has
424 "type" set to "pty3".
425  
426 =head2 prepare
427  
428 $rwf->on(prepare => sub ($rwf, $fh) { ... });
429  
430 Emitted right before the child process is forked. C<$fh> can contain the
431 example hash below or a subset:
432  
433 $fh = {
434 stderr_read => $pipe_fh_w_or_pty_object,
435 stderr_read => $stderr_fh_r,
436 stdin_read => $pipe_fh_r,
437 stdin_write => $pipe_fh_r_or_pty_object,
438 stdin_write => $stderr_fh_w,
439 stdout_read => $pipe_fh_w_or_pty_object,
440 stdout_read => $stderr_fh_r,
441 stdout_write => $pipe_fh_w,
442 };
443  
444 =head2 read
445  
446 $rwf->on(read => sub ($rwf, $buf) { ... });
447  
448 Emitted when the child has written a chunk of data to STDOUT or STDERR, and
449 neither "stderr" nor "stdout" is set in the L</conduit>.
450  
451 =head2 spawn
452  
453 $rwf->on(spawn => sub ($rwf) { ... });
454  
455 Emitted after C<fork()> has been called. Note that the child process might not yet have
456 been started. The order of things is impossible to say, but it's something like this:
457  
458 .------.
459 | fork |
460 '------'
461 |
462 ___/ \_______________
463 | |
464 | (parent) | (child)
465 .--------------. |
466 | emit "spawn" | .--------------------.
467 '--------------' | set up filehandles |
468 '--------------------'
469 |
470 .---------------.
471 | exec $program |
472 '---------------'
473  
474 See also L</pid> for example usage of this event.
475  
476 =head2 stderr
477  
478 $rwf->on(stderr => sub ($rwf, $buf) { ... });
479  
480 Emitted when the child has written a chunk of data to STDERR and L</conduit>
481 has the "stderr" key set to a true value or "type" is set to "pty3".
482  
483 =head2 stdout
484  
485 $rwf->on(stdout => sub ($rwf, $buf) { ... });
486  
487 Emitted when the child has written a chunk of data to STDOUT and L</conduit>
488 has the "stdout" key set to a true value or "type" is set to "pty3".
489  
490 =head1 ATTRIBUTES
491  
492 =head2 conduit
493  
494 $hash = $rwf->conduit;
495 $rwf = $rwf->conduit(\%options);
496  
497 Used to set the conduit options. Possible values are:
498  
499 =over 2
500  
501 =item * clone_winsize_from
502  
503 See L<IO::Pty/clone_winsize_from>. This only makes sense if L</conduit> is set
504 to "pty". This can also be specified by using the L</conduit> attribute.
505  
506 =item * raw
507  
508 See L<IO::Pty/set_raw>. This only makes sense if L</conduit> is set to "pty".
509 This can also be specified by using the L</conduit> attribute.
510  
511 =item * stderr
512  
513 This will make L<Mojo::IOLoop::ReadWriteFork> emit "stderr" events, instead of
514 "read" events. Setting this to "0" will close STDERR in the child.
515  
516 =item * stdout
517  
518 This will make L<Mojo::IOLoop::ReadWriteFork> emit "stdout" events, instead of
519 "read" events. Setting this to "0" will close STDOUT in the child.
520  
521 =item * type
522  
523 "type" can be either "pipe", "pty" or "pty3". Default value is "pipe".
524  
525 See also L</Conduits>
526  
527 =back
528  
529 =head2 ioloop
530  
531 $ioloop = $rwf->ioloop;
532 $rwf = $rwf->ioloop(Mojo::IOLoop->singleton);
533  
534 Holds a L<Mojo::IOLoop> object.
535  
536 =head2 pid
537  
538 $int = $rwf->pid;
539  
540 Holds the child process ID. Note that L</start> will start the process after
541 the IO loop is started. This means that the code below will not work:
542  
543 $rwf->run("bash", -c => q(echo $YIKES foo bar baz));
544 warn $rwf->pid; # pid() is not yet set
545  
546 This will work though:
547  
548 $rwf->on(fork => sub ($rwf) { warn $rwf->pid });
549 $rwf->run('bash', -c => q(echo $YIKES foo bar baz));
550  
551 =head1 METHODS
552  
553 =head2 close
554  
555 $rwf = $rwf->close('stdin');
556  
557 Close STDIN stream to the child process immediately.
558  
559 =head2 run
560  
561 $rwf = $rwf->run($program, @program_args);
562 $rwf = $rwf->run(\&Some::Perl::function, @function_args);
563  
564 Simpler version of L</start>. Can either start an application or run a perl
565 function.
566  
567 =head2 run_and_capture_p
568  
569 $p = $rwf->run_and_capture_p(...)->then(sub { my $asset = shift });
570  
571 L</run_and_capture_p> takes the same arguments as L</run_p>, but the
572 fullfillment callback will receive a L<Mojo::Asset> object that holds the
573 output from the command.
574  
575 See also the L</asset> event.
576  
577 =head2 run_p
578  
579 $p = $rwf->run_p($program, @program_args);
580 $p = $rwf->run_p(\&Some::Perl::function, @function_args);
581  
582 Promise based version of L</run>. The L<Mojo::Promise> will be resolved on
583 L</finish> and rejected on L</error>.
584  
585 =head2 start
586  
587 $rwf = $rwf->start(\%args);
588  
589 Used to fork and exec a child process. C<%args> can have:
590  
591 =over 2
592  
593 =item * program
594  
595 Either an application or a CODE ref.
596  
597 =item * program_args
598  
599 A list of options passed on to L</program> or as input to the CODE ref.
600  
601 Note that this module will start L</program> with this code:
602  
603 exec $program, @$program_args;
604  
605 This means that the code is subject for
606 L<shell injection|https://en.wikipedia.org/wiki/Code_injection#Shell_injection>
607 unless invoked with more than one argument. This is considered a feature, but
608 something you should be avare of. See also L<perlfunc/exec> for more details.
609  
610 =item * env
611  
612 Passing in C<env> will override the default set of environment variables,
613 stored in C<%ENV>.
614  
615 =back
616  
617 =head2 write
618  
619 $rwf = $rwf->write($chunk);
620 $rwf = $rwf->write($chunk, $cb);
621  
622 Used to write data to the child process STDIN. An optional callback will be
623 called once the C<$chunk> is written.
624  
625 Example:
626  
627 $rwf->write("some data\n", sub ($rwf) { $rwf->close });
628  
629 =head2 kill
630  
631 $bool = $rwf->kill;
632 $bool = $rwf->kill(15); # default
633  
634 Used to signal the child.
635  
636 =head1 SEE ALSO
637  
638 L<Mojo::IOLoop::ForkCall>.
639  
640 L<https://github.com/jhthorsen/mojo-ioloop-readwritefork/tree/master/examples/tail.pl>
641  
642 =head1 COPYRIGHT AND LICENSE
643  
644 Copyright (C) 2013-2016, Jan Henning Thorsen
645  
646 This program is free software, you can redistribute it and/or modify it under
647 the terms of the Artistic License version 2.0.
648  
649 =head1 AUTHOR
650  
651 Jan Henning Thorsen - C<jhthorsen@cpan.org>
652  
653 =cut
↑ Summary ReadWriteFork/SIGCHLD.pm »