blib/lib/Proc/SyncExec.pm

filter: Statementstmt 82.8% 87 / 105 Branchbran 48.5% 33 / 68 Conditioncond 43.4% 10 / 23 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 65.0% 149 / 229 CC 43 SCAR 30.1
CC
43
Cov
66%
sync_exec
40.2
sync_popen_noshell
20.0
fork_retry
14.3
CRAP 20.3
↑ Summary
1 # $Id: SyncExec.pm,v 1.5 2005/02/04 12:15:57 roderick Exp $
2 #
3 # Copyright (c) 1997 Roderick Schertler. All rights reserved. This
4 # program is free software; you can redistribute it and/or modify it
5 # under the same terms as Perl itself.
6  
7 =head1 NAME
8  
9 Proc::SyncExec - Spawn processes but report exec() errors
10  
11 =head1 SYNOPSIS
12  
13 # Normal-looking piped opens which properly report exec() errors in $!:
14 sync_open WRITER_FH, "|command -with args" or die $!;
15 sync_open READER_FH, "command -with args|" or die $!;
16  
17 # Synchronized fork/exec which reports exec errors in $!:
18 $pid = sync_exec $command, @arg;
19 $pid = sync_exec $code_ref, $cmd, @arg; # run code after fork in kid
20  
21 # fork() which retries if it fails, then croaks() if it still fails.
22 $pid = fork_retry;
23 $pid = fork_retry 100; # retry 100 times rather than 5
24 $pid = fork_retry 100, 2; # sleep 2 rather than 5 seconds between
25  
26 # A couple of interfaces similar to sync_open() but which let you
27 # avoid the shell:
28 $pid = sync_fhpopen_noshell READERFH, 'r', @command;
29 $pid = sync_fhpopen_noshell WRITERFH, 'w', @command;
30 $fh = sync_popen_noshell 'r', @command_which_outputs;
31 $fh = sync_popen_noshell 'w', @command_which_inputs;
32 ($fh, $pid) = sync_popen_noshell 'r', @command_which_outputs;
33 ($fh, $pid)= sync_popen_noshell 'w', @command_which_inputs;
34  
35 =head1 DESCRIPTION
36  
37 This module contains functions for synchronized process spawning with
38 full error return. If the child's exec() call fails the reason for the
39 failure is reported back to the parent.
40  
41 These functions will croak() if they encounter an unexpected system
42 error, such as a pipe() failure or a repeated fork() failure.
43  
44 Nothing is exported by default.
45  
46 =over
47  
48 =cut
49  
50 #';
51  
52 package Proc::SyncExec;
53  
54 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 7 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 7 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
59 7 ▶use Fcntl qw(F_SETFD);
Subroutine BEGIN: called CC=1 SCAR=0.0
60 7 ▶use POSIX qw(EINTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 7 ▶use Symbol qw(gensym qualify_to_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 7 $VERSION = '1.01';
64 7 @ISA = qw(Exporter);
65 7 @EXPORT_OK = qw(fork_retry sync_exec sync_fhpopen_noshell
66 sync_popen_noshell sync_open);
67  
68 =item B<fork_retry> [I<max-retries> [I<sleep-between>]]
69  
70 This function runs fork() until it succeeds or until I<max-retries>
71 (default 5) attempts have been made, sleeping I<sleep-between> seconds
72 (default 5) between attempts. If the last fork() fails B<fork_retry>
73 croak()s.
74  
75 =cut
76  
77 27 ▶sub fork_retry {
Subroutine fork_retry: called CC=8 SCAR=26.6
Pod documented
78 27 ▶ @_ > 2 and croak "Usage: fork_retry max_retries=5 sleep_between=5";
Branch: 1 branch
BranchTrueFalseTotal
@_ > 2 and croak("Usage: fork_retry max_retries=5 sleep_between=5") 0 27 27
79 27 my ($max_retries, $sleep) = @_;
80 27 my ($retries, $kid);
81  
82 27 ▶ $max_retries = 5 if !defined $max_retries or $max_retries < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max_retries or $max_retries < 0 27 0 27
Condition33%1/3
not defined $max_retries or $max_retries < 0
l!l&&r!l&&!r
2700
MC/DC0%0/2
not defined $max_retries or $max_retries < 0
not defined $max_retries $max_retries < 0
Truth table33%1/3
not(A) or B
A= not defined $max_retriesB= $max_retries < 0
ABresult
1X1
011
000
83 27 ▶ $sleep = 5 if !defined $sleep or $sleep < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $sleep or $sleep < 0 27 0 27
Condition33%1/3
not defined $sleep or $sleep < 0
l!l&&r!l&&!r
2700
MC/DC0%0/2
not defined $sleep or $sleep < 0
not defined $sleep $sleep < 0
Truth table33%1/3
not(A) or B
A= not defined $sleepB= $sleep < 0
ABresult
1X1
011
000
84  
85 27 $retries = 0;
86 27 ▶ while (!defined($kid = fork)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($kid = fork)) 0 27 27
87 0 ▶ croak "Can't fork: $!" if $retries++ >= $max_retries;
Branch: 1 branch
BranchTrueFalseTotal
if $retries++ >= $max_retries 0 0 0
88 0 sleep $sleep;
89 }
90 27 return $kid;
91 }
92  
93 =item B<sync_exec> [I<code>] I<command>...
94  
95 This function is similar to a fork()/exec() sequence but with a few
96 twists.
97  
98 B<sync_exec> does not return until after the fork()ed child has already
99 performed its exec(). The synchronization this provides is useful in
100 some unusual circumstances.
101  
102 Normally the pid of the child process is returned. However, if the
103 child fails its exec() B<sync_exec> returns undef and sets $! to the
104 reason for the child's exec() failure.
105  
106 Since the @cmd array is passed directly to Perl's exec() Perl might
107 choose to invoke the command via the shell if @cmd contains only one
108 element and it looks like it needs a shell to interpret it. If this
109 happens the return value of B<sync_exec> only indicates whether the
110 exec() of the shell worked.
111  
112 The optional initial I<code> argument must be a code reference. If it
113 is present it is run in the child just before exec() is called. You can
114 use this to set up redirections or whatever. If I<code> returns false
115 no exec is performed, instead a failure is returned using the current $!
116 value (or EINTR if $! is 0).
117  
118 If the fork() fails or if there is some other unexpected system error
119 B<sync_exec> croak()s rather than returning.
120  
121 =cut
122  
123 20 ▶sub sync_exec {
Subroutine sync_exec: called CC=20 SCAR=36.9
Pod documented
124 20 ▶ my $code = (@_ && ref $_[0] eq 'CODE') ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'CODE' ? : 5 15 20
Condition66%2/3
@_ and ref $_[0] eq "CODE"
!ll&&!rl&&r
0155
MC/DC50%1/2
@_ and ref $_[0] eq "CODE"
@_ ref $_[0] eq "CODE"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "CODE"
ABresult
0X0
100
111
125 20 ▶ @_ or croak 'Usage: sync_exec [code] cmd [arg]...';
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("Usage: sync_exec [code] cmd [arg]...") 20 0 20
126 20 my @cmd = @_;
127  
128 20 my ($reader, $writer) = (gensym, gensym);
129 20 ▶ pipe $reader, $writer
Branch: 1 branch
BranchTrueFalseTotal
pipe $reader, $writer or croak("Can't pipe(): $!") 20 0 20
130 or croak "Can't pipe(): $!";
131 20 my $pid = fork_retry;
132 20 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 5 15 20
133 5 my $ok = 1;
134 5 ▶ $ok = close $reader if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 5 0 5
135 5 ▶ $ok = fcntl $writer, F_SETFD, 1 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 5 0 5
136 5 ▶ $ok = &$code() if $ok && $code;
Branch: 1 branch
BranchTrueFalseTotal
if $ok and $code 2 3 5
Condition66%2/3
$ok and $code
!ll&&!rl&&r
032
MC/DC50%1/2
$ok and $code
$ok $code
Truth table66%2/3
A and B
A= $okB= $code
ABresult
0X0
100
111
137 5 $^W = 0; # turn off "Can't exec" message
138 5 ▶ if (!$ok or !exec @cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok and exec @cmd) 0 0 0
Condition33%1/3
$ok and exec @cmd
!ll&&!rl&&r
050
MC/DC0%0/2
$ok and exec @cmd
$ok exec @cmd
Truth table33%1/3
A and B
A= $okB= exec @cmd
ABresult
0X0
100
111
139 0 select $writer;
140 0 $| = 1;
141 0 print $!+0;
142 0 POSIX::_exit 1;
143 }
144 }
145 15 ▶ close $writer or croak "Error closing parent's write pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $writer or croak("Error closing parent's write pipe: $!") 15 0 15
146  
147 15 my ($nread, $buf);
148 15 while (1) {
149 15 $nread = sysread $reader, $buf, 16;
150 15 ▶ last if defined $nread;
Branch: 1 branch
BranchTrueFalseTotal
if defined $nread 15 0 15
151 0 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
152 0 croak "Error reading from pipe: $!";
153 }
154 15 ▶ close $reader or croak "Error closing parent's read pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $reader or croak("Error closing parent's read pipe: $!") 15 0 15
155 15 ▶ if ($nread) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nread) 11 4 15
156 11 ▶ while (waitpid($pid, 0) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (waitpid($pid, 0) == -1) 0 11 11
157 0 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
158 0 croak "Error waiting for child: $!";
159 }
160 11 $pid = undef;
161 11 ▶ $! = $buf+0 || EINTR;
Condition50%1/2
$buf + 0 || 4
l!l
110
MC/DC0%0/1
$buf + 0 || 4
$buf + 0
Truth table50%1/2
A
A= $buf + 0
Aresult
11
00
162 }
163 15 return $pid;
164 }
165  
166 =item B<sync_fhpopen_noshell> I<fh> I<type> I<cmd> [I<arg>]...
167  
168 This is a popen() but it never invokes the shell and it uses sync_exec()
169 under the covers. See L</sync_exec>.
170  
171 The I<type> is either C<'r'> to read from the process or C<'w'> to write
172 to it.
173  
174 The return value is the pid of the forked process.
175  
176 =cut
177  
178 5 ▶sub sync_fhpopen_noshell {
Subroutine sync_fhpopen_noshell: called CC=6 SCAR=18.6
Pod documented
179 5 ▶ @_ >= 3 or croak 'Usage: sync_fhpopen_noshell fh type cmd...';
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 3 or croak("Usage: sync_fhpopen_noshell fh type cmd...") 5 0 5
180 5 my $fh_parent = qualify_to_ref shift, caller;
181 5 my ($type, @cmd) = @_;
182 5 my ($fh_child, $fh_dup_to, $fh_dup_type, $result);
183  
184 5 $fh_child = gensym;
185 5 ▶ if ($type eq 'w') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'w') { } 2 3 5
elsif ($type eq 'r') { } 3 0 3
186 2 $result = pipe $fh_child, $fh_parent;
187 2 $fh_dup_to = \*STDIN;
188 2 $fh_dup_type = '<&';
189 }
190 elsif ($type eq 'r') {
191 3 $result = pipe $fh_parent, $fh_child;
192 3 $fh_dup_to = \*STDOUT;
193 3 $fh_dup_type = '>&';
194 }
195 else {
196 0 croak "Invalid popen type `$type'";
197 }
198  
199 5 ▶ $result or croak "Can't pipe(): $!";
Branch: 1 branch
BranchTrueFalseTotal
$result or croak("Can't pipe(): $!") 5 0 5
200 2 ▶ $result = sync_exec sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
201 2 ▶ close $fh_parent
Branch: 1 branch
BranchTrueFalseTotal
close $fh_parent and open $fh_dup_to, $fh_dup_type . fileno($fh_child) and close $fh_child 2 0 2
Condition33%1/3
close $fh_parent and open $fh_dup_to, $fh_dup_type . fileno($fh_child)
!ll&&!rl&&r
002
MC/DC0%0/2
close $fh_parent and open $fh_dup_to, $fh_dup_type . fileno($fh_child)
close $fh_parent open $fh_dup_to, $fh_dup_type . fileno($fh_child)
Truth table33%1/3
A and B
A= close $fh_parentB= open $fh_dup_to, $fh_dup_type . fileno($fh_child)
ABresult
0X0
100
111
202 and open $fh_dup_to, $fh_dup_type . fileno $fh_child
203 and close $fh_child
204 5 }, @cmd;
205 3 my $errno = $!;
206 3 ▶ close $fh_child
Branch: 1 branch
BranchTrueFalseTotal
close $fh_child or croak("Error closing parent pipe: $!") 3 0 3
207 or croak "Error closing parent pipe: $!";
208 3 $! = $errno;
209 3 return $result;
210 }
211  
212 =item B<sync_popen_noshell> I<type> I<cmd> I<arg>...
213  
214 This is like B<sync_fhpopen_noshell>, but you don't have to supply
215 the filehandle.
216  
217 If called in an array context the return value is a list consisting of
218 the filehandle and the PID of the child. In a scalar context only the
219 filehandle is returned.
220  
221 =cut
222  
223 #'
224  
225 0 ▶sub sync_popen_noshell {
Subroutine sync_popen_noshell: not called CC=4 SCAR=30.0
Pod documented
226 0 ▶ @_ >= 2 or croak 'Usage: sync_popen_noshell type cmd...';
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 or croak("Usage: sync_popen_noshell type cmd...") 0 0 0
227 0 my ($type, @cmd) = @_;
228 0 my $fh = gensym;
229 0 ▶ my $pid = sync_fhpopen_noshell $fh, $type, @cmd
Branch: 1 branch
BranchTrueFalseTotal
my $pid = sync_fhpopen_noshell($fh, $type, @cmd) or return 0 0 0
230 or return;
231 0 ▶ wantarray ? ($fh, $pid) : $fh;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
232 }
233  
234 =item B<sync_open> I<fh> [I<open-spec>]
235  
236 This is like a Perl open() except that if a pipe is involved and the
237 implied exec() fails sync_open() fails with $! set appropriately. See
238 L</sync_exec>.
239  
240 Like B<sync_exec>, B<sync_open> croak()s if there is an unexpected
241 system error (such as a failed pipe()).
242  
243 Also like B<sync_exec>, if you use a command which Perl needs to use the
244 shell to interpret you'll only know if the exec of the shell worked.
245 Use B<sync_fhpopen_noshell> or B<sync_exec> to be sure that this doesn't
246 happen.
247  
248 =cut
249  
250 5 ▶sub sync_open {
Subroutine sync_open: called CC=7 SCAR=21.3
Pod documented
251 5 ▶ @_ == 1 or @_ == 2 or croak 'Usage: sync_open fh [open-spec]';
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or croak("Usage: sync_open fh [open-spec]") 5 0 5
Condition33%1/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
050
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table33%1/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
252 5 my $fh = qualify_to_ref shift, caller;
253 5 ▶ my $cmd = @_ ? shift : $fh;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 5 0 5
254 5 my $type;
255  
256 5 $cmd =~ s/^\s+//;
257 5 $cmd =~ s/\s+$//;
258 5 ▶ if ($cmd =~ s/^\|//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd =~ s/^\|//) { } 2 3 5
elsif ($cmd =~ s/\|$//) { } 3 0 3
259 2 ▶ if (substr($cmd, -1) eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($cmd, -1) eq "|") 0 2 2
260 0 croak "Can't do bidirectional pipe";
261 }
262 2 $type = 'w';
263 }
264 elsif ($cmd =~ s/\|$//) {
265 3 $type = 'r';
266 }
267 else {
268 # Not a pipe, just do a regular open.
269 0 return open $fh, $cmd;
270 }
271 5 return sync_fhpopen_noshell $fh, $type, $cmd;
272 }
273  
274  
275 7 1
276  
277 __END__
↑ Summary