blib/lib/AnyEvent/Run.pm

filter: Statementstmt 79.7% 75 / 94 Branchbran 52.7% 19 / 36 Conditioncond 75.0% 9 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 72.6% 117 / 161 CC 24 SCAR 32.2
CC
24
Cov
73%
new
39.4
_set_priority
3.2
DESTROY
2.0
CRAP 25.0
↑ Summary
1 package AnyEvent::Run;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use base 'AnyEvent::Handle';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use AnyEvent::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 our $VERSION = 0.01;
12  
13 8 ▶our $FD_MAX = eval { POSIX::sysconf(&POSIX::_SC_OPEN_MAX) - 1 } || 1023;
Condition50%1/2
eval { do { POSIX::sysconf(&POSIX::_SC_OPEN_MAX) - 1 } } || 1023
l!l
80
MC/DC0%0/1
eval { do { POSIX::sysconf(&POSIX::_SC_OPEN_MAX) - 1 } } || 1023
eval { do { POSIX::sysconf(&POSIX::_SC_OPEN_MAX) - 1 } }
Truth table50%1/2
A
A= eval { do { POSIX::sysconf(&POSIX::_SC_OPEN_MAX) - 1 } }
Aresult
11
00
14  
15 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 if ( AnyEvent::WIN32 ) {
17 eval { require Win32 };
18 die "Win32 failed to load:\n$@" if $@;
19
20 eval { require Win32::Console };
21 die "Win32::Console failed to load:\n$@" if $@;
22 Win32::Console->import();
23
24 eval { require Win32API::File };
25 die "Win32API::File failed to load:\n$@" if $@;
26 Win32API::File->import('FdGetOsFHandle');
27
28 eval { require Win32::Job };
29 die "Win32::Job failed to load:\n$@" if $@;
30 }
31 };
32  
33 30 ▶sub new {
Subroutine new: called CC=21 SCAR=36.7
Pod documented
34 30 my ( $class, %args ) = @_;
35
36 30 my $cls = $args{class};
37 30 my $cmd = $args{cmd};
38
39 30 ▶ unless ( $cls || $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cls or $cmd) 0 30 30
Condition66%2/3
$cls or $cmd
l!l&&r!l&&!r
5250
MC/DC0%0/2
$cls or $cmd
$cls $cmd
Truth table66%2/3
A or B
A= $clsB= $cmd
ABresult
1X1
011
000
40 0 croak "mandatory argument cmd or class is missing";
41 }
42
43 30 ▶ if ( $cls ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cls) 5 25 30
44 5 ▶ my $method = $args{method} || 'main';
Condition100%2/2
$args{'method'} || 'main'
l!l
23
MC/DC100%1/1
$args{'method'} || 'main'
$args{'method'}
Truth table100%2/2
A
A= $args{'method'}
Aresult
11
00
45 # double quotes around -e needed on Windows for some reason
46 5 $cmd = "$^X -M$cls -I" . join( ' -I', @INC ) . " -e \"${cls}::${method}()\"";
47 }
48
49 30 ▶ $args{args} ||= [];
Condition100%2/2
$args{"args"} ||= []
l!l
525
MC/DC100%1/1
$args{"args"} ||= []
$args{"args"}
Truth table100%2/2
A
A= $args{"args"}
Aresult
11
00
50
51 30 ▶ my ($parent, $child) = AnyEvent::Util::portable_socketpair
Branch: 1 branch
BranchTrueFalseTotal
() = &AnyEvent::Util::portable_socketpair() or croak("unable to create AnyEvent::Run socketpair: $!") 30 0 30
52 or croak "unable to create AnyEvent::Run socketpair: $!";
53
54 30 $args{fh} = $child;
55
56 30 my $self = $class->SUPER::new(%args);
57  
58 30 my $pid = fork;
59
60 30 ▶ if ( $pid == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 6 24 30
61 # child
62
63 6 close $child;
64
65 # Stdio should not be tied.
66 6 ▶ if (tied *STDOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied *STDOUT) 0 6 6
67 0 carp "Cannot redirect into tied STDOUT. Untying it";
68 0 untie *STDOUT;
69 }
70 6 ▶ if (tied *STDERR) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied *STDERR) 0 6 6
71 0 carp "Cannot redirect into tied STDERR. Untying it";
72 0 untie *STDERR;
73 }
74
75 # Set priority if requested
76 6 ▶ if ( $args{priority} && $args{priority} =~ /^-?\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"priority"} and $args{"priority"} =~ /^-?\d+$/) 2 4 6
Condition66%2/3
$args{"priority"} and $args{"priority"} =~ /^-?\d+$/
!ll&&!rl&&r
402
MC/DC50%1/2
$args{"priority"} and $args{"priority"} =~ /^-?\d+$/
$args{"priority"} $args{"priority"} =~ /^-?\d+$/
Truth table66%2/3
A and B
A= $args{"priority"}B= $args{"priority"} =~ /^-?\d+$/
ABresult
0X0
100
111
77 2 $self->_set_priority();
78 }
79
80 # Redirect STDIN from the read end of the stdin pipe.
81 6 close STDIN if AnyEvent::WIN32;
82 6 ▶ open STDIN, "<&" . fileno($parent)
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, '<&' . fileno($parent) or croak("can't redirect STDIN in child pid ${$}: $!") 6 0 6
83 or croak "can't redirect STDIN in child pid $$: $!";
84  
85 # Redirect STDOUT
86 6 close STDOUT if AnyEvent::WIN32;
87 6 ▶ open STDOUT, ">&" . fileno($parent)
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>&' . fileno($parent) or croak("can't redirect stdout in child pid ${$}: $!") 6 0 6
88 or croak "can't redirect stdout in child pid $$: $!";
89  
90 # Redirect STDERR
91 6 close STDERR if AnyEvent::WIN32;
92 6 ▶ open STDERR, ">&" . fileno($parent)
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, '>&' . fileno($parent) or die "can't redirect stderr in child: $!" 6 0 6
93 or die "can't redirect stderr in child: $!";
94
95 # Make STDOUT and STDERR auto-flush.
96 6 select STDERR; $| = 1;
97 6 select STDOUT; $| = 1;
98
99 6 if ( AnyEvent::WIN32 ) {
100 # The Win32 pseudo fork sets up the std handles in the child
101 # based on the true win32 handles For the exec these get
102 # remembered, so manipulation of STDIN/OUT/ERR is not enough.
103 # Only necessary for the exec, as Perl CODE subroutine goes
104 # through 0/1/2 which are correct. But of course that coderef
105 # might invoke exec, so better do it regardless.
106 # HACK: Using Win32::Console as nothing else exposes SetStdHandle
107 Win32::Console::_SetStdHandle(
108 STD_INPUT_HANDLE(),
109 FdGetOsFHandle(fileno($parent))
110 );
111 Win32::Console::_SetStdHandle(
112 STD_OUTPUT_HANDLE(),
113 FdGetOsFHandle(fileno($parent))
114 );
115 Win32::Console::_SetStdHandle(
116 STD_ERROR_HANDLE(),
117 FdGetOsFHandle(fileno($parent))
118 );
119 }
120
121 6 ▶ if ( ref $cmd eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd eq "CODE") 0 6 6
122 0 unless ( AnyEvent::WIN32 ) {
123 0 my @fd_keep = (
124 fileno(STDIN),
125 fileno(STDOUT),
126 fileno(STDERR),
127 fileno($parent),
128 );
129
130 0 for my $fd ( 0..$FD_MAX ) {
131 0 ▶ next if grep { $_ == $fd } @fd_keep;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $fd;} @fd_keep 0 0 0
132 0 POSIX::close($fd);
133 }
134 }
135
136 0 $cmd->( @{$args{args}} );
137
138 0 close $parent;
139
140 0 if ( AnyEvent::WIN32 ) {
141 sleep 10; # give parent a chance to kill us
142 exit 1;
143 }
144 else {
145 0 POSIX::_exit(0);
146 }
147 }
148
149 6 if ( AnyEvent::WIN32 ) {
150 my $exitcode = 0;
151
152 # XXX: should close open fd's, but it doesn't seem to work right on win32
153  
154 my ($appname, $cmdline);
155  
156 if ( ref $cmd eq 'ARRAY' ) {
157 $appname = $cmd->[0];
158 $cmdline = join(' ', map { /\s/ && ! /"/ ? qq{"$_"} : $_ } (@{$cmd}, @{$args{args}}) );
159 }
160 else {
161 $appname = undef;
162 $cmdline = join(' ', $cmd, map { /\s/ && ! /"/ ? qq{"$_"} : $_ } @{$args{args}} );
163 }
164  
165 my $w32job;
166  
167 unless ( $w32job = Win32::Job->new() ) {
168 die Win32::FormatMessage( Win32::GetLastError() );
169 }
170  
171 my $w32pid;
172  
173 unless ( $w32pid = $w32job->spawn( $appname, $cmdline ) ) {
174 die Win32::FormatMessage( Win32::GetLastError() );
175 }
176 else {
177 0 ▶ my $ok = $w32job->watch( sub { 0 }, 60 );
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 my $hashref = $w32job->status();
179 $exitcode = $hashref->{$w32pid}->{exitcode};
180 }
181  
182 close $parent;
183
184 sleep 10; # give parent a chance to kill us
185 exit($exitcode);
186 }
187
188 6 ▶ if ( ref $cmd eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd eq 'ARRAY') { } 3 3 6
189 3 exec( @{$cmd}, @{$args{args}} )
190 0 ▶ or die "can't exec (" . @{$cmd} . ") in child pid $$: $!";
Branch: 1 branch
BranchTrueFalseTotal
exec @{$cmd;}, @{$args{"args"};} or die q[can't exec (] . @{$cmd;} . ") in child pid ${$}: $!" 0 0 0
191 }
192 else {
193 3 ▶ exec( join(" ", $cmd, @{$args{args}} ) )
Branch: 1 branch
BranchTrueFalseTotal
exec join(" ", $cmd, @{$args{"args"};}) or die "can't exec ($cmd) in child pid ${$}: $!" 0 0 0
194 or die "can't exec ($cmd) in child pid $$: $!";
195 }
196
197 # end of child
198 }
199
200 # parent
201 24 close $parent;
202
203 24 $self->{child_pid} = $pid;
204
205 24 return $self;
206 }
207  
208 2 ▶sub _set_priority {
Subroutine _set_priority: called CC=3 SCAR=11.6
209 2 my $self = shift;
210
211 2 my $pri = $self->{priority};
212
213 2 if ( AnyEvent::WIN32 ) {
214 eval { require Win32::API };
215 die "Win32::API failed to load:\n$@" if $@;
216
217 eval { require Win32::Process };
218 die "Win32::Process failed to load:\n$@" if $@;
219
220 # ABOVE_NORMAL_PRIORITY_CLASS and BELOW_NORMAL_PRIORITY_CLASS aren't
221 # provided by Win32::Process so their values have been hardcoded.
222 $pri = $pri <= -16 ? Win32::Process::HIGH_PRIORITY_CLASS()
223 : $pri <= -6 ? 0x00008000 # ABOVE_NORMAL
224 : $pri <= 4 ? Win32::Process::NORMAL_PRIORITY_CLASS()
225 : $pri <= 14 ? 0x00004000 # BELOW_NORMAL
226 : Win32::Process::IDLE_PRIORITY_CLASS();
227
228 my $getCurrentProcess = Win32::API->new('kernel32', 'GetCurrentProcess', ['V'], 'N');
229 my $setPriorityClass = Win32::API->new('kernel32', 'SetPriorityClass', ['N', 'N'], 'N');
230
231 my $processHandle = eval { $getCurrentProcess->Call(0) };
232
233 if ( !$processHandle || $@ ) {
234 carp "Can't get process handle ($^E) [$@]";
235 return;
236 }
237
238 eval { $setPriorityClass->Call($processHandle, $pri) };
239
240 if ( $@ ) {
241 carp "Couldn't set priority to $pri ($^E) [$@]";
242 }
243 }
244 else {
245 2 eval {
246 2 ▶ unless ( setpriority( 0, $$, $pri ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (setpriority 0, $$, $pri) 0 2 2
247 0 die "unable to set child priority to $pri\n";
248 }
249 };
250 2 ▶ carp $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
251 }
252 }
253  
254 24 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
255 24 my $self = shift;
256
257 # XXX: doesn't play nice with linger option, so clear wbuf
258 24 $self->{wbuf} = '';
259
260 24 $self->SUPER::DESTROY(@_);
261
262 24 ▶ if ( $self->{child_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"child_pid"}) 24 0 24
263 24 kill 9 => $self->{child_pid};
264 24 waitpid $self->{child_pid}, 0;
265 }
266 }
267  
268 8 1;
269 __END__
↑ Summary