blib/lib/Daemon/Daemonize.pm

filter: Statementstmt 86.2% 100 / 116 Branchbran 53.8% 42 / 78 Conditioncond 42.3% 11 / 26 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 89.4% 17 / 19 Podpod 88.8% 8 / 9 totaltotal 67.5% 179 / 265 CC 50 SCAR 32.0
CC
50
Cov
70%
daemonize
54.0
can_signal_process
12.0
check_port
12.0
CRAP 24.5
↑ Summary
1 package Daemon::Daemonize;
2 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 $Daemon::Daemonize::VERSION = '0.0052';
4 }
5 # ABSTRACT: An easy-to-use daemon(izing) toolkit
6  
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 10 ▶use Sub::Exporter::Util qw/ curry_method /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Sub::Exporter -setup => { exports => [ map { $_ => curry_method } qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
13 daemonize
14 superclose
15 write_pidfile read_pidfile check_pidfile delete_pidfile
16 does_process_exist can_signal_process check_port
17 10 / ] };
18 10 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 14 ▶sub _fork_or_die {
Subroutine _fork_or_die: called CC=2 SCAR=7.0
23 14 my $self = shift;
24  
25 14 my $pid = fork;
26 14 ▶ confess "Unable to fork" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 14 14
27 14 return $pid;
28 }
29  
30 1 ▶sub superclose {
Subroutine superclose: called CC=6 SCAR=19.9
Pod undocumented
31 1 my $self = shift;
32 1 ▶ my $from = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
33  
34 1 my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX );
35 1 ▶ $openmax = 64 if ! defined( $openmax ) || $openmax < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $openmax or $openmax < 0 0 1 1
Condition33%1/3
not defined $openmax or $openmax < 0
l!l&&r!l&&!r
001
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
36  
37 1 ▶ return unless $from < $openmax;
Branch: 1 branch
BranchTrueFalseTotal
unless $from < $openmax 0 1 1
38  
39 1 POSIX::close( $_ ) foreach ($from .. $openmax - 1);
40 }
41  
42  
43 18 ▶sub daemonize {
Subroutine daemonize: called CC=27 SCAR=39.9
Pod documented
44 18 my $self = shift;
45 18 my %options = @_;
46  
47 {
48 18 ▶ if ( my $run = delete $options{run} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $run = delete $options{"run"}) 9 9 18
49  
50 9 ▶ if ( -1 == $self->daemonize( %options, continue => 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == $self->daemonize(%options, 'continue', 1)) { } 4 1 5
51 # We're the parent, continue on...
52 }
53 else {
54 # We've daemonized... launch into the code we've been given...
55 1 $run->();
56 1 exit 0;
57 }
58  
59 4 return; # Daemonization actually handled in call above... Abort, abort, pull-up!
60 }
61 }
62  
63 9 ▶ my $chdir = exists $options{chdir} ? $options{chdir} : '/';
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'chdir'} ? : 0 9 9
64 9 ▶ my $close = defined $options{close} ? $options{close} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'close'} ? : 0 9 9
65  
66 # Fork once to go into the background
67 {
68 9 ▶ if ( my $pid = $self->_fork_or_die ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $self->_fork_or_die) 4 5 9
69 4 ▶ return -1 if $options{continue};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"continue"} 4 0 4
70 0 exit 0;
71 }
72 }
73  
74 # Create new session
75 5 ▶ (POSIX::setsid)
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() or confess("Cannot detach from controlling process") 5 0 5
76 || confess "Cannot detach from controlling process";
77  
78 # Fork again to ensure that daemon never reacquires a control terminal
79 5 ▶ $self->_fork_or_die && exit 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_fork_or_die and exit 0 4 1 5
80  
81 # Clear the file creation mask
82 1 umask 0;
83  
84 1 ▶ if ( defined $chdir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chdir) 1 0 1
85 1 ▶ chdir $chdir or confess "Unable to chdir to \"$chdir\": $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $chdir or confess(qq[Unable to chdir to "$chdir": $!]) 1 0 1
86 }
87  
88 1 ▶ if ( $close eq 1 || $close eq '!std' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close eq 1 or $close eq "!std") 1 0 1
Condition33%1/3
$close eq 1 or $close eq "!std"
l!l&&r!l&&!r
100
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
89 # Close any open file descriptors
90 1 ▶ $self->superclose( $close eq '!std' ? 3 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$close eq '!std' ? : 0 1 1
91 }
92  
93 1 ▶ my $stdout_file = $ENV{DAEMON_DAEMONIZE_STDOUT} || $options{stdout};
Condition33%1/3
$ENV{'DAEMON_DAEMONIZE_STDOUT'} || $options{'stdout'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'DAEMON_DAEMONIZE_STDOUT'} || $options{'stdout'}
$ENV{'DAEMON_DAEMONIZE_STDOUT'} $options{'stdout'}
Truth table33%1/3
A || B
A= $ENV{'DAEMON_DAEMONIZE_STDOUT'}B= $options{'stdout'}
ABresult
1X1
011
000
94 1 ▶ my $stderr_file = $ENV{DAEMON_DAEMONIZE_STDERR} || $options{stderr};
Condition33%1/3
$ENV{'DAEMON_DAEMONIZE_STDERR'} || $options{'stderr'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'DAEMON_DAEMONIZE_STDERR'} || $options{'stderr'}
$ENV{'DAEMON_DAEMONIZE_STDERR'} $options{'stderr'}
Truth table33%1/3
A || B
A= $ENV{'DAEMON_DAEMONIZE_STDERR'}B= $options{'stderr'}
ABresult
1X1
011
000
95  
96 1 ▶ if ( $close eq 1 || $close eq 'std' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close eq 1 or $close eq "std") 1 0 1
Condition33%1/3
$close eq 1 or $close eq "std"
l!l&&r!l&&!r
100
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
97 # Re-open STDIN, STDOUT, STDERR to /dev/null
98 1 ▶ 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") 1 0 1
99  
100 1 ▶ unless ( $stdout_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stdout_file) 0 1 1
101 0 ▶ 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") 0 0 0
102 }
103  
104 1 ▶ unless ( $stderr_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stderr_file) 0 1 1
105 0 ▶ 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") 0 0 0
106 }
107  
108 # Avoid 'stdin reopened for output' warning (taken from MooseX::Daemonize)
109 1 local *_NIL;
110 1 open( _NIL, '/dev/null' );
111 1 <_NIL> if 0;
112 }
113  
114 1 ▶ if ( $stdout_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout_file) 1 0 1
115 1 ▶ open STDOUT, ">>", $stdout_file or confess "Could not redirect STDOUT to $stdout_file : $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">>", $stdout_file or confess("Could not redirect STDOUT to $stdout_file : $!") 1 0 1
116 }
117  
118 1 ▶ if ( $stderr_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr_file) 1 0 1
119 1 ▶ open STDERR, ">>", $stderr_file or confess "Could not redirect STDERR to $stderr_file : $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">>", $stderr_file or confess("Could not redirect STDERR to $stderr_file : $!") 1 0 1
120 }
121  
122 1 return 1;
123 }
124  
125 29 ▶sub _pidfile($) {
Subroutine _pidfile: called CC=3 SCAR=11.7
126 29 my $pidfile = shift;
127 29 ▶ confess "No pidfile given" unless defined $pidfile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pidfile 0 29 29
128 29 ▶ return Path::Class::File->new( ref $pidfile eq 'ARRAY' ? @$pidfile : "$pidfile" );
Branch: 1 branch
BranchTrueFalseTotal
ref $pidfile eq 'ARRAY' ? : 0 29 29
129 }
130  
131  
132 16 ▶sub read_pidfile {
Subroutine read_pidfile: called CC=4 SCAR=13.9
Pod documented
133 16 my $self = shift;
134 16 my $pidfile = _pidfile shift;
135  
136 16 ▶ return unless -s $pidfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -s $pidfile 3 13 16
137 13 ▶ return unless -f $pidfile && -r $pidfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $pidfile and -r $pidfile 2 11 13
Condition66%2/3
-f $pidfile and -r $pidfile
!ll&&!rl&&r
2011
MC/DC50%1/2
-f $pidfile and -r $pidfile
-f $pidfile -r $pidfile
Truth table66%2/3
A and B
A= -f $pidfileB= -r $pidfile
ABresult
0X0
100
111
138 11 return scalar $pidfile->slurp( chomp => 1 );
139 }
140  
141  
142 9 ▶sub check_pidfile {
Subroutine check_pidfile: called CC=3 SCAR=11.0
Pod documented
143 9 my $self = shift;
144 9 my $pidfile = _pidfile shift;
145  
146 9 my $pid = $self->read_pidfile( $pidfile );
147 9 ▶ return 0 unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 2 7 9
148 7 ▶ return 0 unless $self->does_process_exist( $pid );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->does_process_exist($pid) 1 6 7
149 6 return $pid;
150 }
151  
152  
153 3 ▶sub write_pidfile {
Subroutine write_pidfile: called CC=2 SCAR=7.0
Pod documented
154 3 my $self = shift;
155 3 my $pidfile = _pidfile shift;
156 3 ▶ my $pid = shift || $$;
Condition66%2/3
shift() || $$
l!l&&r!l&&!r
120
MC/DC0%0/2
shift() || $$
shift() $$
Truth table66%2/3
A || B
A= shift()B= $$
ABresult
1X1
011
000
157  
158 3 my $fh = $pidfile->openw;
159 3 $fh->print( $pid . "\n" );
160 3 $fh->close;
161 }
162  
163  
164 1 ▶sub delete_pidfile {
Subroutine delete_pidfile: called CC=1 SCAR=0.0
Pod documented
165 1 my $self = shift;
166 1 my $pidfile = _pidfile shift;
167  
168 1 $pidfile->remove;
169 }
170  
171  
172 7 ▶sub does_process_exist {
Subroutine does_process_exist: called CC=6 SCAR=18.8
Pod documented
173 7 my $self = shift;
174 7 my $pid = shift;
175  
176 7 ▶ croak "No pid given to check" unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 7 7
177  
178 7 ▶ return 1 if kill 0, $pid;
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pid 6 1 7
179 1 my $errno = $!;
180  
181 1 ▶ if ( eval { require Errno } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Errno } }) 1 0 1
182 1 ▶ return 1 if exists &Errno::EPERM && $errno == &Errno::EPERM;
Branch: 1 branch
BranchTrueFalseTotal
if exists &Errno::EPERM and $errno == &Errno::EPERM 0 1 1
Condition33%1/3
exists &Errno::EPERM and $errno == &Errno::EPERM
!ll&&!rl&&r
010
MC/DC0%0/2
exists &Errno::EPERM and $errno == &Errno::EPERM
exists &Errno::EPERM $errno == &Errno::EPERM
Truth table33%1/3
A and B
A= exists &Errno::EPERMB= $errno == &Errno::EPERM
ABresult
0X0
100
111
183 }
184  
185 # So $errno == ESRCH, or we don't have Errno.pm, ... just going to assume non-existent
186 1 return 0;
187 }
188  
189  
190 0 ▶sub can_signal_process {
Subroutine can_signal_process: not called CC=3 SCAR=24.8
Pod documented
191 0 my $self = shift;
192 0 my $pid = shift;
193  
194 0 ▶ croak "No pid given to check" unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
195  
196 0 ▶ return kill 0, $pid ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pid ? : 0 0 0
197 # So $! is ESRCH or EPERM or something else, so we can't signal/control it
198 }
199  
200  
201 0 ▶sub check_port {
Subroutine check_port: not called CC=3 SCAR=24.8
Pod documented
202 0 require IO::Socket::INET;
203 0 my $self = shift;
204 0 my $port = shift;
205  
206 0 ▶ croak "No port given to check" unless $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 0 0 0
207  
208 0 my $socket = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => $port, Proto => 'tcp' );
209 0 ▶ if ( $socket ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket) 0 0 0
210 0 $socket->close;
211 0 return 1;
212 }
213 0 return 0;
214 }
215  
216  
217 10 1;
218  
219 __END__
↑ Summary