blib/lib/Proc/Background/Unix.pm

filter: Statementstmt 82.5% 99 / 120 Branchbran 66.6% 56 / 84 Conditioncond 54.2% 19 / 35 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 64.2% 9 / 14 Podpod n/a totaltotal 68.2% 185 / 271 CC 53 SCAR 37.3
CC
53
Cov
73%
_start
72.9
_terminate
9.9
_waitpid
8.6
CRAP 41.5
« blib/lib/Proc/Background/Win32.pm ↑ Summary blib/lib/Proc/Background.pm »
1 package Proc::Background::Unix;
2 28 $Proc::Background::Unix::VERSION = '1.32';
3 # ABSTRACT: Unix-specific implementation of process create/wait/kill
4 28 require 5.004_04;
5  
6 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use POSIX qw( :errno_h :sys_wait_h );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Test for existence of FD_CLOEXEC, needed for child-error-through-pipe trick
12 28 my ($FD_CLOEXEC);
13 28 eval {
14 28 require Fcntl;
15 28 $FD_CLOEXEC= Fcntl::FD_CLOEXEC();
16 };
17  
18 # For un-explained mysterious reasons, Time::HiRes::alarm seem to misbehave on 5.10 and earlier
19 # but core alarm works fine.
20 28 my $alarm= ($] >= 5.012)? do { require Time::HiRes; \&Time::HiRes::alarm; }
21 0 ▶ : sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 # round up to whole seconds
23 0 CORE::alarm(POSIX::ceil($_[0]));
24 28 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.012 ? : 28 0 28
25  
26 28 @Proc::Background::Unix::ISA = qw(Exporter);
27  
28 # Start the background process. If it is started sucessfully, then record
29 # the process id in $self->{_os_obj}.
30 114 ▶sub _start {
Subroutine _start: called CC=35 SCAR=42.9
31 114 my ($self, $options)= @_;
32  
33 # There are three main scenarios for how-to-exec:
34 # * single-string command, to be handled by shell
35 # * arrayref command, to be handled by execve
36 # * arrayref command with 'exe' (fake argv0)
37 # and one that isn't logical:
38 # * single-string command with exe
39 # throw an error for that last one rather than trying something awkward
40 # like splitting the command string.
41  
42 114 my @argv;
43 114 my ($cmd, $exe)= @{$self}{'_command','_exe'};
44  
45 114 ▶ if (ref $cmd eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cmd eq 'ARRAY') { } 103 11 114
elsif (defined $exe) { } 0 11 11
46 103 @argv= @$cmd;
47 103 ▶ ($exe, my $err) = Proc::Background::_resolve_path(defined $exe? $exe : $argv[0]);
Branch: 1 branch
BranchTrueFalseTotal
defined $exe ? : 0 103 103
48 103 ▶ return $self->_fatal($err) unless defined $exe;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exe 2 101 103
49 101 $self->{_exe}= $exe;
50 } elsif (defined $exe) {
51 0 croak "Can't combine 'exe' option with single-string 'command', use arrayref 'command' instead.";
52 }
53  
54 112 ▶ if (defined $options->{cwd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"cwd"}) 7 105 112
55 -d $options->{cwd}
56 7 ▶ or return $self->_fatal("directory does not exist: '$options->{cwd}'");
Branch: 1 branch
BranchTrueFalseTotal
-d $options->{"cwd"} or return $self->_fatal(qq[directory does not exist: '$options->{"cwd"}']) 5 2 7
57 }
58  
59 110 my ($new_stdin, $new_stdout, $new_stderr);
60 $new_stdin= _resolve_file_handle($options->{stdin}, '<', \*STDIN)
61 110 ▶ if exists $options->{stdin};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"stdin"} 9 101 110
62 $new_stdout= _resolve_file_handle($options->{stdout}, '>>', \*STDOUT)
63 110 ▶ if exists $options->{stdout};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"stdout"} 14 96 110
64 $new_stderr= _resolve_file_handle($options->{stderr}, '>>', \*STDERR)
65 110 ▶ if exists $options->{stderr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"stderr"} 9 101 110
66  
67 # Fork a child process.
68 110 my ($pipe_r, $pipe_w);
69 110 ▶ if (defined $FD_CLOEXEC) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $FD_CLOEXEC) 110 0 110
70 # use a pipe for the child to report exec() errors
71 110 ▶ pipe $pipe_r, $pipe_w or return $self->_fatal("pipe: $!");
Branch: 1 branch
BranchTrueFalseTotal
pipe $pipe_r, $pipe_w or return $self->_fatal("pipe: $!") 110 0 110
72 # This pipe needs to be in the non-preserved range that doesn't exist after exec().
73 # In the edge case where a pipe received a FD less than $^F, the CLOEXEC flag isn't set.
74 # Try again on higher descriptors, then close the lower ones.
75 110 my @rejects;
76 110 ▶ while (fileno $pipe_r <= $^F or fileno $pipe_w <= $^F) {
Branch: 1 branch
BranchTrueFalseTotal
if (fileno $pipe_r <= $^F or fileno $pipe_w <= $^F) 0 110 110
Condition33%1/3
fileno $pipe_r <= $^F or fileno $pipe_w <= $^F
l!l&&r!l&&!r
00110
MC/DC0%0/2
fileno $pipe_r <= $^F or fileno $pipe_w <= $^F
fileno $pipe_r <= $^F fileno $pipe_w <= $^F
Truth table33%1/3
A or B
A= fileno $pipe_r <= $^FB= fileno $pipe_w <= $^F
ABresult
1X1
011
000
77 0 push @rejects, $pipe_r, $pipe_w;
78 0 ▶ pipe $pipe_r, $pipe_w or return $self->_fatal("pipe: $!");
Branch: 1 branch
BranchTrueFalseTotal
pipe $pipe_r, $pipe_w or return $self->_fatal("pipe: $!") 0 0 0
79 }
80 }
81 110 my $pid;
82 {
83 110 ▶ if ($pid = fork()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($pid = fork) { } 90 20 110
elsif (defined $pid) { } 20 0 20
elsif ($! == 11) { } 0 0 0
84 # parent
85 90 $self->{_os_obj} = $pid;
86 90 $self->{_pid} = $pid;
87 90 ▶ if (defined $pipe_r) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pipe_r) 90 0 90
88 90 close $pipe_w;
89 # wait for child to reply or close the pipe
90 90 ▶ local $SIG{PIPE}= sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
91 90 my $msg= '';
92 90 ▶ while (0 < read $pipe_r, $msg, 1024, length $msg) {}
Branch: 1 branch
BranchTrueFalseTotal
if (0 < read($pipe_r, $msg, 1024, length $msg)) 0 90 90
93 90 close $pipe_r;
94 # If child wrote anything to the pipe, it failed to exec.
95 # Reap it before dying.
96 90 ▶ if (length $msg) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $msg) 0 90 90
97 0 waitpid $pid, 0;
98 0 return $self->_fatal($msg);
99 }
100 }
101 90 last;
102 } elsif (defined $pid) {
103 # child
104 # Make absolutely sure nothing in this block interacts with the rest of the
105 # process state, and that flow control never skips the _exit().
106 0 ▶ $SIG{$_}= sub{die;} for qw( INT HUP QUIT TERM ); # clear custom signal handlers
Subroutine __ANON__: not called CC=1 SCAR=1.2
107 20 $SIG{$_}= 'DEFAULT' for qw( __WARN__ __DIE__ );
108 20 eval {
109 20 eval {
110 chdir($options->{cwd}) or die "chdir($options->{cwd}): $!\n"
111 20 ▶ if defined $options->{cwd};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"cwd"} 2 18 20
Condition60%3/5
defined $options->{'cwd'} && (chdir $options->{'cwd'} || die("chdir($options->{'cwd'}): $!\n"))
!ll
1820
chdir $options->{"cwd"} or die qq[chdir($options->{"cwd"}): $!\n]
l!l
20
MC/DC0%0/2
defined $options->{'cwd'} && (chdir $options->{'cwd'} || die("chdir($options->{'cwd'}): $!\n"))
defined $options->{'cwd'} chdir $options->{"cwd"}
Truth table33%1/3
A && B
A= defined $options->{'cwd'}B= chdir $options->{"cwd"}
ABresult
0X0
100
111
112  
113 20 ▶ open STDIN, '<&'.fileno($new_stdin) or die "Can't redirect STDIN: $!\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_stdin 3 17 20
Condition60%3/5
defined $new_stdin && (open(STDIN, '<&' . fileno($new_stdin)) || die("Can't redirect STDIN: $!\n"))
!ll
1730
open STDIN, '<&' . fileno($new_stdin) or die "Can't redirect STDIN: $!\n"
l!l
30
MC/DC0%0/2
defined $new_stdin && (open(STDIN, '<&' . fileno($new_stdin)) || die("Can't redirect STDIN: $!\n"))
defined $new_stdin open STDIN, '<&' . fileno($new_stdin)
Truth table33%1/3
A && B
A= defined $new_stdinB= open STDIN, '<&' . fileno($new_stdin)
ABresult
0X0
100
111
114 if defined $new_stdin;
115 20 ▶ open STDOUT, '>&'.fileno($new_stdout) or die "Can't redirect STDOUT: $!\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_stdout 4 16 20
Condition60%3/5
defined $new_stdout && (open(STDOUT, '>&' . fileno($new_stdout)) || die("Can't redirect STDOUT: $!\n"))
!ll
1640
open STDOUT, '>&' . fileno($new_stdout) or die "Can't redirect STDOUT: $!\n"
l!l
40
MC/DC0%0/2
defined $new_stdout && (open(STDOUT, '>&' . fileno($new_stdout)) || die("Can't redirect STDOUT: $!\n"))
defined $new_stdout open STDOUT, '>&' . fileno($new_stdout)
Truth table33%1/3
A && B
A= defined $new_stdoutB= open STDOUT, '>&' . fileno($new_stdout)
ABresult
0X0
100
111
116 if defined $new_stdout;
117 20 ▶ open STDERR, '>&'.fileno($new_stderr) or die "Can't redirect STDERR: $!\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_stderr 3 17 20
Condition60%3/5
defined $new_stderr && (open(STDERR, '>&' . fileno($new_stderr)) || die("Can't redirect STDERR: $!\n"))
!ll
1730
open STDERR, '>&' . fileno($new_stderr) or die "Can't redirect STDERR: $!\n"
l!l
30
MC/DC0%0/2
defined $new_stderr && (open(STDERR, '>&' . fileno($new_stderr)) || die("Can't redirect STDERR: $!\n"))
defined $new_stderr open STDERR, '>&' . fileno($new_stderr)
Truth table33%1/3
A && B
A= defined $new_stderrB= open STDERR, '>&' . fileno($new_stderr)
ABresult
0X0
100
111
118 if defined $new_stderr;
119  
120 20 ▶ if (defined $exe) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exe) { } 16 4 20
121 16 ▶ exec { $exe } @argv or die "$0: exec failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
exec {$exe;} @argv or die "$0: exec failed: $!\n" 0 0 0
122 } else {
123 4 ▶ exec $cmd or die "$0: exec failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
exec $cmd or die "$0: exec failed: $!\n" 0 0 0
124 }
125 };
126 0 ▶ if (defined $pipe_w) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pipe_w) { } 0 0 0
127 0 print $pipe_w $@;
128 0 close $pipe_w; # force it to flush. Nothing else needs closed because we are about to _exit
129 } else {
130 0 print STDERR $@;
131 }
132 };
133 0 POSIX::_exit(1);
134 } elsif ($! == EAGAIN) {
135 0 sleep 5;
136 0 redo;
137 } else {
138 0 return $self->_fatal("fork: $!");
139 }
140 }
141  
142 90 $self;
143 }
144  
145 32 ▶sub _resolve_file_handle {
Subroutine _resolve_file_handle: called CC=6 SCAR=18.0
146 32 my ($thing, $mode, $default)= @_;
147 32 ▶ if (!defined $thing) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $thing) { } 11 21 32
elsif (ref $thing) { } 19 2 21
148 11 ▶ open my $fh, $mode, '/dev/null' or croak "open(/dev/null): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, "/dev/null" or croak("open(/dev/null): $!") 11 0 11
149 11 return $fh;
150 } elsif (ref $thing) {
151 # use 'undef' to mean no-change
152 19 ▶ return (fileno($thing) == fileno($default))? undef : $thing;
Branch: 1 branch
BranchTrueFalseTotal
fileno $thing == fileno $default ? : 2 17 19
153 } else {
154 2 ▶ open my $fh, $mode, $thing or croak "open($thing): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $mode, $thing or croak("open($thing): $!") 2 0 2
155 2 return $fh;
156 }
157 }
158  
159 # Wait for the child.
160 # (0, exit_value) : sucessfully waited on.
161 # (1, undef) : process already reaped and exit value lost.
162 # (2, undef) : process still running.
163 215 ▶sub _waitpid {
Subroutine _waitpid: called CC=8 SCAR=21.5
164 215 my ($self, $blocking, $wait_seconds) = @_;
165  
166 {
167 # Try to wait on the process.
168 # Implement the optional timeout with the 'alarm' call.
169 215 my $result= 0;
170 215 ▶ if ($blocking && $wait_seconds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocking and $wait_seconds) { } 62 153 215
Condition100%3/3
$blocking and $wait_seconds
!ll&&!rl&&r
1302362
MC/DC100%2/2
$blocking and $wait_seconds
$blocking $wait_seconds
Truth table100%3/3
A and B
A= $blockingB= $wait_seconds
ABresult
0X0
100
111
171 62 ▶ local $SIG{ALRM}= sub { die "alarm\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
172 62 $alarm->($wait_seconds);
173 62 eval { $result= waitpid($self->{_os_obj}, 0); };
174 62 $alarm->(0);
175 }
176 else {
177 153 ▶ $result= waitpid($self->{_os_obj}, $blocking? 0 : WNOHANG);
Branch: 1 branch
BranchTrueFalseTotal
$blocking ? : 23 130 153
178 }
179  
180 # Process finished. Grab the exit value.
181 215 ▶ if ($result == $self->{_os_obj}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($result == $self->{'_os_obj'}) { } 90 125 215
elsif ($result == -1 and $! == 10) { } 0 125 125
elsif ($result == 0) { } 125 0 125
182 90 delete $self->{_suspended};
183 90 return (0, $?);
184 }
185 # Process already reaped. We don't know the exist status.
186 ▶ elsif ($result == -1 and $! == ECHILD) {
Condition33%1/3
$result == -1 and $! == 10
!ll&&!rl&&r
12500
MC/DC0%0/2
$result == -1 and $! == 10
$result == -1 $! == 10
Truth table33%1/3
A and B
A= $result == -1B= $! == 10
ABresult
0X0
100
111
187 0 return (1, 0);
188 }
189 # Process still running.
190 elsif ($result == 0) {
191 125 return (2, 0);
192 }
193 # If we reach here, then waitpid caught a signal, so let's retry it.
194 0 redo;
195 }
196 0 return 0;
197 }
198  
199 0 ▶sub _suspend {
Subroutine _suspend: not called CC=1 SCAR=6.9
200 0 kill STOP => $_[0]->{_os_obj};
201 }
202  
203 0 ▶sub _resume {
Subroutine _resume: not called CC=1 SCAR=6.9
204 0 kill CONT => $_[0]->{_os_obj};
205 }
206  
207 28 ▶sub _terminate {
Subroutine _terminate: called CC=7 SCAR=23.0
208 28 my $self = shift;
209 0 ▶ my @kill_sequence= @_ && ref $_[0] eq 'ARRAY'? @{ $_[0] } : qw( TERM 2 TERM 8 KILL 3 KILL 7 );
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'ARRAY' ? : 0 28 28
Condition33%1/3
@_ and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
2800
MC/DC0%0/2
@_ and ref $_[0] eq "ARRAY"
@_ ref $_[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @_B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
210 # Try to kill the process with different signals. Calling alive() will
211 # collect the exit status of the program.
212 28 ▶ while (@kill_sequence and $self->alive) {
Branch: 1 branch
BranchTrueFalseTotal
if (@kill_sequence and $self->alive) 28 0 28
Condition33%1/3
@kill_sequence and $self->alive
!ll&&!rl&&r
0028
MC/DC0%0/2
@kill_sequence and $self->alive
@kill_sequence $self->alive
Truth table33%1/3
A and B
A= @kill_sequenceB= $self->alive
ABresult
0X0
100
111
213 28 my $sig= shift @kill_sequence;
214 28 my $delay= shift @kill_sequence;
215 28 kill($sig, $self->{_os_obj});
216 28 ▶ next unless defined $delay;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delay 0 28 28
217 28 ▶ last if $self->_reap(1, $delay); # block before sending next signal
Branch: 1 branch
BranchTrueFalseTotal
if $self->_reap(1, $delay) 28 0 28
218 }
219 }
220  
221 28 1;
222  
223 __END__
« blib/lib/Proc/Background/Win32.pm ↑ Summary blib/lib/Proc/Background.pm »