blib/lib/Spawn/Safe.pm

filter: Statementstmt 90.4% 133 / 147 Branchbran 61.8% 47 / 76 Conditioncond 64.2% 9 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 79.2% 202 / 255 CC 47 SCAR 41.4
CC
47
Cov
80%
spawn_safe
73.0
BEGIN
1.0
BEGIN
1.0
CRAP 62.5
↑ Summary
1 package Spawn::Safe;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Based off of the smallest PIPE_BUF I've seen.
9 6 ▶use constant PIPE_BUF_SIZE => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 $VERSION = '2.006';
13  
14 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 our ( @ISA, @EXPORT );
17  
18 6 @ISA = qw(Exporter);
19 6 @EXPORT = qw/ spawn_safe /;
20 }
21  
22 =head1 NAME
23  
24 Spawn::Safe - Fork and exec a process "safely".
25  
26 =head1 EXAMPLE
27  
28 A basic example:
29  
30 use Spawn::Safe;
31 use Data::Dumper;
32 my $results = spawn_safe({ argv => [ 'ls', '-al', '/var/' ], timeout => 2 });
33 die Dumper $results;
34  
35 As a replacement for backticks:
36  
37 use Spawn::Safe;
38 # $output = `ls -al /var/`;
39 $output = spawn_safe(qw{ ls -al /var/ })->{stdout};
40  
41 =head1 SYNOPSIS
42  
43 Spawn::Safe is a module designed to make "safe" calls to outside binaries
44 easier and more reliable. Spawn::Safe never invokes a shell (unless the shell
45 is explicitly requested), so escaping for the shell is not a concern. An
46 optional timeout is made available, so scripts will not hang forever, and the
47 caller is able to retrieve both stdout and stderr. An optional string can be
48 passed to the executed program's standard input stream.
49  
50 =head1 FUNCTIONS
51  
52 =head2 spawn_safe
53  
54 Spawn (via fork and exec) the specified binary and capture its output.
55  
56 =head3 Parameters
57  
58 If passed a single scalar, spawn_safe will assume that to be the the target
59 binary, and execute it without a limit on runtime.
60  
61 If passed an array, spawn_safe will execute the first element of the array as
62 the target binary, with the remaining elements passed as parameters to the
63 target binary, without a limit on runtime.
64  
65 The preferred mode is to pass in a single hash reference. When called this
66 way, the following keys are available:
67  
68 =over 4
69  
70 =item * argv
71  
72 Either a string containing the name of the binary which will be called with no
73 parameters:
74  
75 my $r = spawn_safe({ argv => 'ls' });
76  
77 Or an array reference containing the binary and all of its parameters:
78  
79 my $r = spawn_safe({ argv => [ 'ls', '-al' ] });
80  
81 =item * timeout
82  
83 The amount of time, in seconds, the binary will be allowed to run before being
84 killed and a timeout error being returned. If false (or is otherwise undefined
85 or unset), the timeout will be infinite.
86  
87 =item * env
88  
89 A hash reference containing the new environment for the executed binary. If
90 false (or otherwise undefined or unset), it will default to the current
91 environment. You must specify the complete environment, as the current
92 environment will be overwritten as a whole. To alter only one variable, a copy
93 of the enviornment must be made, altered, and then passed in as a whole, eg:
94  
95 my %new_env = %ENV;
96 $new_env{'TMP'} = '/var/tmp/';
97 my $r = spawn_safe({ argv => 'ls', env => \%new_env });
98  
99 Please note that if a new environment is specified, the new binary's
100 environment will be altered before the call to exec() (but after the fork(),
101 so the caller's environment will be unchanged), so the new environment will
102 take effect before the new binary is launched. This means that if you alter a
103 part of the environment needed to launch the binary (eg, by changing PATH,
104 LD_LIBRARY_PATH, etc), these new variables will need to be set such that the
105 binary can be executed successfully.
106  
107 =item * stdin
108  
109 A string to be passed to the target binary's standard input stream. The string
110 will be written into the stream and then the stream will be closed.
111  
112 my $r = spawn_safe({ argv => [ '/usr/bin/tr', 'a', 'b' ], stdin => 'aaa' });
113  
114 =back
115  
116 =head3 Return value
117  
118 A hash reference will be returned containing one of the following sets of
119 values:
120  
121 =over 4
122  
123 =item * If the binary could not be spawned, the single key, 'error' will be
124 set, which is a text description of the reason the binary could not be spawned.
125  
126 =item * If the binary was executed successfully, but terminated due to a
127 timeout, the keys 'error', 'stdout', and 'stderr', will be set. The value for
128 'error' will be set to 'timed out'. Any data collected from the executed
129 binary's stdout or stderr will also be made available, but since the binary was
130 forcefully terminated, the data may be incomplete.
131  
132 =item * If the binary was executed successfully and ran to completion, the keys
133 'exit_code', 'stdout, and 'stderr', will all be available.
134  
135 =back
136  
137 The key "exit_zero" will always be present, which is true if the binary is
138 executed successfully and exited with a code of zero.
139  
140 =head3 Notes
141  
142 The current PATH will be searched for the binary, if available. Open
143 filehandles are subject to Perl's standard close-on-exec behavior. A shell will
144 not be invoked unless explicitly defined as the target binary, as such output
145 redirection and other shell features are unavailable.
146  
147 If passed invalid parameters, spawn_safe will croak.
148  
149 Please note that when specifying a timeout, alarm() is no longer used. If the
150 clock is stepped significantly backwards during a timeout, a possibly false
151 timeout error may be thrown. Timeout accuracy should be within one second.
152  
153 If a timeout does occur, the spawned program will be sent a SIGKILL before
154 spawn_safe returns.
155  
156 =head1 COMPATIBILITY
157  
158 This module attempts to work on MSWin32 but I've been unable to get it working
159 due to strange issues with IO::Select. I haven't been able to track down the
160 exact cause, so for now I don't believe this module functions on MSWin32.
161  
162 Linux and BSD are tested and supported platforms.
163  
164 =cut
165  
166 16 ▶sub spawn_safe {
Subroutine spawn_safe: called CC=47 SCAR=42.9
Pod documented
167 16 my ( $params ) = @_;
168 16 my @binary_and_params;
169 my $timeout;
170 16 my $start_time;
171 16 my $new_env;
172 16 my $for_stdin;
173 16 my $for_stdin_offset = 0;
174  
175 16 ▶ if ( ref $params eq '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $params eq '') { } 0 16 16
elsif (ref $params eq 'HASH') { } 16 0 16
176 0 @binary_and_params = @_;
177 } elsif ( ref $params eq 'HASH' ) {
178 16 ▶ if ( !$params->{'argv'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"argv"}) 0 16 16
179 0 croak "Invalid parameters (missing argv)";
180 }
181 16 ▶ if ( ref $params->{'argv'} eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $params->{'argv'} eq 'ARRAY') { } 16 0 16
elsif (ref $params->{'argv'} eq '') { } 0 0 0
182 16 @binary_and_params = @{ $params->{'argv'} };
183 } elsif ( ref $params->{'argv'} eq '' ) {
184 0 @binary_and_params = $params->{'argv'};
185 } else {
186 0 croak "Invalid parameters (what is argv?)";
187 }
188  
189 16 ▶ if ( ref $params->{'env'} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{"env"} eq "HASH") 0 16 16
190 0 $new_env = $params->{'env'};
191 }
192  
193 16 ▶ $timeout = $params->{'timeout'} || undef;
Condition50%1/2
$params->{'timeout'} || undef
l!l
160
MC/DC0%0/1
$params->{'timeout'} || undef
$params->{'timeout'}
Truth table50%1/2
A
A= $params->{'timeout'}
Aresult
11
00
194 16 ▶ $for_stdin = $params->{'stdin'} || undef;
Condition100%2/2
$params->{'stdin'} || undef
l!l
214
MC/DC100%1/1
$params->{'stdin'} || undef
$params->{'stdin'}
Truth table100%2/2
A
A= $params->{'stdin'}
Aresult
11
00
195 } else {
196 0 croak "Invalid parameters";
197 }
198  
199 16 my ( $child_pid, $exit_code );
200 16 my ( $parent_read_stdout, $child_write_stdout );
201 16 my ( $parent_read_stderr, $child_write_stderr );
202 16 my ( $parent_signal, $child_wait );
203 16 my ( $parent_read_errors, $child_write_errors );
204 16 my ( $child_read_stdin, $parent_write_stdin );
205  
206 16 my ( $read_stdout, $read_stderr, $read_errors ) = ( '' ) x 3;
207  
208 16 ▶ pipe( $parent_read_stdout, $child_write_stdout ) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
pipe $parent_read_stdout, $child_write_stdout or die $! 16 0 16
209 16 ▶ pipe( $parent_read_stderr, $child_write_stderr ) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
pipe $parent_read_stderr, $child_write_stderr or die $! 16 0 16
210 16 ▶ pipe( $parent_read_errors, $child_write_errors ) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
pipe $parent_read_errors, $child_write_errors or die $! 16 0 16
211 16 ▶ pipe( $child_read_stdin, $parent_write_stdin ) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
pipe $child_read_stdin, $parent_write_stdin or die $! 16 0 16
212 16 ▶ pipe( $child_wait, $parent_signal ) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
pipe $child_wait, $parent_signal or die $! 16 0 16
213  
214 16 $child_pid = fork();
215 16 ▶ if ( !defined $child_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child_pid) 0 16 16
216 0 die "Unable to fork: $!";
217 }
218  
219 16 ▶ if ( !$child_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child_pid) 5 11 16
220 5 close( $parent_signal );
221 5 close( $parent_read_stdout );
222 5 close( $parent_read_stderr );
223 5 close( $parent_read_errors );
224 5 close( $parent_write_stdin );
225  
226 0 ▶ if ( tied( *STDIN ) ) { untie *STDIN; }
Branch: 1 branch
BranchTrueFalseTotal
if (tied *STDIN) 0 5 5
227 0 ▶ if ( tied( *STDOUT ) ) { untie *STDOUT; }
Branch: 1 branch
BranchTrueFalseTotal
if (tied *STDOUT) 0 5 5
228 0 ▶ if ( tied( *STDERR ) ) { untie *STDERR; }
Branch: 1 branch
BranchTrueFalseTotal
if (tied *STDERR) 0 5 5
229  
230 # Be 5.6 compatible and do it the old way.
231 5 ▶ open( STDOUT, '>&' . fileno( $child_write_stdout ) ) || goto CHILD_ERR;
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>&' . fileno($child_write_stdout) or goto CHILD_ERR 5 0 5
232 5 ▶ open( STDERR, '>&' . fileno( $child_write_stderr ) ) || goto CHILD_ERR;
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, '>&' . fileno($child_write_stderr) or goto CHILD_ERR 5 0 5
233 5 ▶ open( STDIN, '<&' . fileno( $child_read_stdin ) ) || goto CHILD_ERR;
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, '<&' . fileno($child_read_stdin) or goto CHILD_ERR 5 0 5
234  
235 0 ▶ if ( $new_env ) { %ENV = %{$new_env}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($new_env) 0 5 5
236  
237 5 <$child_wait>;
238 5 close( $child_wait );
239  
240 5 { exec { $binary_and_params[0] } @binary_and_params; }
241 CHILD_ERR:
242 1 print $child_write_errors $!;
243 1 close( $child_write_errors );
244 1 close( $child_write_stdout );
245 1 close( $child_write_stderr );
246  
247 # Exit code here isn't actually used.
248 1 exit 42;
249 }
250  
251 11 close( $child_write_stdout );
252 11 close( $child_write_stderr );
253 11 close( $child_read_stdin );
254 11 close( $child_wait );
255 11 close( $child_write_errors );
256 11 ▶ my $sel = IO::Select->new( $parent_read_stdout, $parent_read_stderr, $parent_read_errors )
Condition50%1/2
'IO::Select'->new($parent_read_stdout, $parent_read_stderr, $parent_read_errors) || die('Failed to create IO::Select object!')
l!l
110
MC/DC0%0/1
'IO::Select'->new($parent_read_stdout, $parent_read_stderr, $parent_read_errors) || die('Failed to create IO::Select object!')
'IO::Select'->new($parent_read_stdout, $parent_read_stderr, $parent_read_errors)
Truth table50%1/2
A
A= 'IO::Select'->new($parent_read_stdout, $parent_read_stderr, $parent_read_errors)
Aresult
11
00
257 || die "Failed to create IO::Select object!";
258 11 my $wsel;
259  
260 11 ▶ if ( defined $for_stdin ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $for_stdin) { } 1 10 11
261 1 ▶ $wsel = IO::Select->new( $parent_write_stdin )
Condition50%1/2
'IO::Select'->new($parent_write_stdin) || die('Failed to create IO::Select object!')
l!l
10
MC/DC0%0/1
'IO::Select'->new($parent_write_stdin) || die('Failed to create IO::Select object!')
'IO::Select'->new($parent_write_stdin)
Truth table50%1/2
A
A= 'IO::Select'->new($parent_write_stdin)
Aresult
11
00
262 || die "Failed to create IO::Select object!";
263 } else {
264 10 close( $parent_write_stdin );
265 }
266 11 close( $parent_signal );
267  
268 # Don't bother calling time if we're never going to timeout.
269 11 ▶ $start_time = defined $timeout ? time() : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 11 0 11
270 11 my $select_time = $timeout;
271 11 MAIN_WHILE: while ( 1 ) {
272 93 my ( $readus, $writeus, undef ) = IO::Select::select( $sel, $wsel, undef, $select_time );
273 93 ▶ if ( ref $readus eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $readus eq "ARRAY") 93 0 93
274 93 foreach my $readme ( @{$readus} ) {
275 70 my $read;
276 70 my $r = sysread( $readme, $read, PIPE_BUF_SIZE );
277 70 ▶ if ( ( !defined $r ) || ( $r < 1 ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $r or $r < 1) { } 29 41 70
elsif ($readme == $parent_read_stdout) { } 38 3 41
elsif ($readme == $parent_read_stderr) { } 0 3 3
elsif ($readme == $parent_read_errors) { } 3 0 3
Condition66%2/3
not defined $r or $r < 1
l!l&&r!l&&!r
02941
MC/DC50%1/2
not defined $r or $r < 1
not defined $r $r < 1
Truth table66%2/3
not(A) or B
A= not defined $rB= $r < 1
ABresult
1X1
011
000
278 29 $sel->remove( $readme );
279 29 ▶ if ( $sel->count() == 0 ) { last MAIN_WHILE; }
Branch: 1 branch
BranchTrueFalseTotal
if ($sel->count == 0) 9 20 29
280 } elsif ( $readme == $parent_read_stdout ) {
281 38 $read_stdout .= $read;
282 } elsif ( $readme == $parent_read_stderr ) {
283 0 $read_stderr .= $read;
284 } elsif ( $readme == $parent_read_errors ) {
285 3 $read_errors .= $read;
286 } else {
287 0 die 'Should not be here!';
288 }
289 }
290 }
291 84 ▶ if ( ref $writeus eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $writeus eq "ARRAY") 84 0 84
292 84 foreach my $writeme ( @{$writeus} ) {
293 33 ▶ if ( $writeme == $parent_write_stdin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writeme == $parent_write_stdin) 33 0 33
294 33 ▶ my $write_size = PIPE_BUF_SIZE <= length( $for_stdin ) ? PIPE_BUF_SIZE : length( $for_stdin );
Branch: 1 branch
BranchTrueFalseTotal
512 <= length $for_stdin ? : 33 0 33
295 33 syswrite( $parent_write_stdin, $for_stdin, $write_size, $for_stdin_offset );
296 33 $for_stdin_offset += $write_size;
297 33 ▶ if ( $for_stdin_offset >= length( $for_stdin ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($for_stdin_offset >= length $for_stdin) 1 32 33
298 1 $wsel->remove( $parent_write_stdin );
299 1 close( $parent_write_stdin );
300 }
301 }
302 }
303 }
304 84 ▶ if ( defined $timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 84 0 84
305 # We do a little gymnastics here to check if the time has rolled
306 # backwards (ie, ntpd stepped the time backwards). If it went
307 # backwards, there's no way to tell how long we've waited, so
308 # it's probably safer to assume we've waited too long. Hopefully
309 # steps backwards will be infrequent, as ntpd usually slews rather
310 # than steps.
311 # If the time rolls over, we should end up with a hugely negative
312 # $timeout after subtraction, so that will probably trigger a
313 # timeout as well. Imperfect, but somewhat better than waiting
314 # forever. Fortunately this probably won't ever come up.
315 84 my $timenow = time();
316 84 $select_time = $timeout - ( $timenow - $start_time );
317 84 ▶ if ( $timenow < $start_time || $select_time <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timenow < $start_time or $select_time <= 0) 2 82 84
Condition66%2/3
$timenow < $start_time or $select_time <= 0
l!l&&r!l&&!r
0282
MC/DC50%1/2
$timenow < $start_time or $select_time <= 0
$timenow < $start_time $select_time <= 0
Truth table66%2/3
A or B
A= $timenow < $start_timeB= $select_time <= 0
ABresult
1X1
011
000
318 2 undef $start_time;
319 2 last;
320 }
321 }
322 }
323 # Did we timeout? undef $start_time is our timeout flag.
324 11 ▶ if ( defined $start_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start_time) 9 2 11
325 9 waitpid( $child_pid, 0 );
326 9 $exit_code = $? >> 8;
327 }
328  
329 11 close( $parent_read_stdout );
330 11 close( $parent_read_stderr );
331 11 close( $parent_read_errors );
332  
333 11 ▶ if ( !defined $start_time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start_time) 2 9 11
334 # If the child is still running, kill it.
335 2 ▶ if ( waitpid( $child_pid, WNOHANG ) != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($child_pid, 1) != -1) 2 0 2
336 2 kill( 9, $child_pid );
337 2 waitpid( $child_pid, 0 );
338 }
339  
340 return {
341 2 'error' => 'timed out',
342 'stdout' => $read_stdout,
343 'stderr' => $read_stderr,
344 'exit_zero' => 0,
345 };
346 }
347  
348 9 ▶ if ( $read_errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_errors) 3 6 9
349 return {
350 3 'error' => $read_errors,
351 'exit_zero' => 0,
352 };
353 }
354 return {
355 6 'exit_code' => $exit_code,
356 'stdout' => $read_stdout,
357 'stderr' => $read_stderr,
358 'exit_zero' => $exit_code == 0,
359 };
360 }
361  
362 =head1 LICENSE
363  
364 This module is licensed under the same terms as Perl itself.
365  
366 =head1 CHANGES
367  
368 =head2 Version 2.006 - 2013-11-12, jeagle
369  
370 Modify PIPE_BUF_SIZE to be more conservative to ensure non-blocking writes on
371 all OSs.
372  
373 =head2 Version 2.005 - 2013-11-11, jeagle
374  
375 Add stdin option, clarify docs, add exit_zero return flag.
376  
377 =head2 Version 2.004 - 2012-08-13, jeagle
378  
379 Include license. Oops.
380  
381 =head2 Version 2.003 - 2012-04-01, jeagle
382  
383 Untie any tied filehandles before we re-open them to ourselves to work around
384 any weird tie behavior (should fix issues running under FCGI). Thanks Charly.
385  
386 =head2 Version 2.002 - 2012-01-04, jeagle
387  
388 Correct documentation (RT#72831, thanks Stas)
389  
390 Update unit tests to specify number of tests instead of using no_plan,
391 otherwse CPAN Testers reports tests fail.
392  
393 =head2 Version 2.001 - 2011-06-13, jeagle
394  
395 Give the spawned program its own STDIN.
396  
397 =head2 Version 2.000 - 2011-05-12, jeagle
398  
399 Correct timeout handling. Attempt to correct unit tests for MSWin32, but
400 there seems to be an issue with IO::Select preventing it from working
401 properly. Update docs for MSWin32.
402  
403 =head2 Version 1.9 - 2011-05-10, jeagle
404  
405 Don't use clock_gettime(), use time() and return a timeout if time steps
406 backwards.
407  
408 =head2 Version 1.8 - 2011-05-09, jeagle
409  
410 Clean up docs, stop using SIGALARM for timeouts.
411  
412 =head2 Version 1.7 - 2010-07-09, jeagle
413  
414 Clean up for release to CPAN.
415  
416 =head2 Version 0.4 - 2009-05-13, jeagle
417  
418 Correct a warning issued when using spawn_safe without a timeout.
419  
420 Fix compatibility with perl < 5.8.
421  
422 =head2 Version 0.3 - 2009-04-21, jeagle
423  
424 Clarify documentation regarding use of SIGALRM and for passing of a new
425 environment.
426  
427 Correct a warning thrown by exec().
428  
429 Correct an issue with incorrectly handled timeouts.
430  
431 =head2 Version 0.2 - 2009-04-20, jeagle
432  
433 Modify API, breaking compatibility, for clarity and expandability.
434  
435 Add the ability to specify the target program's environment.
436  
437 Return the (partial) stdout and stderr on a timeout.
438  
439 Update and clarify documentation.
440  
441 =head2 Version 0.1 - 2009-04-11, jeagle
442  
443 Inital release.
444  
445 =cut
446  
447 5 1;
↑ Summary