blib/lib/App/Daemon.pm

filter: Statementstmt 79.0% 200 / 253 Branchbran 61.1% 77 / 126 Conditioncond 52.1% 12 / 23 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 93.3% 28 / 30 Podpod 7.1% 1 / 14 totaltotal 69.9% 321 / 459 CC 78 SCAR 28.1
CC
78
Cov
72%
cmd_line_parse
31.2
daemonize
29.6
processes_running_by_name
20.0
CRAP 16.6
↑ Summary eg/test-fork-daemon »
1 package App::Daemon;
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 16 our $VERSION = '0.22';
6  
7 16 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use Fcntl qw/:flock/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 16 our @ISA = qw(Exporter);
16 16 our @EXPORT_OK = qw(daemonize cmd_line_parse detach);
17  
18 16 ▶use constant LSB_OK => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use constant LSB_DEAD_PID_EXISTS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 16 ▶use constant LSB_DEAD_LOCK_EXISTS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶use constant LSB_NOT_RUNNING => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 16 ▶use constant LSB_UNKNOWN => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 16 ▶use constant ALREADY_RUNNING => 150;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 16 our ($pidfile, $logfile, $l4p_conf, $as_user, $as_group, $background,
26 $loglevel, $action, $appname, $default_pid_dir, $default_log_dir);
27 16 $action = "";
28 16 $appname = appname();
29  
30 16 $default_pid_dir = ".";
31 16 $default_log_dir = ".";
32  
33 16 our $kill_retries = 3;
34 16 our $kill_sig = SIGTERM; # maps to 15 via POSIX.pm
35  
36 ###########################################
37 11 ▶sub cmd_line_parse {
Subroutine cmd_line_parse: called CC=26 SCAR=34.4
Pod undocumented
38 ###########################################
39  
40 11 ▶ if( find_option("-h") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_option("-h")) 0 11 11
41 0 pod2usage();
42 }
43  
44 11 ▶ if(my $_pidfile = find_option('-p', 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $_pidfile = find_option('-p', 1)) { } 9 2 11
45 9 $pidfile = $_pidfile;
46 }
47 else {
48 2 ▶ $pidfile ||= ( "$default_pid_dir/" . $appname . ".pid" );
Condition50%1/2
$App::Daemon::pidfile ||= "$App::Daemon::default_pid_dir/" . $App::Daemon::appname . '.pid'
l!l
02
MC/DC0%0/1
$App::Daemon::pidfile ||= "$App::Daemon::default_pid_dir/" . $App::Daemon::appname . '.pid'
$App::Daemon::pidfile
Truth table50%1/2
A
A= $App::Daemon::pidfile
Aresult
11
00
49 }
50  
51 11 ▶ if(my $_logfile = find_option('-l', 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $_logfile = find_option('-l', 1)) { } 9 2 11
52 9 $logfile = $_logfile;
53 }
54 else {
55 2 ▶ $logfile ||= ( "$default_log_dir/" . $appname . ".log" );
Condition50%1/2
$App::Daemon::logfile ||= "$App::Daemon::default_log_dir/" . $App::Daemon::appname . '.log'
l!l
02
MC/DC0%0/1
$App::Daemon::logfile ||= "$App::Daemon::default_log_dir/" . $App::Daemon::appname . '.log'
$App::Daemon::logfile
Truth table50%1/2
A
A= $App::Daemon::logfile
Aresult
11
00
56 }
57  
58 11 ▶ if(my $_l4p_conf = find_option('-l4p', 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $_l4p_conf = find_option("-l4p", 1)) 0 11 11
59 0 $l4p_conf = $_l4p_conf;
60 }
61  
62 11 ▶ if(my $_as_user = find_option('-u', 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $_as_user = find_option('-u', 1)) { } 0 11 11
63 0 $as_user = $_as_user;
64 }
65 else {
66 11 ▶ $as_user ||= 'nobody';
Condition50%1/2
$App::Daemon::as_user ||= "nobody"
l!l
011
MC/DC0%0/1
$App::Daemon::as_user ||= "nobody"
$App::Daemon::as_user
Truth table50%1/2
A
A= $App::Daemon::as_user
Aresult
11
00
67 }
68  
69 11 ▶ if(my $_as_group = find_option('-g', 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $_as_group = find_option('-g', 1)) { } 0 11 11
70 0 $as_group = $_as_group;
71 }
72 else {
73 11 ▶ $as_group ||= 'nogroup';
Condition50%1/2
$App::Daemon::as_group ||= "nogroup"
l!l
011
MC/DC0%0/1
$App::Daemon::as_group ||= "nogroup"
$App::Daemon::as_group
Truth table50%1/2
A
A= $App::Daemon::as_group
Aresult
11
00
74 }
75  
76 11 ▶ if($> != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($> != 0) 11 0 11
77 # Not root? Then we're ourselves
78 11 ($as_user) = getpwuid($>);
79 11 ($as_group) = getgrgid(POSIX::getgid());
80 }
81  
82 11 ▶ $background = 1 if(!defined $background);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $App::Daemon::background 10 1 11
83 11 ▶ $background = find_option('-X') ? 0 : $background;
Branch: 1 branch
BranchTrueFalseTotal
find_option('-X') ? : 1 10 11
84  
85 11 ▶ $loglevel = $background ? $INFO : $DEBUG
Branch: 2 branches
BranchTrueFalseTotal
unless defined $App::Daemon::loglevel 11 0 11
$App::Daemon::background ? : 9 2 11
86 if(!defined $loglevel);
87 11 ▶ $loglevel = find_option('-v') ? $DEBUG : $loglevel;
Branch: 1 branch
BranchTrueFalseTotal
find_option('-v') ? : 3 8 11
88  
89 11 for (qw(start stop restart status)) {
90 26 ▶ if( find_option( $_ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_option($_)) 9 17 26
91 9 $action = $_;
92 9 last;
93 }
94 }
95
96 11 ▶ if($action eq "stop" or $action eq "status") {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Daemon::action eq "stop" or $App::Daemon::action eq "status") 5 6 11
Condition100%3/3
$App::Daemon::action eq "stop" or $App::Daemon::action eq "status"
l!l&&r!l&&!r
326
MC/DC100%2/2
$App::Daemon::action eq "stop" or $App::Daemon::action eq "status"
$App::Daemon::action eq "stop" $App::Daemon::action eq "status"
Truth table100%3/3
A or B
A= $App::Daemon::action eq "stop"B= $App::Daemon::action eq "status"
ABresult
1X1
011
000
97 5 $background = 0;
98 }
99  
100 11 ▶ if( Log::Log4perl->initialized() ) {
Branch: 4 branches
BranchTrueFalseTotal
if ('Log::Log4perl'->initialized) { } 2 9 11
elsif ($App::Daemon::action eq 'status') { } 2 7 9
elsif ($App::Daemon::l4p_conf) { } 0 7 7
elsif ($App::Daemon::logfile) { } 7 0 7
101 2 DEBUG "Log4perl already initialized, doing nothing";
102 } elsif( $action eq "status" ) {
103 2 Log::Log4perl->easy_init( $loglevel );
104 } elsif( $l4p_conf ) {
105 0 Log::Log4perl->init( $l4p_conf );
106 } elsif( $logfile ) {
107 7 my $levelstring = Log::Log4perl::Level::to_level( $loglevel );
108 7 Log::Log4perl->init(\ qq{
109 log4perl.logger = $levelstring, FileApp
110 log4perl.appender.FileApp = Log::Log4perl::Appender::File
111 log4perl.appender.FileApp.filename = $logfile
112 log4perl.appender.FileApp.owner = $as_user
113 # this umask is only temporary
114 log4perl.appender.FileApp.umask = 0133
115 log4perl.appender.FileApp.layout = PatternLayout
116 log4perl.appender.FileApp.layout.ConversionPattern = %d %m%n
117 });
118 }
119  
120 11 ▶ if(!$background) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $App::Daemon::background) 7 4 11
121 7 DEBUG "Running in foreground";
122 }
123 }
124  
125 ###########################################
126 11 ▶sub daemonize {
Subroutine daemonize: called CC=18 SCAR=33.9
Pod undocumented
127 ###########################################
128 11 cmd_line_parse();
129  
130 # Check beforehand so the user knows what's going on.
131 11 ▶ if(! -w dirname($pidfile) or -f $pidfile and ! -w $pidfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -w dirname($App::Daemon::pidfile) or -f $App::Daemon::pidfile and not -w $App::Daemon::pidfile) 0 11 11
Condition50%3/6
not -w dirname($App::Daemon::pidfile) or -f $App::Daemon::pidfile and not -w $App::Daemon::pidfile
l!l&&r!l&&!r
0011
-f $App::Daemon::pidfile and not -w $App::Daemon::pidfile
!ll&&!rl&&r
740
MC/DC0%0/3
not -w dirname($App::Daemon::pidfile) or -f $App::Daemon::pidfile and not -w $App::Daemon::pidfile
not -w dirname($App::Daemon::pidfile) -f $App::Daemon::pidfile not -w $App::Daemon::pidfile
Truth table50%2/4
not(A) or B and not(C)
A= not -w dirname($App::Daemon::pidfile)B= -f $App::Daemon::pidfileC= not -w $App::Daemon::pidfile
ABCresult
1XX1
0111
00X0
0100
132 0 my ($name,$passwd,$uid) = getpwuid($>);
133 0 LOGDIE "$pidfile not writable by user $name";
134 }
135
136 11 ▶ if($action eq "status") {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Daemon::action eq "status") 2 9 11
137 2 exit status();
138 }
139  
140 9 ▶ if($action eq "stop" or $action eq "restart") {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Daemon::action eq "stop" or $App::Daemon::action eq "restart") 3 6 9
Condition66%2/3
$App::Daemon::action eq "stop" or $App::Daemon::action eq "restart"
l!l&&r!l&&!r
306
MC/DC50%1/2
$App::Daemon::action eq "stop" or $App::Daemon::action eq "restart"
$App::Daemon::action eq "stop" $App::Daemon::action eq "restart"
Truth table66%2/3
A or B
A= $App::Daemon::action eq "stop"B= $App::Daemon::action eq "restart"
ABresult
1X1
011
000
141 3 my $exit_code = LSB_NOT_RUNNING;
142  
143 3 ▶ if(-f $pidfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $App::Daemon::pidfile) { } 2 1 3
144 2 my $pid = pid_file_read();
145 2 ▶ if(kill 0, $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid) { } 2 0 2
146 2 kill $kill_sig, $pid;
147 2 my $killed = 0;
148 2 for (1..$kill_retries) {
149 4 ▶ if(!kill 0, $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (kill 0, $pid) 2 2 4
150 2 INFO "Process $pid stopped successfully.";
151 2 ▶ unlink $pidfile or die "Can't remove $pidfile ($!)";
Branch: 1 branch
BranchTrueFalseTotal
unlink $App::Daemon::pidfile or die "Can't remove $App::Daemon::pidfile ($!)" 2 0 2
152 2 $exit_code = LSB_OK;
153 2 $killed++;
154 2 last;
155 }
156 2 INFO "Process $pid still running, waiting ...";
157 2 sleep 1;
158 }
159 2 ▶ if(! $killed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($killed) 0 2 2
160 0 ERROR "Process $pid still up, out of retries, giving up.";
161 0 $exit_code = LSB_DEAD_PID_EXISTS;
162 }
163 } else {
164 0 ERROR "Process $pid not running\n";
165 0 ▶ unlink $pidfile or die "Can't remove $pidfile ($!)";
Branch: 1 branch
BranchTrueFalseTotal
unlink $App::Daemon::pidfile or die "Can't remove $App::Daemon::pidfile ($!)" 0 0 0
166 0 $exit_code = LSB_NOT_RUNNING;
167 }
168 } else {
169 1 ERROR "According to my pidfile, there's no instance ",
170 "of me running.";
171 1 $exit_code = LSB_NOT_RUNNING;
172 }
173  
174 3 ▶ if($action eq "restart") {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Daemon::action eq 'restart') { } 0 3 3
175 0 sleep 1;
176 } else {
177 3 exit $exit_code;
178 }
179 }
180
181 6 ▶ if ( my $num = pid_file_process_running() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = pid_file_process_running()) 1 5 6
182 1 LOGWARN "Already running: $num (pidfile=$pidfile)\n";
183 1 exit ALREADY_RUNNING;
184 }
185  
186 5 ▶ if( $background ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($App::Daemon::background) { } 3 2 5
elsif ($App::Daemon::as_user) { } 2 0 2
187 3 detach( $as_user );
188 } elsif ($as_user) {
189 2 id_switch();
190 }
191  
192 3 my $prev_sig = $SIG{__DIE__};
193 3 my $master_pid = $$;
194  
195 3 DEBUG "Defining die handler";
196  
197 1 ▶ $SIG{__DIE__} = sub {
Subroutine __ANON__: called CC=6 SCAR=28.1
198 1 DEBUG __PACKAGE__, " die handler triggered.";
199 # In case we had a previously defined signal handler, call
200 # it first and add ours to the end of the chain.
201 1 ▶ $prev_sig->(@_) if ($prev_sig);
Branch: 1 branch
BranchTrueFalseTotal
if $prev_sig 0 1 1
202  
203 1 ▶ if( $master_pid != $$ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($master_pid != $$) { } 1 0 1
elsif (not defined $^S or $^S != 0) { } 0 0 0
204 # Verify that it's the main process calling the
205 # handler and not a previously forked child.
206 1 DEBUG "Die handler called for pid $$ but master pid is $master_pid";
207 ▶ } elsif( !defined $^S or $^S != 0 ) {
Condition0%0/3
not defined $^S or $^S != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $^S or $^S != 0
not defined $^S $^S != 0
Truth table0%0/3
not(A) or B
A= not defined $^SB= $^S != 0
ABresult
1X1
011
000
208 # Make sure it's not an eval{} triggering the handler.
209 0 DEBUG "Die handler called by eval. Ignored.";
210 } else {
211 0 DEBUG "Die handler removes pidfile $pidfile";
212 0 ▶ unlink $pidfile or warn "Cannot remove $pidfile";
Branch: 1 branch
BranchTrueFalseTotal
unlink $App::Daemon::pidfile or warn "Cannot remove $App::Daemon::pidfile" 0 0 0
213 }
214 3 };
215
216 3 return 1;
217 }
218  
219 ###########################################
220 5 ▶sub detach {
Subroutine detach: called CC=6 SCAR=18.3
Pod undocumented
221 ###########################################
222 5 my($as_user) = @_;
223  
224 # [rt #75219]
225 5 umask(0);
226
227 # Make sure the child isn't killed when the user closes the
228 # terminal session before the child detaches from the tty.
229 5 $SIG{'HUP'} = 'IGNORE';
230
231 5 my $child = fork();
232
233 5 ▶ if(! defined $child ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child) 0 5 5
234 0 LOGDIE "Fork failed ($!)";
235 }
236
237 5 ▶ if( $child ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 3 2 5
238 # parent doesn't do anything
239 3 exit 0;
240 }
241
242 # Become the session leader of a new session, become the
243 # process group leader of a new process group.
244 2 POSIX::setsid();
245
246 2 ▶ if( defined $pidfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $App::Daemon::pidfile) 1 1 2
247 1 INFO "Process ID is $$";
248 1 pid_file_write($$);
249 1 INFO "Written to $pidfile";
250 }
251  
252 2 ▶ if($as_user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as_user) 1 1 2
253 1 id_switch();
254 }
255
256 # close std file descriptors
257 2 ▶ if(-e "/dev/null") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e '/dev/null') { } 2 0 2
258 # On Unix, we want to point these file descriptors at /dev/null,
259 # so that any libary routines that try to read form stdin or
260 # write to stdout/err will have no effect (Stevens, APitUE, p. 426
261 # and [RT 51066].
262 2 open STDIN, '/dev/null';
263 2 open STDOUT, '>>/dev/null';
264 2 open STDERR, '>>/dev/null';
265 } else {
266 0 close(STDIN);
267 0 close(STDOUT);
268 0 close(STDERR);
269 }
270 }
271  
272 ###########################################
273 3 ▶sub id_switch {
Subroutine id_switch: called CC=4 SCAR=26.7
Pod undocumented
274 ###########################################
275 3 ▶ if($> == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($> == 0) 0 3 3
276 # If we're root, become user set as 'as_user' and the group in
277 # 'as_group'.
278  
279 # Set the group first because it only works when still root
280 0 my ($group,undef,$gid) = getgrnam($as_group);
281  
282 0 ▶ if(! defined $group) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $group) 0 0 0
283 0 LOGDIE "Cannot switch to group $as_group";
284 }
285 0 POSIX::setgid($gid);
286  
287 0 my ($name,$passwd,$uid) = getpwnam($as_user);
288 0 ▶ if(! defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
289 0 LOGDIE "Cannot switch to user $as_user";
290 }
291 0 POSIX::setuid( $uid );
292 }
293 }
294
295 ###########################################
296 2 ▶sub status {
Subroutine status: called CC=6 SCAR=19.5
Pod documented
297 ###########################################
298  
299 # Define exit codes according to
300 # http://refspecs.freestandards.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
301 2 my $exit_code = LSB_UNKNOWN;
302  
303 2 print "Pid file: $pidfile\n";
304 2 ▶ if(-f $pidfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $App::Daemon::pidfile) { } 1 1 2
305 1 my $pid = pid_file_read();
306 1 my $running = process_running($pid);
307 1 print "Pid in file: $pid\n";
308 1 ▶ print "Running: ", $running ? "yes" : "no", "\n";
Branch: 1 branch
BranchTrueFalseTotal
$running ? : 1 0 1
309 1 ▶ if($running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($running) { } 1 0 1
310 # see above
311 1 $exit_code = LSB_OK;
312 } else {
313 # see above
314 0 $exit_code = LSB_DEAD_PID_EXISTS;
315 }
316 } else {
317 1 print "No pidfile found\n";
318 1 $exit_code = LSB_NOT_RUNNING;
319 }
320  
321 2 ▶ if( proc_processtable_available() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (proc_processtable_available()) 0 2 2
322 0 my @cmdlines = processes_running_by_name( $appname );
323 0 print "Name match: ", scalar @cmdlines, "\n";
324 0 for(@cmdlines) {
325 0 print " ", $_, "\n";
326 }
327 }
328  
329 2 return $exit_code;
330 }
331  
332  
333 ###########################################
334 2 ▶sub process_running {
Subroutine process_running: called CC=4 SCAR=20.4
Pod undocumented
335 ###########################################
336 2 my($pid) = @_;
337  
338 2 my $rc = kill( 0, $pid );
339  
340 2 ▶ if( $rc ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rc) { } 2 0 2
elsif ($! == 3) { } 0 0 0
elsif ($! == 1) { } 0 0 0
341 # pseudo signal got delivered, process exists
342 2 return 1;
343 } elsif( $! == ESRCH ) {
344 # process doesn't exist
345 0 return 0;
346 } elsif( $! == EPERM ) {
347 # process does exist, but we don't have permission to
348 # send the signal
349 0 return 1;
350 }
351  
352 # Weirdness ensued.
353 0 return 0;
354 }
355  
356 ###########################################
357 0 ▶sub processes_running_by_name {
Subroutine processes_running_by_name: not called CC=4 SCAR=30.0
Pod undocumented
358 ###########################################
359 0 my($name) = @_;
360  
361 0 require Proc::ProcessTable;
362  
363 0 $name = basename($name);
364 0 my @procs = ();
365  
366 0 my $t = Proc::ProcessTable->new();
367  
368 0 foreach my $p ( @{$t->table} ){
369 0 ▶ if($p->cmndline() =~ /\b\Q${name}\E\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->cmndline =~ /\b\Q$name\E\b/) 0 0 0
370 0 ▶ next if $p->pid() == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $p->pid == $$ 0 0 0
371 0 DEBUG "Match: ", $p->cmndline();
372 0 push @procs, $p->cmndline();
373 }
374 }
375 0 return @procs;
376 }
377  
378 ###########################################
379 16 ▶sub appname {
Subroutine appname: called CC=1 SCAR=0.0
Pod undocumented
380 ###########################################
381 16 my $appname = basename($0);
382  
383 # Make sure -T regards it as untainted now
384 16 ($appname) = ($appname =~ /([\w-]+)/);
385  
386 16 return $appname;
387 }
388  
389 ###########################################
390 114 ▶sub find_option {
Subroutine find_option: called CC=4 SCAR=13.9
Pod undocumented
391 ###########################################
392 114 my($opt, $has_arg) = @_;
393  
394 114 my $idx = 0;
395  
396 114 for my $argv (@ARGV) {
397 166 ▶ if($argv eq $opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argv eq $opt) 31 135 166
398 31 ▶ if( $has_arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_arg) { } 18 13 31
399 18 my @args = splice @ARGV, $idx, 2;
400 18 return $args[1];
401 } else {
402 13 return splice @ARGV, $idx, 1;
403 }
404 }
405  
406 135 $idx++;
407 }
408  
409 83 return undef;
410 }
411  
412 ###########################################
413 0 ▶sub def_or {
Subroutine def_or: not called CC=2 SCAR=17.9
Pod undocumented
414 ###########################################
415 0 ▶ if(! defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[0]) 0 0 0
416 0 $_[0] = $_[1];
417 }
418 }
419  
420 ###########################################
421 1 ▶sub pid_file_write {
Subroutine pid_file_write: called CC=2 SCAR=7.0
Pod undocumented
422 ###########################################
423 1 my($pid) = @_;
424  
425 1 ▶ sysopen FILE, $pidfile, O_RDWR|O_CREAT, 0644 or
Branch: 1 branch
BranchTrueFalseTotal
sysopen FILE, $App::Daemon::pidfile, 66, 420 or LOGDIE("Cannot open pidfile $App::Daemon::pidfile") 1 0 1
426 LOGDIE "Cannot open pidfile $pidfile";
427 1 flock FILE, LOCK_EX;
428 1 seek(FILE, 0, 0);
429 1 print FILE "$pid\n";
430 1 close FILE;
431 }
432  
433 ###########################################
434 4 ▶sub pid_file_read {
Subroutine pid_file_read: called CC=3 SCAR=11.2
Pod undocumented
435 ###########################################
436 4 ▶ open FILE, "<$pidfile" or LOGDIE "Cannot open pidfile $pidfile";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$App::Daemon::pidfile" or LOGDIE("Cannot open pidfile $App::Daemon::pidfile") 4 0 4
437 4 flock FILE, LOCK_SH;
438 4 my $pid = <FILE>;
439 4 ▶ chomp $pid if defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pid 4 0 4
440 4 close FILE;
441 4 $pid =~ /^(\d+)$/; # Untaint
442 4 return $1;
443 }
444  
445 ###########################################
446 6 ▶sub pid_file_process_running {
Subroutine pid_file_process_running: called CC=4 SCAR=14.8
Pod undocumented
447 ###########################################
448 6 ▶ if(! -f $pidfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $App::Daemon::pidfile) 5 1 6
449 5 return undef;
450 }
451 1 my $pid = pid_file_read();
452 1 ▶ if(! $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 1 1
453 0 return undef;
454 }
455 1 ▶ if(process_running($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (process_running($pid)) 1 0 1
456 1 return $pid;
457 }
458  
459 0 return undef;
460 }
461  
462 ###########################################
463 2 ▶sub proc_processtable_available {
Subroutine proc_processtable_available: called CC=2 SCAR=7.4
Pod undocumented
464 ###########################################
465 2 my $module = "Proc::ProcessTable";
466  
467 2 eval "require $module;";
468  
469 2 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 0 2
470 2 return 0;
471 }
472  
473 0 return 1;
474 }
475  
476 16 1;
477  
478 __END__
↑ Summary eg/test-fork-daemon »