| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Okay, we daemonize before running POE | ||||||||||||||||||||||||||||
| 72 | 32 | do_daemonize( $type ); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 12 | ▶ | if ( $timing eq 'after' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 6 | eval "use POE; use POE::Session;"; | |||||||||||||||||||||||||||
| 76 | 6 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 4 | ▶ | $pid && CORE::exit(0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 183 | 4 | ▶ | open STDOUT, '>/dev/null' or die "Can't open STDOUT to /dev/null: [$!]\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 4 | ▶ | open STDERR, '>&STDOUT' or die "Can't open STDERR to STDOUT: [$!]\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 4 | my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX ); | |||||||||||||||||||||||||||
| 207 | 4 | ▶ | $openmax = 64 if ! defined( $openmax ) || $openmax < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $openmax or $openmax < 0
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
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 4 | ▶ | return unless $from < $openmax; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 220 | 4 | CORE::exit 0; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # Create new session | ||||||||||||||||||||||||||||
| 225 | 8 | ▶ | (POSIX::setsid) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # Clear the file creation mask | ||||||||||||||||||||||||||||
| 232 | 4 | umask 0; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 4 | ▶ | if ( $close eq 1 || $close eq '!std' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$close eq 1 or $close eq "!std"
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"
| |||||||||||||||||||||||||||||
| 235 | # Close any open file descriptors | ||||||||||||||||||||||||||||
| 236 | 4 | ▶ | superclose( $close eq '!std' ? 3 : 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 4 | ▶ | if ( $close eq 1 || $close eq 'std' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$close eq 1 or $close eq "std"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 4 | ▶ | open( STDOUT, "+>&STDIN" ) or confess "Could not redirect STDOUT to /dev/null"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 4 | ▶ | open( STDERR, "+>&STDIN" ) or confess "Could not redirect STDERR to /dev/null"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 271 | || confess "Cannot detach from controlling process"; | ||||||||||||||||||||||||||||
| 272 | { | ||||||||||||||||||||||||||||
| 273 | 8 | $SIG{'HUP'} = 'IGNORE'; | |||||||||||||||||||||||||||
| 274 | 8 | ▶ | fork && CORE::exit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
not defined $openmax or $openmax < 0
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 288 | or confess "Could not redirect STDOUT to /dev/null"; | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | # fixup STDOUT ... | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 4 | ▶ | open(STDOUT, "+>&STDIN") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | or confess "Could not redirect STDOUT to /dev/null"; | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # fixup STDERR ... | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 4 | ▶ | open(STDERR, "+>&STDIN") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | } | ||||||||||||||||||||||||||||