| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package IPC::Cmd; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
1080
|
use strict; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
115
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
BEGIN { |
|
6
|
|
|
|
|
|
|
|
|
7
|
2
|
50
|
|
2
|
|
11
|
use constant IS_VMS => $^O eq 'VMS' ? 1 : 0; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
172
|
|
|
8
|
2
|
50
|
|
2
|
|
12
|
use constant IS_WIN32 => $^O eq 'MSWin32' ? 1 : 0; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
169
|
|
|
9
|
2
|
|
|
2
|
|
14
|
use constant IS_WIN98 => (IS_WIN32 and !Win32::IsWinNT()) ? 1 : 0; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
87
|
|
|
10
|
2
|
|
|
2
|
|
9
|
use constant ALARM_CLASS => __PACKAGE__ . '::TimeOut'; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
106
|
|
|
11
|
2
|
|
|
2
|
|
13
|
use constant SPECIAL_CHARS => qw[< > | &]; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
131
|
|
|
12
|
2
|
|
|
2
|
|
10
|
use constant QUOTE => do { IS_WIN32 ? q["] : q['] }; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
83
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
10
|
use Exporter (); |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
71
|
|
|
15
|
2
|
|
|
|
|
553
|
use vars qw[ @ISA $VERSION @EXPORT_OK $VERBOSE $DEBUG |
|
16
|
|
|
|
|
|
|
$USE_IPC_RUN $USE_IPC_OPEN3 $CAN_USE_RUN_FORKED $WARN |
|
17
|
|
|
|
|
|
|
$INSTANCES $ALLOW_NULL_ARGS |
|
18
|
|
|
|
|
|
|
$HAVE_MONOTONIC |
|
19
|
2
|
|
|
2
|
|
10
|
]; |
|
|
2
|
|
|
|
|
7
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
2
|
|
|
2
|
|
7
|
$VERSION = '1.00'; |
|
22
|
2
|
|
|
|
|
3
|
$VERBOSE = 0; |
|
23
|
2
|
|
|
|
|
3
|
$DEBUG = 0; |
|
24
|
2
|
|
|
|
|
4
|
$WARN = 1; |
|
25
|
2
|
|
|
|
|
3
|
$USE_IPC_RUN = IS_WIN32 && !IS_WIN98; |
|
26
|
2
|
|
|
|
|
4
|
$USE_IPC_OPEN3 = not IS_VMS; |
|
27
|
2
|
|
|
|
|
3
|
$ALLOW_NULL_ARGS = 0; |
|
28
|
|
|
|
|
|
|
|
|
29
|
2
|
|
|
|
|
3
|
$CAN_USE_RUN_FORKED = 0; |
|
30
|
2
|
|
|
|
|
3
|
eval { |
|
31
|
2
|
|
|
|
|
914
|
require POSIX; POSIX->import(); |
|
|
2
|
|
|
|
|
10767
|
|
|
32
|
2
|
|
|
|
|
5470
|
require IPC::Open3; IPC::Open3->import(); |
|
|
2
|
|
|
|
|
5990
|
|
|
33
|
2
|
|
|
|
|
814
|
require IO::Select; IO::Select->import(); |
|
|
2
|
|
|
|
|
2951
|
|
|
34
|
2
|
|
|
|
|
498
|
require IO::Handle; IO::Handle->import(); |
|
|
2
|
|
|
|
|
4756
|
|
|
35
|
2
|
|
|
|
|
830
|
require FileHandle; FileHandle->import(); |
|
|
2
|
|
|
|
|
4875
|
|
|
36
|
2
|
|
|
|
|
1448
|
require Socket; |
|
37
|
2
|
|
|
|
|
7032
|
require Time::HiRes; Time::HiRes->import(); |
|
|
2
|
|
|
|
|
2305
|
|
|
38
|
2
|
|
|
|
|
132
|
require Win32 if IS_WIN32; |
|
39
|
|
|
|
|
|
|
}; |
|
40
|
2
|
|
50
|
|
|
24
|
$CAN_USE_RUN_FORKED = $@ || !IS_VMS && !IS_WIN32; |
|
41
|
|
|
|
|
|
|
|
|
42
|
2
|
|
|
|
|
3
|
eval { |
|
43
|
2
|
|
|
|
|
10
|
my $wait_start_time = Time::HiRes::clock_gettime(&Time::HiRes::CLOCK_MONOTONIC); |
|
44
|
|
|
|
|
|
|
}; |
|
45
|
2
|
50
|
|
|
|
61
|
if ($@) { |
|
46
|
0
|
|
|
|
|
0
|
$HAVE_MONOTONIC = 0; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
else { |
|
49
|
2
|
|
|
|
|
4
|
$HAVE_MONOTONIC = 1; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
2
|
|
|
|
|
41
|
@ISA = qw[Exporter]; |
|
53
|
2
|
|
|
|
|
65
|
@EXPORT_OK = qw[can_run run run_forked QUOTE]; |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
require Carp; |
|
57
|
2
|
|
|
2
|
|
11
|
use File::Spec; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
48
|
|
|
58
|
2
|
|
|
2
|
|
858
|
use Params::Check qw[check]; |
|
|
2
|
|
|
|
|
7154
|
|
|
|
2
|
|
|
|
|
100
|
|
|
59
|
2
|
|
|
2
|
|
816
|
use Text::ParseWords (); # import ONLY if needed! |
|
|
2
|
|
|
|
|
2204
|
|
|
|
2
|
|
|
|
|
53
|
|
|
60
|
2
|
|
|
2
|
|
913
|
use Module::Load::Conditional qw[can_load]; |
|
|
2
|
|
|
|
|
23818
|
|
|
|
2
|
|
|
|
|
150
|
|
|
61
|
2
|
|
|
2
|
|
16
|
use Locale::Maketext::Simple Style => 'gettext'; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
15
|
|
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
local $Module::Load::Conditional::FORCE_SAFE_INC = 1; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=pod |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 NAME |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
IPC::Cmd - finding and running system commands made easy |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
use IPC::Cmd qw[can_run run run_forked]; |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $full_path = can_run('wget') or warn 'wget is not installed!'; |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
### commands can be arrayrefs or strings ### |
|
78
|
|
|
|
|
|
|
my $cmd = "$full_path -b theregister.co.uk"; |
|
79
|
|
|
|
|
|
|
my $cmd = [$full_path, '-b', 'theregister.co.uk']; |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
### in scalar context ### |
|
82
|
|
|
|
|
|
|
my $buffer; |
|
83
|
|
|
|
|
|
|
if( scalar run( command => $cmd, |
|
84
|
|
|
|
|
|
|
verbose => 0, |
|
85
|
|
|
|
|
|
|
buffer => \$buffer, |
|
86
|
|
|
|
|
|
|
timeout => 20 ) |
|
87
|
|
|
|
|
|
|
) { |
|
88
|
|
|
|
|
|
|
print "fetched webpage successfully: $buffer\n"; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
### in list context ### |
|
93
|
|
|
|
|
|
|
my( $success, $error_message, $full_buf, $stdout_buf, $stderr_buf ) = |
|
94
|
|
|
|
|
|
|
run( command => $cmd, verbose => 0 ); |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
if( $success ) { |
|
97
|
|
|
|
|
|
|
print "this is what the command printed:\n"; |
|
98
|
|
|
|
|
|
|
print join "", @$full_buf; |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
### run_forked example ### |
|
102
|
|
|
|
|
|
|
my $result = run_forked("$full_path -q -O - theregister.co.uk", {'timeout' => 20}); |
|
103
|
|
|
|
|
|
|
if ($result->{'exit_code'} eq 0 && !$result->{'timeout'}) { |
|
104
|
|
|
|
|
|
|
print "this is what wget returned:\n"; |
|
105
|
|
|
|
|
|
|
print $result->{'stdout'}; |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
### check for features |
|
109
|
|
|
|
|
|
|
print "IPC::Open3 available: " . IPC::Cmd->can_use_ipc_open3; |
|
110
|
|
|
|
|
|
|
print "IPC::Run available: " . IPC::Cmd->can_use_ipc_run; |
|
111
|
|
|
|
|
|
|
print "Can capture buffer: " . IPC::Cmd->can_capture_buffer; |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
### don't have IPC::Cmd be verbose, ie don't print to stdout or |
|
114
|
|
|
|
|
|
|
### stderr when running commands -- default is '0' |
|
115
|
|
|
|
|
|
|
$IPC::Cmd::VERBOSE = 0; |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
IPC::Cmd allows you to run commands platform independently, |
|
121
|
|
|
|
|
|
|
interactively if desired, but have them still work. |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
The C function can tell you if a certain binary is installed |
|
124
|
|
|
|
|
|
|
and if so where, whereas the C function can actually execute any |
|
125
|
|
|
|
|
|
|
of the commands you give it and give you a clear return value, as well |
|
126
|
|
|
|
|
|
|
as adhere to your verbosity settings. |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 CLASS METHODS |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 $ipc_run_version = IPC::Cmd->can_use_ipc_run( [VERBOSE] ) |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Utility function that tells you if C is available. |
|
133
|
|
|
|
|
|
|
If the C flag is passed, it will print diagnostic messages |
|
134
|
|
|
|
|
|
|
if L can not be found or loaded. |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=cut |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub can_use_ipc_run { |
|
140
|
1
|
|
|
1
|
1
|
5658
|
my $self = shift; |
|
141
|
1
|
|
50
|
|
|
9
|
my $verbose = shift || 0; |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
### IPC::Run doesn't run on win98 |
|
144
|
1
|
|
|
|
|
2
|
return if IS_WIN98; |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
### if we don't have ipc::run, we obviously can't use it. |
|
147
|
1
|
50
|
33
|
|
|
9
|
return unless can_load( |
|
148
|
|
|
|
|
|
|
modules => { 'IPC::Run' => '0.55' }, |
|
149
|
|
|
|
|
|
|
verbose => ($WARN && $verbose), |
|
150
|
|
|
|
|
|
|
); |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
### otherwise, we're good to go |
|
153
|
0
|
|
|
|
|
0
|
return $IPC::Run::VERSION; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 $ipc_open3_version = IPC::Cmd->can_use_ipc_open3( [VERBOSE] ) |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Utility function that tells you if C is available. |
|
159
|
|
|
|
|
|
|
If the verbose flag is passed, it will print diagnostic messages |
|
160
|
|
|
|
|
|
|
if C can not be found or loaded. |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub can_use_ipc_open3 { |
|
166
|
141
|
|
|
141
|
1
|
1188
|
my $self = shift; |
|
167
|
141
|
|
100
|
|
|
883
|
my $verbose = shift || 0; |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
### IPC::Open3 is not working on VMS because of a lack of fork. |
|
170
|
141
|
|
|
|
|
419
|
return if IS_VMS; |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
### IPC::Open3 works on every non-VMS platform, but it can't |
|
173
|
|
|
|
|
|
|
### capture buffers on win32 :( |
|
174
|
|
|
|
|
|
|
return unless can_load( |
|
175
|
141
|
50
|
66
|
|
|
517
|
modules => { map {$_ => '0.0'} qw|IPC::Open3 IO::Select Symbol| }, |
|
|
423
|
|
|
|
|
3256
|
|
|
176
|
|
|
|
|
|
|
verbose => ($WARN && $verbose), |
|
177
|
|
|
|
|
|
|
); |
|
178
|
|
|
|
|
|
|
|
|
179
|
141
|
|
|
|
|
44354
|
return $IPC::Open3::VERSION; |
|
180
|
|
|
|
|
|
|
} |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 $bool = IPC::Cmd->can_capture_buffer |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Utility function that tells you if C is capable of |
|
185
|
|
|
|
|
|
|
capturing buffers in it's current configuration. |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub can_capture_buffer { |
|
190
|
200
|
|
|
200
|
1
|
136577
|
my $self = shift; |
|
191
|
|
|
|
|
|
|
|
|
192
|
200
|
50
|
33
|
|
|
698
|
return 1 if $USE_IPC_RUN && $self->can_use_ipc_run; |
|
193
|
200
|
100
|
66
|
|
|
1237
|
return 1 if $USE_IPC_OPEN3 && $self->can_use_ipc_open3; |
|
194
|
100
|
|
|
|
|
366
|
return; |
|
195
|
|
|
|
|
|
|
} |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head2 $bool = IPC::Cmd->can_use_run_forked |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Utility function that tells you if C is capable of |
|
200
|
|
|
|
|
|
|
providing C on the current platform. |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head2 $path = can_run( PROGRAM ); |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
C takes only one argument: the name of a binary you wish |
|
207
|
|
|
|
|
|
|
to locate. C works much like the unix binary C or the bash |
|
208
|
|
|
|
|
|
|
command C, which scans through your path, looking for the requested |
|
209
|
|
|
|
|
|
|
binary. |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Unlike C and C, this function is platform independent and |
|
212
|
|
|
|
|
|
|
will also work on, for example, Win32. |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
If called in a scalar context it will return the full path to the binary |
|
215
|
|
|
|
|
|
|
you asked for if it was found, or C if it was not. |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
If called in a list context and the global variable C<$INSTANCES> is a true |
|
218
|
|
|
|
|
|
|
value, it will return a list of the full paths to instances |
|
219
|
|
|
|
|
|
|
of the binary where found in C, or an empty list if it was not found. |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=cut |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub can_run { |
|
224
|
7
|
|
|
7
|
1
|
1288
|
my $command = shift; |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# a lot of VMS executables have a symbol defined |
|
227
|
|
|
|
|
|
|
# check those first |
|
228
|
7
|
50
|
|
|
|
26
|
if ( $^O eq 'VMS' ) { |
|
229
|
0
|
|
|
|
|
0
|
require VMS::DCLsym; |
|
230
|
0
|
|
|
|
|
0
|
my $syms = VMS::DCLsym->new; |
|
231
|
0
|
0
|
|
|
|
0
|
return $command if scalar $syms->getsym( uc $command ); |
|
232
|
|
|
|
|
|
|
} |
|
233
|
|
|
|
|
|
|
|
|
234
|
7
|
|
|
|
|
40
|
require File::Spec; |
|
235
|
7
|
|
|
|
|
1843
|
require ExtUtils::MakeMaker; |
|
236
|
|
|
|
|
|
|
|
|
237
|
7
|
|
|
|
|
197459
|
my @possibles; |
|
238
|
|
|
|
|
|
|
|
|
239
|
7
|
100
|
|
|
|
48
|
if( File::Spec->file_name_is_absolute($command) ) { |
|
240
|
1
|
|
|
|
|
11
|
return MM->maybe_command($command); |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
} else { |
|
243
|
6
|
|
|
|
|
64
|
for my $dir ( |
|
244
|
|
|
|
|
|
|
File::Spec->path, |
|
245
|
|
|
|
|
|
|
( IS_WIN32 ? File::Spec->curdir : () ) |
|
246
|
|
|
|
|
|
|
) { |
|
247
|
54
|
100
|
66
|
|
|
1413
|
next if ! $dir || ! -d $dir; |
|
248
|
48
|
|
|
|
|
347
|
my $abs = File::Spec->catfile( IS_WIN32 ? Win32::GetShortPathName( $dir ) : $dir, $command); |
|
249
|
48
|
100
|
|
|
|
128
|
push @possibles, $abs if $abs = MM->maybe_command($abs); |
|
250
|
|
|
|
|
|
|
} |
|
251
|
|
|
|
|
|
|
} |
|
252
|
6
|
0
|
33
|
|
|
22
|
return @possibles if wantarray and $INSTANCES; |
|
253
|
6
|
|
|
|
|
22
|
return shift @possibles; |
|
254
|
|
|
|
|
|
|
} |
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head2 $ok | ($ok, $err, $full_buf, $stdout_buff, $stderr_buff) = run( command => COMMAND, [verbose => BOOL, buffer => \$SCALAR, timeout => DIGIT] ); |
|
257
|
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
C takes 4 arguments: |
|
259
|
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=over 4 |
|
261
|
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=item command |
|
263
|
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
This is the command to execute. It may be either a string or an array |
|
265
|
|
|
|
|
|
|
reference. |
|
266
|
|
|
|
|
|
|
This is a required argument. |
|
267
|
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
See L<"Caveats"> for remarks on how commands are parsed and their |
|
269
|
|
|
|
|
|
|
limitations. |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=item verbose |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
This controls whether all output of a command should also be printed |
|
274
|
|
|
|
|
|
|
to STDOUT/STDERR or should only be trapped in buffers (NOTE: buffers |
|
275
|
|
|
|
|
|
|
require L to be installed, or your system able to work with |
|
276
|
|
|
|
|
|
|
L). |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
It will default to the global setting of C<$IPC::Cmd::VERBOSE>, |
|
279
|
|
|
|
|
|
|
which by default is 0. |
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=item buffer |
|
282
|
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
This will hold all the output of a command. It needs to be a reference |
|
284
|
|
|
|
|
|
|
to a scalar. |
|
285
|
|
|
|
|
|
|
Note that this will hold both the STDOUT and STDERR messages, and you |
|
286
|
|
|
|
|
|
|
have no way of telling which is which. |
|
287
|
|
|
|
|
|
|
If you require this distinction, run the C command in list context |
|
288
|
|
|
|
|
|
|
and inspect the individual buffers. |
|
289
|
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
Of course, this requires that the underlying call supports buffers. See |
|
291
|
|
|
|
|
|
|
the note on buffers above. |
|
292
|
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=item timeout |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
Sets the maximum time the command is allowed to run before aborting, |
|
296
|
|
|
|
|
|
|
using the built-in C call. If the timeout is triggered, the |
|
297
|
|
|
|
|
|
|
C in the return value will be set to an object of the |
|
298
|
|
|
|
|
|
|
C class. See the L<"error message"> section below for |
|
299
|
|
|
|
|
|
|
details. |
|
300
|
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
Defaults to C<0>, meaning no timeout is set. |
|
302
|
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=back |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
C will return a simple C or C when called in scalar |
|
306
|
|
|
|
|
|
|
context. |
|
307
|
|
|
|
|
|
|
In list context, you will be returned a list of the following items: |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=over 4 |
|
310
|
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=item success |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
A simple boolean indicating if the command executed without errors or |
|
314
|
|
|
|
|
|
|
not. |
|
315
|
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=item error message |
|
317
|
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
If the first element of the return value (C) was 0, then some |
|
319
|
|
|
|
|
|
|
error occurred. This second element is the error message the command |
|
320
|
|
|
|
|
|
|
you requested exited with, if available. This is generally a pretty |
|
321
|
|
|
|
|
|
|
printed value of C<$?> or C<$@>. See C for details on |
|
322
|
|
|
|
|
|
|
what they can contain. |
|
323
|
|
|
|
|
|
|
If the error was a timeout, the C will be prefixed with |
|
324
|
|
|
|
|
|
|
the string C, the timeout class. |
|
325
|
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=item full_buffer |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
This is an array reference containing all the output the command |
|
329
|
|
|
|
|
|
|
generated. |
|
330
|
|
|
|
|
|
|
Note that buffers are only available if you have L installed, |
|
331
|
|
|
|
|
|
|
or if your system is able to work with L -- see below). |
|
332
|
|
|
|
|
|
|
Otherwise, this element will be C. |
|
333
|
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
=item out_buffer |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
This is an array reference containing all the output sent to STDOUT the |
|
337
|
|
|
|
|
|
|
command generated. The notes from L<"full_buffer"> apply. |
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=item error_buffer |
|
340
|
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
This is an arrayreference containing all the output sent to STDERR the |
|
342
|
|
|
|
|
|
|
command generated. The notes from L<"full_buffer"> apply. |
|
343
|
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
=back |
|
346
|
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
See the L<"HOW IT WORKS"> section below to see how C decides |
|
348
|
|
|
|
|
|
|
what modules or function calls to use when issuing a command. |
|
349
|
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
=cut |
|
351
|
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
{ my @acc = qw[ok error _fds]; |
|
353
|
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
### autogenerate accessors ### |
|
355
|
|
|
|
|
|
|
for my $key ( @acc ) { |
|
356
|
2
|
|
|
2
|
|
1546
|
no strict 'refs'; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
11031
|
|
|
357
|
|
|
|
|
|
|
*{__PACKAGE__."::$key"} = sub { |
|
358
|
400
|
100
|
|
400
|
|
2384
|
$_[0]->{$key} = $_[1] if @_ > 1; |
|
359
|
400
|
|
|
|
|
4923
|
return $_[0]->{$key}; |
|
360
|
|
|
|
|
|
|
} |
|
361
|
|
|
|
|
|
|
} |
|
362
|
|
|
|
|
|
|
} |
|
363
|
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
sub can_use_run_forked { |
|
365
|
110
|
|
|
110
|
1
|
4184
|
return $CAN_USE_RUN_FORKED eq "1"; |
|
366
|
|
|
|
|
|
|
} |
|
367
|
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
sub get_monotonic_time { |
|
369
|
6941
|
50
|
|
6941
|
0
|
15469
|
if ($HAVE_MONOTONIC) { |
|
370
|
6941
|
|
|
|
|
20514
|
return Time::HiRes::clock_gettime(&Time::HiRes::CLOCK_MONOTONIC); |
|
371
|
|
|
|
|
|
|
} |
|
372
|
|
|
|
|
|
|
else { |
|
373
|
0
|
|
|
|
|
0
|
return time(); |
|
374
|
|
|
|
|
|
|
} |
|
375
|
|
|
|
|
|
|
} |
|
376
|
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
sub adjust_monotonic_start_time { |
|
378
|
6721
|
|
|
6721
|
0
|
14003
|
my ($ref_vars, $now, $previous) = @_; |
|
379
|
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
# workaround only for those systems which don't have |
|
381
|
|
|
|
|
|
|
# Time::HiRes::CLOCK_MONOTONIC (Mac OSX in particular) |
|
382
|
6721
|
50
|
|
|
|
13965
|
return if $HAVE_MONOTONIC; |
|
383
|
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
# don't have previous monotonic value (only happens once |
|
385
|
|
|
|
|
|
|
# in the beginning of the program execution) |
|
386
|
0
|
0
|
|
|
|
0
|
return unless $previous; |
|
387
|
|
|
|
|
|
|
|
|
388
|
0
|
|
|
|
|
0
|
my $time_diff = $now - $previous; |
|
389
|
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
# adjust previously saved time with the skew value which is |
|
391
|
|
|
|
|
|
|
# either negative when clock moved back or more than 5 seconds -- |
|
392
|
|
|
|
|
|
|
# assuming that event loop does happen more often than once |
|
393
|
|
|
|
|
|
|
# per five seconds, which might not be always true (!) but |
|
394
|
|
|
|
|
|
|
# hopefully that's ok, because it's just a workaround |
|
395
|
0
|
0
|
0
|
|
|
0
|
if ($time_diff > 5 || $time_diff < 0) { |
|
396
|
0
|
|
|
|
|
0
|
foreach my $ref_var (@{$ref_vars}) { |
|
|
0
|
|
|
|
|
0
|
|
|
397
|
0
|
0
|
|
|
|
0
|
if (defined($$ref_var)) { |
|
398
|
0
|
|
|
|
|
0
|
$$ref_var = $$ref_var + $time_diff; |
|
399
|
|
|
|
|
|
|
} |
|
400
|
|
|
|
|
|
|
} |
|
401
|
|
|
|
|
|
|
} |
|
402
|
|
|
|
|
|
|
} |
|
403
|
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
sub uninstall_signals { |
|
405
|
108
|
50
|
|
108
|
0
|
560
|
return unless defined($IPC::Cmd::{'__old_signals'}); |
|
406
|
|
|
|
|
|
|
|
|
407
|
0
|
|
|
|
|
0
|
foreach my $sig_name (keys %{$IPC::Cmd::{'__old_signals'}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
408
|
0
|
|
|
|
|
0
|
$SIG{$sig_name} = $IPC::Cmd::{'__old_signals'}->{$sig_name}; |
|
409
|
|
|
|
|
|
|
} |
|
410
|
|
|
|
|
|
|
} |
|
411
|
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
# incompatible with POSIX::SigAction |
|
413
|
|
|
|
|
|
|
# |
|
414
|
|
|
|
|
|
|
sub install_layered_signal { |
|
415
|
0
|
|
|
0
|
0
|
0
|
my ($s, $handler_code) = @_; |
|
416
|
|
|
|
|
|
|
|
|
417
|
0
|
|
|
|
|
0
|
my %available_signals = map {$_ => 1} keys %SIG; |
|
|
0
|
|
|
|
|
0
|
|
|
418
|
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
Carp::confess("install_layered_signal got nonexistent signal name [$s]") |
|
420
|
0
|
0
|
|
|
|
0
|
unless defined($available_signals{$s}); |
|
421
|
0
|
0
|
0
|
|
|
0
|
Carp::confess("install_layered_signal expects coderef") |
|
422
|
|
|
|
|
|
|
if !ref($handler_code) || ref($handler_code) ne 'CODE'; |
|
423
|
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
$IPC::Cmd::{'__old_signals'} = {} |
|
425
|
0
|
0
|
|
|
|
0
|
unless defined($IPC::Cmd::{'__old_signals'}); |
|
426
|
0
|
|
|
|
|
0
|
$IPC::Cmd::{'__old_signals'}->{$s} = $SIG{$s}; |
|
427
|
|
|
|
|
|
|
|
|
428
|
0
|
|
|
|
|
0
|
my $previous_handler = $SIG{$s}; |
|
429
|
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
my $sig_handler = sub { |
|
431
|
0
|
|
|
0
|
|
0
|
my ($called_sig_name, @sig_param) = @_; |
|
432
|
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
# $s is a closure referring to real signal name |
|
434
|
|
|
|
|
|
|
# for which this handler is being installed. |
|
435
|
|
|
|
|
|
|
# it is used to distinguish between |
|
436
|
|
|
|
|
|
|
# real signal handlers and aliased signal handlers |
|
437
|
0
|
|
|
|
|
0
|
my $signal_name = $s; |
|
438
|
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
# $called_sig_name is a signal name which |
|
440
|
|
|
|
|
|
|
# was passed to this signal handler; |
|
441
|
|
|
|
|
|
|
# it doesn't equal $signal_name in case |
|
442
|
|
|
|
|
|
|
# some signal handlers in %SIG point |
|
443
|
|
|
|
|
|
|
# to other signal handler (CHLD and CLD, |
|
444
|
|
|
|
|
|
|
# ABRT and IOT) |
|
445
|
|
|
|
|
|
|
# |
|
446
|
|
|
|
|
|
|
# initial signal handler for aliased signal |
|
447
|
|
|
|
|
|
|
# calls some other signal handler which |
|
448
|
|
|
|
|
|
|
# should not execute the same handler_code again |
|
449
|
0
|
0
|
|
|
|
0
|
if ($called_sig_name eq $signal_name) { |
|
450
|
0
|
|
|
|
|
0
|
$handler_code->($signal_name); |
|
451
|
|
|
|
|
|
|
} |
|
452
|
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
# run original signal handler if any (including aliased) |
|
454
|
|
|
|
|
|
|
# |
|
455
|
0
|
0
|
|
|
|
0
|
if (ref($previous_handler)) { |
|
456
|
0
|
|
|
|
|
0
|
$previous_handler->($called_sig_name, @sig_param); |
|
457
|
|
|
|
|
|
|
} |
|
458
|
0
|
|
|
|
|
0
|
}; |
|
459
|
|
|
|
|
|
|
|
|
460
|
0
|
|
|
|
|
0
|
$SIG{$s} = $sig_handler; |
|
461
|
|
|
|
|
|
|
} |
|
462
|
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
# give process a chance sending TERM, |
|
464
|
|
|
|
|
|
|
# waiting for a while (2 seconds) |
|
465
|
|
|
|
|
|
|
# and killing it with KILL |
|
466
|
|
|
|
|
|
|
sub kill_gently { |
|
467
|
4
|
|
|
4
|
0
|
27
|
my ($pid, $opts) = @_; |
|
468
|
|
|
|
|
|
|
|
|
469
|
4
|
|
|
|
|
126
|
require POSIX; |
|
470
|
|
|
|
|
|
|
|
|
471
|
4
|
100
|
|
|
|
62
|
$opts = {} unless $opts; |
|
472
|
4
|
100
|
|
|
|
32
|
$opts->{'wait_time'} = 2 unless defined($opts->{'wait_time'}); |
|
473
|
4
|
100
|
|
|
|
41
|
$opts->{'first_kill_type'} = 'just_process' unless $opts->{'first_kill_type'}; |
|
474
|
4
|
100
|
|
|
|
25
|
$opts->{'final_kill_type'} = 'just_process' unless $opts->{'final_kill_type'}; |
|
475
|
|
|
|
|
|
|
|
|
476
|
4
|
100
|
|
|
|
55
|
if ($opts->{'first_kill_type'} eq 'just_process') { |
|
|
|
50
|
|
|
|
|
|
|
477
|
2
|
|
|
|
|
47
|
kill(15, $pid); |
|
478
|
|
|
|
|
|
|
} |
|
479
|
|
|
|
|
|
|
elsif ($opts->{'first_kill_type'} eq 'process_group') { |
|
480
|
2
|
|
|
|
|
868
|
kill(-15, $pid); |
|
481
|
|
|
|
|
|
|
} |
|
482
|
|
|
|
|
|
|
|
|
483
|
4
|
|
|
|
|
26
|
my $do_wait = 1; |
|
484
|
4
|
|
|
|
|
14
|
my $child_finished = 0; |
|
485
|
|
|
|
|
|
|
|
|
486
|
4
|
|
|
|
|
23
|
my $wait_start_time = get_monotonic_time(); |
|
487
|
4
|
|
|
|
|
32
|
my $now; |
|
488
|
|
|
|
|
|
|
my $previous_monotonic_value; |
|
489
|
|
|
|
|
|
|
|
|
490
|
4
|
|
|
|
|
18
|
while ($do_wait) { |
|
491
|
8
|
|
|
|
|
54
|
$previous_monotonic_value = $now; |
|
492
|
8
|
|
|
|
|
136
|
$now = get_monotonic_time(); |
|
493
|
|
|
|
|
|
|
|
|
494
|
8
|
|
|
|
|
88
|
adjust_monotonic_start_time([\$wait_start_time], $now, $previous_monotonic_value); |
|
495
|
|
|
|
|
|
|
|
|
496
|
8
|
50
|
|
|
|
48
|
if ($now > $wait_start_time + $opts->{'wait_time'}) { |
|
497
|
0
|
|
|
|
|
0
|
$do_wait = 0; |
|
498
|
0
|
|
|
|
|
0
|
next; |
|
499
|
|
|
|
|
|
|
} |
|
500
|
|
|
|
|
|
|
|
|
501
|
8
|
|
|
|
|
2617
|
my $waitpid = waitpid($pid, POSIX::WNOHANG); |
|
502
|
|
|
|
|
|
|
|
|
503
|
8
|
100
|
|
|
|
60
|
if ($waitpid eq -1) { |
|
504
|
4
|
|
|
|
|
14
|
$child_finished = 1; |
|
505
|
4
|
|
|
|
|
19
|
$do_wait = 0; |
|
506
|
4
|
|
|
|
|
17
|
next; |
|
507
|
|
|
|
|
|
|
} |
|
508
|
|
|
|
|
|
|
|
|
509
|
4
|
|
|
|
|
752159
|
Time::HiRes::usleep(250000); # quarter of a second |
|
510
|
|
|
|
|
|
|
} |
|
511
|
|
|
|
|
|
|
|
|
512
|
4
|
50
|
|
|
|
22
|
if (!$child_finished) { |
|
513
|
0
|
0
|
|
|
|
0
|
if ($opts->{'final_kill_type'} eq 'just_process') { |
|
|
|
0
|
|
|
|
|
|
|
514
|
0
|
|
|
|
|
0
|
kill(9, $pid); |
|
515
|
|
|
|
|
|
|
} |
|
516
|
|
|
|
|
|
|
elsif ($opts->{'final_kill_type'} eq 'process_group') { |
|
517
|
0
|
|
|
|
|
0
|
kill(-9, $pid); |
|
518
|
|
|
|
|
|
|
} |
|
519
|
|
|
|
|
|
|
} |
|
520
|
|
|
|
|
|
|
} |
|
521
|
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
sub open3_run { |
|
523
|
0
|
|
|
0
|
0
|
0
|
my ($cmd, $opts) = @_; |
|
524
|
|
|
|
|
|
|
|
|
525
|
0
|
0
|
|
|
|
0
|
$opts = {} unless $opts; |
|
526
|
|
|
|
|
|
|
|
|
527
|
0
|
|
|
|
|
0
|
my $child_in = FileHandle->new; |
|
528
|
0
|
|
|
|
|
0
|
my $child_out = FileHandle->new; |
|
529
|
0
|
|
|
|
|
0
|
my $child_err = FileHandle->new; |
|
530
|
0
|
|
|
|
|
0
|
$child_out->autoflush(1); |
|
531
|
0
|
|
|
|
|
0
|
$child_err->autoflush(1); |
|
532
|
|
|
|
|
|
|
|
|
533
|
0
|
|
|
|
|
0
|
my $pid = open3($child_in, $child_out, $child_err, $cmd); |
|
534
|
0
|
|
|
|
|
0
|
Time::HiRes::usleep(1); |
|
535
|
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
# push my child's pid to our parent |
|
537
|
|
|
|
|
|
|
# so in case i am killed parent |
|
538
|
|
|
|
|
|
|
# could stop my child (search for |
|
539
|
|
|
|
|
|
|
# child_child_pid in parent code) |
|
540
|
0
|
0
|
|
|
|
0
|
if ($opts->{'parent_info'}) { |
|
541
|
0
|
|
|
|
|
0
|
my $ps = $opts->{'parent_info'}; |
|
542
|
0
|
|
|
|
|
0
|
print $ps "spawned $pid\n"; |
|
543
|
|
|
|
|
|
|
} |
|
544
|
|
|
|
|
|
|
|
|
545
|
0
|
0
|
0
|
|
|
0
|
if ($child_in && $child_out->opened && $opts->{'child_stdin'}) { |
|
|
|
|
0
|
|
|
|
|
|
546
|
|
|
|
|
|
|
# If the child process dies for any reason, |
|
547
|
|
|
|
|
|
|
# the next write to CHLD_IN is likely to generate |
|
548
|
|
|
|
|
|
|
# a SIGPIPE in the parent, which is fatal by default. |
|
549
|
|
|
|
|
|
|
# So you may wish to handle this signal. |
|
550
|
|
|
|
|
|
|
# |
|
551
|
|
|
|
|
|
|
# from http://perldoc.perl.org/IPC/Open3.html, |
|
552
|
|
|
|
|
|
|
# absolutely needed to catch piped commands errors. |
|
553
|
|
|
|
|
|
|
# |
|
554
|
0
|
|
|
0
|
|
0
|
local $SIG{'PIPE'} = sub { 1; }; |
|
|
0
|
|
|
|
|
0
|
|
|
555
|
|
|
|
|
|
|
|
|
556
|
0
|
|
|
|
|
0
|
print $child_in $opts->{'child_stdin'}; |
|
557
|
|
|
|
|
|
|
} |
|
558
|
0
|
|
|
|
|
0
|
close($child_in); |
|
559
|
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
my $child_output = { |
|
561
|
|
|
|
|
|
|
'out' => $child_out->fileno, |
|
562
|
|
|
|
|
|
|
'err' => $child_err->fileno, |
|
563
|
|
|
|
|
|
|
$child_out->fileno => { |
|
564
|
|
|
|
|
|
|
'parent_socket' => $opts->{'parent_stdout'}, |
|
565
|
|
|
|
|
|
|
'scalar_buffer' => "", |
|
566
|
|
|
|
|
|
|
'child_handle' => $child_out, |
|
567
|
|
|
|
|
|
|
'block_size' => ($child_out->stat)[11] || 1024, |
|
568
|
|
|
|
|
|
|
}, |
|
569
|
|
|
|
|
|
|
$child_err->fileno => { |
|
570
|
0
|
|
0
|
|
|
0
|
'parent_socket' => $opts->{'parent_stderr'}, |
|
|
|
|
0
|
|
|
|
|
|
571
|
|
|
|
|
|
|
'scalar_buffer' => "", |
|
572
|
|
|
|
|
|
|
'child_handle' => $child_err, |
|
573
|
|
|
|
|
|
|
'block_size' => ($child_err->stat)[11] || 1024, |
|
574
|
|
|
|
|
|
|
}, |
|
575
|
|
|
|
|
|
|
}; |
|
576
|
|
|
|
|
|
|
|
|
577
|
0
|
|
|
|
|
0
|
my $select = IO::Select->new(); |
|
578
|
0
|
|
|
|
|
0
|
$select->add($child_out, $child_err); |
|
579
|
|
|
|
|
|
|
|
|
580
|
|
|
|
|
|
|
# pass any signal to the child |
|
581
|
|
|
|
|
|
|
# effectively creating process |
|
582
|
|
|
|
|
|
|
# strongly attached to the child: |
|
583
|
|
|
|
|
|
|
# it will terminate only after child |
|
584
|
|
|
|
|
|
|
# has terminated (except for SIGKILL, |
|
585
|
|
|
|
|
|
|
# which is specially handled) |
|
586
|
0
|
|
|
|
|
0
|
SIGNAL: foreach my $s (keys %SIG) { |
|
587
|
0
|
0
|
0
|
|
|
0
|
next SIGNAL if $s eq '__WARN__' or $s eq '__DIE__'; # Skip and don't clobber __DIE__ & __WARN__ |
|
588
|
0
|
|
|
|
|
0
|
my $sig_handler; |
|
589
|
|
|
|
|
|
|
$sig_handler = sub { |
|
590
|
0
|
|
|
0
|
|
0
|
kill("$s", $pid); |
|
591
|
0
|
|
|
|
|
0
|
$SIG{$s} = $sig_handler; |
|
592
|
0
|
|
|
|
|
0
|
}; |
|
593
|
0
|
|
|
|
|
0
|
$SIG{$s} = $sig_handler; |
|
594
|
|
|
|
|
|
|
} |
|
595
|
|
|
|
|
|
|
|
|
596
|
0
|
|
|
|
|
0
|
my $child_finished = 0; |
|
597
|
|
|
|
|
|
|
|
|
598
|
0
|
|
|
|
|
0
|
my $real_exit; |
|
599
|
|
|
|
|
|
|
my $exit_value; |
|
600
|
|
|
|
|
|
|
|
|
601
|
0
|
|
|
|
|
0
|
while(!$child_finished) { |
|
602
|
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
# parent was killed otherwise we would have got |
|
604
|
|
|
|
|
|
|
# the same signal as parent and process it same way |
|
605
|
0
|
0
|
|
|
|
0
|
if (getppid() eq "1") { |
|
606
|
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
# end my process group with all the children |
|
608
|
|
|
|
|
|
|
# (i am the process group leader, so my pid |
|
609
|
|
|
|
|
|
|
# equals to the process group id) |
|
610
|
|
|
|
|
|
|
# |
|
611
|
|
|
|
|
|
|
# same thing which is done |
|
612
|
|
|
|
|
|
|
# with $opts->{'clean_up_children'} |
|
613
|
|
|
|
|
|
|
# in run_forked |
|
614
|
|
|
|
|
|
|
# |
|
615
|
0
|
|
|
|
|
0
|
kill(-9, $$); |
|
616
|
|
|
|
|
|
|
|
|
617
|
0
|
|
|
|
|
0
|
POSIX::_exit 1; |
|
618
|
|
|
|
|
|
|
} |
|
619
|
|
|
|
|
|
|
|
|
620
|
0
|
|
|
|
|
0
|
my $waitpid = waitpid($pid, POSIX::WNOHANG); |
|
621
|
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
# child finished, catch it's exit status |
|
623
|
0
|
0
|
0
|
|
|
0
|
if ($waitpid ne 0 && $waitpid ne -1) { |
|
624
|
0
|
|
|
|
|
0
|
$real_exit = $?; |
|
625
|
0
|
|
|
|
|
0
|
$exit_value = $? >> 8; |
|
626
|
|
|
|
|
|
|
} |
|
627
|
|
|
|
|
|
|
|
|
628
|
0
|
0
|
|
|
|
0
|
if ($waitpid eq -1) { |
|
629
|
0
|
|
|
|
|
0
|
$child_finished = 1; |
|
630
|
|
|
|
|
|
|
} |
|
631
|
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
|
|
633
|
0
|
|
|
|
|
0
|
my $ready_fds = []; |
|
634
|
0
|
|
|
|
|
0
|
push @{$ready_fds}, $select->can_read(1/100); |
|
|
0
|
|
|
|
|
0
|
|
|
635
|
|
|
|
|
|
|
|
|
636
|
0
|
|
|
|
|
0
|
READY_FDS: while (scalar(@{$ready_fds})) { |
|
|
0
|
|
|
|
|
0
|
|
|
637
|
0
|
|
|
|
|
0
|
my $fd = shift @{$ready_fds}; |
|
|
0
|
|
|
|
|
0
|
|
|
638
|
0
|
|
|
|
|
0
|
$ready_fds = [grep {$_ ne $fd} @{$ready_fds}]; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
639
|
|
|
|
|
|
|
|
|
640
|
0
|
|
|
|
|
0
|
my $str = $child_output->{$fd->fileno}; |
|
641
|
0
|
0
|
|
|
|
0
|
Carp::confess("child stream not found: $fd") unless $str; |
|
642
|
|
|
|
|
|
|
|
|
643
|
0
|
|
|
|
|
0
|
my $data; |
|
644
|
0
|
|
|
|
|
0
|
my $count = $fd->sysread($data, $str->{'block_size'}); |
|
645
|
|
|
|
|
|
|
|
|
646
|
0
|
0
|
|
|
|
0
|
if ($count) { |
|
|
|
0
|
|
|
|
|
|
|
647
|
0
|
0
|
|
|
|
0
|
if ($str->{'parent_socket'}) { |
|
648
|
0
|
|
|
|
|
0
|
my $ph = $str->{'parent_socket'}; |
|
649
|
0
|
|
|
|
|
0
|
print $ph $data; |
|
650
|
|
|
|
|
|
|
} |
|
651
|
|
|
|
|
|
|
else { |
|
652
|
0
|
|
|
|
|
0
|
$str->{'scalar_buffer'} .= $data; |
|
653
|
|
|
|
|
|
|
} |
|
654
|
|
|
|
|
|
|
} |
|
655
|
|
|
|
|
|
|
elsif ($count eq 0) { |
|
656
|
0
|
|
|
|
|
0
|
$select->remove($fd); |
|
657
|
0
|
|
|
|
|
0
|
$fd->close(); |
|
658
|
|
|
|
|
|
|
} |
|
659
|
|
|
|
|
|
|
else { |
|
660
|
0
|
|
|
|
|
0
|
Carp::confess("error during sysread: " . $!); |
|
661
|
|
|
|
|
|
|
} |
|
662
|
|
|
|
|
|
|
|
|
663
|
0
|
0
|
|
|
|
0
|
push @{$ready_fds}, $select->can_read(1/100) if $child_finished; |
|
|
0
|
|
|
|
|
0
|
|
|
664
|
|
|
|
|
|
|
} |
|
665
|
|
|
|
|
|
|
|
|
666
|
0
|
|
|
|
|
0
|
Time::HiRes::usleep(1); |
|
667
|
|
|
|
|
|
|
} |
|
668
|
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
# since we've successfully reaped the child, |
|
670
|
|
|
|
|
|
|
# let our parent know about this. |
|
671
|
|
|
|
|
|
|
# |
|
672
|
0
|
0
|
|
|
|
0
|
if ($opts->{'parent_info'}) { |
|
673
|
0
|
|
|
|
|
0
|
my $ps = $opts->{'parent_info'}; |
|
674
|
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
# child was killed, inform parent |
|
676
|
0
|
0
|
|
|
|
0
|
if ($real_exit & 127) { |
|
677
|
0
|
|
|
|
|
0
|
print $ps "$pid killed with " . ($real_exit & 127) . "\n"; |
|
678
|
|
|
|
|
|
|
} |
|
679
|
|
|
|
|
|
|
|
|
680
|
0
|
|
|
|
|
0
|
print $ps "reaped $pid\n"; |
|
681
|
|
|
|
|
|
|
} |
|
682
|
|
|
|
|
|
|
|
|
683
|
0
|
0
|
0
|
|
|
0
|
if ($opts->{'parent_stdout'} || $opts->{'parent_stderr'}) { |
|
684
|
0
|
|
|
|
|
0
|
return $exit_value; |
|
685
|
|
|
|
|
|
|
} |
|
686
|
|
|
|
|
|
|
else { |
|
687
|
|
|
|
|
|
|
return { |
|
688
|
|
|
|
|
|
|
'stdout' => $child_output->{$child_output->{'out'}}->{'scalar_buffer'}, |
|
689
|
0
|
|
|
|
|
0
|
'stderr' => $child_output->{$child_output->{'err'}}->{'scalar_buffer'}, |
|
690
|
|
|
|
|
|
|
'exit_code' => $exit_value, |
|
691
|
|
|
|
|
|
|
}; |
|
692
|
|
|
|
|
|
|
} |
|
693
|
|
|
|
|
|
|
} |
|
694
|
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
=head2 $hashref = run_forked( COMMAND, { child_stdin => SCALAR, timeout => DIGIT, stdout_handler => CODEREF, stderr_handler => CODEREF} ); |
|
696
|
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
C is used to execute some program or a coderef, |
|
698
|
|
|
|
|
|
|
optionally feed it with some input, get its return code |
|
699
|
|
|
|
|
|
|
and output (both stdout and stderr into separate buffers). |
|
700
|
|
|
|
|
|
|
In addition, it allows to terminate the program |
|
701
|
|
|
|
|
|
|
if it takes too long to finish. |
|
702
|
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
The important and distinguishing feature of run_forked |
|
704
|
|
|
|
|
|
|
is execution timeout which at first seems to be |
|
705
|
|
|
|
|
|
|
quite a simple task but if you think |
|
706
|
|
|
|
|
|
|
that the program which you're spawning |
|
707
|
|
|
|
|
|
|
might spawn some children itself (which |
|
708
|
|
|
|
|
|
|
in their turn could do the same and so on) |
|
709
|
|
|
|
|
|
|
it turns out to be not a simple issue. |
|
710
|
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
C is designed to survive and |
|
712
|
|
|
|
|
|
|
successfully terminate almost any long running task, |
|
713
|
|
|
|
|
|
|
even a fork bomb in case your system has the resources |
|
714
|
|
|
|
|
|
|
to survive during given timeout. |
|
715
|
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
This is achieved by creating separate watchdog process |
|
717
|
|
|
|
|
|
|
which spawns the specified program in a separate |
|
718
|
|
|
|
|
|
|
process session and supervises it: optionally |
|
719
|
|
|
|
|
|
|
feeds it with input, stores its exit code, |
|
720
|
|
|
|
|
|
|
stdout and stderr, terminates it in case |
|
721
|
|
|
|
|
|
|
it runs longer than specified. |
|
722
|
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
Invocation requires the command to be executed or a coderef and optionally a hashref of options: |
|
724
|
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
=over |
|
726
|
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
=item C |
|
728
|
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
Specify in seconds how long to run the command before it is killed with SIG_KILL (9), |
|
730
|
|
|
|
|
|
|
which effectively terminates it and all of its children (direct or indirect). |
|
731
|
|
|
|
|
|
|
|
|
732
|
|
|
|
|
|
|
=item C |
|
733
|
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
Specify some text that will be passed into the C of the executed program. |
|
735
|
|
|
|
|
|
|
|
|
736
|
|
|
|
|
|
|
=item C |
|
737
|
|
|
|
|
|
|
|
|
738
|
|
|
|
|
|
|
Coderef of a subroutine to call when a portion of data is received on |
|
739
|
|
|
|
|
|
|
STDOUT from the executing program. |
|
740
|
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
=item C |
|
742
|
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
Coderef of a subroutine to call when a portion of data is received on |
|
744
|
|
|
|
|
|
|
STDERR from the executing program. |
|
745
|
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
=item C |
|
747
|
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
Coderef of a subroutine to call inside of the main waiting loop |
|
749
|
|
|
|
|
|
|
(while C waits for the external to finish or fail). |
|
750
|
|
|
|
|
|
|
It is useful to stop running external process before it ends |
|
751
|
|
|
|
|
|
|
by itself, e.g. |
|
752
|
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
my $r = run_forked("some external command", { |
|
754
|
|
|
|
|
|
|
'wait_loop_callback' => sub { |
|
755
|
|
|
|
|
|
|
if (condition) { |
|
756
|
|
|
|
|
|
|
kill(1, $$); |
|
757
|
|
|
|
|
|
|
} |
|
758
|
|
|
|
|
|
|
}, |
|
759
|
|
|
|
|
|
|
'terminate_on_signal' => 'HUP', |
|
760
|
|
|
|
|
|
|
}); |
|
761
|
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
Combined with C and C allows terminating |
|
763
|
|
|
|
|
|
|
external command based on its output. Could also be used as a timer |
|
764
|
|
|
|
|
|
|
without engaging with L (signals). |
|
765
|
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
Remember that this code could be called every millisecond (depending |
|
767
|
|
|
|
|
|
|
on the output which external command generates), so try to make it |
|
768
|
|
|
|
|
|
|
as lightweight as possible. |
|
769
|
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
=item C |
|
771
|
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
Discards the buffering of the standard output and standard errors for return by run_forked(). |
|
773
|
|
|
|
|
|
|
With this option you have to use the std*_handlers to read what the command outputs. |
|
774
|
|
|
|
|
|
|
Useful for commands that send a lot of output. |
|
775
|
|
|
|
|
|
|
|
|
776
|
|
|
|
|
|
|
=item C |
|
777
|
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
Enable this option if you wish all spawned processes to be killed if the initially spawned |
|
779
|
|
|
|
|
|
|
process (the parent) is killed or dies without waiting for child processes. |
|
780
|
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
=back |
|
782
|
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
C will return a HASHREF with the following keys: |
|
784
|
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
=over |
|
786
|
|
|
|
|
|
|
|
|
787
|
|
|
|
|
|
|
=item C |
|
788
|
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
The exit code of the executed program. |
|
790
|
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
=item C |
|
792
|
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
The number of seconds the program ran for before being terminated, or 0 if no timeout occurred. |
|
794
|
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
=item C |
|
796
|
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
Holds the standard output of the executed command (or empty string if |
|
798
|
|
|
|
|
|
|
there was no STDOUT output or if C was used; it's always defined!) |
|
799
|
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
=item C |
|
801
|
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
Holds the standard error of the executed command (or empty string if |
|
803
|
|
|
|
|
|
|
there was no STDERR output or if C was used; it's always defined!) |
|
804
|
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
=item C |
|
806
|
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
Holds the standard output and error of the executed command merged into one stream |
|
808
|
|
|
|
|
|
|
(or empty string if there was no output at all or if C was used; it's always defined!) |
|
809
|
|
|
|
|
|
|
|
|
810
|
|
|
|
|
|
|
=item C |
|
811
|
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
Holds some explanation in the case of an error. |
|
813
|
|
|
|
|
|
|
|
|
814
|
|
|
|
|
|
|
=back |
|
815
|
|
|
|
|
|
|
|
|
816
|
|
|
|
|
|
|
=cut |
|
817
|
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
sub run_forked { |
|
819
|
|
|
|
|
|
|
### container to store things in |
|
820
|
108
|
|
|
108
|
1
|
686817
|
my $self = bless {}, __PACKAGE__; |
|
821
|
|
|
|
|
|
|
|
|
822
|
108
|
50
|
|
|
|
1369
|
if (!can_use_run_forked()) { |
|
823
|
0
|
|
|
|
|
0
|
Carp::carp("run_forked is not available: $CAN_USE_RUN_FORKED"); |
|
824
|
0
|
|
|
|
|
0
|
return; |
|
825
|
|
|
|
|
|
|
} |
|
826
|
|
|
|
|
|
|
|
|
827
|
108
|
|
|
|
|
2561
|
require POSIX; |
|
828
|
|
|
|
|
|
|
|
|
829
|
108
|
|
|
|
|
1200
|
my ($cmd, $opts) = @_; |
|
830
|
108
|
100
|
|
|
|
1669
|
if (ref($cmd) eq 'ARRAY') { |
|
831
|
1
|
|
|
|
|
8
|
$cmd = join(" ", @{$cmd}); |
|
|
1
|
|
|
|
|
8
|
|
|
832
|
|
|
|
|
|
|
} |
|
833
|
|
|
|
|
|
|
|
|
834
|
108
|
50
|
|
|
|
1604
|
if (!$cmd) { |
|
835
|
0
|
|
|
|
|
0
|
Carp::carp("run_forked expects command to run"); |
|
836
|
0
|
|
|
|
|
0
|
return; |
|
837
|
|
|
|
|
|
|
} |
|
838
|
|
|
|
|
|
|
|
|
839
|
108
|
100
|
|
|
|
1900
|
$opts = {} unless $opts; |
|
840
|
108
|
100
|
|
|
|
1900
|
$opts->{'timeout'} = 0 unless $opts->{'timeout'}; |
|
841
|
108
|
50
|
|
|
|
1072
|
$opts->{'terminate_wait_time'} = 2 unless defined($opts->{'terminate_wait_time'}); |
|
842
|
|
|
|
|
|
|
|
|
843
|
|
|
|
|
|
|
# turned on by default |
|
844
|
108
|
50
|
|
|
|
602
|
$opts->{'clean_up_children'} = 1 unless defined($opts->{'clean_up_children'}); |
|
845
|
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
# sockets to pass child stdout to parent |
|
847
|
108
|
|
|
|
|
1551
|
my $child_stdout_socket; |
|
848
|
|
|
|
|
|
|
my $parent_stdout_socket; |
|
849
|
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
# sockets to pass child stderr to parent |
|
851
|
108
|
|
|
|
|
0
|
my $child_stderr_socket; |
|
852
|
108
|
|
|
|
|
0
|
my $parent_stderr_socket; |
|
853
|
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
# sockets for child -> parent internal communication |
|
855
|
108
|
|
|
|
|
0
|
my $child_info_socket; |
|
856
|
108
|
|
|
|
|
0
|
my $parent_info_socket; |
|
857
|
|
|
|
|
|
|
|
|
858
|
108
|
50
|
|
|
|
11171
|
socketpair($child_stdout_socket, $parent_stdout_socket, &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC) || |
|
859
|
|
|
|
|
|
|
Carp::confess ("socketpair: $!"); |
|
860
|
108
|
50
|
|
|
|
7236
|
socketpair($child_stderr_socket, $parent_stderr_socket, &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC) || |
|
861
|
|
|
|
|
|
|
Carp::confess ("socketpair: $!"); |
|
862
|
108
|
50
|
|
|
|
7176
|
socketpair($child_info_socket, $parent_info_socket, &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC) || |
|
863
|
|
|
|
|
|
|
Carp::confess ("socketpair: $!"); |
|
864
|
|
|
|
|
|
|
|
|
865
|
108
|
|
|
|
|
1701
|
$child_stdout_socket->autoflush(1); |
|
866
|
108
|
|
|
|
|
12744
|
$parent_stdout_socket->autoflush(1); |
|
867
|
108
|
|
|
|
|
3814
|
$child_stderr_socket->autoflush(1); |
|
868
|
108
|
|
|
|
|
3503
|
$parent_stderr_socket->autoflush(1); |
|
869
|
108
|
|
|
|
|
3481
|
$child_info_socket->autoflush(1); |
|
870
|
108
|
|
|
|
|
3317
|
$parent_info_socket->autoflush(1); |
|
871
|
|
|
|
|
|
|
|
|
872
|
108
|
|
|
|
|
3417
|
my $start_time = get_monotonic_time(); |
|
873
|
|
|
|
|
|
|
|
|
874
|
108
|
|
|
|
|
827
|
my $pid; |
|
875
|
108
|
50
|
|
|
|
108226
|
if ($pid = fork) { |
|
876
|
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
# we are a parent |
|
878
|
108
|
|
|
|
|
5012
|
close($parent_stdout_socket); |
|
879
|
108
|
|
|
|
|
2446
|
close($parent_stderr_socket); |
|
880
|
108
|
|
|
|
|
1233
|
close($parent_info_socket); |
|
881
|
|
|
|
|
|
|
|
|
882
|
108
|
|
|
|
|
1023
|
my $flags; |
|
883
|
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
# prepare sockets to read from child |
|
885
|
|
|
|
|
|
|
|
|
886
|
108
|
|
33
|
|
|
1173
|
$flags = fcntl($child_stdout_socket, POSIX::F_GETFL, 0) || Carp::confess "can't fnctl F_GETFL: $!"; |
|
887
|
108
|
|
|
|
|
363
|
$flags |= POSIX::O_NONBLOCK; |
|
888
|
108
|
50
|
|
|
|
2000
|
fcntl($child_stdout_socket, POSIX::F_SETFL, $flags) || Carp::confess "can't fnctl F_SETFL: $!"; |
|
889
|
|
|
|
|
|
|
|
|
890
|
108
|
|
33
|
|
|
980
|
$flags = fcntl($child_stderr_socket, POSIX::F_GETFL, 0) || Carp::confess "can't fnctl F_GETFL: $!"; |
|
891
|
108
|
|
|
|
|
268
|
$flags |= POSIX::O_NONBLOCK; |
|
892
|
108
|
50
|
|
|
|
826
|
fcntl($child_stderr_socket, POSIX::F_SETFL, $flags) || Carp::confess "can't fnctl F_SETFL: $!"; |
|
893
|
|
|
|
|
|
|
|
|
894
|
108
|
|
33
|
|
|
959
|
$flags = fcntl($child_info_socket, POSIX::F_GETFL, 0) || Carp::confess "can't fnctl F_GETFL: $!"; |
|
895
|
108
|
|
|
|
|
309
|
$flags |= POSIX::O_NONBLOCK; |
|
896
|
108
|
50
|
|
|
|
879
|
fcntl($child_info_socket, POSIX::F_SETFL, $flags) || Carp::confess "can't fnctl F_SETFL: $!"; |
|
897
|
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
# print "child $pid started\n"; |
|
899
|
|
|
|
|
|
|
|
|
900
|
108
|
|
50
|
|
|
5149
|
my $child_output = { |
|
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
901
|
|
|
|
|
|
|
$child_stdout_socket->fileno => { |
|
902
|
|
|
|
|
|
|
'scalar_buffer' => "", |
|
903
|
|
|
|
|
|
|
'child_handle' => $child_stdout_socket, |
|
904
|
|
|
|
|
|
|
'block_size' => ($child_stdout_socket->stat)[11] || 1024, |
|
905
|
|
|
|
|
|
|
'protocol' => 'stdout', |
|
906
|
|
|
|
|
|
|
}, |
|
907
|
|
|
|
|
|
|
$child_stderr_socket->fileno => { |
|
908
|
|
|
|
|
|
|
'scalar_buffer' => "", |
|
909
|
|
|
|
|
|
|
'child_handle' => $child_stderr_socket, |
|
910
|
|
|
|
|
|
|
'block_size' => ($child_stderr_socket->stat)[11] || 1024, |
|
911
|
|
|
|
|
|
|
'protocol' => 'stderr', |
|
912
|
|
|
|
|
|
|
}, |
|
913
|
|
|
|
|
|
|
$child_info_socket->fileno => { |
|
914
|
|
|
|
|
|
|
'scalar_buffer' => "", |
|
915
|
|
|
|
|
|
|
'child_handle' => $child_info_socket, |
|
916
|
|
|
|
|
|
|
'block_size' => ($child_info_socket->stat)[11] || 1024, |
|
917
|
|
|
|
|
|
|
'protocol' => 'info', |
|
918
|
|
|
|
|
|
|
}, |
|
919
|
|
|
|
|
|
|
}; |
|
920
|
|
|
|
|
|
|
|
|
921
|
108
|
|
|
|
|
29150
|
my $select = IO::Select->new(); |
|
922
|
108
|
|
|
|
|
7850
|
$select->add($child_stdout_socket, $child_stderr_socket, $child_info_socket); |
|
923
|
|
|
|
|
|
|
|
|
924
|
108
|
|
|
|
|
17732
|
my $child_timedout = 0; |
|
925
|
108
|
|
|
|
|
196
|
my $child_finished = 0; |
|
926
|
108
|
|
|
|
|
1020
|
my $child_stdout = ''; |
|
927
|
108
|
|
|
|
|
1044
|
my $child_stderr = ''; |
|
928
|
108
|
|
|
|
|
1028
|
my $child_merged = ''; |
|
929
|
108
|
|
|
|
|
350
|
my $child_exit_code = 0; |
|
930
|
108
|
|
|
|
|
849
|
my $child_killed_by_signal = 0; |
|
931
|
108
|
|
|
|
|
359
|
my $parent_died = 0; |
|
932
|
|
|
|
|
|
|
|
|
933
|
108
|
|
|
|
|
152
|
my $last_parent_check = 0; |
|
934
|
108
|
|
|
|
|
910
|
my $got_sig_child = 0; |
|
935
|
108
|
|
|
|
|
247
|
my $got_sig_quit = 0; |
|
936
|
108
|
|
|
|
|
378
|
my $orig_sig_child = $SIG{'CHLD'}; |
|
937
|
|
|
|
|
|
|
|
|
938
|
108
|
|
|
108
|
|
8910
|
$SIG{'CHLD'} = sub { $got_sig_child = get_monotonic_time(); }; |
|
|
108
|
|
|
|
|
16218
|
|
|
939
|
|
|
|
|
|
|
|
|
940
|
108
|
50
|
|
|
|
571
|
if ($opts->{'terminate_on_signal'}) { |
|
941
|
0
|
|
|
0
|
|
0
|
install_layered_signal($opts->{'terminate_on_signal'}, sub { $got_sig_quit = time(); }); |
|
|
0
|
|
|
|
|
0
|
|
|
942
|
|
|
|
|
|
|
} |
|
943
|
|
|
|
|
|
|
|
|
944
|
108
|
|
|
|
|
353
|
my $child_child_pid; |
|
945
|
|
|
|
|
|
|
my $now; |
|
946
|
108
|
|
|
|
|
0
|
my $previous_monotonic_value; |
|
947
|
|
|
|
|
|
|
|
|
948
|
108
|
|
|
|
|
246
|
while (!$child_finished) { |
|
949
|
6713
|
|
|
|
|
18940
|
$previous_monotonic_value = $now; |
|
950
|
6713
|
|
|
|
|
14730
|
$now = get_monotonic_time(); |
|
951
|
|
|
|
|
|
|
|
|
952
|
6713
|
|
|
|
|
52230
|
adjust_monotonic_start_time([\$start_time, \$last_parent_check, \$got_sig_child], $now, $previous_monotonic_value); |
|
953
|
|
|
|
|
|
|
|
|
954
|
6713
|
50
|
|
|
|
18131
|
if ($opts->{'terminate_on_parent_sudden_death'}) { |
|
955
|
|
|
|
|
|
|
# check for parent once each five seconds |
|
956
|
0
|
0
|
|
|
|
0
|
if ($now > $last_parent_check + 5) { |
|
957
|
0
|
0
|
|
|
|
0
|
if (getppid() eq "1") { |
|
958
|
|
|
|
|
|
|
kill_gently ($pid, { |
|
959
|
|
|
|
|
|
|
'first_kill_type' => 'process_group', |
|
960
|
|
|
|
|
|
|
'final_kill_type' => 'process_group', |
|
961
|
0
|
|
|
|
|
0
|
'wait_time' => $opts->{'terminate_wait_time'} |
|
962
|
|
|
|
|
|
|
}); |
|
963
|
0
|
|
|
|
|
0
|
$parent_died = 1; |
|
964
|
|
|
|
|
|
|
} |
|
965
|
|
|
|
|
|
|
|
|
966
|
0
|
|
|
|
|
0
|
$last_parent_check = $now; |
|
967
|
|
|
|
|
|
|
} |
|
968
|
|
|
|
|
|
|
} |
|
969
|
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
# user specified timeout |
|
971
|
6713
|
100
|
|
|
|
14439
|
if ($opts->{'timeout'}) { |
|
972
|
296
|
100
|
|
|
|
1191
|
if ($now > $start_time + $opts->{'timeout'}) { |
|
973
|
|
|
|
|
|
|
kill_gently ($pid, { |
|
974
|
|
|
|
|
|
|
'first_kill_type' => 'process_group', |
|
975
|
|
|
|
|
|
|
'final_kill_type' => 'process_group', |
|
976
|
2
|
|
|
|
|
98
|
'wait_time' => $opts->{'terminate_wait_time'} |
|
977
|
|
|
|
|
|
|
}); |
|
978
|
2
|
|
|
|
|
12
|
$child_timedout = 1; |
|
979
|
|
|
|
|
|
|
} |
|
980
|
|
|
|
|
|
|
} |
|
981
|
|
|
|
|
|
|
|
|
982
|
|
|
|
|
|
|
# give OS 10 seconds for correct return of waitpid, |
|
983
|
|
|
|
|
|
|
# kill process after that and finish wait loop; |
|
984
|
|
|
|
|
|
|
# shouldn't ever happen -- remove this code? |
|
985
|
6713
|
100
|
|
|
|
11595
|
if ($got_sig_child) { |
|
986
|
205
|
50
|
|
|
|
1645
|
if ($now > $got_sig_child + 10) { |
|
987
|
0
|
|
|
|
|
0
|
print STDERR "waitpid did not return -1 for 10 seconds after SIG_CHLD, killing [$pid]\n"; |
|
988
|
0
|
|
|
|
|
0
|
kill (-9, $pid); |
|
989
|
0
|
|
|
|
|
0
|
$child_finished = 1; |
|
990
|
|
|
|
|
|
|
} |
|
991
|
|
|
|
|
|
|
} |
|
992
|
|
|
|
|
|
|
|
|
993
|
6713
|
50
|
|
|
|
9590
|
if ($got_sig_quit) { |
|
994
|
|
|
|
|
|
|
kill_gently ($pid, { |
|
995
|
|
|
|
|
|
|
'first_kill_type' => 'process_group', |
|
996
|
|
|
|
|
|
|
'final_kill_type' => 'process_group', |
|
997
|
0
|
|
|
|
|
0
|
'wait_time' => $opts->{'terminate_wait_time'} |
|
998
|
|
|
|
|
|
|
}); |
|
999
|
0
|
|
|
|
|
0
|
$child_finished = 1; |
|
1000
|
|
|
|
|
|
|
} |
|
1001
|
|
|
|
|
|
|
|
|
1002
|
6713
|
|
|
|
|
54737
|
my $waitpid = waitpid($pid, POSIX::WNOHANG); |
|
1003
|
|
|
|
|
|
|
|
|
1004
|
|
|
|
|
|
|
# child finished, catch it's exit status |
|
1005
|
6713
|
100
|
100
|
|
|
24696
|
if ($waitpid ne 0 && $waitpid ne -1) { |
|
1006
|
106
|
|
|
|
|
1310
|
$child_exit_code = $? >> 8; |
|
1007
|
|
|
|
|
|
|
} |
|
1008
|
|
|
|
|
|
|
|
|
1009
|
6713
|
100
|
|
|
|
11489
|
if ($waitpid eq -1) { |
|
1010
|
108
|
|
|
|
|
144
|
$child_finished = 1; |
|
1011
|
|
|
|
|
|
|
} |
|
1012
|
|
|
|
|
|
|
|
|
1013
|
6713
|
|
|
|
|
10450
|
my $ready_fds = []; |
|
1014
|
6713
|
|
|
|
|
8896
|
push @{$ready_fds}, $select->can_read(1/100); |
|
|
6713
|
|
|
|
|
25546
|
|
|
1015
|
|
|
|
|
|
|
|
|
1016
|
6713
|
|
|
|
|
4242713
|
READY_FDS: while (scalar(@{$ready_fds})) { |
|
|
14259
|
|
|
|
|
83200
|
|
|
1017
|
7546
|
|
|
|
|
8818
|
my $fd = shift @{$ready_fds}; |
|
|
7546
|
|
|
|
|
11048
|
|
|
1018
|
7546
|
|
|
|
|
9543
|
$ready_fds = [grep {$_ ne $fd} @{$ready_fds}]; |
|
|
437
|
|
|
|
|
2323
|
|
|
|
7546
|
|
|
|
|
14001
|
|
|
1019
|
|
|
|
|
|
|
|
|
1020
|
7546
|
|
|
|
|
25726
|
my $str = $child_output->{$fd->fileno}; |
|
1021
|
7546
|
50
|
|
|
|
51096
|
Carp::confess("child stream not found: $fd") unless $str; |
|
1022
|
|
|
|
|
|
|
|
|
1023
|
7546
|
|
|
|
|
11272
|
my $data = ""; |
|
1024
|
7546
|
|
|
|
|
19406
|
my $count = $fd->sysread($data, $str->{'block_size'}); |
|
1025
|
|
|
|
|
|
|
|
|
1026
|
7546
|
100
|
|
|
|
136735
|
if ($count) { |
|
|
|
50
|
|
|
|
|
|
|
1027
|
|
|
|
|
|
|
# extract all the available lines and store the rest in temporary buffer |
|
1028
|
7222
|
50
|
|
|
|
44813
|
if ($data =~ /(.+\n)([^\n]*)/so) { |
|
1029
|
7222
|
|
|
|
|
43489
|
$data = $str->{'scalar_buffer'} . $1; |
|
1030
|
7222
|
|
100
|
|
|
31605
|
$str->{'scalar_buffer'} = $2 || ""; |
|
1031
|
|
|
|
|
|
|
} |
|
1032
|
|
|
|
|
|
|
else { |
|
1033
|
0
|
|
|
|
|
0
|
$str->{'scalar_buffer'} .= $data; |
|
1034
|
0
|
|
|
|
|
0
|
$data = ""; |
|
1035
|
|
|
|
|
|
|
} |
|
1036
|
|
|
|
|
|
|
} |
|
1037
|
|
|
|
|
|
|
elsif ($count eq 0) { |
|
1038
|
324
|
|
|
|
|
1801
|
$select->remove($fd); |
|
1039
|
324
|
|
|
|
|
16443
|
$fd->close(); |
|
1040
|
324
|
50
|
|
|
|
8460
|
if ($str->{'scalar_buffer'}) { |
|
1041
|
0
|
|
|
|
|
0
|
$data = $str->{'scalar_buffer'} . "\n"; |
|
1042
|
|
|
|
|
|
|
} |
|
1043
|
|
|
|
|
|
|
} |
|
1044
|
|
|
|
|
|
|
else { |
|
1045
|
0
|
|
|
|
|
0
|
Carp::confess("error during sysread on [$fd]: " . $!); |
|
1046
|
|
|
|
|
|
|
} |
|
1047
|
|
|
|
|
|
|
|
|
1048
|
|
|
|
|
|
|
# $data contains only full lines (or last line if it was unfinished read |
|
1049
|
|
|
|
|
|
|
# or now new-line in the output of the child); dat is processed |
|
1050
|
|
|
|
|
|
|
# according to the "protocol" of socket |
|
1051
|
7546
|
100
|
|
|
|
16949
|
if ($str->{'protocol'} eq 'info') { |
|
1052
|
322
|
100
|
|
|
|
2671
|
if ($data =~ /^spawned ([0-9]+?)\n(.*?)/so) { |
|
1053
|
107
|
|
|
|
|
383
|
$child_child_pid = $1; |
|
1054
|
107
|
|
|
|
|
256
|
$data = $2; |
|
1055
|
|
|
|
|
|
|
} |
|
1056
|
322
|
100
|
|
|
|
1282
|
if ($data =~ /^reaped ([0-9]+?)\n(.*?)/so) { |
|
1057
|
105
|
|
|
|
|
197
|
$child_child_pid = undef; |
|
1058
|
105
|
|
|
|
|
300
|
$data = $2; |
|
1059
|
|
|
|
|
|
|
} |
|
1060
|
322
|
100
|
|
|
|
710
|
if ($data =~ /^[\d]+ killed with ([0-9]+?)\n(.*?)/so) { |
|
1061
|
2
|
|
|
|
|
16
|
$child_killed_by_signal = $1; |
|
1062
|
2
|
|
|
|
|
13
|
$data = $2; |
|
1063
|
|
|
|
|
|
|
} |
|
1064
|
|
|
|
|
|
|
|
|
1065
|
|
|
|
|
|
|
# we don't expect any other data in info socket, so it's |
|
1066
|
|
|
|
|
|
|
# some strange violation of protocol, better know about this |
|
1067
|
322
|
50
|
|
|
|
640
|
if ($data) { |
|
1068
|
0
|
|
|
|
|
0
|
Carp::confess("info protocol violation: [$data]"); |
|
1069
|
|
|
|
|
|
|
} |
|
1070
|
|
|
|
|
|
|
} |
|
1071
|
7546
|
100
|
|
|
|
14883
|
if ($str->{'protocol'} eq 'stdout') { |
|
1072
|
7113
|
100
|
|
|
|
14549
|
if (!$opts->{'discard_output'}) { |
|
1073
|
7109
|
|
|
|
|
67980
|
$child_stdout .= $data; |
|
1074
|
7109
|
|
|
|
|
62864
|
$child_merged .= $data; |
|
1075
|
|
|
|
|
|
|
} |
|
1076
|
|
|
|
|
|
|
|
|
1077
|
7113
|
100
|
66
|
|
|
22653
|
if ($opts->{'stdout_handler'} && ref($opts->{'stdout_handler'}) eq 'CODE') { |
|
1078
|
2
|
|
|
|
|
177
|
$opts->{'stdout_handler'}->($data); |
|
1079
|
|
|
|
|
|
|
} |
|
1080
|
|
|
|
|
|
|
} |
|
1081
|
7546
|
100
|
|
|
|
15038
|
if ($str->{'protocol'} eq 'stderr') { |
|
1082
|
111
|
100
|
|
|
|
326
|
if (!$opts->{'discard_output'}) { |
|
1083
|
108
|
|
|
|
|
226
|
$child_stderr .= $data; |
|
1084
|
108
|
|
|
|
|
186
|
$child_merged .= $data; |
|
1085
|
|
|
|
|
|
|
} |
|
1086
|
|
|
|
|
|
|
|
|
1087
|
111
|
100
|
66
|
|
|
483
|
if ($opts->{'stderr_handler'} && ref($opts->{'stderr_handler'}) eq 'CODE') { |
|
1088
|
2
|
|
|
|
|
24
|
$opts->{'stderr_handler'}->($data); |
|
1089
|
|
|
|
|
|
|
} |
|
1090
|
|
|
|
|
|
|
} |
|
1091
|
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
# process may finish (waitpid returns -1) before |
|
1093
|
|
|
|
|
|
|
# we've read all of its output because of buffering; |
|
1094
|
|
|
|
|
|
|
# so try to read all the way it is possible to read |
|
1095
|
|
|
|
|
|
|
# in such case - this shouldn't be too much (unless |
|
1096
|
|
|
|
|
|
|
# the buffer size is HUGE -- should introduce |
|
1097
|
|
|
|
|
|
|
# another counter in such case, maybe later) |
|
1098
|
|
|
|
|
|
|
# |
|
1099
|
7546
|
100
|
|
|
|
16605
|
push @{$ready_fds}, $select->can_read(1/100) if $child_finished; |
|
|
1109
|
|
|
|
|
4915
|
|
|
1100
|
|
|
|
|
|
|
} |
|
1101
|
|
|
|
|
|
|
|
|
1102
|
6713
|
50
|
33
|
|
|
14640
|
if ($opts->{'wait_loop_callback'} && ref($opts->{'wait_loop_callback'}) eq 'CODE') { |
|
1103
|
0
|
|
|
|
|
0
|
$opts->{'wait_loop_callback'}->(); |
|
1104
|
|
|
|
|
|
|
} |
|
1105
|
|
|
|
|
|
|
|
|
1106
|
6713
|
|
|
|
|
644155
|
Time::HiRes::usleep(1); |
|
1107
|
|
|
|
|
|
|
} |
|
1108
|
|
|
|
|
|
|
|
|
1109
|
|
|
|
|
|
|
# $child_pid_pid is not defined in two cases: |
|
1110
|
|
|
|
|
|
|
# * when our child was killed before |
|
1111
|
|
|
|
|
|
|
# it had chance to tell us the pid |
|
1112
|
|
|
|
|
|
|
# of the child it spawned. we can do |
|
1113
|
|
|
|
|
|
|
# nothing in this case :( |
|
1114
|
|
|
|
|
|
|
# * our child successfully reaped its child, |
|
1115
|
|
|
|
|
|
|
# we have nothing left to do in this case |
|
1116
|
|
|
|
|
|
|
# |
|
1117
|
|
|
|
|
|
|
# defined $child_pid_pid means child's child |
|
1118
|
|
|
|
|
|
|
# has not died but nobody is waiting for it, |
|
1119
|
|
|
|
|
|
|
# killing it brutally. |
|
1120
|
|
|
|
|
|
|
# |
|
1121
|
108
|
100
|
|
|
|
510
|
if ($child_child_pid) { |
|
1122
|
2
|
|
|
|
|
16
|
kill_gently($child_child_pid); |
|
1123
|
|
|
|
|
|
|
} |
|
1124
|
|
|
|
|
|
|
|
|
1125
|
|
|
|
|
|
|
# in case there are forks in child which |
|
1126
|
|
|
|
|
|
|
# do not forward or process signals (TERM) correctly |
|
1127
|
|
|
|
|
|
|
# kill whole child process group, effectively trying |
|
1128
|
|
|
|
|
|
|
# not to return with some children or their parts still running |
|
1129
|
|
|
|
|
|
|
# |
|
1130
|
|
|
|
|
|
|
# to be more accurate -- we need to be sure |
|
1131
|
|
|
|
|
|
|
# that this is process group created by our child |
|
1132
|
|
|
|
|
|
|
# (and not some other process group with the same pgid, |
|
1133
|
|
|
|
|
|
|
# created just after death of our child) -- fortunately |
|
1134
|
|
|
|
|
|
|
# this might happen only when process group ids |
|
1135
|
|
|
|
|
|
|
# are reused quickly (there are lots of processes |
|
1136
|
|
|
|
|
|
|
# spawning new process groups for example) |
|
1137
|
|
|
|
|
|
|
# |
|
1138
|
108
|
50
|
|
|
|
401
|
if ($opts->{'clean_up_children'}) { |
|
1139
|
108
|
|
|
|
|
924
|
kill(-9, $pid); |
|
1140
|
|
|
|
|
|
|
} |
|
1141
|
|
|
|
|
|
|
|
|
1142
|
|
|
|
|
|
|
# print "child $pid finished\n"; |
|
1143
|
|
|
|
|
|
|
|
|
1144
|
108
|
|
|
|
|
471
|
close($child_stdout_socket); |
|
1145
|
108
|
|
|
|
|
205
|
close($child_stderr_socket); |
|
1146
|
108
|
|
|
|
|
190
|
close($child_info_socket); |
|
1147
|
|
|
|
|
|
|
|
|
1148
|
|
|
|
|
|
|
my $o = { |
|
1149
|
|
|
|
|
|
|
'stdout' => $child_stdout, |
|
1150
|
|
|
|
|
|
|
'stderr' => $child_stderr, |
|
1151
|
|
|
|
|
|
|
'merged' => $child_merged, |
|
1152
|
108
|
100
|
|
|
|
56172
|
'timeout' => $child_timedout ? $opts->{'timeout'} : 0, |
|
1153
|
|
|
|
|
|
|
'exit_code' => $child_exit_code, |
|
1154
|
|
|
|
|
|
|
'parent_died' => $parent_died, |
|
1155
|
|
|
|
|
|
|
'killed_by_signal' => $child_killed_by_signal, |
|
1156
|
|
|
|
|
|
|
'child_pgid' => $pid, |
|
1157
|
|
|
|
|
|
|
'cmd' => $cmd, |
|
1158
|
|
|
|
|
|
|
}; |
|
1159
|
|
|
|
|
|
|
|
|
1160
|
108
|
|
|
|
|
827
|
my $err_msg = ''; |
|
1161
|
108
|
100
|
|
|
|
507
|
if ($o->{'exit_code'}) { |
|
1162
|
2
|
|
|
|
|
29
|
$err_msg .= "exited with code [$o->{'exit_code'}]\n"; |
|
1163
|
|
|
|
|
|
|
} |
|
1164
|
108
|
100
|
|
|
|
341
|
if ($o->{'timeout'}) { |
|
1165
|
2
|
|
|
|
|
28
|
$err_msg .= "ran more than [$o->{'timeout'}] seconds\n"; |
|
1166
|
|
|
|
|
|
|
} |
|
1167
|
108
|
50
|
|
|
|
370
|
if ($o->{'parent_died'}) { |
|
1168
|
0
|
|
|
|
|
0
|
$err_msg .= "parent died\n"; |
|
1169
|
|
|
|
|
|
|
} |
|
1170
|
108
|
100
|
66
|
|
|
1707
|
if ($o->{'stdout'} && !$opts->{'non_empty_stdout_ok'}) { |
|
1171
|
103
|
|
|
|
|
48207
|
$err_msg .= "stdout:\n" . $o->{'stdout'} . "\n"; |
|
1172
|
|
|
|
|
|
|
} |
|
1173
|
108
|
100
|
|
|
|
554
|
if ($o->{'stderr'}) { |
|
1174
|
2
|
|
|
|
|
33
|
$err_msg .= "stderr:\n" . $o->{'stderr'} . "\n"; |
|
1175
|
|
|
|
|
|
|
} |
|
1176
|
108
|
100
|
|
|
|
321
|
if ($o->{'killed_by_signal'}) { |
|
1177
|
2
|
|
|
|
|
19
|
$err_msg .= "killed by signal [" . $o->{'killed_by_signal'} . "]\n"; |
|
1178
|
|
|
|
|
|
|
} |
|
1179
|
108
|
|
|
|
|
421
|
$o->{'err_msg'} = $err_msg; |
|
1180
|
|
|
|
|
|
|
|
|
1181
|
108
|
50
|
|
|
|
268
|
if ($orig_sig_child) { |
|
1182
|
0
|
|
|
|
|
0
|
$SIG{'CHLD'} = $orig_sig_child; |
|
1183
|
|
|
|
|
|
|
} |
|
1184
|
|
|
|
|
|
|
else { |
|
1185
|
108
|
|
|
|
|
2446
|
delete($SIG{'CHLD'}); |
|
1186
|
|
|
|
|
|
|
} |
|
1187
|
|
|
|
|
|
|
|
|
1188
|
108
|
|
|
|
|
1029
|
uninstall_signals(); |
|
1189
|
|
|
|
|
|
|
|
|
1190
|
108
|
|
|
|
|
7526
|
return $o; |
|
1191
|
|
|
|
|
|
|
} |
|
1192
|
|
|
|
|
|
|
else { |
|
1193
|
0
|
0
|
|
|
|
0
|
Carp::confess("cannot fork: $!") unless defined($pid); |
|
1194
|
|
|
|
|
|
|
|
|
1195
|
|
|
|
|
|
|
# create new process session for open3 call, |
|
1196
|
|
|
|
|
|
|
# so we hopefully can kill all the subprocesses |
|
1197
|
|
|
|
|
|
|
# which might be spawned in it (except for those |
|
1198
|
|
|
|
|
|
|
# which do setsid theirselves -- can't do anything |
|
1199
|
|
|
|
|
|
|
# with those) |
|
1200
|
|
|
|
|
|
|
|
|
1201
|
0
|
0
|
|
|
|
0
|
POSIX::setsid() || Carp::confess("Error running setsid: " . $!); |
|
1202
|
|
|
|
|
|
|
|
|
1203
|
0
|
0
|
0
|
|
|
0
|
if ($opts->{'child_BEGIN'} && ref($opts->{'child_BEGIN'}) eq 'CODE') { |
|
1204
|
0
|
|
|
|
|
0
|
$opts->{'child_BEGIN'}->(); |
|
1205
|
|
|
|
|
|
|
} |
|
1206
|
|
|
|
|
|
|
|
|
1207
|
0
|
|
|
|
|
0
|
close($child_stdout_socket); |
|
1208
|
0
|
|
|
|
|
0
|
close($child_stderr_socket); |
|
1209
|
0
|
|
|
|
|
0
|
close($child_info_socket); |
|
1210
|
|
|
|
|
|
|
|
|
1211
|
0
|
|
|
|
|
0
|
my $child_exit_code; |
|
1212
|
|
|
|
|
|
|
|
|
1213
|
|
|
|
|
|
|
# allow both external programs |
|
1214
|
|
|
|
|
|
|
# and internal perl calls |
|
1215
|
0
|
0
|
|
|
|
0
|
if (!ref($cmd)) { |
|
|
|
0
|
|
|
|
|
|
|
1216
|
|
|
|
|
|
|
$child_exit_code = open3_run($cmd, { |
|
1217
|
|
|
|
|
|
|
'parent_info' => $parent_info_socket, |
|
1218
|
|
|
|
|
|
|
'parent_stdout' => $parent_stdout_socket, |
|
1219
|
|
|
|
|
|
|
'parent_stderr' => $parent_stderr_socket, |
|
1220
|
0
|
|
|
|
|
0
|
'child_stdin' => $opts->{'child_stdin'}, |
|
1221
|
|
|
|
|
|
|
}); |
|
1222
|
|
|
|
|
|
|
} |
|
1223
|
|
|
|
|
|
|
elsif (ref($cmd) eq 'CODE') { |
|
1224
|
|
|
|
|
|
|
# reopen STDOUT and STDERR for child code: |
|
1225
|
|
|
|
|
|
|
# https://rt.cpan.org/Ticket/Display.html?id=85912 |
|
1226
|
0
|
|
0
|
|
|
0
|
open STDOUT, '>&', $parent_stdout_socket || Carp::confess("Unable to reopen STDOUT: $!\n"); |
|
1227
|
0
|
|
0
|
|
|
0
|
open STDERR, '>&', $parent_stderr_socket || Carp::confess("Unable to reopen STDERR: $!\n"); |
|
1228
|
|
|
|
|
|
|
|
|
1229
|
|
|
|
|
|
|
$child_exit_code = $cmd->({ |
|
1230
|
|
|
|
|
|
|
'opts' => $opts, |
|
1231
|
|
|
|
|
|
|
'parent_info' => $parent_info_socket, |
|
1232
|
|
|
|
|
|
|
'parent_stdout' => $parent_stdout_socket, |
|
1233
|
|
|
|
|
|
|
'parent_stderr' => $parent_stderr_socket, |
|
1234
|
0
|
|
|
|
|
0
|
'child_stdin' => $opts->{'child_stdin'}, |
|
1235
|
|
|
|
|
|
|
}); |
|
1236
|
|
|
|
|
|
|
} |
|
1237
|
|
|
|
|
|
|
else { |
|
1238
|
0
|
|
|
|
|
0
|
print $parent_stderr_socket "Invalid command reference: " . ref($cmd) . "\n"; |
|
1239
|
0
|
|
|
|
|
0
|
$child_exit_code = 1; |
|
1240
|
|
|
|
|
|
|
} |
|
1241
|
|
|
|
|
|
|
|
|
1242
|
0
|
|
|
|
|
0
|
close($parent_stdout_socket); |
|
1243
|
0
|
|
|
|
|
0
|
close($parent_stderr_socket); |
|
1244
|
0
|
|
|
|
|
0
|
close($parent_info_socket); |
|
1245
|
|
|
|
|
|
|
|
|
1246
|
0
|
0
|
0
|
|
|
0
|
if ($opts->{'child_END'} && ref($opts->{'child_END'}) eq 'CODE') { |
|
1247
|
0
|
|
|
|
|
0
|
$opts->{'child_END'}->(); |
|
1248
|
|
|
|
|
|
|
} |
|
1249
|
|
|
|
|
|
|
|
|
1250
|
0
|
|
|
|
|
0
|
$| = 1; |
|
1251
|
0
|
|
|
|
|
0
|
POSIX::_exit $child_exit_code; |
|
1252
|
|
|
|
|
|
|
} |
|
1253
|
|
|
|
|
|
|
} |
|
1254
|
|
|
|
|
|
|
|
|
1255
|
|
|
|
|
|
|
sub run { |
|
1256
|
|
|
|
|
|
|
### container to store things in |
|
1257
|
80
|
|
|
80
|
1
|
175918
|
my $self = bless {}, __PACKAGE__; |
|
1258
|
|
|
|
|
|
|
|
|
1259
|
80
|
|
|
|
|
775
|
my %hash = @_; |
|
1260
|
|
|
|
|
|
|
|
|
1261
|
|
|
|
|
|
|
### if the user didn't provide a buffer, we'll store it here. |
|
1262
|
80
|
|
|
|
|
689
|
my $def_buf = ''; |
|
1263
|
|
|
|
|
|
|
|
|
1264
|
80
|
|
|
|
|
462
|
my($verbose,$cmd,$buffer,$timeout); |
|
1265
|
|
|
|
|
|
|
my $tmpl = { |
|
1266
|
|
|
|
|
|
|
verbose => { default => $VERBOSE, store => \$verbose }, |
|
1267
|
|
|
|
|
|
|
buffer => { default => \$def_buf, store => \$buffer }, |
|
1268
|
|
|
|
|
|
|
command => { required => 1, store => \$cmd, |
|
1269
|
80
|
100
|
|
80
|
|
15604
|
allow => sub { !ref($_[0]) or ref($_[0]) eq 'ARRAY' }, |
|
1270
|
|
|
|
|
|
|
}, |
|
1271
|
80
|
|
|
|
|
1993
|
timeout => { default => 0, store => \$timeout }, |
|
1272
|
|
|
|
|
|
|
}; |
|
1273
|
|
|
|
|
|
|
|
|
1274
|
80
|
50
|
|
|
|
977
|
unless( check( $tmpl, \%hash, $VERBOSE ) ) { |
|
1275
|
0
|
|
|
|
|
0
|
Carp::carp( loc( "Could not validate input: %1", |
|
1276
|
|
|
|
|
|
|
Params::Check->last_error ) ); |
|
1277
|
0
|
|
|
|
|
0
|
return; |
|
1278
|
|
|
|
|
|
|
}; |
|
1279
|
|
|
|
|
|
|
|
|
1280
|
80
|
|
|
|
|
3631
|
$cmd = _quote_args_vms( $cmd ) if IS_VMS; |
|
1281
|
|
|
|
|
|
|
|
|
1282
|
|
|
|
|
|
|
### strip any empty elements from $cmd if present |
|
1283
|
80
|
50
|
|
|
|
594
|
if ( $ALLOW_NULL_ARGS ) { |
|
1284
|
0
|
0
|
|
|
|
0
|
$cmd = [ grep { defined } @$cmd ] if ref $cmd; |
|
|
0
|
|
|
|
|
0
|
|
|
1285
|
|
|
|
|
|
|
} |
|
1286
|
|
|
|
|
|
|
else { |
|
1287
|
80
|
50
|
|
|
|
690
|
$cmd = [ grep { defined && length } @$cmd ] if ref $cmd; |
|
|
144
|
100
|
|
|
|
1055
|
|
|
1288
|
|
|
|
|
|
|
} |
|
1289
|
|
|
|
|
|
|
|
|
1290
|
80
|
100
|
|
|
|
392
|
my $pp_cmd = (ref $cmd ? "@$cmd" : $cmd); |
|
1291
|
80
|
50
|
|
|
|
208
|
print loc("Running [%1]...\n", $pp_cmd ) if $verbose; |
|
1292
|
|
|
|
|
|
|
|
|
1293
|
|
|
|
|
|
|
### did the user pass us a buffer to fill or not? if so, set this |
|
1294
|
|
|
|
|
|
|
### flag so we know what is expected of us |
|
1295
|
|
|
|
|
|
|
### XXX this is now being ignored. in the future, we could add diagnostic |
|
1296
|
|
|
|
|
|
|
### messages based on this logic |
|
1297
|
|
|
|
|
|
|
#my $user_provided_buffer = $buffer == \$def_buf ? 0 : 1; |
|
1298
|
|
|
|
|
|
|
|
|
1299
|
|
|
|
|
|
|
### buffers that are to be captured |
|
1300
|
80
|
|
|
|
|
148
|
my( @buffer, @buff_err, @buff_out ); |
|
1301
|
|
|
|
|
|
|
|
|
1302
|
|
|
|
|
|
|
### capture STDOUT |
|
1303
|
|
|
|
|
|
|
my $_out_handler = sub { |
|
1304
|
32
|
|
|
32
|
|
106
|
my $buf = shift; |
|
1305
|
32
|
50
|
|
|
|
113
|
return unless defined $buf; |
|
1306
|
|
|
|
|
|
|
|
|
1307
|
32
|
50
|
|
|
|
96
|
print STDOUT $buf if $verbose; |
|
1308
|
32
|
|
|
|
|
220
|
push @buffer, $buf; |
|
1309
|
32
|
|
|
|
|
227
|
push @buff_out, $buf; |
|
1310
|
80
|
|
|
|
|
554
|
}; |
|
1311
|
|
|
|
|
|
|
|
|
1312
|
|
|
|
|
|
|
### capture STDERR |
|
1313
|
|
|
|
|
|
|
my $_err_handler = sub { |
|
1314
|
8
|
|
|
8
|
|
35
|
my $buf = shift; |
|
1315
|
8
|
50
|
|
|
|
72
|
return unless defined $buf; |
|
1316
|
|
|
|
|
|
|
|
|
1317
|
8
|
50
|
|
|
|
51
|
print STDERR $buf if $verbose; |
|
1318
|
8
|
|
|
|
|
72
|
push @buffer, $buf; |
|
1319
|
8
|
|
|
|
|
40
|
push @buff_err, $buf; |
|
1320
|
80
|
|
|
|
|
463
|
}; |
|
1321
|
|
|
|
|
|
|
|
|
1322
|
|
|
|
|
|
|
|
|
1323
|
|
|
|
|
|
|
### flag to indicate we have a buffer captured |
|
1324
|
80
|
100
|
|
|
|
332
|
my $have_buffer = $self->can_capture_buffer ? 1 : 0; |
|
1325
|
|
|
|
|
|
|
|
|
1326
|
|
|
|
|
|
|
### flag indicating if the subcall went ok |
|
1327
|
80
|
|
|
|
|
182
|
my $ok; |
|
1328
|
|
|
|
|
|
|
|
|
1329
|
|
|
|
|
|
|
### don't look at previous errors: |
|
1330
|
80
|
|
|
|
|
352
|
local $?; |
|
1331
|
80
|
|
|
|
|
148
|
local $@; |
|
1332
|
80
|
|
|
|
|
501
|
local $!; |
|
1333
|
|
|
|
|
|
|
|
|
1334
|
|
|
|
|
|
|
### we might be having a timeout set |
|
1335
|
80
|
|
|
|
|
190
|
eval { |
|
1336
|
|
|
|
|
|
|
local $SIG{ALRM} = sub { die bless sub { |
|
1337
|
0
|
|
|
|
|
0
|
ALARM_CLASS . |
|
1338
|
|
|
|
|
|
|
qq[: Command '$pp_cmd' aborted by alarm after $timeout seconds] |
|
1339
|
80
|
50
|
|
0
|
|
580
|
}, ALARM_CLASS } if $timeout; |
|
|
0
|
|
|
|
|
0
|
|
|
1340
|
80
|
50
|
|
|
|
718
|
alarm $timeout || 0; |
|
1341
|
|
|
|
|
|
|
|
|
1342
|
|
|
|
|
|
|
### IPC::Run is first choice if $USE_IPC_RUN is set. |
|
1343
|
80
|
50
|
33
|
|
|
693
|
if( !IS_WIN32 and $USE_IPC_RUN and $self->can_use_ipc_run( 1 ) ) { |
|
|
|
100
|
66
|
|
|
|
|
|
1344
|
|
|
|
|
|
|
### ipc::run handlers needs the command as a string or an array ref |
|
1345
|
|
|
|
|
|
|
|
|
1346
|
0
|
0
|
|
|
|
0
|
$self->_debug( "# Using IPC::Run. Have buffer: $have_buffer" ) |
|
1347
|
|
|
|
|
|
|
if $DEBUG; |
|
1348
|
|
|
|
|
|
|
|
|
1349
|
0
|
|
|
|
|
0
|
$ok = $self->_ipc_run( $cmd, $_out_handler, $_err_handler ); |
|
1350
|
|
|
|
|
|
|
|
|
1351
|
|
|
|
|
|
|
### since IPC::Open3 works on all platforms, and just fails on |
|
1352
|
|
|
|
|
|
|
### win32 for capturing buffers, do that ideally |
|
1353
|
|
|
|
|
|
|
} elsif ( $USE_IPC_OPEN3 and $self->can_use_ipc_open3( 1 ) ) { |
|
1354
|
|
|
|
|
|
|
|
|
1355
|
40
|
50
|
|
|
|
121
|
$self->_debug("# Using IPC::Open3. Have buffer: $have_buffer") |
|
1356
|
|
|
|
|
|
|
if $DEBUG; |
|
1357
|
|
|
|
|
|
|
|
|
1358
|
|
|
|
|
|
|
### in case there are pipes in there; |
|
1359
|
|
|
|
|
|
|
### IPC::Open3 will call exec and exec will do the right thing |
|
1360
|
|
|
|
|
|
|
|
|
1361
|
40
|
|
|
|
|
98
|
my $method = IS_WIN32 ? '_open3_run_win32' : '_open3_run'; |
|
1362
|
|
|
|
|
|
|
|
|
1363
|
40
|
|
|
|
|
433
|
$ok = $self->$method( |
|
1364
|
|
|
|
|
|
|
$cmd, $_out_handler, $_err_handler, $verbose |
|
1365
|
|
|
|
|
|
|
); |
|
1366
|
|
|
|
|
|
|
|
|
1367
|
|
|
|
|
|
|
### if we are allowed to run verbose, just dispatch the system command |
|
1368
|
|
|
|
|
|
|
} else { |
|
1369
|
40
|
50
|
|
|
|
124
|
$self->_debug( "# Using system(). Have buffer: $have_buffer" ) |
|
1370
|
|
|
|
|
|
|
if $DEBUG; |
|
1371
|
40
|
|
|
|
|
178
|
$ok = $self->_system_run( $cmd, $verbose ); |
|
1372
|
|
|
|
|
|
|
} |
|
1373
|
|
|
|
|
|
|
|
|
1374
|
80
|
|
|
|
|
1213
|
alarm 0; |
|
1375
|
|
|
|
|
|
|
}; |
|
1376
|
|
|
|
|
|
|
|
|
1377
|
|
|
|
|
|
|
### restore STDIN after duping, or STDIN will be closed for |
|
1378
|
|
|
|
|
|
|
### this current perl process! |
|
1379
|
80
|
50
|
|
|
|
1688
|
$self->__reopen_fds( @{ $self->_fds} ) if $self->_fds; |
|
|
80
|
|
|
|
|
202
|
|
|
1380
|
|
|
|
|
|
|
|
|
1381
|
80
|
|
|
|
|
170
|
my $err; |
|
1382
|
80
|
50
|
|
|
|
226
|
unless( $ok ) { |
|
1383
|
|
|
|
|
|
|
### alarm happened |
|
1384
|
0
|
0
|
0
|
|
|
0
|
if ( $@ and ref $@ and $@->isa( ALARM_CLASS ) ) { |
|
|
|
|
0
|
|
|
|
|
|
1385
|
0
|
|
|
|
|
0
|
$err = $@->(); # the error code is an expired alarm |
|
1386
|
|
|
|
|
|
|
|
|
1387
|
|
|
|
|
|
|
### another error happened, set by the dispatchub |
|
1388
|
|
|
|
|
|
|
} else { |
|
1389
|
0
|
|
|
|
|
0
|
$err = $self->error; |
|
1390
|
|
|
|
|
|
|
} |
|
1391
|
|
|
|
|
|
|
} |
|
1392
|
|
|
|
|
|
|
|
|
1393
|
|
|
|
|
|
|
### fill the buffer; |
|
1394
|
80
|
100
|
|
|
|
984
|
$$buffer = join '', @buffer if @buffer; |
|
1395
|
|
|
|
|
|
|
|
|
1396
|
|
|
|
|
|
|
### return a list of flags and buffers (if available) in list |
|
1397
|
|
|
|
|
|
|
### context, or just a simple 'ok' in scalar |
|
1398
|
|
|
|
|
|
|
return wantarray |
|
1399
|
80
|
100
|
|
|
|
11788
|
? $have_buffer |
|
|
|
100
|
|
|
|
|
|
|
1400
|
|
|
|
|
|
|
? ($ok, $err, \@buffer, \@buff_out, \@buff_err) |
|
1401
|
|
|
|
|
|
|
: ($ok, $err ) |
|
1402
|
|
|
|
|
|
|
: $ok |
|
1403
|
|
|
|
|
|
|
|
|
1404
|
|
|
|
|
|
|
|
|
1405
|
|
|
|
|
|
|
} |
|
1406
|
|
|
|
|
|
|
|
|
1407
|
|
|
|
|
|
|
sub _open3_run_win32 { |
|
1408
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
1409
|
0
|
|
|
|
|
0
|
my $cmd = shift; |
|
1410
|
0
|
|
|
|
|
0
|
my $outhand = shift; |
|
1411
|
0
|
|
|
|
|
0
|
my $errhand = shift; |
|
1412
|
|
|
|
|
|
|
|
|
1413
|
0
|
|
|
|
|
0
|
require Socket; |
|
1414
|
|
|
|
|
|
|
|
|
1415
|
|
|
|
|
|
|
my $pipe = sub { |
|
1416
|
0
|
0
|
|
0
|
|
0
|
socketpair($_[0], $_[1], &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC) |
|
1417
|
|
|
|
|
|
|
or return undef; |
|
1418
|
0
|
|
|
|
|
0
|
shutdown($_[0], 1); # No more writing for reader |
|
1419
|
0
|
|
|
|
|
0
|
shutdown($_[1], 0); # No more reading for writer |
|
1420
|
0
|
|
|
|
|
0
|
return 1; |
|
1421
|
0
|
|
|
|
|
0
|
}; |
|
1422
|
|
|
|
|
|
|
|
|
1423
|
|
|
|
|
|
|
my $open3 = sub { |
|
1424
|
0
|
|
|
0
|
|
0
|
local (*TO_CHLD_R, *TO_CHLD_W); |
|
1425
|
0
|
|
|
|
|
0
|
local (*FR_CHLD_R, *FR_CHLD_W); |
|
1426
|
0
|
|
|
|
|
0
|
local (*FR_CHLD_ERR_R, *FR_CHLD_ERR_W); |
|
1427
|
|
|
|
|
|
|
|
|
1428
|
0
|
0
|
|
|
|
0
|
$pipe->(*TO_CHLD_R, *TO_CHLD_W ) or die $^E; |
|
1429
|
0
|
0
|
|
|
|
0
|
$pipe->(*FR_CHLD_R, *FR_CHLD_W ) or die $^E; |
|
1430
|
0
|
0
|
|
|
|
0
|
$pipe->(*FR_CHLD_ERR_R, *FR_CHLD_ERR_W) or die $^E; |
|
1431
|
|
|
|
|
|
|
|
|
1432
|
0
|
|
|
|
|
0
|
my $pid = IPC::Open3::open3('>&TO_CHLD_R', '<&FR_CHLD_W', '<&FR_CHLD_ERR_W', @_); |
|
1433
|
|
|
|
|
|
|
|
|
1434
|
0
|
|
|
|
|
0
|
return ( $pid, *TO_CHLD_W, *FR_CHLD_R, *FR_CHLD_ERR_R ); |
|
1435
|
0
|
|
|
|
|
0
|
}; |
|
1436
|
|
|
|
|
|
|
|
|
1437
|
0
|
0
|
|
|
|
0
|
$cmd = [ grep { defined && length } @$cmd ] if ref $cmd; |
|
|
0
|
0
|
|
|
|
0
|
|
|
1438
|
0
|
|
|
|
|
0
|
$cmd = $self->__fix_cmd_whitespace_and_special_chars( $cmd ); |
|
1439
|
|
|
|
|
|
|
|
|
1440
|
0
|
0
|
|
|
|
0
|
my ($pid, $to_chld, $fr_chld, $fr_chld_err) = |
|
1441
|
|
|
|
|
|
|
$open3->( ( ref $cmd ? @$cmd : $cmd ) ); |
|
1442
|
|
|
|
|
|
|
|
|
1443
|
0
|
|
|
|
|
0
|
my $in_sel = IO::Select->new(); |
|
1444
|
0
|
|
|
|
|
0
|
my $out_sel = IO::Select->new(); |
|
1445
|
|
|
|
|
|
|
|
|
1446
|
0
|
|
|
|
|
0
|
my %objs; |
|
1447
|
|
|
|
|
|
|
|
|
1448
|
0
|
|
|
|
|
0
|
$objs{ fileno( $fr_chld ) } = $outhand; |
|
1449
|
0
|
|
|
|
|
0
|
$objs{ fileno( $fr_chld_err ) } = $errhand; |
|
1450
|
0
|
|
|
|
|
0
|
$in_sel->add( $fr_chld ); |
|
1451
|
0
|
|
|
|
|
0
|
$in_sel->add( $fr_chld_err ); |
|
1452
|
|
|
|
|
|
|
|
|
1453
|
0
|
|
|
|
|
0
|
close($to_chld); |
|
1454
|
|
|
|
|
|
|
|
|
1455
|
0
|
|
|
|
|
0
|
while ($in_sel->count() + $out_sel->count()) { |
|
1456
|
0
|
|
|
|
|
0
|
my ($ins, $outs) = IO::Select::select($in_sel, $out_sel, undef); |
|
1457
|
|
|
|
|
|
|
|
|
1458
|
0
|
|
|
|
|
0
|
for my $fh (@$ins) { |
|
1459
|
0
|
|
|
|
|
0
|
my $obj = $objs{ fileno($fh) }; |
|
1460
|
0
|
|
|
|
|
0
|
my $buf; |
|
1461
|
0
|
|
|
|
|
0
|
my $bytes_read = sysread($fh, $buf, 64*1024 ); #, length($buf)); |
|
1462
|
0
|
0
|
|
|
|
0
|
if (!$bytes_read) { |
|
1463
|
0
|
|
|
|
|
0
|
$in_sel->remove($fh); |
|
1464
|
|
|
|
|
|
|
} |
|
1465
|
|
|
|
|
|
|
else { |
|
1466
|
0
|
|
|
|
|
0
|
$obj->( "$buf" ); |
|
1467
|
|
|
|
|
|
|
} |
|
1468
|
|
|
|
|
|
|
} |
|
1469
|
|
|
|
|
|
|
|
|
1470
|
0
|
|
|
|
|
0
|
for my $fh (@$outs) { |
|
1471
|
|
|
|
|
|
|
} |
|
1472
|
|
|
|
|
|
|
} |
|
1473
|
|
|
|
|
|
|
|
|
1474
|
0
|
|
|
|
|
0
|
waitpid($pid, 0); |
|
1475
|
|
|
|
|
|
|
|
|
1476
|
|
|
|
|
|
|
### some error occurred |
|
1477
|
0
|
0
|
|
|
|
0
|
if( $? ) { |
|
1478
|
0
|
|
|
|
|
0
|
$self->error( $self->_pp_child_error( $cmd, $? ) ); |
|
1479
|
0
|
|
|
|
|
0
|
$self->ok( 0 ); |
|
1480
|
0
|
|
|
|
|
0
|
return; |
|
1481
|
|
|
|
|
|
|
} else { |
|
1482
|
0
|
|
|
|
|
0
|
return $self->ok( 1 ); |
|
1483
|
|
|
|
|
|
|
} |
|
1484
|
|
|
|
|
|
|
} |
|
1485
|
|
|
|
|
|
|
|
|
1486
|
|
|
|
|
|
|
sub _open3_run { |
|
1487
|
40
|
|
|
40
|
|
133
|
my $self = shift; |
|
1488
|
40
|
|
|
|
|
75
|
my $cmd = shift; |
|
1489
|
40
|
|
|
|
|
77
|
my $_out_handler = shift; |
|
1490
|
40
|
|
|
|
|
78
|
my $_err_handler = shift; |
|
1491
|
40
|
|
50
|
|
|
431
|
my $verbose = shift || 0; |
|
1492
|
|
|
|
|
|
|
|
|
1493
|
|
|
|
|
|
|
### Following code are adapted from Friar 'abstracts' in the |
|
1494
|
|
|
|
|
|
|
### Perl Monastery (http://www.perlmonks.org/index.pl?node_id=151886). |
|
1495
|
|
|
|
|
|
|
### XXX that code didn't work. |
|
1496
|
|
|
|
|
|
|
### we now use the following code, thanks to theorbtwo |
|
1497
|
|
|
|
|
|
|
|
|
1498
|
|
|
|
|
|
|
### define them beforehand, so we always have defined FH's |
|
1499
|
|
|
|
|
|
|
### to read from. |
|
1500
|
2
|
|
|
2
|
|
19
|
use Symbol; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
3825
|
|
|
1501
|
40
|
|
|
|
|
217
|
my $kidout = Symbol::gensym(); |
|
1502
|
40
|
|
|
|
|
1812
|
my $kiderror = Symbol::gensym(); |
|
1503
|
|
|
|
|
|
|
|
|
1504
|
|
|
|
|
|
|
### Dup the filehandle so we can pass 'our' STDIN to the |
|
1505
|
|
|
|
|
|
|
### child process. This stops us from having to pump input |
|
1506
|
|
|
|
|
|
|
### from ourselves to the childprocess. However, we will need |
|
1507
|
|
|
|
|
|
|
### to revive the FH afterwards, as IPC::Open3 closes it. |
|
1508
|
|
|
|
|
|
|
### We'll do the same for STDOUT and STDERR. It works without |
|
1509
|
|
|
|
|
|
|
### duping them on non-unix derivatives, but not on win32. |
|
1510
|
40
|
|
|
|
|
813
|
my @fds_to_dup = ( IS_WIN32 && !$verbose |
|
1511
|
|
|
|
|
|
|
? qw[STDIN STDOUT STDERR] |
|
1512
|
|
|
|
|
|
|
: qw[STDIN] |
|
1513
|
|
|
|
|
|
|
); |
|
1514
|
40
|
|
|
|
|
172
|
$self->_fds( \@fds_to_dup ); |
|
1515
|
40
|
|
|
|
|
199
|
$self->__dup_fds( @fds_to_dup ); |
|
1516
|
|
|
|
|
|
|
|
|
1517
|
|
|
|
|
|
|
### pipes have to come in a quoted string, and that clashes with |
|
1518
|
|
|
|
|
|
|
### whitespace. This sub fixes up such commands so they run properly |
|
1519
|
40
|
|
|
|
|
371
|
$cmd = $self->__fix_cmd_whitespace_and_special_chars( $cmd ); |
|
1520
|
|
|
|
|
|
|
|
|
1521
|
|
|
|
|
|
|
### don't stringify @$cmd, so spaces in filenames/paths are |
|
1522
|
|
|
|
|
|
|
### treated properly |
|
1523
|
40
|
|
|
|
|
87
|
my $pid = eval { |
|
1524
|
40
|
100
|
|
|
|
268
|
IPC::Open3::open3( |
|
1525
|
|
|
|
|
|
|
'<&STDIN', |
|
1526
|
|
|
|
|
|
|
(IS_WIN32 ? '>&STDOUT' : $kidout), |
|
1527
|
|
|
|
|
|
|
(IS_WIN32 ? '>&STDERR' : $kiderror), |
|
1528
|
|
|
|
|
|
|
( ref $cmd ? @$cmd : $cmd ), |
|
1529
|
|
|
|
|
|
|
); |
|
1530
|
|
|
|
|
|
|
}; |
|
1531
|
|
|
|
|
|
|
|
|
1532
|
|
|
|
|
|
|
### open3 error occurred |
|
1533
|
40
|
50
|
33
|
|
|
197338
|
if( $@ and $@ =~ /^open3:/ ) { |
|
1534
|
0
|
|
|
|
|
0
|
$self->ok( 0 ); |
|
1535
|
0
|
|
|
|
|
0
|
$self->error( $@ ); |
|
1536
|
0
|
|
|
|
|
0
|
return; |
|
1537
|
|
|
|
|
|
|
}; |
|
1538
|
|
|
|
|
|
|
|
|
1539
|
|
|
|
|
|
|
### use OUR stdin, not $kidin. Somehow, |
|
1540
|
|
|
|
|
|
|
### we never get the input.. so jump through |
|
1541
|
|
|
|
|
|
|
### some hoops to do it :( |
|
1542
|
40
|
|
|
|
|
1563
|
my $selector = IO::Select->new( |
|
1543
|
|
|
|
|
|
|
(IS_WIN32 ? \*STDERR : $kiderror), |
|
1544
|
|
|
|
|
|
|
\*STDIN, |
|
1545
|
|
|
|
|
|
|
(IS_WIN32 ? \*STDOUT : $kidout) |
|
1546
|
|
|
|
|
|
|
); |
|
1547
|
|
|
|
|
|
|
|
|
1548
|
40
|
|
|
|
|
7689
|
STDOUT->autoflush(1); STDERR->autoflush(1); STDIN->autoflush(1); |
|
|
40
|
|
|
|
|
4541
|
|
|
|
40
|
|
|
|
|
1740
|
|
|
1549
|
40
|
50
|
|
|
|
1788
|
$kidout->autoflush(1) if UNIVERSAL::can($kidout, 'autoflush'); |
|
1550
|
40
|
50
|
|
|
|
1664
|
$kiderror->autoflush(1) if UNIVERSAL::can($kiderror, 'autoflush'); |
|
1551
|
|
|
|
|
|
|
|
|
1552
|
|
|
|
|
|
|
### add an explicit break statement |
|
1553
|
|
|
|
|
|
|
### code courtesy of theorbtwo from #london.pm |
|
1554
|
40
|
|
|
|
|
1562
|
my $stdout_done = 0; |
|
1555
|
40
|
|
|
|
|
81
|
my $stderr_done = 0; |
|
1556
|
40
|
|
|
|
|
422
|
OUTER: while ( my @ready = $selector->can_read ) { |
|
1557
|
|
|
|
|
|
|
|
|
1558
|
80
|
|
|
|
|
86545
|
for my $h ( @ready ) { |
|
1559
|
120
|
|
|
|
|
250
|
my $buf; |
|
1560
|
|
|
|
|
|
|
|
|
1561
|
|
|
|
|
|
|
### $len is the amount of bytes read |
|
1562
|
120
|
|
|
|
|
1202
|
my $len = sysread( $h, $buf, 4096 ); # try to read 4096 bytes |
|
1563
|
|
|
|
|
|
|
|
|
1564
|
|
|
|
|
|
|
### see perldoc -f sysread: it returns undef on error, |
|
1565
|
|
|
|
|
|
|
### so bail out. |
|
1566
|
120
|
50
|
|
|
|
475
|
if( not defined $len ) { |
|
1567
|
0
|
|
|
|
|
0
|
warn(loc("Error reading from process: %1", $!)); |
|
1568
|
0
|
|
|
|
|
0
|
last OUTER; |
|
1569
|
|
|
|
|
|
|
} |
|
1570
|
|
|
|
|
|
|
|
|
1571
|
|
|
|
|
|
|
### check for $len. it may be 0, at which point we're |
|
1572
|
|
|
|
|
|
|
### done reading, so don't try to process it. |
|
1573
|
|
|
|
|
|
|
### if we would print anyway, we'd provide bogus information |
|
1574
|
120
|
100
|
100
|
|
|
1125
|
$_out_handler->( "$buf" ) if $len && $h == $kidout; |
|
1575
|
120
|
100
|
100
|
|
|
950
|
$_err_handler->( "$buf" ) if $len && $h == $kiderror; |
|
1576
|
|
|
|
|
|
|
|
|
1577
|
|
|
|
|
|
|
### Wait till child process is done printing to both |
|
1578
|
|
|
|
|
|
|
### stdout and stderr. |
|
1579
|
120
|
100
|
100
|
|
|
558
|
$stdout_done = 1 if $h == $kidout and $len == 0; |
|
1580
|
120
|
100
|
100
|
|
|
490
|
$stderr_done = 1 if $h == $kiderror and $len == 0; |
|
1581
|
120
|
100
|
100
|
|
|
851
|
last OUTER if ($stdout_done && $stderr_done); |
|
1582
|
|
|
|
|
|
|
} |
|
1583
|
|
|
|
|
|
|
} |
|
1584
|
|
|
|
|
|
|
|
|
1585
|
40
|
|
|
|
|
922
|
waitpid $pid, 0; # wait for it to die |
|
1586
|
|
|
|
|
|
|
|
|
1587
|
|
|
|
|
|
|
### restore STDIN after duping, or STDIN will be closed for |
|
1588
|
|
|
|
|
|
|
### this current perl process! |
|
1589
|
|
|
|
|
|
|
### done in the parent call now |
|
1590
|
|
|
|
|
|
|
# $self->__reopen_fds( @fds_to_dup ); |
|
1591
|
|
|
|
|
|
|
|
|
1592
|
|
|
|
|
|
|
### some error occurred |
|
1593
|
40
|
50
|
|
|
|
465
|
if( $? ) { |
|
1594
|
0
|
|
|
|
|
0
|
$self->error( $self->_pp_child_error( $cmd, $? ) ); |
|
1595
|
0
|
|
|
|
|
0
|
$self->ok( 0 ); |
|
1596
|
0
|
|
|
|
|
0
|
return; |
|
1597
|
|
|
|
|
|
|
} else { |
|
1598
|
40
|
|
|
|
|
861
|
return $self->ok( 1 ); |
|
1599
|
|
|
|
|
|
|
} |
|
1600
|
|
|
|
|
|
|
} |
|
1601
|
|
|
|
|
|
|
|
|
1602
|
|
|
|
|
|
|
### Text::ParseWords::shellwords() uses unix semantics. that will break |
|
1603
|
|
|
|
|
|
|
### on win32 |
|
1604
|
|
|
|
|
|
|
{ my $parse_sub = IS_WIN32 |
|
1605
|
|
|
|
|
|
|
? __PACKAGE__->can('_split_like_shell_win32') |
|
1606
|
|
|
|
|
|
|
: Text::ParseWords->can('shellwords'); |
|
1607
|
|
|
|
|
|
|
|
|
1608
|
|
|
|
|
|
|
sub _ipc_run { |
|
1609
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
|
1610
|
0
|
|
|
|
|
0
|
my $cmd = shift; |
|
1611
|
0
|
|
|
|
|
0
|
my $_out_handler = shift; |
|
1612
|
0
|
|
|
|
|
0
|
my $_err_handler = shift; |
|
1613
|
|
|
|
|
|
|
|
|
1614
|
0
|
|
|
|
|
0
|
STDOUT->autoflush(1); STDERR->autoflush(1); |
|
|
0
|
|
|
|
|
0
|
|
|
1615
|
|
|
|
|
|
|
|
|
1616
|
|
|
|
|
|
|
### a command like: |
|
1617
|
|
|
|
|
|
|
# [ |
|
1618
|
|
|
|
|
|
|
# '/usr/bin/gzip', |
|
1619
|
|
|
|
|
|
|
# '-cdf', |
|
1620
|
|
|
|
|
|
|
# '/Users/kane/sources/p4/other/archive-extract/t/src/x.tgz', |
|
1621
|
|
|
|
|
|
|
# '|', |
|
1622
|
|
|
|
|
|
|
# '/usr/bin/tar', |
|
1623
|
|
|
|
|
|
|
# '-tf -' |
|
1624
|
|
|
|
|
|
|
# ] |
|
1625
|
|
|
|
|
|
|
### needs to become: |
|
1626
|
|
|
|
|
|
|
# [ |
|
1627
|
|
|
|
|
|
|
# ['/usr/bin/gzip', '-cdf', |
|
1628
|
|
|
|
|
|
|
# '/Users/kane/sources/p4/other/archive-extract/t/src/x.tgz'] |
|
1629
|
|
|
|
|
|
|
# '|', |
|
1630
|
|
|
|
|
|
|
# ['/usr/bin/tar', '-tf -'] |
|
1631
|
|
|
|
|
|
|
# ] |
|
1632
|
|
|
|
|
|
|
|
|
1633
|
|
|
|
|
|
|
|
|
1634
|
0
|
|
|
|
|
0
|
my @command; |
|
1635
|
|
|
|
|
|
|
my $special_chars; |
|
1636
|
|
|
|
|
|
|
|
|
1637
|
0
|
|
|
|
|
0
|
my $re = do { my $x = join '', SPECIAL_CHARS; qr/([$x])/ }; |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
1638
|
0
|
0
|
|
|
|
0
|
if( ref $cmd ) { |
|
1639
|
0
|
|
|
|
|
0
|
my $aref = []; |
|
1640
|
0
|
|
|
|
|
0
|
for my $item (@$cmd) { |
|
1641
|
0
|
0
|
|
|
|
0
|
if( $item =~ $re ) { |
|
1642
|
0
|
|
|
|
|
0
|
push @command, $aref, $item; |
|
1643
|
0
|
|
|
|
|
0
|
$aref = []; |
|
1644
|
0
|
|
|
|
|
0
|
$special_chars .= $1; |
|
1645
|
|
|
|
|
|
|
} else { |
|
1646
|
0
|
|
|
|
|
0
|
push @$aref, $item; |
|
1647
|
|
|
|
|
|
|
} |
|
1648
|
|
|
|
|
|
|
} |
|
1649
|
0
|
|
|
|
|
0
|
push @command, $aref; |
|
1650
|
|
|
|
|
|
|
} else { |
|
1651
|
0
|
0
|
|
|
|
0
|
@command = map { if( $_ =~ $re ) { |
|
|
0
|
|
|
|
|
0
|
|
|
1652
|
0
|
|
|
|
|
0
|
$special_chars .= $1; $_; |
|
|
0
|
|
|
|
|
0
|
|
|
1653
|
|
|
|
|
|
|
} else { |
|
1654
|
|
|
|
|
|
|
# [ split /\s+/ ] |
|
1655
|
0
|
0
|
|
|
|
0
|
[ map { m/[ ]/ ? qq{'$_'} : $_ } $parse_sub->($_) ] |
|
|
0
|
|
|
|
|
0
|
|
|
1656
|
|
|
|
|
|
|
} |
|
1657
|
|
|
|
|
|
|
} split( /\s*$re\s*/, $cmd ); |
|
1658
|
|
|
|
|
|
|
} |
|
1659
|
|
|
|
|
|
|
|
|
1660
|
|
|
|
|
|
|
### if there's a pipe in the command, *STDIN needs to |
|
1661
|
|
|
|
|
|
|
### be inserted *BEFORE* the pipe, to work on win32 |
|
1662
|
|
|
|
|
|
|
### this also works on *nix, so we should do it when possible |
|
1663
|
|
|
|
|
|
|
### this should *also* work on multiple pipes in the command |
|
1664
|
|
|
|
|
|
|
### if there's no pipe in the command, append STDIN to the back |
|
1665
|
|
|
|
|
|
|
### of the command instead. |
|
1666
|
|
|
|
|
|
|
### XXX seems IPC::Run works it out for itself if you just |
|
1667
|
|
|
|
|
|
|
### don't pass STDIN at all. |
|
1668
|
|
|
|
|
|
|
# if( $special_chars and $special_chars =~ /\|/ ) { |
|
1669
|
|
|
|
|
|
|
# ### only add STDIN the first time.. |
|
1670
|
|
|
|
|
|
|
# my $i; |
|
1671
|
|
|
|
|
|
|
# @command = map { ($_ eq '|' && not $i++) |
|
1672
|
|
|
|
|
|
|
# ? ( \*STDIN, $_ ) |
|
1673
|
|
|
|
|
|
|
# : $_ |
|
1674
|
|
|
|
|
|
|
# } @command; |
|
1675
|
|
|
|
|
|
|
# } else { |
|
1676
|
|
|
|
|
|
|
# push @command, \*STDIN; |
|
1677
|
|
|
|
|
|
|
# } |
|
1678
|
|
|
|
|
|
|
|
|
1679
|
|
|
|
|
|
|
# \*STDIN is already included in the @command, see a few lines up |
|
1680
|
0
|
|
|
|
|
0
|
my $ok = eval { IPC::Run::run( @command, |
|
|
0
|
|
|
|
|
0
|
|
|
1681
|
|
|
|
|
|
|
fileno(STDOUT).'>', |
|
1682
|
|
|
|
|
|
|
$_out_handler, |
|
1683
|
|
|
|
|
|
|
fileno(STDERR).'>', |
|
1684
|
|
|
|
|
|
|
$_err_handler |
|
1685
|
|
|
|
|
|
|
) |
|
1686
|
|
|
|
|
|
|
}; |
|
1687
|
|
|
|
|
|
|
|
|
1688
|
|
|
|
|
|
|
### all is well |
|
1689
|
0
|
0
|
|
|
|
0
|
if( $ok ) { |
|
1690
|
0
|
|
|
|
|
0
|
return $self->ok( $ok ); |
|
1691
|
|
|
|
|
|
|
|
|
1692
|
|
|
|
|
|
|
### some error occurred |
|
1693
|
|
|
|
|
|
|
} else { |
|
1694
|
0
|
|
|
|
|
0
|
$self->ok( 0 ); |
|
1695
|
|
|
|
|
|
|
|
|
1696
|
|
|
|
|
|
|
### if the eval fails due to an exception, deal with it |
|
1697
|
|
|
|
|
|
|
### unless it's an alarm |
|
1698
|
0
|
0
|
0
|
|
|
0
|
if( $@ and not UNIVERSAL::isa( $@, ALARM_CLASS ) ) { |
|
|
|
0
|
|
|
|
|
|
|
1699
|
0
|
|
|
|
|
0
|
$self->error( $@ ); |
|
1700
|
|
|
|
|
|
|
|
|
1701
|
|
|
|
|
|
|
### if it *is* an alarm, propagate |
|
1702
|
|
|
|
|
|
|
} elsif( $@ ) { |
|
1703
|
0
|
|
|
|
|
0
|
die $@; |
|
1704
|
|
|
|
|
|
|
|
|
1705
|
|
|
|
|
|
|
### some error in the sub command |
|
1706
|
|
|
|
|
|
|
} else { |
|
1707
|
0
|
|
|
|
|
0
|
$self->error( $self->_pp_child_error( $cmd, $? ) ); |
|
1708
|
|
|
|
|
|
|
} |
|
1709
|
|
|
|
|
|
|
|
|
1710
|
0
|
|
|
|
|
0
|
return; |
|
1711
|
|
|
|
|
|
|
} |
|
1712
|
|
|
|
|
|
|
} |
|
1713
|
|
|
|
|
|
|
} |
|
1714
|
|
|
|
|
|
|
|
|
1715
|
|
|
|
|
|
|
sub _system_run { |
|
1716
|
40
|
|
|
40
|
|
79
|
my $self = shift; |
|
1717
|
40
|
|
|
|
|
78
|
my $cmd = shift; |
|
1718
|
40
|
|
50
|
|
|
727
|
my $verbose = shift || 0; |
|
1719
|
|
|
|
|
|
|
|
|
1720
|
|
|
|
|
|
|
### pipes have to come in a quoted string, and that clashes with |
|
1721
|
|
|
|
|
|
|
### whitespace. This sub fixes up such commands so they run properly |
|
1722
|
40
|
|
|
|
|
183
|
$cmd = $self->__fix_cmd_whitespace_and_special_chars( $cmd ); |
|
1723
|
|
|
|
|
|
|
|
|
1724
|
40
|
50
|
|
|
|
222
|
my @fds_to_dup = $verbose ? () : qw[STDOUT STDERR]; |
|
1725
|
40
|
|
|
|
|
173
|
$self->_fds( \@fds_to_dup ); |
|
1726
|
40
|
|
|
|
|
250
|
$self->__dup_fds( @fds_to_dup ); |
|
1727
|
|
|
|
|
|
|
|
|
1728
|
|
|
|
|
|
|
### system returns 'true' on failure -- the exit code of the cmd |
|
1729
|
40
|
|
|
|
|
160
|
$self->ok( 1 ); |
|
1730
|
40
|
100
|
|
|
|
286922
|
system( ref $cmd ? @$cmd : $cmd ) == 0 or do { |
|
|
|
50
|
|
|
|
|
|
|
1731
|
0
|
|
|
|
|
0
|
$self->error( $self->_pp_child_error( $cmd, $? ) ); |
|
1732
|
0
|
|
|
|
|
0
|
$self->ok( 0 ); |
|
1733
|
|
|
|
|
|
|
}; |
|
1734
|
|
|
|
|
|
|
|
|
1735
|
|
|
|
|
|
|
### done in the parent call now |
|
1736
|
|
|
|
|
|
|
#$self->__reopen_fds( @fds_to_dup ); |
|
1737
|
|
|
|
|
|
|
|
|
1738
|
40
|
50
|
|
|
|
1599
|
return unless $self->ok; |
|
1739
|
40
|
|
|
|
|
191
|
return $self->ok; |
|
1740
|
|
|
|
|
|
|
} |
|
1741
|
|
|
|
|
|
|
|
|
1742
|
|
|
|
|
|
|
{ my %sc_lookup = map { $_ => $_ } SPECIAL_CHARS; |
|
1743
|
|
|
|
|
|
|
|
|
1744
|
|
|
|
|
|
|
|
|
1745
|
|
|
|
|
|
|
sub __fix_cmd_whitespace_and_special_chars { |
|
1746
|
80
|
|
|
80
|
|
529
|
my $self = shift; |
|
1747
|
80
|
|
|
|
|
267
|
my $cmd = shift; |
|
1748
|
|
|
|
|
|
|
|
|
1749
|
|
|
|
|
|
|
### command has a special char in it |
|
1750
|
80
|
100
|
100
|
|
|
468
|
if( ref $cmd and grep { $sc_lookup{$_} } @$cmd ) { |
|
|
144
|
|
|
|
|
659
|
|
|
1751
|
|
|
|
|
|
|
|
|
1752
|
|
|
|
|
|
|
### since we have special chars, we have to quote white space |
|
1753
|
|
|
|
|
|
|
### this *may* conflict with the parsing :( |
|
1754
|
16
|
|
|
|
|
57
|
my $fixed; |
|
1755
|
16
|
100
|
|
|
|
50
|
my @cmd = map { / / ? do { $fixed++; QUOTE.$_.QUOTE } : $_ } @$cmd; |
|
|
88
|
|
|
|
|
487
|
|
|
|
8
|
|
|
|
|
30
|
|
|
|
8
|
|
|
|
|
72
|
|
|
1756
|
|
|
|
|
|
|
|
|
1757
|
16
|
50
|
33
|
|
|
66
|
$self->_debug( "# Quoted $fixed arguments containing whitespace" ) |
|
1758
|
|
|
|
|
|
|
if $DEBUG && $fixed; |
|
1759
|
|
|
|
|
|
|
|
|
1760
|
|
|
|
|
|
|
### stringify it, so the special char isn't escaped as argument |
|
1761
|
|
|
|
|
|
|
### to the program |
|
1762
|
16
|
|
|
|
|
142
|
$cmd = join ' ', @cmd; |
|
1763
|
|
|
|
|
|
|
} |
|
1764
|
|
|
|
|
|
|
|
|
1765
|
80
|
|
|
|
|
260
|
return $cmd; |
|
1766
|
|
|
|
|
|
|
} |
|
1767
|
|
|
|
|
|
|
} |
|
1768
|
|
|
|
|
|
|
|
|
1769
|
|
|
|
|
|
|
### Command-line arguments (but not the command itself) must be quoted |
|
1770
|
|
|
|
|
|
|
### to ensure case preservation. Borrowed from Module::Build with adaptations. |
|
1771
|
|
|
|
|
|
|
### Patch for this supplied by Craig Berry, see RT #46288: [PATCH] Add argument |
|
1772
|
|
|
|
|
|
|
### quoting for run() on VMS |
|
1773
|
|
|
|
|
|
|
sub _quote_args_vms { |
|
1774
|
|
|
|
|
|
|
### Returns a command string with proper quoting so that the subprocess |
|
1775
|
|
|
|
|
|
|
### sees this same list of args, or if we get a single arg that is an |
|
1776
|
|
|
|
|
|
|
### array reference, quote the elements of it (except for the first) |
|
1777
|
|
|
|
|
|
|
### and return the reference. |
|
1778
|
0
|
|
|
0
|
|
0
|
my @args = @_; |
|
1779
|
0
|
0
|
0
|
|
|
0
|
my $got_arrayref = (scalar(@args) == 1 |
|
1780
|
|
|
|
|
|
|
&& UNIVERSAL::isa($args[0], 'ARRAY')) |
|
1781
|
|
|
|
|
|
|
? 1 |
|
1782
|
|
|
|
|
|
|
: 0; |
|
1783
|
|
|
|
|
|
|
|
|
1784
|
0
|
0
|
0
|
|
|
0
|
@args = split(/\s+/, $args[0]) unless $got_arrayref || scalar(@args) > 1; |
|
1785
|
|
|
|
|
|
|
|
|
1786
|
0
|
0
|
|
|
|
0
|
my $cmd = $got_arrayref ? shift @{$args[0]} : shift @args; |
|
|
0
|
|
|
|
|
0
|
|
|
1787
|
|
|
|
|
|
|
|
|
1788
|
|
|
|
|
|
|
### Do not quote qualifiers that begin with '/' or previously quoted args. |
|
1789
|
0
|
0
|
|
|
|
0
|
map { if (/^[^\/\"]/) { |
|
1790
|
0
|
|
|
|
|
0
|
$_ =~ s/\"/""/g; # escape C<"> by doubling |
|
1791
|
0
|
|
|
|
|
0
|
$_ = q(").$_.q("); |
|
1792
|
|
|
|
|
|
|
} |
|
1793
|
|
|
|
|
|
|
} |
|
1794
|
0
|
0
|
|
|
|
0
|
($got_arrayref ? @{$args[0]} |
|
|
0
|
|
|
|
|
0
|
|
|
1795
|
|
|
|
|
|
|
: @args |
|
1796
|
|
|
|
|
|
|
); |
|
1797
|
|
|
|
|
|
|
|
|
1798
|
0
|
0
|
|
|
|
0
|
$got_arrayref ? unshift(@{$args[0]}, $cmd) : unshift(@args, $cmd); |
|
|
0
|
|
|
|
|
0
|
|
|
1799
|
|
|
|
|
|
|
|
|
1800
|
0
|
0
|
|
|
|
0
|
return $got_arrayref ? $args[0] |
|
1801
|
|
|
|
|
|
|
: join(' ', @args); |
|
1802
|
|
|
|
|
|
|
} |
|
1803
|
|
|
|
|
|
|
|
|
1804
|
|
|
|
|
|
|
|
|
1805
|
|
|
|
|
|
|
### XXX this is cribbed STRAIGHT from M::B 0.30 here: |
|
1806
|
|
|
|
|
|
|
### http://search.cpan.org/src/KWILLIAMS/Module-Build-0.30/lib/Module/Build/Platform/Windows.pm:split_like_shell |
|
1807
|
|
|
|
|
|
|
### XXX this *should* be integrated into text::parsewords |
|
1808
|
|
|
|
|
|
|
sub _split_like_shell_win32 { |
|
1809
|
|
|
|
|
|
|
# As it turns out, Windows command-parsing is very different from |
|
1810
|
|
|
|
|
|
|
# Unix command-parsing. Double-quotes mean different things, |
|
1811
|
|
|
|
|
|
|
# backslashes don't necessarily mean escapes, and so on. So we |
|
1812
|
|
|
|
|
|
|
# can't use Text::ParseWords::shellwords() to break a command string |
|
1813
|
|
|
|
|
|
|
# into words. The algorithm below was bashed out by Randy and Ken |
|
1814
|
|
|
|
|
|
|
# (mostly Randy), and there are a lot of regression tests, so we |
|
1815
|
|
|
|
|
|
|
# should feel free to adjust if desired. |
|
1816
|
|
|
|
|
|
|
|
|
1817
|
0
|
|
|
0
|
|
0
|
local $_ = shift; |
|
1818
|
|
|
|
|
|
|
|
|
1819
|
0
|
|
|
|
|
0
|
my @argv; |
|
1820
|
0
|
0
|
0
|
|
|
0
|
return @argv unless defined() && length(); |
|
1821
|
|
|
|
|
|
|
|
|
1822
|
0
|
|
|
|
|
0
|
my $arg = ''; |
|
1823
|
0
|
|
|
|
|
0
|
my( $i, $quote_mode ) = ( 0, 0 ); |
|
1824
|
|
|
|
|
|
|
|
|
1825
|
0
|
|
|
|
|
0
|
while ( $i < length() ) { |
|
1826
|
|
|
|
|
|
|
|
|
1827
|
0
|
|
|
|
|
0
|
my $ch = substr( $_, $i , 1 ); |
|
1828
|
0
|
|
|
|
|
0
|
my $next_ch = substr( $_, $i+1, 1 ); |
|
1829
|
|
|
|
|
|
|
|
|
1830
|
0
|
0
|
0
|
|
|
0
|
if ( $ch eq '\\' && $next_ch eq '"' ) { |
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1831
|
0
|
|
|
|
|
0
|
$arg .= '"'; |
|
1832
|
0
|
|
|
|
|
0
|
$i++; |
|
1833
|
|
|
|
|
|
|
} elsif ( $ch eq '\\' && $next_ch eq '\\' ) { |
|
1834
|
0
|
|
|
|
|
0
|
$arg .= '\\'; |
|
1835
|
0
|
|
|
|
|
0
|
$i++; |
|
1836
|
|
|
|
|
|
|
} elsif ( $ch eq '"' && $next_ch eq '"' && $quote_mode ) { |
|
1837
|
0
|
|
|
|
|
0
|
$quote_mode = !$quote_mode; |
|
1838
|
0
|
|
|
|
|
0
|
$arg .= '"'; |
|
1839
|
0
|
|
|
|
|
0
|
$i++; |
|
1840
|
|
|
|
|
|
|
} elsif ( $ch eq '"' && $next_ch eq '"' && !$quote_mode && |
|
1841
|
|
|
|
|
|
|
( $i + 2 == length() || |
|
1842
|
|
|
|
|
|
|
substr( $_, $i + 2, 1 ) eq ' ' ) |
|
1843
|
|
|
|
|
|
|
) { # for cases like: a"" => [ 'a' ] |
|
1844
|
0
|
|
|
|
|
0
|
push( @argv, $arg ); |
|
1845
|
0
|
|
|
|
|
0
|
$arg = ''; |
|
1846
|
0
|
|
|
|
|
0
|
$i += 2; |
|
1847
|
|
|
|
|
|
|
} elsif ( $ch eq '"' ) { |
|
1848
|
0
|
|
|
|
|
0
|
$quote_mode = !$quote_mode; |
|
1849
|
|
|
|
|
|
|
} elsif ( $ch eq ' ' && !$quote_mode ) { |
|
1850
|
0
|
0
|
0
|
|
|
0
|
push( @argv, $arg ) if defined( $arg ) && length( $arg ); |
|
1851
|
0
|
|
|
|
|
0
|
$arg = ''; |
|
1852
|
0
|
|
|
|
|
0
|
++$i while substr( $_, $i + 1, 1 ) eq ' '; |
|
1853
|
|
|
|
|
|
|
} else { |
|
1854
|
0
|
|
|
|
|
0
|
$arg .= $ch; |
|
1855
|
|
|
|
|
|
|
} |
|
1856
|
|
|
|
|
|
|
|
|
1857
|
0
|
|
|
|
|
0
|
$i++; |
|
1858
|
|
|
|
|
|
|
} |
|
1859
|
|
|
|
|
|
|
|
|
1860
|
0
|
0
|
0
|
|
|
0
|
push( @argv, $arg ) if defined( $arg ) && length( $arg ); |
|
1861
|
0
|
|
|
|
|
0
|
return @argv; |
|
1862
|
|
|
|
|
|
|
} |
|
1863
|
|
|
|
|
|
|
|
|
1864
|
|
|
|
|
|
|
|
|
1865
|
|
|
|
|
|
|
|
|
1866
|
2
|
|
|
2
|
|
20
|
{ use File::Spec; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
76
|
|
|
1867
|
2
|
|
|
2
|
|
18
|
use Symbol; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
1655
|
|
|
1868
|
|
|
|
|
|
|
|
|
1869
|
|
|
|
|
|
|
my %Map = ( |
|
1870
|
|
|
|
|
|
|
STDOUT => [qw|>&|, \*STDOUT, Symbol::gensym() ], |
|
1871
|
|
|
|
|
|
|
STDERR => [qw|>&|, \*STDERR, Symbol::gensym() ], |
|
1872
|
|
|
|
|
|
|
STDIN => [qw|<&|, \*STDIN, Symbol::gensym() ], |
|
1873
|
|
|
|
|
|
|
); |
|
1874
|
|
|
|
|
|
|
|
|
1875
|
|
|
|
|
|
|
### dups FDs and stores them in a cache |
|
1876
|
|
|
|
|
|
|
sub __dup_fds { |
|
1877
|
80
|
|
|
80
|
|
167
|
my $self = shift; |
|
1878
|
80
|
|
|
|
|
575
|
my @fds = @_; |
|
1879
|
|
|
|
|
|
|
|
|
1880
|
80
|
50
|
|
|
|
302
|
__PACKAGE__->_debug( "# Closing the following fds: @fds" ) if $DEBUG; |
|
1881
|
|
|
|
|
|
|
|
|
1882
|
80
|
|
|
|
|
547
|
for my $name ( @fds ) { |
|
1883
|
120
|
50
|
|
|
|
282
|
my($redir, $fh, $glob) = @{$Map{$name}} or ( |
|
|
120
|
|
|
|
|
592
|
|
|
1884
|
|
|
|
|
|
|
Carp::carp(loc("No such FD: '%1'", $name)), next ); |
|
1885
|
|
|
|
|
|
|
|
|
1886
|
|
|
|
|
|
|
### MUST use the 2-arg version of open for dup'ing for |
|
1887
|
|
|
|
|
|
|
### 5.6.x compatibility. 5.8.x can use 3-arg open |
|
1888
|
|
|
|
|
|
|
### see perldoc5.6.2 -f open for details |
|
1889
|
120
|
50
|
|
|
|
3265
|
open $glob, $redir . fileno($fh) or ( |
|
1890
|
|
|
|
|
|
|
Carp::carp(loc("Could not dup '$name': %1", $!)), |
|
1891
|
|
|
|
|
|
|
return |
|
1892
|
|
|
|
|
|
|
); |
|
1893
|
|
|
|
|
|
|
|
|
1894
|
|
|
|
|
|
|
### we should re-open this filehandle right now, not |
|
1895
|
|
|
|
|
|
|
### just dup it |
|
1896
|
|
|
|
|
|
|
### Use 2-arg version of open, as 5.5.x doesn't support |
|
1897
|
|
|
|
|
|
|
### 3-arg version =/ |
|
1898
|
120
|
100
|
|
|
|
1303
|
if( $redir eq '>&' ) { |
|
1899
|
80
|
50
|
|
|
|
4679
|
open( $fh, '>' . File::Spec->devnull ) or ( |
|
1900
|
|
|
|
|
|
|
Carp::carp(loc("Could not reopen '$name': %1", $!)), |
|
1901
|
|
|
|
|
|
|
return |
|
1902
|
|
|
|
|
|
|
); |
|
1903
|
|
|
|
|
|
|
} |
|
1904
|
|
|
|
|
|
|
} |
|
1905
|
|
|
|
|
|
|
|
|
1906
|
80
|
|
|
|
|
315
|
return 1; |
|
1907
|
|
|
|
|
|
|
} |
|
1908
|
|
|
|
|
|
|
|
|
1909
|
|
|
|
|
|
|
### reopens FDs from the cache |
|
1910
|
|
|
|
|
|
|
sub __reopen_fds { |
|
1911
|
80
|
|
|
80
|
|
421
|
my $self = shift; |
|
1912
|
80
|
|
|
|
|
1046
|
my @fds = @_; |
|
1913
|
|
|
|
|
|
|
|
|
1914
|
80
|
50
|
|
|
|
328
|
__PACKAGE__->_debug( "# Reopening the following fds: @fds" ) if $DEBUG; |
|
1915
|
|
|
|
|
|
|
|
|
1916
|
80
|
|
|
|
|
528
|
for my $name ( @fds ) { |
|
1917
|
120
|
50
|
|
|
|
274
|
my($redir, $fh, $glob) = @{$Map{$name}} or ( |
|
|
120
|
|
|
|
|
1425
|
|
|
1918
|
|
|
|
|
|
|
Carp::carp(loc("No such FD: '%1'", $name)), next ); |
|
1919
|
|
|
|
|
|
|
|
|
1920
|
|
|
|
|
|
|
### MUST use the 2-arg version of open for dup'ing for |
|
1921
|
|
|
|
|
|
|
### 5.6.x compatibility. 5.8.x can use 3-arg open |
|
1922
|
|
|
|
|
|
|
### see perldoc5.6.2 -f open for details |
|
1923
|
120
|
50
|
|
|
|
5429
|
open( $fh, $redir . fileno($glob) ) or ( |
|
1924
|
|
|
|
|
|
|
Carp::carp(loc("Could not restore '$name': %1", $!)), |
|
1925
|
|
|
|
|
|
|
return |
|
1926
|
|
|
|
|
|
|
); |
|
1927
|
|
|
|
|
|
|
|
|
1928
|
|
|
|
|
|
|
### close this FD, we're not using it anymore |
|
1929
|
120
|
|
|
|
|
1563
|
close $glob; |
|
1930
|
|
|
|
|
|
|
} |
|
1931
|
80
|
|
|
|
|
308
|
return 1; |
|
1932
|
|
|
|
|
|
|
|
|
1933
|
|
|
|
|
|
|
} |
|
1934
|
|
|
|
|
|
|
} |
|
1935
|
|
|
|
|
|
|
|
|
1936
|
|
|
|
|
|
|
sub _debug { |
|
1937
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
1938
|
0
|
0
|
|
|
|
|
my $msg = shift or return; |
|
1939
|
0
|
|
0
|
|
|
|
my $level = shift || 0; |
|
1940
|
|
|
|
|
|
|
|
|
1941
|
0
|
|
|
|
|
|
local $Carp::CarpLevel += $level; |
|
1942
|
0
|
|
|
|
|
|
Carp::carp($msg); |
|
1943
|
|
|
|
|
|
|
|
|
1944
|
0
|
|
|
|
|
|
return 1; |
|
1945
|
|
|
|
|
|
|
} |
|
1946
|
|
|
|
|
|
|
|
|
1947
|
|
|
|
|
|
|
sub _pp_child_error { |
|
1948
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
1949
|
0
|
0
|
|
|
|
|
my $cmd = shift or return; |
|
1950
|
0
|
0
|
|
|
|
|
my $ce = shift or return; |
|
1951
|
0
|
0
|
|
|
|
|
my $pp_cmd = ref $cmd ? "@$cmd" : $cmd; |
|
1952
|
|
|
|
|
|
|
|
|
1953
|
|
|
|
|
|
|
|
|
1954
|
0
|
|
|
|
|
|
my $str; |
|
1955
|
0
|
0
|
|
|
|
|
if( $ce == -1 ) { |
|
|
|
0
|
|
|
|
|
|
|
1956
|
|
|
|
|
|
|
### Include $! in the error message, so that the user can |
|
1957
|
|
|
|
|
|
|
### see 'No such file or directory' versus 'Permission denied' |
|
1958
|
|
|
|
|
|
|
### versus 'Cannot fork' or whatever the cause was. |
|
1959
|
0
|
|
|
|
|
|
$str = "Failed to execute '$pp_cmd': $!"; |
|
1960
|
|
|
|
|
|
|
|
|
1961
|
|
|
|
|
|
|
} elsif ( $ce & 127 ) { |
|
1962
|
|
|
|
|
|
|
### some signal |
|
1963
|
0
|
0
|
|
|
|
|
$str = loc( "'%1' died with signal %2, %3 coredump", |
|
1964
|
|
|
|
|
|
|
$pp_cmd, ($ce & 127), ($ce & 128) ? 'with' : 'without'); |
|
1965
|
|
|
|
|
|
|
|
|
1966
|
|
|
|
|
|
|
} else { |
|
1967
|
|
|
|
|
|
|
### Otherwise, the command run but gave error status. |
|
1968
|
0
|
|
|
|
|
|
$str = "'$pp_cmd' exited with value " . ($ce >> 8); |
|
1969
|
|
|
|
|
|
|
} |
|
1970
|
|
|
|
|
|
|
|
|
1971
|
0
|
0
|
|
|
|
|
$self->_debug( "# Child error '$ce' translated to: $str" ) if $DEBUG; |
|
1972
|
|
|
|
|
|
|
|
|
1973
|
0
|
|
|
|
|
|
return $str; |
|
1974
|
|
|
|
|
|
|
} |
|
1975
|
|
|
|
|
|
|
|
|
1976
|
|
|
|
|
|
|
1; |
|
1977
|
|
|
|
|
|
|
|
|
1978
|
|
|
|
|
|
|
__END__ |