| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojo::IOLoop::ReadWriteFork; |
|
2
|
26
|
|
|
26
|
|
4477599
|
use Mojo::Base 'Mojo::EventEmitter'; |
|
|
26
|
|
|
|
|
282
|
|
|
|
26
|
|
|
|
|
119
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
26
|
|
|
26
|
|
38661
|
use Errno qw(EAGAIN ECONNRESET EINTR EPIPE EWOULDBLOCK EIO); |
|
|
26
|
|
|
|
|
21755
|
|
|
|
26
|
|
|
|
|
2180
|
|
|
5
|
26
|
|
|
26
|
|
146
|
use IO::Handle; |
|
|
26
|
|
|
|
|
43
|
|
|
|
26
|
|
|
|
|
695
|
|
|
6
|
26
|
|
|
26
|
|
8555
|
use IO::Pty; |
|
|
26
|
|
|
|
|
229760
|
|
|
|
26
|
|
|
|
|
1083
|
|
|
7
|
26
|
|
|
26
|
|
9077
|
use Mojo::Asset::Memory; |
|
|
26
|
|
|
|
|
620500
|
|
|
|
26
|
|
|
|
|
199
|
|
|
8
|
26
|
|
|
26
|
|
10859
|
use Mojo::IOLoop; |
|
|
26
|
|
|
|
|
2350467
|
|
|
|
26
|
|
|
|
|
145
|
|
|
9
|
26
|
|
|
26
|
|
1158
|
use Mojo::IOLoop::Stream; |
|
|
26
|
|
|
|
|
48
|
|
|
|
26
|
|
|
|
|
138
|
|
|
10
|
26
|
|
|
26
|
|
10106
|
use Mojo::IOLoop::ReadWriteFork::SIGCHLD; |
|
|
26
|
|
|
|
|
1428
|
|
|
|
26
|
|
|
|
|
141
|
|
|
11
|
26
|
|
|
26
|
|
815
|
use Mojo::Promise; |
|
|
26
|
|
|
|
|
41
|
|
|
|
26
|
|
|
|
|
138
|
|
|
12
|
26
|
|
|
26
|
|
543
|
use Mojo::Util qw(term_escape); |
|
|
26
|
|
|
|
|
45
|
|
|
|
26
|
|
|
|
|
1128
|
|
|
13
|
26
|
|
|
26
|
|
117
|
use Scalar::Util qw(blessed); |
|
|
26
|
|
|
|
|
46
|
|
|
|
26
|
|
|
|
|
1001
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
26
|
|
100
|
26
|
|
112
|
use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072; |
|
|
26
|
|
|
|
|
43
|
|
|
|
26
|
|
|
|
|
2888
|
|
|
16
|
26
|
|
50
|
26
|
|
228
|
use constant DEBUG => $ENV{MOJO_READWRITEFORK_DEBUG} && 1; |
|
|
26
|
|
|
|
|
39
|
|
|
|
26
|
|
|
|
|
77730
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '2.02'; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our @SAFE_SIG |
|
21
|
|
|
|
|
|
|
= grep { !m!^(NUM\d+|__[A-Z0-9]+__|ALL|CATCHALL|DEFER|HOLD|IGNORE|MAX|PAUSE|RTMAX|RTMIN|SEGV|SETS)$! } keys %SIG; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $SIGCHLD = Mojo::IOLoop::ReadWriteFork::SIGCHLD->singleton; |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has conduit => sub { +{type => 'pipe'} }; |
|
26
|
8
|
100
|
|
8
|
1
|
221
|
sub pid { shift->{pid} || 0; } |
|
27
|
|
|
|
|
|
|
has ioloop => sub { Mojo::IOLoop->singleton }, weak => 1; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub close { |
|
30
|
6
|
|
|
6
|
1
|
359
|
my $self = shift; |
|
31
|
6
|
50
|
|
|
|
110
|
my $fh = delete $self->{stdin_write} or return $self; |
|
32
|
|
|
|
|
|
|
|
|
33
|
6
|
100
|
66
|
|
|
84
|
if (blessed $fh and $fh->isa('IO::Pty')) { |
|
34
|
2
|
|
|
|
|
23
|
for my $name (qw(pty stdout)) { |
|
35
|
4
|
|
66
|
|
|
152
|
my $stream = $self->{stream}{$name} && $self->ioloop->stream($self->{stream}{$name}); |
|
36
|
4
|
100
|
100
|
|
|
78
|
$stream->close if $stream and $stream->handle eq $fh; |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
6
|
50
|
|
|
|
166
|
if (!$fh->close) { |
|
41
|
0
|
|
|
|
|
0
|
$self->emit(error => $!); |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
6
|
|
|
|
|
271
|
return $self; |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub run { |
|
48
|
36
|
100
|
|
36
|
1
|
3881
|
my $args = ref $_[-1] eq 'HASH' ? pop : {}; |
|
49
|
36
|
|
|
|
|
105
|
my ($self, $program, @program_args) = @_; |
|
50
|
36
|
|
|
|
|
338
|
return $self->start({%$args, program => $program, program_args => \@program_args}); |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub run_and_capture_p { |
|
54
|
1
|
|
|
1
|
1
|
163
|
my $self = shift; |
|
55
|
1
|
|
|
|
|
9
|
my $asset = Mojo::Asset::Memory->new(auto_upgrade => 1); |
|
56
|
1
|
50
|
|
|
|
9
|
my $read_event = $self->conduit->{stdout} ? 'stdout' : 'read'; |
|
57
|
1
|
|
|
2
|
|
4
|
my $read_cb = $self->on($read_event => sub { $asset->add_chunk($_[1]) }); |
|
|
2
|
|
|
|
|
24
|
|
|
58
|
1
|
|
|
1
|
|
8
|
$asset->once(upgrade => sub { $asset = $_[1]; $self->emit(asset => $asset) }); |
|
|
1
|
|
|
|
|
189
|
|
|
|
1
|
|
|
|
|
10
|
|
|
59
|
1
|
|
|
1
|
|
77
|
return $self->emit(asset => $asset)->run_p(@_)->then(sub {$asset}) |
|
60
|
1
|
|
|
1
|
|
18
|
->finally(sub { $self->unsubscribe($read_event => $read_cb) }); |
|
|
1
|
|
|
|
|
187
|
|
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub run_p { |
|
64
|
13
|
|
|
13
|
1
|
18472
|
my $self = shift; |
|
65
|
13
|
|
|
|
|
113
|
my $p = Mojo::Promise->new; |
|
66
|
13
|
|
|
|
|
504
|
my @cb; |
|
67
|
13
|
|
|
0
|
|
119
|
push @cb, $self->once(error => sub { shift->unsubscribe(finish => $cb[1]); $p->reject(@_) }); |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
68
|
13
|
|
|
11
|
|
301
|
push @cb, $self->once(finish => sub { shift->unsubscribe(error => $cb[0]); $p->resolve(@_) }); |
|
|
11
|
|
|
|
|
473
|
|
|
|
11
|
|
|
|
|
381
|
|
|
69
|
13
|
|
|
|
|
195
|
$self->run(@_); |
|
70
|
13
|
|
|
|
|
98
|
return $p; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub start { |
|
74
|
42
|
|
|
42
|
1
|
5207
|
my $self = shift; |
|
75
|
42
|
100
|
|
|
|
158
|
my $args = ref $_[0] ? $_[0] : {@_}; |
|
76
|
42
|
|
|
|
|
210
|
my $conduit = $self->conduit; |
|
77
|
|
|
|
|
|
|
|
|
78
|
42
|
|
33
|
|
|
464
|
$args->{$_} //= $conduit->{$_} for keys %$conduit; |
|
79
|
42
|
|
66
|
|
|
344
|
$args->{conduit} ||= delete $args->{type}; |
|
80
|
42
|
|
100
|
|
|
736
|
$args->{env} ||= {%ENV}; |
|
81
|
42
|
|
|
|
|
157
|
$self->{errno} = 0; |
|
82
|
42
|
100
|
|
|
|
166
|
$args->{program} or die 'program is required input'; |
|
83
|
40
|
|
|
40
|
|
203
|
$self->ioloop->next_tick(sub { $self->_start($args) }); |
|
|
40
|
|
|
|
|
6112
|
|
|
84
|
40
|
|
|
|
|
3059
|
return $self; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _start { |
|
88
|
40
|
|
|
40
|
|
101
|
my ($self, $args) = @_; |
|
89
|
40
|
|
|
|
|
65
|
my %fh; |
|
90
|
|
|
|
|
|
|
|
|
91
|
40
|
100
|
|
|
|
231
|
if ($args->{conduit} eq 'pipe') { |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
92
|
13
|
|
|
|
|
47
|
@fh{qw(stdin_read stdin_write)} = $self->_pipe; |
|
93
|
13
|
|
|
|
|
50
|
@fh{qw(stdout_read stdout_write)} = $self->_pipe; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
elsif ($args->{conduit} eq 'pty') { |
|
96
|
26
|
|
|
|
|
384
|
$fh{stdin_write} = $fh{stdout_read} = IO::Pty->new; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
elsif ($args->{conduit} eq 'pty3') { |
|
99
|
1
|
|
50
|
|
|
21
|
$args->{$_} //= 1 for qw(stdin stdout stderr); |
|
100
|
1
|
|
|
|
|
27
|
@fh{stdin_write} = IO::Pty->new; |
|
101
|
1
|
|
|
|
|
504
|
@fh{qw(stdout_read stdout_write)} = $self->_pipe; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
else { |
|
104
|
0
|
|
|
|
|
0
|
warn "[RWF] Invalid conduit ($args->{conduit})\n" if DEBUG; |
|
105
|
0
|
|
|
|
|
0
|
return $self->emit(error => "Invalid conduit ($args->{conduit})"); |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
|
|
108
|
40
|
100
|
|
|
|
15193
|
@fh{qw(stderr_read stderr_write)} = $self->_pipe if $args->{stderr}; |
|
109
|
|
|
|
|
|
|
|
|
110
|
40
|
|
|
|
|
305
|
$self->emit(before_fork => \%fh); # LEGACY |
|
111
|
40
|
|
|
|
|
591
|
$self->emit(prepare => \%fh); |
|
112
|
|
|
|
|
|
|
|
|
113
|
40
|
50
|
|
|
|
98520
|
return $self->emit(error => "Couldn't fork ($!)") unless defined($self->{pid} = fork); |
|
114
|
40
|
100
|
|
|
|
3125
|
return $self->{pid} ? $self->_start_parent($args, \%fh) : $self->_start_child($args, \%fh); |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub _start_child { |
|
118
|
4
|
|
|
4
|
|
95
|
my ($self, $args, $fh) = @_; |
|
119
|
|
|
|
|
|
|
|
|
120
|
4
|
100
|
66
|
|
|
455
|
if (my $pty = blessed $fh->{stdin_write} && $fh->{stdin_write}->isa('IO::Pty') && $fh->{stdin_write}) { |
|
121
|
2
|
|
|
|
|
72
|
$pty->make_slave_controlling_terminal; |
|
122
|
2
|
|
|
|
|
1448
|
$fh->{stdin_read} = $pty->slave; |
|
123
|
2
|
50
|
|
|
|
144
|
$fh->{stdin_read}->set_raw if $args->{raw}; |
|
124
|
2
|
50
|
|
|
|
21
|
$fh->{stdin_read}->clone_winsize_from($args->{clone_winsize_from}) if $args->{clone_winsize_from}; |
|
125
|
2
|
|
33
|
|
|
53
|
$fh->{stdout_write} ||= $fh->{stdin_read}; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
4
|
|
33
|
|
|
276
|
my $stdout_no = ($args->{stdout} // 1) && fileno($fh->{stdout_write}); |
|
129
|
4
|
|
33
|
|
|
186
|
my $stderr_no = ($args->{stderr} // 1) && fileno($fh->{stderr_write} || $fh->{stdout_write}); |
|
130
|
4
|
50
|
|
|
|
374
|
open STDIN, '<&' . fileno($fh->{stdin_read}) or die $!; |
|
131
|
4
|
50
|
50
|
|
|
136
|
open STDOUT, '>&' . $stdout_no or die $! if $stdout_no; |
|
132
|
4
|
50
|
50
|
|
|
202
|
open STDERR, '>&' . $stderr_no or die $! if $stderr_no; |
|
133
|
4
|
50
|
|
|
|
255
|
$stdout_no ? STDOUT->autoflush(1) : STDOUT->close; |
|
134
|
4
|
50
|
|
|
|
624
|
$stderr_no ? STDERR->autoflush(1) : STDERR->close; |
|
135
|
|
|
|
|
|
|
|
|
136
|
4
|
|
|
|
|
237
|
$fh->{stdin_write}->close; |
|
137
|
4
|
|
|
|
|
83
|
$fh->{stdout_read}->close; |
|
138
|
4
|
50
|
|
|
|
61
|
$fh->{stderr_read}->close if $fh->{stderr_read}; |
|
139
|
|
|
|
|
|
|
|
|
140
|
4
|
|
|
|
|
23
|
%ENV = %{$args->{env}}; |
|
|
4
|
|
|
|
|
1159
|
|
|
141
|
|
|
|
|
|
|
|
|
142
|
4
|
|
|
|
|
27
|
my $errno; |
|
143
|
4
|
50
|
|
|
|
45
|
if (ref $args->{program} eq 'CODE') { |
|
144
|
0
|
|
|
|
|
0
|
$! = 0; |
|
145
|
0
|
|
|
|
|
0
|
@SIG{@SAFE_SIG} = ('DEFAULT') x @SAFE_SIG; |
|
146
|
0
|
0
|
|
|
|
0
|
eval { $args->{program}->(@{$args->{program_args} || []}); }; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
147
|
0
|
0
|
|
|
|
0
|
$errno = $@ ? 255 : $!; |
|
148
|
0
|
0
|
|
|
|
0
|
print STDERR $@ if length $@; |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
else { |
|
151
|
4
|
50
|
|
|
|
25
|
exec $args->{program}, @{$args->{program_args} || []}; |
|
|
4
|
|
|
|
|
0
|
|
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
|
|
154
|
0
|
|
0
|
|
|
0
|
eval { POSIX::_exit($errno // $!); }; |
|
|
0
|
|
|
|
|
0
|
|
|
155
|
0
|
|
0
|
|
|
0
|
die($errno // $!); |
|
156
|
|
|
|
|
|
|
} |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub _start_parent { |
|
159
|
36
|
|
|
36
|
|
908
|
my ($self, $args, $fh) = @_; |
|
160
|
|
|
|
|
|
|
|
|
161
|
36
|
|
|
|
|
268
|
$self->_d("Forked $args->{program} @{$args->{program_args} || []}") if DEBUG; |
|
162
|
36
|
|
|
|
|
1620
|
@$self{qw(stdin_write stdout_read stderr_read)} = @$fh{qw(stdin_write stdout_read stderr_read)}; |
|
163
|
36
|
|
|
|
|
559
|
@$self{qw(wait_eof wait_sigchld)} = (1, 1); |
|
164
|
|
|
|
|
|
|
|
|
165
|
36
|
100
|
66
|
|
|
2491
|
$fh->{stdin_write}->close_slave if blessed $fh->{stdin_write} and $fh->{stdin_write}->isa('IO::Pty'); |
|
166
|
36
|
100
|
|
|
|
2696
|
$self->{stream}{pty} = $self->_stream(pty => $fh->{stdin_write}) if $args->{conduit} eq 'pty3'; |
|
167
|
36
|
100
|
|
|
|
886
|
$self->{stream}{stderr} = $self->_stream(stderr => $fh->{stderr_read}) if $fh->{stderr_read}; |
|
168
|
36
|
100
|
100
|
|
|
1765
|
$self->{stream}{stdout} = $self->_stream(stdout => $fh->{stdout_read}) if !$fh->{stderr_read} or $args->{stdout}; |
|
169
|
|
|
|
|
|
|
|
|
170
|
36
|
|
|
35
|
|
13703
|
$SIGCHLD->waitpid($self->{pid} => sub { $self->_sigchld(@_) }); |
|
|
35
|
|
|
|
|
243
|
|
|
171
|
36
|
|
|
|
|
1752
|
$self->emit('fork'); # LEGACY |
|
172
|
36
|
|
|
|
|
444
|
$self->emit('spawn'); |
|
173
|
36
|
|
|
|
|
1984
|
$self->_write; |
|
174
|
|
|
|
|
|
|
} |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub write { |
|
177
|
6
|
|
|
6
|
1
|
657
|
my ($self, $chunk, $cb) = @_; |
|
178
|
6
|
100
|
|
|
|
34
|
$self->once(drain => $cb) if $cb; |
|
179
|
6
|
|
|
|
|
74
|
$self->{stdin_buffer} .= $chunk; |
|
180
|
6
|
100
|
|
|
|
22
|
$self->_write if $self->{stdin_write}; |
|
181
|
6
|
|
|
|
|
58
|
return $self; |
|
182
|
|
|
|
|
|
|
} |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub kill { |
|
185
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
186
|
0
|
|
0
|
|
|
0
|
my $signal = shift // 15; |
|
187
|
0
|
0
|
|
|
|
0
|
return undef unless my $pid = $self->{pid}; |
|
188
|
0
|
|
|
|
|
0
|
$self->_d("kill $signal $pid") if DEBUG; |
|
189
|
0
|
|
|
|
|
0
|
return kill $signal, $pid; |
|
190
|
|
|
|
|
|
|
} |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub _error { |
|
193
|
0
|
0
|
0
|
0
|
|
0
|
return if $! == EAGAIN || $! == EINTR || $! == EWOULDBLOCK; |
|
|
|
|
0
|
|
|
|
|
|
194
|
0
|
0
|
0
|
|
|
0
|
return $_[0]->kill if $! == ECONNRESET || $! == EPIPE; |
|
195
|
0
|
|
|
|
|
0
|
return $_[0]->emit(error => $!)->kill; |
|
196
|
|
|
|
|
|
|
} |
|
197
|
|
|
|
|
|
|
|
|
198
|
0
|
|
|
0
|
|
0
|
sub _d { warn "-- [$_[0]->{pid}] $_[1]\n" } |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub _maybe_terminate { |
|
201
|
73
|
|
|
73
|
|
325
|
my ($self, $pending_event) = @_; |
|
202
|
73
|
|
|
|
|
277
|
delete $self->{$pending_event}; |
|
203
|
73
|
100
|
100
|
|
|
726
|
return if $self->{wait_eof} or $self->{wait_sigchld}; |
|
204
|
|
|
|
|
|
|
|
|
205
|
35
|
|
|
|
|
375
|
delete $self->{stdin_write}; |
|
206
|
35
|
|
|
|
|
1726
|
delete $self->{stdout_read}; |
|
207
|
35
|
|
|
|
|
174
|
delete $self->{stderr_read}; |
|
208
|
|
|
|
|
|
|
|
|
209
|
35
|
|
|
|
|
103
|
my @errors; |
|
210
|
35
|
|
|
|
|
88
|
for my $cb (@{$self->subscribers('close')}, @{$self->subscribers('finish')}) { |
|
|
35
|
|
|
|
|
355
|
|
|
|
35
|
|
|
|
|
388
|
|
|
211
|
41
|
100
|
|
|
|
1776
|
push @errors, $@ unless eval { $self->$cb(@$self{qw(exit_value signal)}); 1 }; |
|
|
41
|
|
|
|
|
493
|
|
|
|
40
|
|
|
|
|
6178
|
|
|
212
|
|
|
|
|
|
|
} |
|
213
|
|
|
|
|
|
|
|
|
214
|
35
|
|
|
|
|
1041
|
$self->emit(error => $_) for @errors; |
|
215
|
|
|
|
|
|
|
} |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub _pipe { |
|
218
|
30
|
|
|
30
|
|
74
|
my $self = shift; |
|
219
|
30
|
50
|
|
|
|
982
|
pipe my $read, my $write or return $self->emit(error => "pipe: $!"); |
|
220
|
30
|
|
|
|
|
234
|
$write->autoflush(1); |
|
221
|
30
|
|
|
|
|
1290
|
return $read, $write; |
|
222
|
|
|
|
|
|
|
} |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
sub _sigchld { |
|
225
|
35
|
|
|
35
|
|
199
|
my ($self, $status, $pid) = @_; |
|
226
|
35
|
|
|
|
|
228
|
my ($exit_value, $signal) = ($status >> 8, $status & 127); |
|
227
|
35
|
|
|
|
|
84
|
$self->_d("Exit exit_value=$exit_value, signal=$signal") if DEBUG; |
|
228
|
35
|
|
|
|
|
196
|
@$self{qw(exit_value signal)} = ($exit_value, $signal); |
|
229
|
35
|
|
|
|
|
244
|
$self->_maybe_terminate('wait_sigchld'); |
|
230
|
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
sub _stream { |
|
233
|
39
|
|
|
39
|
|
440
|
my ($self, $conduit, $handle) = @_; |
|
234
|
39
|
|
|
|
|
1866
|
my $stream = Mojo::IOLoop::Stream->new($handle)->timeout(0); |
|
235
|
|
|
|
|
|
|
|
|
236
|
39
|
100
|
|
|
|
12904
|
my $event_name = $self->{stderr_read} ? $conduit : 'read'; |
|
237
|
|
|
|
|
|
|
my $read_cb = sub { |
|
238
|
53
|
|
|
53
|
|
150668
|
$self->_d(sprintf ">>> RWF:%s ($event_name)\n%s", uc $conduit, term_escape $_[1]) if DEBUG; |
|
239
|
53
|
|
|
|
|
298
|
$self->emit($event_name => $_[1]); |
|
240
|
39
|
|
|
|
|
993
|
}; |
|
241
|
|
|
|
|
|
|
|
|
242
|
39
|
50
|
|
22
|
|
1712
|
$stream->on(error => sub { $! != EIO && $self->emit(error => "Read error: $_[1]") }); |
|
|
22
|
|
|
|
|
25022
|
|
|
243
|
39
|
|
|
38
|
|
1474
|
$stream->on(close => sub { $self->_maybe_terminate('wait_eof') }); |
|
|
38
|
|
|
|
|
31457
|
|
|
244
|
39
|
|
|
|
|
562
|
$stream->on(read => $read_cb); |
|
245
|
|
|
|
|
|
|
|
|
246
|
39
|
|
|
|
|
915
|
return $self->ioloop->stream($stream); |
|
247
|
|
|
|
|
|
|
} |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
sub _write { |
|
250
|
38
|
|
|
38
|
|
88
|
my $self = shift; |
|
251
|
38
|
100
|
|
|
|
3250
|
return unless length $self->{stdin_buffer}; |
|
252
|
|
|
|
|
|
|
|
|
253
|
6
|
|
|
|
|
18
|
my $stdin_write = $self->{stdin_write}; |
|
254
|
6
|
|
|
|
|
164
|
my $written = $stdin_write->syswrite($self->{stdin_buffer}); |
|
255
|
6
|
50
|
|
|
|
369
|
return $self->_error unless defined $written; |
|
256
|
|
|
|
|
|
|
|
|
257
|
6
|
|
|
|
|
34
|
my $chunk = substr $self->{stdin_buffer}, 0, $written, ''; |
|
258
|
6
|
|
|
|
|
25
|
$self->_d(sprintf "<<< RWF:STDIN\n%s", term_escape $chunk) if DEBUG; |
|
259
|
|
|
|
|
|
|
|
|
260
|
6
|
50
|
|
|
|
43
|
if (length $self->{stdin_buffer}) { |
|
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
|
0
|
|
0
|
|
0
|
$self->ioloop->timer(0.01 => sub { $self and $self->_write }); |
|
|
0
|
|
|
|
|
0
|
|
|
265
|
|
|
|
|
|
|
} |
|
266
|
|
|
|
|
|
|
else { |
|
267
|
6
|
|
|
|
|
40
|
$self->emit('drain'); |
|
268
|
|
|
|
|
|
|
} |
|
269
|
|
|
|
|
|
|
} |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
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 |
|
309
|
|
|
|
|
|
|
for an example usage from a L. |
|
310
|
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
L enable you to fork a child process and L |
|
314
|
|
|
|
|
|
|
and L data to. You can also L 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 can write to STDIN or a L object, and |
|
321
|
|
|
|
|
|
|
read from STDOUT or STDERR, depending on the "type" given to L. |
|
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 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. |
|
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 object will be |
|
355
|
|
|
|
|
|
|
used for STDIN and STDOUT instead of a plain pipe. In addition, it is possible |
|
356
|
|
|
|
|
|
|
to pass in C and C: |
|
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 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 |
|
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. C<$asset> can be |
|
393
|
|
|
|
|
|
|
either a L or L 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 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. |
|
450
|
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
=head2 spawn |
|
452
|
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
$rwf->on(spawn => sub ($rwf) { ... }); |
|
454
|
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
Emitted after C 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 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 |
|
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 |
|
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. This only makes sense if L is set |
|
504
|
|
|
|
|
|
|
to "pty". This can also be specified by using the L attribute. |
|
505
|
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
=item * raw |
|
507
|
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
See L. This only makes sense if L is set to "pty". |
|
509
|
|
|
|
|
|
|
This can also be specified by using the L attribute. |
|
510
|
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
=item * stderr |
|
512
|
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
This will make L 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 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 |
|
526
|
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
=back |
|
528
|
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
=head2 ioloop |
|
530
|
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
$ioloop = $rwf->ioloop; |
|
532
|
|
|
|
|
|
|
$rwf = $rwf->ioloop(Mojo::IOLoop->singleton); |
|
533
|
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
Holds a L object. |
|
535
|
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
=head2 pid |
|
537
|
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
$int = $rwf->pid; |
|
539
|
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
Holds the child process ID. Note that L 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. 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 takes the same arguments as L, but the |
|
572
|
|
|
|
|
|
|
fullfillment callback will receive a L object that holds the |
|
573
|
|
|
|
|
|
|
output from the command. |
|
574
|
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
See also the L 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. The L will be resolved on |
|
583
|
|
|
|
|
|
|
L and rejected on L. |
|
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 or as input to the CODE ref. |
|
600
|
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
Note that this module will start L with this code: |
|
602
|
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
exec $program, @$program_args; |
|
604
|
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
This means that the code is subject for |
|
606
|
|
|
|
|
|
|
L |
|
607
|
|
|
|
|
|
|
unless invoked with more than one argument. This is considered a feature, but |
|
608
|
|
|
|
|
|
|
something you should be avare of. See also L for more details. |
|
609
|
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
=item * env |
|
611
|
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
Passing in C 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. |
|
639
|
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
L |
|
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 |
|
652
|
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
=cut |