blib/lib/POE/Component/Daemon.pm

filter: Statementstmt 56.0% 366 / 653 Branchbran 38.1% 132 / 346 Conditioncond 31.3% 32 / 102 MC/DCmcdc 14.5% 9 / 62 Subroutinesub 85.0% 51 / 60 Podpod 29.2% 12 / 41 totaltotal 47.6% 602 / 1264 CC 222 SCAR 52.8
CC
222
Cov
48%
babysit
992.0
peek
707.4
rogues
182.0
CRAP 195.5
↑ Summary blib/lib/POE/Component/Daemon/ScoreboardSemaphore.pm »
1 #$Id: Daemon.pm 911 2012-07-29 13:52:13Z monaco $
2 ########################################################
3 package POE::Component::Daemon;
4  
5 13 ▶use 5.00405;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use POSIX qw(EAGAIN ECHILD SIGINT SIGKILL SIGTERM);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use File::Copy qw( move );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 ▶use POE::Component::Daemon::Scoreboard;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 13 $VERSION = '0.1301';
21  
22 sub DEBUG () { 0 }
23 sub DEBUG_SC () { DEBUG or 0 }
24  
25 13 our $NO_PEEK;
26 13 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.4
27 0 ▶ eval 'use POE::API::Peek;';
Subroutine BEGIN: called CC=1 SCAR=1.2
28 13 ▶ $NO_PEEK = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 13 0 13
29 # warn $NO_PEEK;
30 }
31  
32 ########################################################
33 sub new
34 14 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
35 14 my($package)=shift;
36 14 my $param;
37 14 ▶ if(1==@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @_) { } 12 2 14
38 12 $param=shift;
39 }
40 else {
41 2 $param={@_};
42 }
43  
44 14 my $self=bless $param, $package;
45  
46 14 $self->{package}=$package;
47 14 ▶ unless( $self->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"alias"}) 2 12 14
48 2 $self->{alias} = $package;
49 2 $self->{alias} =~ s/\W+/-/g;
50 }
51  
52 14 return $self;
53 }
54  
55 ########################################################
56 sub spawn
57 11 ▶{
Subroutine spawn: called CC=4 SCAR=16.8
Pod documented
58 11 my($self)=shift;
59 11 my $param={@_};
60  
61 11 ▶ unless(ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 11 11
62 11 $self = $self->new($param);
63 }
64 else {
65 0 ▶ while(my($k, $v)=each %$param) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$param) 0 0 0
66 0 ▶ $self->{$k}=$v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
67 }
68 }
69  
70 11 $self->open_logfile;
71 11 $self->drop_privs;
72 11 $self->detach;
73  
74 11 return $self->create_session;
75 }
76  
77 ########################################################
78 sub drop_privs
79 11 ▶{
Subroutine drop_privs: called CC=4 SCAR=17.1
Pod undocumented
80 11 my($self)=@_;
81 11 ▶ return if $self->{"drop_privs done"}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"drop_privs done"}++ 0 11 11
82 11 ▶ return unless $self->{UID} or $self->{GID};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"UID"} or $self->{"GID"} 11 0 11
Condition33%1/3
$self->{"UID"} or $self->{"GID"}
l!l&&r!l&&!r
0011
MC/DC0%0/2
$self->{"UID"} or $self->{"GID"}
$self->{"UID"} $self->{"GID"}
Truth table33%1/3
A or B
A= $self->{"UID"}B= $self->{"GID"}
ABresult
1X1
011
000
83 0 warn ref($self), "->drop_prives TODO";
84 }
85  
86  
87 ########################################################
88 sub open_logfile
89 22 ▶{
Subroutine open_logfile: called CC=5 SCAR=16.6
Pod undocumented
90 22 my($self)=@_;
91 22 my $logfile = $self->logfile_name;
92 22 ▶ return unless $logfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $logfile 0 22 22
93 22 ▶ return if $self->{"logfile done"}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"logfile done"}++ 0 22 22
94 22 DEBUG and warn "Log file is $logfile\n";
95 22 ▶ open STDOUT, ">>$logfile" or die "Unable to write to $logfile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">>$logfile" or die "Unable to write to ${logfile}: $!\n" 22 0 22
96 22 ▶ open STDERR, ">&STDOUT" or die "Unable to reopen STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or die "Unable to reopen STDERR: $!\n" 22 0 22
97 22 STDERR->autoflush(1);
98 22 STDOUT->autoflush(1);
99 22 return 1;
100 }
101  
102 sub logfile_name
103 22 ▶{
Subroutine logfile_name: called CC=4 SCAR=15.9
Pod undocumented
104 22 my( $self ) = @_;
105 22 ▶ return unless $self->{logfile};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"logfile"} 0 22 22
106 22 my $logfile = $self->{logfile};
107 22 ▶ if( $self->{logfile_per_process} and $self->{'is a child'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"logfile_per_process"} and $self->{"is a child"}) 0 22 22
Condition33%1/3
$self->{"logfile_per_process"} and $self->{"is a child"}
!ll&&!rl&&r
2200
MC/DC0%0/2
$self->{"logfile_per_process"} and $self->{"is a child"}
$self->{"logfile_per_process"} $self->{"is a child"}
Truth table33%1/3
A and B
A= $self->{"logfile_per_process"}B= $self->{"is a child"}
ABresult
0X0
100
111
108 0 $logfile .= "-$$";
109 }
110 22 return $logfile;
111 }
112  
113  
114 sub reopen_logfile
115 10 ▶{
Subroutine reopen_logfile: called CC=1 SCAR=0.0
Pod undocumented
116 10 my( $self ) = @_;
117 10 delete $self->{"logfile done"};
118 10 $self->open_logfile;
119 10 DEBUG and warn "Start log\n";
120 }
121  
122 sub rotate_logfile
123 0 ▶{
Subroutine rotate_logfile: not called CC=3 SCAR=24.8
Pod undocumented
124 0 my( $self, $dir ) = @_;
125 0 my $logfile = $self->logfile_name;
126 0 ▶ return unless $logfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $logfile 0 0 0
127 0 my $file = basename $logfile;
128 0 my $dest = File::Spec->catfile( $dir, $file );
129 0 ▶ eval { move $file, $dest } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { move($file, $dest) } } or warn $@ 0 0 0
130 }
131  
132 ########################################################
133 sub detach
134 13 ▶{
Subroutine detach: called CC=13 SCAR=35.9
Pod documented
135 13 my($self)=@_;
136 13 ▶ return if $self->{"detach done"}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"detach done"}++ 1 12 13
137 12 ▶ return unless $self->{detach};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"detach"} 1 11 12
138  
139 11 my $gen="grand-parent";
140  
141 11 DEBUG and warn "$$: Detaching from $gen";
142 11 my $pid=fork;
143 11 ▶ die "Unable to fork: $!\n" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 11 11
144 11 ▶ if(0==$pid) { # child
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $pid) 11 0 11
145 11 $gen="parent";
146 11 DEBUG and warn "$$: Detaching from $gen";
147 11 $pid=fork;
148 11 ▶ die "Unable to fork: $!\n" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 11 11
149 11 ▶ if(0==$pid) { # grand-child
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $pid) 11 0 11
150 11 $gen="child";
151 11 DEBUG and warn "$$: We are the $gen";
152 11 ▶ POSIX::setsid() or
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() or warn "${$}: Unable to setsid(): $! (continuing anyway)" 11 0 11
153 warn "$$: Unable to setsid(): $! (continuing anyway)";
154 11 ▶ if( $poe_kernel->can( 'has_forked' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($POE::Component::Daemon::poe_kernel->can("has_forked")) 11 0 11
155 11 $poe_kernel->has_forked;
156 }
157 11 return 1;
158 }
159 }
160 0 DEBUG and warn "$$: I am the $gen, and now I exit.";
161 ## we are parent or child. Exit
162 # stop kernel from griping
163 0 ${$poe_kernel->[POE::Kernel::KR_RUN]} |=
164 POE::Kernel::KR_RUN_CALLED;
165  
166 # This code is lifted from POE::Wheel::Run::_exit_child_any_way_we_can
167 0 my $exitval = 0;
168  
169 # First make sure stdio are flushed.
170 0 ▶ close STDIN if defined fileno(STDIN); # Voodoo?
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno STDIN 0 0 0
171 0 ▶ close STDOUT if defined fileno(STDOUT);
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno STDOUT 0 0 0
172 0 ▶ close STDERR if defined fileno(STDERR);
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno STDERR 0 0 0
173  
174 # On Windows, subprocesses run in separate threads. All the "fancy"
175 # methods act on entire processes, so they also exit the parent.
176  
177 0 ▶ unless (POE::Kernel::RUNNING_IN_HELL) {
Branch: 1 branch
BranchTrueFalseTotal
if (POE::Kernel::RUNNING_IN_HELL()) { } 0 0 0
178 # Try to avoid triggering END blocks and object destructors.
179 0 eval { POSIX::_exit( $exitval ); };
180  
181 # TODO those methods will not exit with $exitval... what to do?
182 0 eval { CORE::kill KILL => $$; };
183 0 eval { exec("$^X -e 0"); };
184 } else {
185 0 eval { CORE::kill( KILL => $$ ); };
186  
187 # TODO Interestingly enough, the KILL is not enough to terminate this process.
188 # However, it *is* enough to stop execution of END blocks/etc
189 # So we will end up falling through to the exit( $exitval ) below
190 }
191  
192 # Do what we must.
193 0 exit( $exitval );
194 }
195  
196 ########################################################
197 sub create_session
198 11 ▶{
Subroutine create_session: called CC=1 SCAR=0.0
Pod undocumented
199 11 my($self)=@_;
200  
201 11 POE::Session->create(
202 object_states => [
203 $self=>[qw(
204 _start _stop status update_status
205 check_scoreboard
206 fork retry waste_time
207 babysit rogues shutdown
208 foreign_child
209 sig_CHLD sig_INT sig_TERM sig_HUP
210 )]
211 ])->ID;
212 }
213  
214 ########################################################
215 sub is_prefork
216 278 ▶{
Subroutine is_prefork: called CC=2 SCAR=6.9
Pod undocumented
217 278 my($self)=@_;
218 278 ▶ return 0 != ($self->{start_children}||0);
Condition100%2/2
$self->{'start_children'} || 0
l!l
19880
MC/DC100%1/1
$self->{'start_children'} || 0
$self->{'start_children'}
Truth table100%2/2
A
A= $self->{'start_children'}
Aresult
11
00
219 }
220  
221 ########################################################
222 sub is_fork
223 44 ▶{
Subroutine is_fork: called CC=3 SCAR=11.7
Pod undocumented
224 44 my($self)=@_;
225 44 ▶ return( 0 != ($self->{max_children}||0) and not $self->is_prefork);
Condition60%3/5
0 != ($self->{'max_children'} || 0) && !$self->is_prefork
!ll&&!rl&&r
0143
$self->{'max_children'} || 0
l!l
440
MC/DC50%1/2
0 != ($self->{'max_children'} || 0) && !$self->is_prefork
0 != ($self->{'max_children'} || 0) !$self->is_prefork
MC/DC0%0/1
$self->{'max_children'} || 0
$self->{'max_children'}
Truth table66%2/3
A && not(B)
A= 0 != ($self->{'max_children'} || 0)B= !$self->is_prefork
ABresult
0X0
100
111
Truth table50%1/2
A
A= $self->{'max_children'}
Aresult
11
00
226 }
227  
228  
229 ########################################################
230 # Set default min and max spare processes for pre-forking
231 sub default_min_max
232 9 ▶{
Subroutine default_min_max: called CC=11 SCAR=27.1
Pod undocumented
233 9 my($self)=@_;
234 9 ▶ if($self->{max_children}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'max_children'}) { } 8 1 9
235 8 ▶ if($self->{max_spare}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'max_spare'}) { } 0 8 8
236 0 ▶ $self->{min_spare} = int($self->{max_spare} /2) unless defined $self->{min_spare};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"min_spare"} 0 0 0
237 }
238 else {
239 8 ▶ $self->{min_spare} = int($self->{max_children} * 0.2) unless defined $self->{min_spare};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"min_spare"} 1 7 8
240 8 ▶ $self->{max_spare} = int($self->{max_children} * 0.8) unless defined $self->{max_spare};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"max_spare"} 8 0 8
241 }
242 8 ▶ $self->{min_spare} = 1 unless defined $self->{min_spare};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"min_spare"} 0 8 8
243 8 ▶ $self->{max_spare} = 2 unless defined $self->{max_spare};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"max_spare"} 0 8 8
244 8 ▶ if( $self->{max_spare} < $self->{min_spare} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"max_spare"} < $self->{"min_spare"}) 0 8 8
245 0 confess "Max_spare can't be smaller then $self->{min_spare}; madness follows.";
246 }
247 }
248 else {
249 # We couldn't be here unless start_children is set
250 1 ▶ $self->{min_spare} = $self->{start_children} unless defined $self->{min_spare};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"min_spare"} 1 0 1
251 1 ▶ $self->{max_spare} = 2*$self->{min_spare} unless defined $self->{max_spare};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"max_spare"} 1 0 1
252 $self->{max_children} = $self->{start_children}
253 1 + $self->{max_spare};
254 }
255 9 DEBUG and warn "$$: min_spare=$self->{min_spare} max_spare=$self->{max_spare} max_children=$self->{max_children}";
256 }
257  
258 ########################################################
259 sub _start
260 11 ▶{
Subroutine _start: called CC=7 SCAR=20.2
261 11 my($self, $kernel)=@_[OBJECT, KERNEL];
262  
263 11 DEBUG and warn "$$: Alias for ".ref($self)." is $self->{alias}";
264 11 $kernel->alias_set($self->{alias});
265 11 $Daemon::alias=$self->{alias};
266  
267 11 $kernel->sig(shutdown => 'shutdown');
268 11 $kernel->sig(TERM => 'sig_TERM');
269 11 $kernel->sig(HUP => 'sig_HUP');
270 11 $kernel->sig(INT => 'sig_INT');
271  
272 11 $self->inform_others( 'daemon_start' );
273  
274 ####
275 11 ▶ if($self->is_prefork) { # pre-forking
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_prefork) { } 7 4 11
elsif ($self->is_fork) { } 4 0 4
276 7 $self->default_min_max;
277 }
278 elsif($self->is_fork) { # forking
279 4 $kernel->yield( 'check_scoreboard' ); # start this loop
280 }
281 else {
282 0 $kernel->yield('waste_time'); # keep the daemon alive
283 0 return; # and do nothing else
284 }
285
286 #### # keep track of children
287 11 $self->{children} = {};
288 11 $self->{'failed forks'} = [];
289 11 ▶ $self->{verbose}||=DEBUG;
Condition50%1/2
$self->{"verbose"} ||= 0
l!l
110
MC/DC0%0/1
$self->{"verbose"} ||= 0
$self->{"verbose"}
Truth table50%1/2
A
A= $self->{"verbose"}
Aresult
11
00
290 11 ▶ $self->{"max requests"}=$self->{requests}||1;
Condition100%2/2
$self->{'requests'} || 1
l!l
74
MC/DC100%1/1
$self->{'requests'} || 1
$self->{'requests'}
Truth table100%2/2
A
A= $self->{'requests'}
Aresult
11
00
291 11 $self->{'is a child'} = 0; # change behavior in child
292 $self->{scoreboard}=
293 11 POE::Component::Daemon::Scoreboard->new($self->{max_children}+5);
294 11 $self->{'pending forks'} = 0;
295  
296 ####
297 11 ▶ if($self->is_prefork) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_prefork) 7 4 11
298 7 DEBUG and warn "$$: Pre-forking children";
299 7 $self->{startup}=1;
300 # fork the initial set of children
301 7 $self->fork_off( $self->{start_children} );
302 }
303 ####
304 11 ▶ if($self->{babysit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"babysit"}) 0 11 11
305 0 $self->{"proctable"}=eval {
306 0 require Proc::ProcessTable;
307 0 return new Proc::ProcessTable;
308 };
309 0 DEBUG and do {
310 warn "$$: Unable to load Proc::ProcessTable: $@" if $@;
311 };
312 0 $kernel->yield('babysit');
313 }
314 ####
315 11 $kernel->yield('waste_time'); # keep the daemon alive
316 11 return;
317 }
318  
319 ########################################################
320 # This event keeps this POE kernel alive
321 sub waste_time
322 11 ▶{
Subroutine waste_time: called CC=4 SCAR=14.1
Pod undocumented
323 11 my($self, $kernel)=@_[OBJECT, KERNEL];
324 11 ▶ return if $self->{'is a child'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is a child"} 1 10 11
325  
326 10 DEBUG and
327 warn "$$: Still alive!";
328  
329 10 ▶ unless($self->{'been told we are parent'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"been told we are parent"}) 10 0 10
330 10 $self->{'been told we are parent'}=1;
331 10 $self->inform_others( 'daemon_parent' );
332 }
333 10 ▶ if($self->{'die'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'die'}) { } 0 10 10
334 0 DEBUG and warn "$$: Orderly shutdown";
335 } else {
336 10 $kernel->delay('waste_time', 600); # TODO : configable
337 }
338 10 return;
339 }
340  
341 ########################################################
342 # Babysit the child processes
343 sub babysit
344 0 ▶{
Subroutine babysit: not called CC=31 SCAR=69.0
Pod documented
345 0 my($self, $kernel)=@_[OBJECT, KERNEL];
346  
347 return if $self->{'die'} or # don't scan if we are dieing
348 0 ▶ $self->{'is a child'}; # or if we are a child
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"die"} or $self->{"is a child"} 0 0 0
Condition0%0/3
$self->{"die"} or $self->{"is a child"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"die"} or $self->{"is a child"}
$self->{"die"} $self->{"is a child"}
Truth table0%0/3
A or B
A= $self->{"die"}B= $self->{"is a child"}
ABresult
1X1
011
000
349  
350 0 my @children=keys %{$self->{children}};
351 0 ▶ ($self->{verbose} or DEBUG) and
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} || 0 and warn "${$}: Babysiting ", scalar @children, " children ", join(", ", sort(@children)) 0 0 0
Condition0%0/2
$self->{"verbose"} or 0
l!l
00
MC/DC0%0/1
$self->{"verbose"} or 0
$self->{"verbose"}
Truth table0%0/2
A
A= $self->{"verbose"}
Aresult
11
00
352 warn "$$: Babysiting ", scalar(@children),
353 " children ", join(", ", sort @children);
354 0 my %table;
355  
356 0 ▶ if($self->{proctable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"proctable"}) 0 0 0
357 0 my $table=$self->{proctable}->table;
358 0 %table=map {($_->pid, $_)} @$table
359 }
360  
361 0 my(%missing, $state, $time, %rogues, %ok);
362 0 foreach my $pid (@children) {
363 0 ▶ if($table{$pid}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($table{$pid}) { } 0 0 0
elsif ($self->{'proctable'}) { } 0 0 0
364 0 $state=$table{$pid}->state;
365  
366 0 ▶ if($state eq 'zombie') {
Branch: 3 branches
BranchTrueFalseTotal
if ($state eq 'zombie') { } 0 0 0
elsif ($state eq 'run') { } 0 0 0
elsif ($state eq 'sleep' or $state eq 'defunct') { } 0 0 0
367 0 my $t=waitpid($pid, POSIX::WNOHANG());
368 0 ▶ if($t==$pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t == $pid) { } 0 0 0
369 # process was reaped, now fake a SIGCHLD
370 # DEBUG and
371 0 warn "$$: Faking a CHLD for $pid";
372 0 $kernel->yield('sig_CHLD', 'CHLD', $pid, $?, 1);
373 0 $ok{$pid}=1;
374 } else {
375 0 ▶ $self->{verbose} and warn "$$: $pid is a $state and couldn't be reaped.";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and warn "${$}: $pid is a $state and couldn't be reaped." 0 0 0
376 0 $missing{$pid}=1;
377 }
378 }
379 elsif($state eq 'run') {
380 0 $time=eval{$table{$pid}->utime + $table{$pid}->stime};
381 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
382 # utime and stime are Linux-only :(
383  
384 0 ▶ if($time and $time > 600000) { # arbitrary limit of 10 minutes
Branch: 1 branch
BranchTrueFalseTotal
if ($time and $time > 600000) { } 0 0 0
Condition0%0/3
$time and $time > 600000
!ll&&!rl&&r
000
MC/DC0%0/2
$time and $time > 600000
$time $time > 600000
Truth table0%0/3
A and B
A= $timeB= $time > 600000
ABresult
0X0
100
111
385 0 $rogues{$pid}=$table{$pid};
386 # DEBUG and
387 0 warn "$$: $pid has gone rogue, time=$time ms";
388 } else {
389 0 warn "$$: $pid time=$time ms";
390 0 $ok{$pid}=1;
391 }
392  
393 ▶ } elsif($state eq 'sleep' or $state eq 'defunct') {
Condition0%0/3
$state eq "sleep" or $state eq "defunct"
l!l&&r!l&&!r
000
MC/DC0%0/2
$state eq "sleep" or $state eq "defunct"
$state eq "sleep" $state eq "defunct"
Truth table0%0/3
A or B
A= $state eq "sleep"B= $state eq "defunct"
ABresult
1X1
011
000
394 0 $ok{$pid}=1;
395 # do nothing
396 } else {
397 0 ▶ $self->{verbose} and warn "$$: $pid has unknown state '$state'";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and warn "${$}: $pid has unknown state '${state}'" 0 0 0
398 0 $ok{$pid}=1;
399 }
400 }
401 elsif($self->{proctable}) {
402 0 ▶ $self->{verbose} and warn "$$: $pid isn't in proctable!";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and warn "${$}: $pid isn't in proctable!" 0 0 0
403 0 $missing{$pid}=1;
404 }
405 else { # try another means.... :/
406 0 ▶ if(-d "/proc" and not -d "/proc/$pid") {
Branch: 2 branches
BranchTrueFalseTotal
if (-d '/proc' and not -d "/proc/$pid") { } 0 0 0
elsif (not $missing{$pid}) { } 0 0 0
Condition0%0/3
-d "/proc" and not -d "/proc/$pid"
!ll&&!rl&&r
000
MC/DC0%0/2
-d "/proc" and not -d "/proc/$pid"
-d "/proc" not -d "/proc/$pid"
Truth table0%0/3
A and not(B)
A= -d "/proc"B= not -d "/proc/$pid"
ABresult
0X0
100
111
407 0 DEBUG and warn "$$: Unable to stat /proc/$pid! Is the child missing";
408 0 $missing{$pid}=1;
409 }
410 elsif(not $missing{$pid}) {
411 0 $ok{$pid}=1;
412 }
413 }
414 }
415  
416 # if a process is MIA, we fake a death, and spawn a new child (if needs be)
417 0 foreach my $pid (keys %missing) {
418 #$self->{verbose} and
419 0 warn "$$: Faking a CHLD for $pid MIA";
420 0 $kernel->yield('sig_CHLD', 'CHLD', $pid, 0, 1);
421 }
422  
423 # we could do the same thing for rogue processes, but instead we
424 # give them time to calm down
425  
426 0 ▶ if($self->{rogues}) { # processes that are %ok are now removed
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rogues"}) 0 0 0
427 # from the list of rogues
428 0 ▶ delete @{$self->{rogues}}{keys %ok} if %ok;
Branch: 1 branch
BranchTrueFalseTotal
if %ok 0 0 0
429 }
430  
431 0 ▶ if(%rogues) {
Branch: 1 branch
BranchTrueFalseTotal
if (%rogues) 0 0 0
432 # Start the rogues delay loop when going from no rogues to have
433 # rogues
434 # NB: yield causes the event to fire after this function exits
435 0 ▶ $kernel->yield('rogues') if not $self->{rogues};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rogues"} 0 0 0
436  
437 0 ▶ $self->{rogues}||={};
Condition0%0/2
$self->{"rogues"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"rogues"} ||= {}
$self->{"rogues"}
Truth table0%0/2
A
A= $self->{"rogues"}
Aresult
11
00
438 0 foreach my $pid (keys %rogues) {
439 0 ▶ if($self->{rogues}{$pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'rogues'}{$pid}) { } 0 0 0
440 0 $self->{rogues}{$pid}{proc}=$rogues{$pid};
441 } else {
442 0 $self->{rogues}{$pid}={proc=>$rogues{$pid}, tries=>0};
443 }
444 }
445 }
446  
447 0 $kernel->delay('babysit', $self->{babysit});
448 0 return;
449 }
450  
451 ########################################################
452 # Deal with rogue child processes
453 sub rogues
454 0 ▶{
Subroutine rogues: not called CC=13 SCAR=52.0
Pod undocumented
455 0 my($self, $kernel)=@_[OBJECT, KERNEL];
456  
457 return if $self->{'die'} or # don't scan if we are dieing
458 0 ▶ $self->{'is a child'}; # or if we are a child
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"die"} or $self->{"is a child"} 0 0 0
Condition0%0/3
$self->{"die"} or $self->{"is a child"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"die"} or $self->{"is a child"}
$self->{"die"} $self->{"is a child"}
Truth table0%0/3
A or B
A= $self->{"die"}B= $self->{"is a child"}
ABresult
1X1
011
000
459  
460 0 ▶ return unless $self->{rogues}; # make sure we have some real work
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"rogues"} 0 0 0
461 0 eval {
462 0 ▶ if(ref($self->{rogues}) ne 'HASH' or not keys %{$self->{rogues}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"rogues"} ne "HASH" or not keys %{$$self{"rogues"};}) 0 0 0
Condition0%0/3
ref $self->{"rogues"} ne "HASH" or not keys %{$$self{"rogues"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self->{"rogues"} ne "HASH" or not keys %{$$self{"rogues"};}
ref $self->{"rogues"} ne "HASH" not keys %{$$self{"rogues"};}
Truth table0%0/3
A or not(B)
A= ref $self->{"rogues"} ne "HASH"B= not keys %{$$self{"rogues"};}
ABresult
1X1
011
000
463 0 delete $self->{rogues};
464 0 return;
465 }
466  
467 0 my $signal;
468 0 ▶ while(my($pid, $rogue)=each %{$self->{rogues}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"rogues"};}) 0 0 0
469 0 $signal=0;
470 0 ▶ if($rogue->{tries} < 1) { $signal=SIGINT; }
Branch: 3 branches
BranchTrueFalseTotal
if ($rogue->{'tries'} < 1) { } 0 0 0
elsif ($rogue->{'tries'} < 2) { } 0 0 0
elsif ($rogue->{'tries'} < 3) { } 0 0 0
471 0 elsif($rogue->{tries} < 2) { $signal=SIGTERM; }
472 0 elsif($rogue->{tries} < 3) { $signal=SIGKILL; }
473
474 0 ▶ if($signal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signal) { } 0 0 0
475 0 DEBUG and warn "$$: Sending signal $signal to rogue $pid";
476 0 ▶ unless($rogue->{proc}->kill($signal)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rogue->{"proc"}->kill($signal)) 0 0 0
477 0 warn "$$: Error sending signal $signal to $pid: $!";
478 0 delete $self->{rogues}{$pid};
479 }
480 }
481 else {
482 # if SIGKILL didn't work, it's beyond hope!
483 0 $kernel->yield('sig_CHLD', 'CHLD', $pid, 0, 1);
484 0 delete $self->{rogues}{$pid};
485 # $self->{verbose} and
486 0 warn "$$: Faking a CHLD for rogue $pid";
487 }
488 0 $rogue->{tries}++;
489 }
490 0 $kernel->delay('rogues', 2*$self->{babysit});
491 };
492 0 ▶ warn "$$: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
493 0 return;
494 }
495  
496 ########################################################
497 # Accept POE's standard _stop event, and stop all the children, too.
498 # The 'children' hash is maintained in the 'fork' and 'sig_CHLD'
499 # handlers. It's empty for children.
500 sub _stop
501 11 ▶{
Subroutine _stop: called CC=1 SCAR=0.0
502 11 my($self, $kernel)=@_[OBJECT, KERNEL];
503 11 $Daemon::alias='';
504  
505 11 DEBUG and warn "$$: Server is stoping";
506 # DEBUG_USR2 and check_kernel($kernel, $self->{'is a child'}, 1);
507 }
508  
509 ########################################################
510 # Someone wants us to exit... oblige
511 sub do_shutdown
512 9 ▶{
Subroutine do_shutdown: called CC=1 SCAR=0.0
Pod undocumented
513 9 DEBUG and print STDERR "$$: do_shutdown\n";
514 9 $poe_kernel->call( $poe_kernel->get_active_session, 'shutdown' );
515 }
516  
517 sub shutdown
518 11 ▶{
Subroutine shutdown: called CC=11 SCAR=25.8
Pod documented
519 11 my($self, $kernel)=@_[OBJECT, KERNEL];
520  
521 # $main::TRACE_REFCNT = 1;
522 # Daemon->peek(1);
523  
524 # $self->{verbose} and
525 11 warn "$$: shutdown";
526 11 ▶ if($self->{rogues}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rogues"}) 0 11 11
527 0 $kernel->delay('rogues'); # we no longer care about rogues
528 }
529  
530 11 ▶ if($self->{children}) { # tell children to go away
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"children"}) 1 10 11
531 1 foreach my $pid (keys %{$self->{children}}) {
532 4 ▶ $self->{verbose} and warn "$$: TERM $pid";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and warn "${$}: TERM $pid" 4 0 4
533 4 ▶ kill SIGTERM, $pid
Branch: 1 branch
BranchTrueFalseTotal
kill 15, $pid or warn "${$}: Unable to kill ${pid}: $!" 4 0 4
534 or warn "$$: Unable to kill $pid: $!";
535 }
536 }
537 11 ▶ if($self->{foreign_children}) { # tell foreign children to go away
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"foreign_children"}) 0 11 11
538 0 foreach my $pid (keys %{$self->{foreign_children}}) {
539 0 ▶ kill SIGTERM, $pid
Branch: 1 branch
BranchTrueFalseTotal
kill 15, $pid or warn "${$}: Unable to kill ${pid}: $!" 0 0 0
540 or warn "$$: Unable to kill $pid: $!";
541 }
542 }
543  
544 11 ▶ if(defined $self->{'my slot'}) { # notice in the scoreboard
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"my slot"}) 10 1 11
545 # this means we are a child
546 10 $self->{scoreboard}->write($self->{'my slot'}, 'e');
547 10 delete $self->{'my slot'};
548 }
549  
550 11 ▶ $kernel->alias_remove(delete $self->{alias}) if $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"alias"} 11 0 11
551 11 $kernel->delay('waste_time'); # get it OVER with
552 11 $kernel->delay('check_scoreboard'); # get it OVER with
553 11 $self->{'die'}=1; # prevent race conditions
554 11 $self->inform_others( 'daemon_shutdown' );
555  
556 # Remove signal handlers so that some versions of POE can shut down
557 11 $kernel->sig( 'CHLD' );
558 11 $kernel->sig( 'HUP' );
559 11 $kernel->sig( 'INT' );
560 11 $kernel->sig( 'TERM' );
561  
562 11 return;
563 }
564  
565 ########################################################
566 # The server has been requested to fork, so fork already.
567 sub fork
568 39 ▶{
Subroutine fork: called CC=11 SCAR=24.6
Pod undocumented
569 39 my ($kernel, $self, $req) = @_[KERNEL, OBJECT, ARG0];
570  
571 # children should not honor this event
572 # Note that the forked POE kernel might have these events in it already
573 # This is unavoidable :-(
574 39 ▶ if( $self->{'is a child'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is a child"}) 2 37 39
575 2 return;
576 }
577 37 ▶ return if not $self->{children} or $self->{'die'};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"children"} or $self->{"die"} 0 37 37
Condition33%1/3
not $self->{"children"} or $self->{"die"}
l!l&&r!l&&!r
0037
MC/DC0%0/2
not $self->{"children"} or $self->{"die"}
not $self->{"children"} $self->{"die"}
Truth table33%1/3
not(A) or B
A= not $self->{"children"}B= $self->{"die"}
ABresult
1X1
011
000
578  
579 ####
580 37 DEBUG and warn "$$: pending forks=$self->{'pending forks'}";
581 37 ▶ if( $self->{"pending forks"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pending forks"}) 27 10 37
582 27 $self->{"pending forks"}--;
583 }
584  
585 ####
586 37 ▶ if( $self->{max_children} <= keys %{$self->{children}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"max_children"} <= keys %{$$self{"children"};}) 1 36 37
587 1 warn "$$: Maximum number of children reached!";
588 1 warn "$$: max_children=$self->{max_children} currently=".(0+keys %{$self->{children}});
589  
590 # 2006/02 This is the most lamentable bit of my algorythm. By
591 # throwing fork events around, I could end up with too many
592 # children. Either I drop requests on the floor (bad), or I save
593 # them via fork_failed, which means the events could end up in other
594 # children (less bad) or I just let them succeed and hope that
595 # > {max_children} isn't all that horrendeous (least bad so far)
596 }
597  
598 37 my $slot=$self->{scoreboard}->add('FORK'); # grap a slot in scoreboard
599  
600 # Failure! We have too many children! AAAGH!
601 37 ▶ unless( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $slot) 0 37 37
602 0 warn "NO FREE SLOT! You should increase max_children to avoid this.";
603 0 return;
604 }
605  
606 DEBUG and
607 37 warn "$$: Forking a child";
608 37 my $pid = fork(); # try to fork
609 37 ▶ unless (defined($pid)) { # did the fork fail?
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 37 37
610 0 $self->{scoreboard}->drop($slot); # give slot back
611 0 $self->fork_failed($!, "$!", $req);
612 0 return;
613 }
614  
615 37 ▶ if ($pid) { # successful fork; parent keeps track
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 27 10 37
616 #print STDERR "$$: parent pid=$pid\n";
617 27 $self->{children}->{$pid} = $slot;
618 DEBUG and
619 warn "$$: Parent server forked a new child. children: (",
620 27 join(' ', sort keys %{$self->{children}}), ")";
621  
622 27 $kernel->sig_child( $pid => 'sig_CHLD');
623  
624 27 ▶ if( not $self->{"pending forks"} and $self->{startup} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"pending forks"} and $self->{"startup"}) 6 21 27
Condition100%3/3
not $self->{"pending forks"} and $self->{"startup"}
!ll&&!rl&&r
7146
MC/DC100%2/2
not $self->{"pending forks"} and $self->{"startup"}
not $self->{"pending forks"} $self->{"startup"}
Truth table100%3/3
not(A) and B
A= not $self->{"pending forks"}B= $self->{"startup"}
ABresult
0X0
100
111
625 # End if pre-forking startup time.
626 6 $self->{startup}=0;
627 6 $kernel->yield('check_scoreboard');
628 }
629 }
630 else { # child becomes a child process
631 10 $self->has_forked;
632 10 $self->{scoreboard}->write( $slot, 'fork' );
633 10 $self->become_child( $slot, $req );
634 }
635 37 return;
636 }
637  
638 ########################################################
639 # We failed to fork!
640 sub fork_failed
641 0 ▶{
Subroutine fork_failed: not called CC=3 SCAR=24.8
Pod undocumented
642 0 my($self, $errnum, $errstr, $req)=@_;
643 0 ▶ if (($errnum == EAGAIN) || ($errnum == ECHILD)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errnum == 11 or $errnum == 10) { } 0 0 0
Condition0%0/3
$errnum == 11 or $errnum == 10
l!l&&r!l&&!r
000
MC/DC0%0/2
$errnum == 11 or $errnum == 10
$errnum == 11 $errnum == 10
Truth table0%0/3
A or B
A= $errnum == 11B= $errnum == 10
ABresult
1X1
011
000
644 # try again later, if a temporary error
645 0 DEBUG and warn "$$: Recoverable forking problem";
646 0 push @{$self->{'failed forks'}}, $req;
647 0 $poe_kernel->delay('retry', 1);
648 }
649 else { # fail permanently, if fatal
650 0 warn "$$: Can't fork: $errstr";
651 0 $poe_kernel->yield('_stop');
652 }
653 0 return;
654 }
655  
656 sub has_forked
657 10 ▶{
Subroutine has_forked: called CC=1 SCAR=0.0
Pod undocumented
658 # This resets some kernel data that was preventing the child
659 # process's kernel from becoming IDLE
660 10 $poe_kernel->has_forked;
661 }
662  
663 ########################################################
664 # Turn ourselves into a child process
665 sub become_child
666 10 ▶{
Subroutine become_child: called CC=10 SCAR=23.6
Pod undocumented
667 10 my($self, $slot, $req)=@_;
668  
669 10 ▶ ( $self->{verbose} or DEBUG )
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} || 0 and warn "${$}: Created ", scalar localtime 10 0 10
Condition50%1/2
$self->{"verbose"} or 0
l!l
100
MC/DC0%0/1
$self->{"verbose"} or 0
$self->{"verbose"}
Truth table50%1/2
A
A= $self->{"verbose"}
Aresult
11
00
670 and warn "$$: Created ", scalar localtime;
671 # This resets some kernel data that was preventing the child process
672 # from handling CHLD
673 # $poe_kernel->_data_sig_cease_polling;
674  
675 ## reset the kernel->ID
676 # Force each process to have a unique ID. IKC depends on unique IDs
677 # unless( $poe_kernel->can( 'has_forked' ) ) {
678 # $poe_kernel->[ POE::Kernel::KR_ID ] = undef();
679 # }
680 # Above is for Olde Schoole, pre-has_forked() POE
681
682  
683 ## Clean out stuff that the parent needs but not the children
684  
685 10 $self->{'is a child'} = 1; # don't allow fork
686 10 $self->{'my slot'} = $slot;
687 10 delete $self->{'pending forks'};
688 10 delete $self->{'failed forks'};
689  
690 10 $poe_kernel->sig('CHLD');
691 10 $poe_kernel->sig('INT');
692 # remove the wait for babysit
693 10 ▶ $poe_kernel->delay('babysit') if $self->{'babysit'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"babysit"} 0 10 10
694 # remove the wait for checking the scorebard
695 10 ▶ $poe_kernel->delay('check_scoreboard') if $self->is_prefork or
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_prefork or $self->is_fork 10 0 10
Condition66%2/3
$self->is_prefork or $self->is_fork
l!l&&r!l&&!r
640
MC/DC0%0/2
$self->is_prefork or $self->is_fork
$self->is_prefork $self->is_fork
Truth table66%2/3
A or B
A= $self->is_preforkB= $self->is_fork
ABresult
1X1
011
000
696 $self->is_fork;
697  
698 10 ▶ foreach my $pid ( keys %{ $self->{children} || {} } ) {
Condition50%1/2
$self->{'children'} || {}
l!l
100
MC/DC0%0/1
$self->{'children'} || {}
$self->{'children'}
Truth table50%1/2
A
A= $self->{'children'}
Aresult
11
00
699 17 $poe_kernel->sig_child( $pid );
700 }
701  
702 # remove these fields
703 10 delete @{$self}{ qw(rogues proctable children) };
704  
705 10 $self->reopen_logfile;
706  
707 # Tell everyone we are now a child
708 10 $self->inform_others( 'daemon_child', $req );
709  
710 10 ▶ if($self->is_prefork) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_prefork) { } 6 4 10
elsif ($self->is_fork) { } 4 0 4
711 ## AAAUGH! Don't send daemon_accept here.
712 ## wait for someone to update the status to 'wait' first!!!1eleven
713 # warn "$$: Sending 1 daemon_accept\n";
714 # $self->inform_others( 'daemon_accept' );
715 6 $self->{requestN}=0;
716 }
717 elsif($self->is_fork) {
718 4 $self->{scoreboard}->write( $slot, 'req' );
719 }
720  
721 10 DEBUG and warn "$$: Child server has been forked";
722 10 return;
723 }
724  
725 ########################################################
726 # Retry failed forks. This is invoked (after a brief delay) if the
727 # 'fork' state encountered a temporary error.
728 sub retry
729 0 ▶{
Subroutine retry: not called CC=3 SCAR=24.8
Pod undocumented
730 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
731 0 ▶ if($self->{'is a child'} or not $self->{children}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is a child"} or not $self->{"children"}) 0 0 0
Condition0%0/3
$self->{"is a child"} or not $self->{"children"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"is a child"} or not $self->{"children"}
$self->{"is a child"} not $self->{"children"}
Truth table0%0/3
A or not(B)
A= $self->{"is a child"}B= not $self->{"children"}
ABresult
1X1
011
000
732 0 warn "$$: We are a child, why are we forking?";
733 0 return;
734 }
735  
736 # Multiplex the delayed 'retry' event into enough 'fork' events to
737 # make up for the temporary fork errors.
738 0 DEBUG and warn "$$: We have $self->{'failed forks'} failed forks";
739 0 $self->fork_off( $self->{'failed forks'} );
740 # reset the failed forks counter
741 0 $self->{'failed forks'} = [];
742 0 return;
743 }
744  
745  
746  
747 ########################################################
748 # $poe_kernel->signal() simply places an event on the queue. This means that
749 # they get handled during the select loop, which is a bad thing for
750 # 'daemon_child' in a forking server.
751 # Note that it requires POE::API::Peek, which is currently broken for 1.300+
752 sub expedite_signal
753 0 ▶{
Subroutine expedite_signal: not called CC=2 SCAR=17.9
Pod undocumented
754 0 my( $self, $signal, @etc ) = @_;
755  
756 0 DEBUG and
757 warn "$$: Expedite signal $signal";
758  
759 0 my $api = POE::API::Peek->new();
760 0 my %watchers = $api->signal_watchers( $signal );
761  
762 0 ▶ while( my( $session, $event ) = each %watchers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %watchers) 0 0 0
763 0 DEBUG and
764 warn "$$: Signal $signal is $session/$event";
765 0 $poe_kernel->call( $session, $event, $poe_kernel, @etc );
766 }
767 0 return;
768 }
769  
770 sub inform_others
771 49 ▶{
Subroutine inform_others: called CC=6 SCAR=26.5
Pod undocumented
772 49 my( $self, $signal, @etc ) = @_;
773  
774 $self->{verbose} and
775 49 ▶ warn "$$: Inform others about $signal";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and warn "${$}: Inform others about $signal" 49 0 49
776  
777 49 ▶ if( !$NO_PEEK and ($signal eq 'daemon_shutdown' or
Branch: 1 branch
BranchTrueFalseTotal
if (not $POE::Component::Daemon::NO_PEEK and $signal eq 'daemon_shutdown' || $signal eq 'daemon_child' && $self->is_fork) { } 0 49 49
Condition11%1/9
not $POE::Component::Daemon::NO_PEEK and $signal eq "daemon_shutdown" || $signal eq "daemon_child" && $self->is_fork
!ll&&!rl&&r
4900
$signal eq "daemon_shutdown" or $signal eq "daemon_child" and $self->is_fork
l!l&&r!l&&!r
000
$signal eq "daemon_child" and $self->is_fork
!ll&&!rl&&r
000
MC/DC0%0/4
not $POE::Component::Daemon::NO_PEEK and $signal eq "daemon_shutdown" || $signal eq "daemon_child" && $self->is_fork
not $POE::Component::Daemon::NO_PEEK $signal eq "daemon_shutdown" $signal eq "daemon_child" $self->is_fork
Truth table20%1/5
not(A) and B or C and D
A= not $POE::Component::Daemon::NO_PEEKB= $signal eq "daemon_shutdown"C= $signal eq "daemon_child"D= $self->is_fork
ABCDresult
0XXX0
100X0
10100
11XX1
10111
778 ($signal eq 'daemon_child') and $self->is_fork) ) {
779 0 $self->expedite_signal( $signal, @etc );
780 }
781 else {
782 49 $poe_kernel->signal($poe_kernel, $signal, @etc );
783 }
784 }
785  
786 ########################################################
787 sub foreign_child
788 0 ▶{
Subroutine foreign_child: not called CC=1 SCAR=6.9
Pod documented
789 0 my( $self, $pid ) = @_[ OBJECT, ARG0 ];
790  
791 0 $self->{foreign_children}{ $pid } = 1;
792 }
793  
794  
795 ########################################################
796 # SIGCHLD causes this session to fork off a replacement for the lost child.
797 sub sig_CHLD
798 10 ▶{
Subroutine sig_CHLD: called CC=11 SCAR=31.3
Pod undocumented
799 10 my ($kernel, $self, $signal, $pid, $status, $fake) =
800 @_[KERNEL, OBJECT, ARG0, ARG1, ARG2, ARG3];
801  
802 10 ▶ ( DEBUG or $self->{verbose} ) and
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and warn "${$}: SIGCHLD pid=$pid" 10 0 10
803 warn "$$: SIGCHLD pid=$pid";
804  
805 ##########
806 10 ▶ if($self->{foreign_children} and $self->{foreign_children}{$pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"foreign_children"} and $self->{"foreign_children"}{$pid}) 0 10 10
Condition33%1/3
$self->{"foreign_children"} and $self->{"foreign_children"}{$pid}
!ll&&!rl&&r
1000
MC/DC0%0/2
$self->{"foreign_children"} and $self->{"foreign_children"}{$pid}
$self->{"foreign_children"} $self->{"foreign_children"}{$pid}
Truth table33%1/3
A and B
A= $self->{"foreign_children"}B= $self->{"foreign_children"}{$pid}
ABresult
0X0
100
111
807 0 DEBUG and warn "$$: Foreign child $pid exited.";
808 0 delete $self->{foreign_children}{ $pid };
809 0 return;
810 }
811  
812 10 ▶ return if $self->{"is a child"};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is a child"} 0 10 10
813  
814 # ( DEBUG or $self->{verbose} ) and
815 10 ▶ $INC{'Test/More.pm'} or
Branch: 1 branch
BranchTrueFalseTotal
$INC{"Test/More.pm"} or warn "${$}: SIGCHLD pid=$pid" 0 10 10
816 warn "$$: SIGCHLD pid=$pid";
817  
818 ##########
819 10 ▶ if($self->{children}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"children"}) 10 0 10
820 # if it was one of ours
821 10 my $slot=delete $self->{children}->{$pid};
822 10 ▶ if (defined $slot) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $slot) { } 10 0 10
elsif ($fake) { } 0 0 0
823 DEBUG and warn "$$: Parent caught SIGCHLD for $pid. children: (",
824 10 join(' ', sort keys %{$self->{children}}),
825 ")";
826 10 ▶ $self->{verbose} and warn "$$: Child $pid ",
Branch: 2 branches
BranchTrueFalseTotal
$self->{"verbose"} and warn "${$}: Child $pid ", $fake ? "is gone" : "exited normaly" 10 0 10
$fake ? : 0 10 10
827 ($fake?'is gone':'exited normaly');
828 10 $self->{scoreboard}->drop($slot);
829  
830 # Don't do anything else; wait for regular check_scoreboard to
831 # do it's thing. Otherwise we have to check min_spare/max_spare
832 # and stuff like that.
833 }
834 elsif($fake) {
835 0 warn "$$: Needless fake CHLD for $pid.";
836 }
837 else {
838 0 warn "$$: CHLD for $pid child of someone else.";
839 0 warn Dumper $self;
840 }
841 }
842  
843 # don't handle terminal signals
844 10 return;
845 }
846  
847 ########################################################
848 # Terminal signals aren't handled, so the session will stop on SIGINT.
849 # The shutdown event handler takes care of cleanup.
850 sub sig_INT
851 0 ▶{
Subroutine sig_INT: not called CC=2 SCAR=17.9
Pod undocumented
852 0 my ($kernel, $self, $signal, $pid, $status) =
853 @_[KERNEL, OBJECT, ARG0, ARG1, ARG2];
854  
855 0 ▶ ( DEBUG or $self->{verbose} ) and
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and warn "${$}: SIGINT" 0 0 0
856 warn "$$: SIGINT";
857 0 $self->do_shutdown;
858 0 $kernel->sig_handled(); # INT is a terminal
859 0 return;
860 }
861  
862 ########################################################
863 # daemontool's svc -d sends a TERM to the parent.
864 # Propagate it down to the children
865 # The shutdown event handler takes care of cleanup.
866 #
867 # Terminal signals aren't handled, so the session will stop on SIGINT.
868 sub sig_TERM
869 5 ▶{
Subroutine sig_TERM: called CC=2 SCAR=7.0
Pod undocumented
870 5 my ($kernel, $self, $signal, $pid, $status) =
871 @_[KERNEL, OBJECT, ARG0, ARG1, ARG2];
872  
873 5 ▶ ( DEBUG or $self->{verbose} ) and
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and warn "${$}: SIGTERM" 5 0 5
874 warn "$$: SIGTERM";
875 5 $self->do_shutdown;
876 5 $kernel->sig_handled(); # TERM is a terminal
877 5 return;
878 }
879  
880 ########################################################
881 # Close the log file and reopen
882 sub sig_HUP
883 1 ▶{
Subroutine sig_HUP: called CC=3 SCAR=11.1
Pod undocumented
884 1 my ($kernel, $self, $signal, $pid, $status) =
885 @_[KERNEL, OBJECT, ARG0, ARG1, ARG2];
886  
887 1 ▶ ( DEBUG or $self->{verbose} ) and
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and warn qq[${$}: SIGHUP (logfile=$self->{"logfile"})] 1 0 1
888 warn "$$: SIGHUP (logfile=$self->{logfile})";
889 1 $kernel->sig_handled();
890  
891 1 $self->inform_others( 'daemon_HUP' );
892  
893 1 ▶ return unless $self->{logfile};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"logfile"} 0 1 1
894  
895 1 DEBUG and warn "Reopening $self->{logfile}";
896  
897 1 $self->{"logfile done"}--;
898 1 $self->open_logfile;
899 1 return;
900 }
901  
902  
903  
904  
905 ########################################################
906 # Start the process of creating a number of children
907 sub fork_off
908 30 ▶{
Subroutine fork_off: called CC=6 SCAR=18.9
Pod undocumented
909 30 my($self, $n)=@_;
910 30 ▶ if(ref $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $n) 10 20 30
911 10 DEBUG and
912 warn "$$: Fork off ", (0+@$n), " children";
913 10 ▶ if( 1==@$n and $self->is_fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @$n and $self->is_fork) { } 10 0 10
Condition33%1/3
1 == @$n and $self->is_fork
!ll&&!rl&&r
0010
MC/DC0%0/2
1 == @$n and $self->is_fork
1 == @$n $self->is_fork
Truth table33%1/3
A and B
A= 1 == @$nB= $self->is_fork
ABresult
0X0
100
111
914  
915 # This fork_off was probably caused by client code doing
916 # update_status( 'req' ) for a new request. That being the case,
917 # we want to prevent the select loop from running.
918  
919 10 $poe_kernel->call( $poe_kernel->get_active_session,
920 'fork', @$n );
921 } else {
922 0 foreach my $req (@$n) {
923 0 $self->{"pending forks"}++;
924 0 $poe_kernel->yield('fork', $req);
925 }
926 }
927 10 return;
928 }
929
930 DEBUG and
931 20 warn "$$: Fork off $n children";
932 20 ▶ for(my $q1=0; $q1 < $n; $q1++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q1 < $n) 29 20 49
933 29 $self->{"pending forks"}++;
934 29 $poe_kernel->yield('fork');
935 }
936 20 return;
937 }
938  
939 ########################################################
940 # Make sure we have min_spare waiting processes
941 # But no more than max_spare
942 sub check_scoreboard
943 161 ▶{
Subroutine check_scoreboard: called CC=16 SCAR=33.5
Pod undocumented
944 161 my($self)=@_;
945 161 DEBUG and warn "$$: check_scoreboard";
946  
947 161 ▶ if($self->{'is a child'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"is a child"}) 0 161 161
948 0 DEBUG and warn "$$: I am a child! I refuse to check the scoreboard!";
949 0 return;
950 }
951 # DEBUG_SC and warn "$$: Checking scoreboard";
952  
953 161 my $slots=$self->{scoreboard}->read_all;
954 161 my @waiting; # PIDs of waiting children
955  
956 161 ▶ while(my($pid, $slot)=each %{$self->{children}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"children"};}) 486 161 647
957 486 DEBUG and warn "$$: child at slot $slot ($pid: $slots->[$slot])";
958 486 ▶ if($slots->[$slot] eq 'w' or
Branch: 1 branch
BranchTrueFalseTotal
if ($slots->[$slot] eq "w" or $slots->[$slot] eq "f") 292 194 486
Condition66%2/3
$slots->[$slot] eq "w" or $slots->[$slot] eq "f"
l!l&&r!l&&!r
2920194
MC/DC50%1/2
$slots->[$slot] eq "w" or $slots->[$slot] eq "f"
$slots->[$slot] eq "w" $slots->[$slot] eq "f"
Truth table66%2/3
A or B
A= $slots->[$slot] eq "w"B= $slots->[$slot] eq "f"
ABresult
1X1
011
000
959 $slots->[$slot] eq 'f') { # waiting for req
960 292 warn "$pid is still forking\n" if DEBUG and $slots->[$slot] eq 'f';
961 292 push @waiting, $pid;
962 }
963 }
964  
965  
966 161 ▶ if( $self->is_prefork ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->is_prefork) { } 157 4 161
elsif ($self->is_fork and $self->{'max_children'} <= keys %{$$self{'children'};}) { } 0 4 4
elsif ($self->{'paused'}) { } 0 4 4
967 157 my $waiting=@waiting;
968 157 DEBUG and warn "$$: waiting=$waiting";
969 157 ▶ if($waiting < $self->{min_spare}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($waiting < $self->{'min_spare'}) { } 13 144 157
elsif ($waiting > $self->{'max_spare'}) { } 0 144 144
elsif (not $self->{'statis'}) { } 3 141 144
970 13 my $n=$self->{min_spare} - $waiting;
971 # DEBUG_SC and
972 13 warn "$$: Spawning $n spares";
973 13 $self->fork_off($n);
974 }
975 elsif($waiting > $self->{max_spare}) {
976 0 my $n=$waiting - $self->{max_spare};
977 # DEBUG_SC and
978 0 warn "$$: Killing $n spares";
979 0 foreach my $pid ( @waiting[0..($n-1)] ) {
980 0 ▶ kill SIGINT, $pid or warn "$$: killing $pid: $!";
Branch: 1 branch
BranchTrueFalseTotal
kill 2, $pid or warn "${$}: killing ${pid}: $!" 0 0 0
981 }
982 }
983 elsif( not $self->{statis} ) {
984 3 ▶ $INC{'Test/More.pm'} or
Branch: 1 branch
BranchTrueFalseTotal
$INC{"Test/More.pm"} or warn "All systems are go\n" 0 3 3
985 warn "All systems are go\n";
986 3 $self->{statis} = 1;
987 }
988 }
989 ▶ elsif( $self->is_fork and
Condition33%1/3
$self->is_fork and $self->{"max_children"} <= keys %{$$self{"children"};}
!ll&&!rl&&r
040
MC/DC0%0/2
$self->is_fork and $self->{"max_children"} <= keys %{$$self{"children"};}
$self->is_fork $self->{"max_children"} <= keys %{$$self{"children"};}
Truth table33%1/3
A and B
A= $self->is_forkB= $self->{"max_children"} <= keys %{$$self{"children"};}
ABresult
0X0
100
111
990 4 $self->{max_children} <= keys %{$self->{children}} ) {
991 0 ▶ unless( $self->{paused} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"paused"}) 0 0 0
992 0 $self->inform_others( 'daemon_pause' );
993 0 $self->{paused} = 1;
994 }
995 }
996 elsif( $self->{paused} ) {
997 0 $self->inform_others( 'daemon_accept' );
998 0 delete $self->{paused};
999 }
1000  
1001 # This also clears any pending delay to us
1002 161 $poe_kernel->delay('check_scoreboard', 1);
1003 }
1004  
1005 ########################################################
1006 # User code wants to update the status
1007 sub update_status
1008 28 ▶{
Subroutine update_status: called CC=4 SCAR=14.2
Pod documented
1009 28 my($self, $status, $parm)=@_[OBJECT, ARG0, ARG1];
1010 28 DEBUG and warn "$$: Update status status=$status parm=(",($parm||''),")";
1011  
1012 28 ▶ if($self->is_prefork) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_prefork) { } 12 16 28
elsif ($self->is_fork) { } 16 0 16
1013 12 return $self->update_status_prefork($status, $parm, @_[CALLER_FILE, CALLER_LINE]);
1014 }
1015  
1016 elsif($self->is_fork) {
1017 16 ▶ if($self->{'is a child'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'is a child'}) { } 6 10 16
1018 6 return $self->update_status_fork_child($status, $parm);
1019 }
1020 else {
1021 10 return $self->update_status_fork_parent($status, $parm);
1022 }
1023 }
1024 0 warn "$$: Non-forking server doesn't need to update status";
1025 0 return;
1026 }
1027  
1028 ########################################################
1029 # User code in a preforked child wants to update the status
1030 sub update_status_prefork
1031 12 ▶{
Subroutine update_status_prefork: called CC=15 SCAR=32.6
Pod undocumented
1032 12 my($self, $status, $parm, $file, $line)=@_;
1033 12 ▶ return if $self->{'die'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"die"} 0 12 12
1034  
1035 12 ▶ unless($self->{'is a child'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"is a child"}) 0 12 12
1036 0 warn "$$: Only child processes should update their status ($status)";
1037 0 return;
1038 }
1039  
1040 12 my $slot=$self->{'my slot'};
1041 12 ▶ unless( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $slot) 0 12 12
1042 0 die "$$: Missing slot. Update sent from $file line $line.\n"
1043 }
1044 12 ▶ my $current_status=$self->{scoreboard}->read($slot)||'unknown';
Condition50%1/2
$self->{'scoreboard'}->read($slot) || 'unknown'
l!l
120
MC/DC0%0/1
$self->{'scoreboard'}->read($slot) || 'unknown'
$self->{'scoreboard'}->read($slot)
Truth table50%1/2
A
A= $self->{'scoreboard'}->read($slot)
Aresult
11
00
1045 12 DEBUG and
1046 warn "$$: current_status=$current_status -> status=$status";
1047  
1048 12 ▶ if($status eq 'wait' or $status eq 'done') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "wait" or $status eq "done") 8 4 12
Condition100%3/3
$status eq "wait" or $status eq "done"
l!l&&r!l&&!r
624
MC/DC100%2/2
$status eq "wait" or $status eq "done"
$status eq "wait" $status eq "done"
Truth table100%3/3
A or B
A= $status eq "wait"B= $status eq "done"
ABresult
1X1
011
000
1049 # DEBUG and
1050 8 warn "$$: Moving to status=wait";
1051 8 $self->{scoreboard}->write($slot, 'wait');
1052 8 ▶ if($self->{requestN} >= $self->{'max requests'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'requestN'} >= $self->{'max requests'}) { } 2 6 8
elsif ($current_status ne 'w') { } 6 0 6
1053 # DEBUG and
1054 2 warn "$$: Handled $self->{requestN} requests, shutting down status=$status";
1055 2 $self->do_shutdown;
1056 }
1057 elsif($current_status ne 'w') {
1058 # use Carp qw( cluck );
1059 # cluck "$$: daemon_accept";
1060 6 warn "$$: requestN=$self->{requestN} < max requests=$self->{'max requests'}";
1061 6 $self->inform_others( 'daemon_accept' );
1062 }
1063 else {
1064 0 warn "Why are we moving from $current_status to $status.".
1065 " Update sent from $file line $line.\n";
1066 }
1067 8 return;
1068 }
1069  
1070 4 ▶ if($status eq 'long') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "long") 0 4 4
1071 0 DEBUG and warn "$$: Long request $self->{requestN}";
1072 0 ▶ if($current_status eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_status eq "r") 0 0 0
1073 0 $self->{scoreboard}->write($slot, 'long');
1074 0 return;
1075 }
1076 # allow to fall through if we didn't get a 'req' previous
1077 # for now this isn't important, but it might be later
1078 }
1079  
1080 4 ▶ if($status eq 'req' or $status eq 'long') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "req" or $status eq "long") 4 0 4
Condition33%1/3
$status eq "req" or $status eq "long"
l!l&&r!l&&!r
400
MC/DC0%0/2
$status eq "req" or $status eq "long"
$status eq "req" $status eq "long"
Truth table33%1/3
A or B
A= $status eq "req"B= $status eq "long"
ABresult
1X1
011
000
1081 4 ▶ if( $current_status ne 'r' and $current_status ne 'l' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_status ne "r" and $current_status ne "l") 4 0 4
Condition33%1/3
$current_status ne "r" and $current_status ne "l"
!ll&&!rl&&r
004
MC/DC0%0/2
$current_status ne "r" and $current_status ne "l"
$current_status ne "r" $current_status ne "l"
Truth table33%1/3
A and B
A= $current_status ne "r"B= $current_status ne "l"
ABresult
0X0
100
111
1082 4 $self->{requestN}++;
1083 4 DEBUG and warn "$$: Handling request $self->{requestN}";
1084 }
1085 4 $self->{scoreboard}->write($slot, $status);
1086 4 return;
1087 }
1088 0 warn "$$: Don't know what do for '$status'! Maybe you meant 'long', 'wait' or 'done' or 'req'?";
1089 }
1090  
1091  
1092 ########################################################
1093 # User code in a forked child wants to update the status
1094 sub update_status_fork_child
1095 6 ▶{
Subroutine update_status_fork_child: called CC=6 SCAR=19.0
Pod undocumented
1096 6 my($self, $status, $parm)=@_;
1097 6 ▶ return if $self->{'die'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"die"} 0 6 6
1098  
1099 6 my $slot=$self->{'my slot'};
1100 6 ▶ unless( defined $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $slot) 0 6 6
1101 0 warn "$$: NO SLOT for status=$status";
1102 0 return;
1103 }
1104  
1105 6 my $current_status=$self->{scoreboard}->read($slot);
1106  
1107 6 ▶ if($status eq 'wait' or $status eq 'done') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "wait" or $status eq "done") 2 4 6
Condition66%2/3
$status eq "wait" or $status eq "done"
l!l&&r!l&&!r
024
MC/DC50%1/2
$status eq "wait" or $status eq "done"
$status eq "wait" $status eq "done"
Truth table66%2/3
A or B
A= $status eq "wait"B= $status eq "done"
ABresult
1X1
011
000
1108 2 $self->{scoreboard}->write($slot, 'wait');
1109 2 $self->do_shutdown;
1110 2 return;
1111 }
1112  
1113 4 ▶ if($status eq 'long') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "long") 4 0 4
1114 4 $self->{scoreboard}->write($slot, $status);
1115 4 return;
1116 }
1117  
1118 0 warn "$$: Don't know what to do for '$status'! Maybe you meant 'long', 'wait' or 'done'?";
1119 }
1120  
1121 ########################################################
1122 # User code in the parent wants to update the status
1123 sub update_status_fork_parent
1124 10 ▶{
Subroutine update_status_fork_parent: called CC=2 SCAR=7.2
Pod undocumented
1125 10 my($self, $status, $parm)=@_;
1126  
1127 10 ▶ if($status eq 'req') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "req") 10 0 10
1128 10 DEBUG and warn "$$: update_status_fork_parent status = $status $parm";
1129 10 $self->fork_off( [$parm] );
1130 10 return;
1131 }
1132  
1133 0 warn "$$: Don't know what to do for '$status'! Maybe you meant 'req'?";
1134 }
1135  
1136  
1137  
1138 ########################################################
1139 sub status
1140 1 ▶{
Subroutine status: called CC=6 SCAR=24.1
Pod documented
1141 1 my($self)=@_;
1142 1 my @ret=ref($self);
1143  
1144 1 my $q1='the parent';
1145 1 ▶ $q1='a child' if $self->{'is a child'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is a child"} 1 0 1
1146 1 ▶ if($self->is_prefork) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_prefork) { } 1 0 1
elsif ($self->is_fork) { } 0 0 0
1147 1 push @ret, "Pre-forking server, we are $q1";
1148 }
1149 elsif($self->is_fork) {
1150 0 push @ret, "Forking server, we are $q1";
1151 }
1152 else {
1153 0 push @ret, "Daemon server";
1154 }
1155  
1156 1 ▶ if($self->{children}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"children"}) 0 1 1
1157 0 my $kids=$self->{children};
1158 0 push @ret, (0+keys %$kids), " children: ", sort join ' ', keys %$kids;
1159  
1160 0 my $doing=$self->{scoreboard}->read_all;
1161 0 foreach my $pid (sort keys %$kids) {
1162 0 push @ret, " $pid: $doing->[$kids->{$pid}]";
1163 }
1164 }
1165 1 return join("\n ", @ret)."\n".$self->{scoreboard}->status;
1166 }
1167  
1168 ##########################################################################
1169 package Daemon;
1170  
1171 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1172 13 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
1173  
1174 # use POE::API::Peek;
1175 13 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
1176  
1177 13 ▶use vars qw($alias);
Subroutine BEGIN: called CC=1 SCAR=0.0
1178  
1179 sub update_status
1180 18 ▶{
Subroutine update_status: called CC=1 SCAR=0.0
Pod documented
1181 18 my($package, $status, $parm)=@_;
1182  
1183 # Must be a call() to prevent the select-loop running
1184 18 $poe_kernel->call( $alias => 'update_status', $status, $parm );
1185 }
1186  
1187 sub foreign_child
1188 0 ▶{
Subroutine foreign_child: not called CC=1 SCAR=6.9
Pod documented
1189 0 my($package, $pid)=@_;
1190 0 $poe_kernel->call( $alias => 'foreign_child', $pid );
1191 }
1192  
1193 sub shutdown
1194 2 ▶{
Subroutine shutdown: called CC=1 SCAR=0.0
Pod documented
1195 2 $poe_kernel->call( $alias => 'shutdown' );
1196 }
1197  
1198 sub status
1199 1 ▶{
Subroutine status: called CC=1 SCAR=0.0
Pod documented
1200 1 return $poe_kernel->call( $alias => 'status' );
1201 }
1202  
1203 ############################################################
1204 sub peek
1205 1 ▶{
Subroutine peek: called CC=27 SCAR=65.6
Pod documented
1206 1 my($package, $verbose)=@_;
1207  
1208 1 ▶ return $POE::Component::Daemon::NO_PEEK if $POE::Component::Daemon::NO_PEEK;
Branch: 1 branch
BranchTrueFalseTotal
if $POE::Component::Daemon::NO_PEEK 1 0 1
1209 0 my $self;
1210 0 my $api=POE::API::Peek->new();
1211 0 my @queue = $api->event_queue_dump();
1212
1213 0 my $ret = "Event Queue ($POE::Component::Daemon::VERSION):\n";
1214
1215 0 my $events = {};
1216  
1217 0 foreach my $item (@queue) {
1218 0 $ret .= "\t* ID: ". $item->{ID}." - Index: ".$item->{index}."\n";
1219 0 $ret .= "\t\tPriority: ".$item->{priority}."\n";
1220 0 $ret .= "\t\tEvent: ".$item->{event}."\n";
1221  
1222 0 ▶ if($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
1223 0 ▶ if( $item->{source}->ID == $item->{destination}->ID ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{'source'}->ID == $item->{'destination'}->ID) { } 0 0 0
1224 0 $events->{ $item->{destination}->ID }{source_destination} ++;
1225 }
1226 else {
1227 0 $events->{ $item->{source}->ID }{source} ++;
1228 0 $events->{ $item->{destination}->ID }{destination} ++;
1229 }
1230 $ret .= "\t\tSource: ".
1231 0 $api->session_id_loggable($item->{source}).
1232 "\n";
1233 $ret .= "\t\tDestination: ".
1234 0 $api->session_id_loggable($item->{destination}).
1235 "\n";
1236 0 $ret .= "\t\tType: ".$item->{type}."\n";
1237 0 $ret .= "\n";
1238 }
1239 }
1240 0 ▶ $ret .="\tEMPTY\n" unless @queue;
Branch: 1 branch
BranchTrueFalseTotal
unless @queue 0 0 0
1241  
1242 0 ▶ if($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
1243 0 ▶ $ret.="Sessions: \n" if $api->session_count;
Branch: 1 branch
BranchTrueFalseTotal
if $api->session_count 0 0 0
1244 0 foreach my $session ( sort { $a->ID cmp $b->ID } $api->session_list) {
1245 0 my $ref=0;
1246 0 $ret.="\tSession ".$api->session_id_loggable($session)." ($session)";
1247  
1248 0 my $refcount=$api->get_session_refcount($session);
1249 0 $ret.="\n\t\tref count: $refcount\n";
1250  
1251 0 my $q1=$api->get_session_extref_count($session);
1252 0 $ref += $q1;
1253 0 ▶ $ret.="\t\textref count: $q1 (Stay alive)\n" if $q1;
Branch: 1 branch
BranchTrueFalseTotal
if $q1 0 0 0
1254  
1255 0 $q1=$api->session_handle_count($session->ID);
1256 0 $ref += $q1;
1257 0 ▶ $ret.="\t\thandle count: $q1 (Stay alive)\n" if $q1;
Branch: 1 branch
BranchTrueFalseTotal
if $q1 0 0 0
1258  
1259 0 my @aliases = $api->session_alias_list($session);
1260 0 $ref += @aliases;
1261 0 $q1=join ',', @aliases;
1262 0 ▶ $ret.="\t\tAliases: $q1\n" if $q1;
Branch: 1 branch
BranchTrueFalseTotal
if $q1 0 0 0
1263  
1264 0 ▶ if( $poe_kernel->can( '_data_sig_session_awaits_pids') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Daemon::poe_kernel->can('_data_sig_session_awaits_pids')) { } 0 0 0
1265 0 $q1 = $poe_kernel->_data_sig_session_awaits_pids( $session->ID );
1266 } else {
1267 0 $q1 = $poe_kernel->_data_sig_pids_ses( $session->ID );
1268 }
1269 0 $ref += $q1;
1270 0 ▶ $ret .= "\t\tPID count: $q1 (Stay alive)\n" if $q1;
Branch: 1 branch
BranchTrueFalseTotal
if $q1 0 0 0
1271  
1272 0 my @children = $api->get_session_children($session);
1273 0 ▶ if(@children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) 0 0 0
1274 0 $ref += @children;
1275 0 $q1 = join ',', map {$api->session_id_loggable($_)} @children;
1276 0 $ret.="\t\tChildren: $q1\n";
1277 }
1278  
1279 0 $q1 = $events->{ $session->ID }{source};
1280 0 ▶ if( $q1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q1) 0 0 0
1281 0 $ret.="\t\tEvent source count: $q1 (Stay alive)\n";
1282 0 $ref += $q1;
1283 }
1284  
1285 0 $q1 = $events->{ $session->ID }{destination};
1286 0 ▶ if( $q1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q1) 0 0 0
1287 0 $ret.="\t\tEvent destination count: $q1 (Stay alive)\n";
1288 0 $ref += $q1;
1289 }
1290  
1291 0 $q1 = $events->{ $session->ID }{source_destination};
1292 0 ▶ if( $q1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q1) 0 0 0
1293 0 $ret.="\t\tEvent source+destination count: $q1 (Stay alive)\n";
1294 0 $ref += $q1;
1295 }
1296  
1297 0 ▶ if($refcount != $ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refcount != $ref) 0 0 0
1298 0 $ret.="\t\tStay alive: refcount=$refcount counted=$ref\n";
1299 }
1300 0 ▶ if( $alias and grep $alias, @aliases ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Daemon::alias and grep $Daemon::alias, @aliases) 0 0 0
Condition0%0/3
$Daemon::alias and grep $Daemon::alias, @aliases
!ll&&!rl&&r
000
MC/DC0%0/2
$Daemon::alias and grep $Daemon::alias, @aliases
$Daemon::alias grep $Daemon::alias, @aliases
Truth table0%0/3
A and B
A= $Daemon::aliasB= grep $Daemon::alias, @aliases
ABresult
0X0
100
111
1301 0 my $state = $session->[ POE::Session::SE_STATES ]{_start};
1302 0 ▶ if( $state and 'ARRAY' eq ref $state and blessed $state->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state and "ARRAY" eq ref $state and builtin::blessed($state->[0])) 0 0 0
Condition0%0/6
$state and "ARRAY" eq ref $state and builtin::blessed($state->[0])
!ll&&!rl&&r
000
$state and "ARRAY" eq ref $state
!ll&&!rl&&r
000
MC/DC0%0/3
$state and "ARRAY" eq ref $state and builtin::blessed($state->[0])
$state "ARRAY" eq ref $state builtin::blessed($state->[0])
Truth table0%0/4
A and B and C
A= $stateB= "ARRAY" eq ref $stateC= builtin::blessed($state->[0])
ABCresult
0XX0
10X0
1100
1111
1303 0 $self = $state->[0];
1304 }
1305 }
1306 }
1307 }
1308  
1309 0 $ret.="\n";
1310  
1311 0 ▶ if( blessed $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) 0 0 0
1312 0 ▶ if( $self->{'my slot'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"my slot"}) 0 0 0
1313 0 my $status = $self->{scoreboard}->read($self->{'my slot'});
1314 0 $ret .= "Scoreboard slot: $self->{'my slot'}\n";
1315 0 $ret .= "Scoreboard: $status\n";
1316 }
1317 0 $ret .= $self->{scoreboard}->status;
1318 0 $ret.="\n\n";
1319 }
1320  
1321 0 $poe_kernel->sig_handled;
1322  
1323 0 ▶ unless( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
1324 0 warn "$$: $ret";
1325 0 return;
1326 }
1327 0 return $ret;
1328 }
1329  
1330 13 *__peek = \&peek;
1331  
1332  
1333  
1334  
1335 13 1;
1336 __END__
↑ Summary blib/lib/POE/Component/Daemon/ScoreboardSemaphore.pm »