mylib/ForkingDaemon.pm

filter: Statementstmt 96.5% 140 / 145 Branchbran 60.2% 53 / 88 Conditioncond 35.7% 5 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 24 / 24 Podpod n/a totaltotal 79.2% 222 / 280 CC 53 SCAR 18.8
CC
53
Cov
80%
dd_daemonize
14.8
daemon_detach
10.1
superclose
7.6
CRAP 6.6
« blib/lib/POE/Filter/Block.pm ↑ Summary blib/lib/POE/Queue/Array.pm »
1 # vim: ts=2 sw=2 expandtab
2 32 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # companion to t/90_regression/rt65460-forking.t
5  
6 32 ▶use POE::Filter::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 my $debug = 0;
12  
13 main();
14  
15 # basically ripped off from SimpleDBI::SubProcess
16 32 ▶sub main {
Subroutine main: called CC=5 SCAR=18.3
17 # Autoflush to avoid weirdness
18 32 $|++;
19  
20 # set binmode, thanks RT #43442
21 32 binmode( STDIN );
22 32 binmode( STDOUT );
23  
24 32 my $filter = POE::Filter::Reference->new();
25  
26 # Okay, now we listen for commands from our parent :)
27 32 ▶ while ( sysread( STDIN, my $buffer = '', 1024 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread STDIN, my $buffer = "", 1024) 32 0 32
28 # Feed the line into the filter
29 32 my $data = $filter->get( [ $buffer ] );
30  
31 # Process each data structure
32 32 foreach my $input ( @$data ) {
33 # should be hashref with data
34 32 ▶ if ( $input->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input->{"debug"}) 0 32 32
35 0 $debug = 1;
36 # enable tracing/asserts
37 0 eval "sub POE::Kernel::TRACE_DEFAULT () { 1 };sub POE::Kernel::ASSERT_DEFAULT () { 1 };";
38 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
39 }
40  
41 32 do_test( $input->{file}, $input->{timing}, $input->{forked}, $input->{type} );
42 12 CORE::exit( 0 );
43 }
44 }
45 }
46  
47 32 ▶sub do_test {
Subroutine do_test: called CC=7 SCAR=19.5
48 32 my ($file,$timing,$forked,$type) = @_;
49  
50 32 my $oldpid = $$;
51  
52 # hook into warnings/die
53 352 ▶ my $handler = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
54 352 my $l = $_[0];
55 352 $l =~ s/(?:\r|\n)+$//;
56 352 ▶ open my $fh, '>>', $file or die "Unable to open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $file or die "Unable to open ${file}: $!" 352 0 352
57 352 $fh->autoflush( 1 );
58 352 print $fh "$l\n";
59 352 close $fh;
60 352 return;
61 32 };
62 32 $SIG{'__WARN__'} = $handler;
63 32 $SIG{'__DIE__'} = $handler;
64  
65 # Load POE before daemonizing or after?
66 32 ▶ if ( $timing eq 'before' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timing eq "before") 16 16 32
67 16 ▶ eval "use POE; use POE::Session;";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
68 16 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 16 16
69 }
70  
71 # Okay, we daemonize before running POE
72 32 do_daemonize( $type );
73  
74 12 ▶ if ( $timing eq 'after' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timing eq "after") 6 6 12
75 6 eval "use POE; use POE::Session;";
76 6 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
77 }
78  
79 # Now we inform our test harness the PID
80 12 ▶ open my $fh, '>>', $file or die "Unable to open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $file or die "Unable to open ${file}: $!" 12 0 12
81 12 $fh->autoflush( 1 );
82 12 print $fh "OLDPID $oldpid\n";
83 12 print $fh "PID $$\n";
84  
85 # start POE and do the test!
86 12 ▶ POE::Kernel->has_forked if $forked eq 'has_fork';
Branch: 1 branch
BranchTrueFalseTotal
if $forked eq "has_fork" 6 6 12
87 12 start_poe();
88  
89 # POE finished running, inform our test harness
90 12 print $fh "DONE\n";
91 12 close $fh;
92 12 return;
93 }
94  
95 32 ▶sub do_daemonize {
Subroutine do_daemonize: called CC=5 SCAR=16.4
96 32 my $type = shift;
97  
98 32 eval {
99 32 ▶ if ( $type eq 'nsd' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'nsd') { } 8 24 32
elsif ($type eq 'dd') { } 12 12 24
elsif ($type eq 'mxd') { } 12 0 12
100 8 nsd_daemonize();
101 } elsif ( $type eq 'dd' ) {
102 12 dd_daemonize();
103 } elsif ( $type eq 'mxd' ) {
104 12 mxd_daemonize();
105 } else {
106 0 die "Unknown daemonization method: $type";
107 }
108 };
109 12 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
110 12 return;
111 }
112  
113 12 ▶sub start_poe {
Subroutine start_poe: called CC=1 SCAR=0.1
114 # start POE with a basic test to see if it handled the daemonization
115 POE::Session->create(
116 inline_states => {
117 12 ▶ _start => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
118 12 ▶ warn "STARTING TEST" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 12 12
119 12 $POE::Kernel::poe_kernel->yield( "do_test" );
120 12 return;
121 },
122 12 ▶ do_test => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
123 12 ▶ warn "STARTING DELAY" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 12 12
124 12 $POE::Kernel::poe_kernel->delay( "done" => 1 );
125 12 return;
126 },
127 12 ▶ done => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
128 12 ▶ warn "DONE WITH DELAY" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 12 12
129 12 return;
130 },
131 },
132 12 );
133  
134 12 POE::Kernel->run;
135  
136 12 return;
137 }
138  
139 # the rest of the code in this file is
140 # ripped off from Net::Server::Daemonize v0.05 as it does single-fork
141 # Removed some unnecessary code like pidfile/uid/gid/chdir stuff
142  
143 ### routine to protect process during fork
144 8 ▶sub safe_fork () {
Subroutine safe_fork: called CC=4 SCAR=14.8
145  
146 ### block signal for fork
147 8 my $sigset = POSIX::SigSet->new(SIGINT);
148 8 ▶ POSIX::sigprocmask(SIG_BLOCK, $sigset)
Branch: 1 branch
BranchTrueFalseTotal
POSIX::sigprocmask(0, $sigset) or die "Can't block SIGINT for fork: [$!]\n" 8 0 8
149 or die "Can't block SIGINT for fork: [$!]\n";
150  
151 ### fork off a child
152 8 my $pid = fork;
153 8 ▶ unless( defined $pid ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 8 8
154 0 die "Couldn't fork: [$!]\n";
155 }
156  
157 ### make SIGINT kill us as it did before
158 8 $SIG{INT} = 'DEFAULT';
159  
160 ### put back to normal
161 8 ▶ POSIX::sigprocmask(SIG_UNBLOCK, $sigset)
Branch: 1 branch
BranchTrueFalseTotal
POSIX::sigprocmask(1, $sigset) or die "Can't unblock SIGINT for fork: [$!]\n" 8 0 8
162 or die "Can't unblock SIGINT for fork: [$!]\n";
163  
164 8 return $pid;
165 }
166  
167 ### routine to completely dissociate from
168 ### terminal process.
169 8 ▶sub nsd_daemonize {
Subroutine nsd_daemonize: called CC=6 SCAR=18.6
170 8 my $pid = safe_fork();
171  
172 ### parent process should do the pid file and exit
173 8 ▶ if( $pid ){
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 4 4 8
174  
175 4 ▶ $pid && CORE::exit(0);
Branch: 1 branch
BranchTrueFalseTotal
$pid and exit 0 4 0 4
176  
177  
178 ### child process will continue on
179 }else{
180 ### close all input/output and separate
181 ### from the parent process group
182 4 ▶ open STDIN, '</dev/null' or die "Can't open STDIN from /dev/null: [$!]\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "</dev/null" or die "Can't open STDIN from /dev/null: [$!]\n" 4 0 4
183 4 ▶ open STDOUT, '>/dev/null' or die "Can't open STDOUT to /dev/null: [$!]\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">/dev/null" or die "Can't open STDOUT to /dev/null: [$!]\n" 4 0 4
184 4 ▶ open STDERR, '>&STDOUT' or die "Can't open STDERR to STDOUT: [$!]\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or die "Can't open STDERR to STDOUT: [$!]\n" 4 0 4
185  
186 ### Turn process into session leader, and ensure no controlling terminal
187 4 POSIX::setsid();
188  
189 4 return 1;
190 }
191 }
192  
193 # the rest of the code in this file is
194 # ripped off from Daemon::Daemonize v0.0052 as it does double-fork
195 # Removed some unnecessary code like pidfile/chdir stuff
196  
197 20 ▶sub _fork_or_die {
Subroutine _fork_or_die: called CC=2 SCAR=7.1
198 20 my $pid = fork;
199 20 ▶ confess "Unable to fork" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 20 20
200 20 return $pid;
201 }
202  
203 4 ▶sub superclose {
Subroutine superclose: called CC=6 SCAR=20.3
204 4 ▶ my $from = shift || 0;
Condition50%1/2
shift() || 0
l!l
04
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
205  
206 4 my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX );
207 4 ▶ $openmax = 64 if ! defined( $openmax ) || $openmax < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $openmax or $openmax < 0 0 4 4
Condition33%1/3
not defined $openmax or $openmax < 0
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $openmax or $openmax < 0
not defined $openmax $openmax < 0
Truth table33%1/3
not(A) or B
A= not defined $openmaxB= $openmax < 0
ABresult
1X1
011
000
208  
209 4 ▶ return unless $from < $openmax;
Branch: 1 branch
BranchTrueFalseTotal
unless $from < $openmax 0 4 4
210  
211 4 POSIX::close( $_ ) foreach ($from .. $openmax - 1);
212 }
213  
214 12 ▶sub dd_daemonize {
Subroutine dd_daemonize: called CC=12 SCAR=26.9
215 12 my $close = 1;
216  
217 # Fork once to go into the background
218 {
219 12 ▶ if ( my $pid = _fork_or_die() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = _fork_or_die()) 4 8 12
220 4 CORE::exit 0;
221 }
222 }
223  
224 # Create new session
225 8 ▶ (POSIX::setsid)
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() or confess("Cannot detach from controlling process") 8 0 8
226 || confess "Cannot detach from controlling process";
227  
228 # Fork again to ensure that daemon never reacquires a control terminal
229 8 ▶ _fork_or_die() && CORE::exit 0;
Branch: 1 branch
BranchTrueFalseTotal
_fork_or_die() and exit 0 4 4 8
230  
231 # Clear the file creation mask
232 4 umask 0;
233  
234 4 ▶ if ( $close eq 1 || $close eq '!std' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close eq 1 or $close eq "!std") 4 0 4
Condition33%1/3
$close eq 1 or $close eq "!std"
l!l&&r!l&&!r
400
MC/DC0%0/2
$close eq 1 or $close eq "!std"
$close eq 1 $close eq "!std"
Truth table33%1/3
A or B
A= $close eq 1B= $close eq "!std"
ABresult
1X1
011
000
235 # Close any open file descriptors
236 4 ▶ superclose( $close eq '!std' ? 3 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$close eq '!std' ? : 0 4 4
237 }
238  
239 4 ▶ if ( $close eq 1 || $close eq 'std' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close eq 1 or $close eq "std") 4 0 4
Condition33%1/3
$close eq 1 or $close eq "std"
l!l&&r!l&&!r
400
MC/DC0%0/2
$close eq 1 or $close eq "std"
$close eq 1 $close eq "std"
Truth table33%1/3
A or B
A= $close eq 1B= $close eq "std"
ABresult
1X1
011
000
240 # Re-open STDIN, STDOUT, STDERR to /dev/null
241 4 ▶ open( STDIN, "+>/dev/null" ) or confess "Could not redirect STDIN to /dev/null";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "+>/dev/null" or confess("Could not redirect STDIN to /dev/null") 4 0 4
242  
243 4 ▶ open( STDOUT, "+>&STDIN" ) or confess "Could not redirect STDOUT to /dev/null";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, "+>&STDIN" or confess("Could not redirect STDOUT to /dev/null") 4 0 4
244  
245 4 ▶ open( STDERR, "+>&STDIN" ) or confess "Could not redirect STDERR to /dev/null";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, "+>&STDIN" or confess("Could not redirect STDERR to /dev/null") 4 0 4
246  
247 # Avoid 'stdin reopened for output' warning (taken from MooseX::Daemonize)
248 4 local *_NIL;
249 4 open( _NIL, '/dev/null' );
250 4 <_NIL> if 0;
251 }
252  
253 4 return 1;
254 }
255  
256 # the rest of the code in this file is
257 # ripped off from MooseX::Daemonize::Core v0.12 as it does some weird things ;)
258 # Removed some unnecessary code like Moose stuff
259  
260 12 ▶sub daemon_fork {
Subroutine daemon_fork: called CC=2 SCAR=6.9
261 12 ▶ if (my $pid = fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 4 8 12
262 4 CORE::exit( 0 );
263 } else {
264 # now in the daemon
265 8 return;
266 }
267 }
268  
269 8 ▶sub daemon_detach {
Subroutine daemon_detach: called CC=9 SCAR=23.2
270 8 ▶ (POSIX::setsid) # set session id
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() or confess("Cannot detach from controlling process") 8 0 8
271 || confess "Cannot detach from controlling process";
272 {
273 8 $SIG{'HUP'} = 'IGNORE';
274 8 ▶ fork && CORE::exit;
Branch: 1 branch
BranchTrueFalseTotal
fork and exit 4 4 8
275 }
276 4 umask 0; # clear the file creation mask
277  
278 # get the max numnber of possible file descriptors
279 4 my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX );
280 4 ▶ $openmax = 64 if !defined($openmax) || $openmax < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $openmax or $openmax < 0 0 4 4
Condition33%1/3
not defined $openmax or $openmax < 0
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $openmax or $openmax < 0
not defined $openmax $openmax < 0
Truth table33%1/3
not(A) or B
A= not defined $openmaxB= $openmax < 0
ABresult
1X1
011
000
281  
282 # close them all
283 4 POSIX::close($_) foreach (0 .. $openmax);
284  
285 # fixup STDIN ...
286  
287 4 ▶ open(STDIN, "+>/dev/null")
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "+>/dev/null" or confess("Could not redirect STDOUT to /dev/null") 4 0 4
288 or confess "Could not redirect STDOUT to /dev/null";
289  
290 # fixup STDOUT ...
291  
292 4 ▶ open(STDOUT, "+>&STDIN")
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, "+>&STDIN" or confess("Could not redirect STDOUT to /dev/null") 4 0 4
293 or confess "Could not redirect STDOUT to /dev/null";
294  
295 # fixup STDERR ...
296  
297 4 ▶ open(STDERR, "+>&STDIN")
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, "+>&STDIN" or confess("Could not redirect STDERR to /dev/null") 4 0 4
298 or confess "Could not redirect STDERR to /dev/null"; ;
299  
300 # do a little house cleaning ...
301  
302 # Avoid 'stdin reopened for output'
303 # warning with newer perls
304 4 open( NULLFH, '/dev/null' );
305 4 <NULLFH> if (0);
306  
307 # return success
308 4 return 1;
309 }
310  
311 12 ▶sub mxd_daemonize {
Subroutine mxd_daemonize: called CC=1 SCAR=0.0
312 12 daemon_fork();
313 8 daemon_detach();
314 }
« blib/lib/POE/Filter/Block.pm ↑ Summary blib/lib/POE/Queue/Array.pm »