lib/Daemon/Generic.pm

filter: Statementstmt 50.6% 157 / 310 Branchbran 40.7% 66 / 162 Conditioncond 27.9% 26 / 93 MC/DCmcdc 10.8% 5 / 46 Subroutinesub 43.1% 22 / 51 Podpod 82.0% 32 / 39 totaltotal 43.9% 308 / 701 CC 117 SCAR 43.1
CC
117
Cov
44%
new
177.5
gd_kill
120.1
gd_can_install
56.0
CRAP 74.6
↑ Summary lib/Daemon/Generic/While1.pm »
1  
2 package Daemon::Generic;
3  
4 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 require Exporter;
7 33 require POSIX;
8 33 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use File::Flock::Forking;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 33 ▶use File::Flock;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 33 our @ISA = qw(Exporter);
13 33 our @EXPORT = qw(newdaemon);
14  
15 33 our $VERSION = 0.85;
16  
17 33 our $force_quit_delay = 15;
18 33 our $package = __PACKAGE__;
19 33 our $caller;
20  
21 sub newdaemon
22 33 ▶{
Subroutine newdaemon: called CC=4 SCAR=15.4
Pod undocumented
23 33 my (%args) = @_;
24 33 ▶ my $pkg = $caller || caller() || 'main';
Condition40%2/5
$Daemon::Generic::caller || caller() || 'main'
l!l
330
$Daemon::Generic::caller or caller()
l!l&&r!l&&!r
3300
MC/DC0%0/2
$Daemon::Generic::caller || caller() || 'main'
$Daemon::Generic::caller caller()
Truth table33%1/3
A or B
A= $Daemon::Generic::callerB= caller()
ABresult
1X1
011
000
25  
26 33 my $foo = bless {}, $pkg;
27  
28 33 ▶ unless ($foo->isa($package)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($foo->isa($Daemon::Generic::package)) 0 33 33
29 33 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 0 my $isa = \@{"${pkg}::ISA"};
31 0 unshift(@$isa, $package);
32 }
33  
34 33 bless $foo, 'This::Package::Does::Not::Exist';
35 33 undef $foo;
36  
37 33 new($pkg, %args);
38 }
39  
40 sub new
41 33 ▶{
Subroutine new: called CC=52 SCAR=51.8
Pod undocumented
42 33 my ($pkg, %args) = @_;
43  
44 33 ▶ if ($pkg eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg eq __PACKAGE__) 0 33 33
45 0 ▶ $pkg = caller() || 'main';
Condition0%0/2
caller() || 'main'
l!l
00
MC/DC0%0/1
caller() || 'main'
caller()
Truth table0%0/2
A
A= caller()
Aresult
11
00
46 }
47  
48 srand(time ^ ($$ << 5))
49 33 ▶ unless $args{no_srand};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"no_srand"} 33 0 33
50  
51 33 my $av0 = $0;
52 33 $av0 =~ s!/!/.!g;
53  
54 my $self = {
55 gd_av0 => $av0,
56 gd_args => \%args,
57 gd_pidfile => $args{pidfile},
58 gd_logpriority => $args{logpriority},
59 gd_progname => $args{progname}
60 ? $args{progname}
61 : $0,
62 gd_pidbase => $args{pidbase}
63 ? $args{pidbase}
64 : ($args{progname}
65 ? "/var/run/$args{progname}"
66 : "/var/run/$av0"),
67 gd_foreground => $args{foreground} || 0,
68 configfile => $args{configfile}
69 ? $args{configfile}
70 : ($args{progname}
71 ? "/etc/$args{progname}.conf"
72 : "/etc/$av0"),
73 33 ▶ debug => $args{debug} || 0,
Branch: 5 branches
BranchTrueFalseTotal
$args{'progname'} ? : 0 33 33
$args{'pidbase'} ? : 0 33 33
$args{'progname'} ? : 0 33 33
$args{'configfile'} ? : 0 33 33
$args{'progname'} ? : 0 33 33
Condition50%2/4
$args{'foreground'} || 0
l!l
033
$args{'debug'} || 0
l!l
033
MC/DC0%0/1
$args{'foreground'} || 0
$args{'foreground'}
MC/DC0%0/1
$args{'debug'} || 0
$args{'debug'}
Truth table50%1/2
A
A= $args{'foreground'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'debug'}
Aresult
11
00
74 };
75 33 bless $self, $pkg;
76  
77 33 $self->gd_getopt;
78 33 $self->gd_parse_argv;
79  
80 33 my $do = $self->{do} = $ARGV[0];
81  
82 33 ▶ $self->gd_help if $do eq 'help';
Branch: 1 branch
BranchTrueFalseTotal
if $do eq "help" 0 33 33
83 33 ▶ $self->gd_version if $do eq 'version';
Branch: 1 branch
BranchTrueFalseTotal
if $do eq "version" 0 33 33
84 33 ▶ $self->gd_install if $do eq 'install';
Branch: 1 branch
BranchTrueFalseTotal
if $do eq "install" 0 33 33
85 33 ▶ $self->gd_uninstall if $do eq 'uninstall';
Branch: 1 branch
BranchTrueFalseTotal
if $do eq "uninstall" 0 33 33
86  
87 33 ▶ $self->gd_pidfile unless $self->{gd_pidfile};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"gd_pidfile"} 33 0 33
88  
89 33 my %newconfig = $self->gd_preconfig;
90  
91 33 ▶ $self->{gd_pidfile} = $newconfig{pidfile} if $newconfig{pidfile};
Branch: 1 branch
BranchTrueFalseTotal
if $newconfig{"pidfile"} 33 0 33
92  
93 33 ▶ print "PIDFILE=$self->{gd_pidfile}\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 33 33
94  
95 33 ▶ print "Configuration looks okay\n" if $do eq 'check';
Branch: 1 branch
BranchTrueFalseTotal
if $do eq "check" 12 21 33
96  
97 33 my $pidfile = $self->{gd_pidfile};
98 33 my $killed = 0;
99 33 my $locked = 0;
100 33 ▶ if (-e $pidfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $pidfile) { } 21 12 33
101 21 ▶ if ($locked = lock($pidfile, undef, 'nonblocking')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locked = Daemon::Generic::lock($pidfile, undef, 'nonblocking')) { } 0 21 21
102 # old process is dead
103 0 ▶ if ($do eq 'status') {
Branch: 1 branch
BranchTrueFalseTotal
if ($do eq "status") 0 0 0
104 0 print "$self->{gd_progname} dead\n";
105 0 exit 1;
106 }
107 } else {
108 21 ▶ sleep(2) if -M $pidfile < 2/86400;
Branch: 1 branch
BranchTrueFalseTotal
if -M $pidfile < "2.31481481481481e-05" 12 9 21
109 21 my $oldpid = read_file($pidfile);
110 21 chomp($oldpid);
111 21 ▶ if ($oldpid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oldpid) { } 21 0 21
112 21 ▶ if ($do eq 'stop' or $do eq 'restart') {
Branch: 5 branches
BranchTrueFalseTotal
if ($do eq 'stop' or $do eq 'restart') { } 12 9 21
elsif ($do eq 'reload') { } 3 6 9
elsif ($do eq 'status') { } 0 6 6
elsif ($do eq 'check') { } 6 0 6
elsif ($do eq 'start' or $do eq 'debug') { } 0 0 0
Condition100%3/3
$do eq "stop" or $do eq "restart"
l!l&&r!l&&!r
669
MC/DC100%2/2
$do eq "stop" or $do eq "restart"
$do eq "stop" $do eq "restart"
Truth table100%3/3
A or B
A= $do eq "stop"B= $do eq "restart"
ABresult
1X1
011
000
113 12 $killed = $self->gd_kill($oldpid);
114 12 $locked = lock($pidfile);
115 12 ▶ if ($do eq 'stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($do eq "stop") 6 6 12
116 6 unlink($pidfile);
117 6 exit;
118 }
119 } elsif ($do eq 'reload') {
120 3 ▶ if (kill(1,$oldpid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 1, $oldpid) { } 3 0 3
121 3 print "Requested reconfiguration\n";
122 3 exit;
123 } else {
124 0 print "Kill failed: $!\n";
125 }
126 } elsif ($do eq 'status') {
127 0 ▶ if (kill(0,$oldpid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $oldpid) { } 0 0 0
128 0 print "$self->{gd_progname} running - pid $oldpid\n";
129 0 $self->gd_check($pidfile, $oldpid);
130 0 exit 0;
131 } else {
132 0 print "$self->{gd_progname} dead\n";
133 0 exit 1;
134 }
135 } elsif ($do eq 'check') {
136 6 ▶ if (kill(0,$oldpid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $oldpid) 6 0 6
137 6 print "$self->{gd_progname} running - pid $oldpid\n";
138 6 $self->gd_check($pidfile, $oldpid);
139 6 exit;
140 }
141 ▶ } elsif ($do eq 'start' || $do eq 'debug') {
Condition0%0/3
$do eq "start" or $do eq "debug"
l!l&&r!l&&!r
000
MC/DC0%0/2
$do eq "start" or $do eq "debug"
$do eq "start" $do eq "debug"
Truth table0%0/3
A or B
A= $do eq "start"B= $do eq "debug"
ABresult
1X1
011
000
142 0 print "\u$self->{gd_progname} is already running (pid $oldpid)\n";
143 0 exit; # according to LSB, this is no error
144 }
145 } else {
146 0 $self->gd_error("Pid file $pidfile is invalid but locked, exiting\n");
147 }
148 }
149 } else {
150 12 ▶ $locked = lock($pidfile, undef, 'nonblocking')
Branch: 1 branch
BranchTrueFalseTotal
$locked = Daemon::Generic::lock($pidfile, undef, "nonblocking") or die "Could not lock pid file ${pidfile}: $!" 12 0 12
151 or die "Could not lock pid file $pidfile: $!";
152 }
153  
154 18 ▶ if ($do eq 'reload' || $do eq 'stop' || $do eq 'check' || ($do eq 'restart' && ! $killed)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do eq "reload" or $do eq "stop" or $do eq "check" or $do eq "restart" and not $killed) 9 9 18
Condition75%9/12
$do eq "reload" or $do eq "stop" or $do eq "check" or $do eq "restart" and not $killed
l!l&&r!l&&!r
639
$do eq "reload" or $do eq "stop" or $do eq "check"
l!l&&r!l&&!r
0612
$do eq "reload" or $do eq "stop"
l!l&&r!l&&!r
0018
$do eq "restart" and not $killed
!ll&&!rl&&r
363
MC/DC60%3/5
$do eq "reload" or $do eq "stop" or $do eq "check" or $do eq "restart" and not $killed
$do eq "reload" $do eq "stop" $do eq "check" $do eq "restart" not $killed
Truth table66%4/6
A or B or C or D and not(E)
A= $do eq "reload"B= $do eq "stop"C= $do eq "check"D= $do eq "restart"E= not $killed
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
155 9 print "No $self->{gd_progname} running\n";
156 }
157  
158 18 ▶ if ($do eq 'stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($do eq "stop") 0 18 18
159 0 unlink($pidfile);
160 0 exit;
161 }
162  
163 18 ▶ if ($do eq 'status') {
Branch: 1 branch
BranchTrueFalseTotal
if ($do eq "status") 0 18 18
164 0 print "No $self->{gd_progname} running\n";
165 0 exit 3;
166 }
167  
168 18 ▶ if ($do eq 'check') {
Branch: 1 branch
BranchTrueFalseTotal
if ($do eq "check") 6 12 18
169 6 $self->gd_check($pidfile);
170 exit
171 6 }
172  
173 12 ▶ unless ($do eq 'reload' || $do eq 'restart' || $do eq 'start' || $do eq 'debug') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($do eq "reload" or $do eq "restart" or $do eq "start" or $do eq "debug") 0 12 12
Condition55%5/9
$do eq "reload" or $do eq "restart" or $do eq "start" or $do eq "debug"
l!l&&r!l&&!r
1200
$do eq "reload" or $do eq "restart" or $do eq "start"
l!l&&r!l&&!r
930
$do eq "reload" or $do eq "restart"
l!l&&r!l&&!r
093
MC/DC0%0/4
$do eq "reload" or $do eq "restart" or $do eq "start" or $do eq "debug"
$do eq "reload" $do eq "restart" $do eq "start" $do eq "debug"
Truth table40%2/5
A or B or C or D
A= $do eq "reload"B= $do eq "restart"C= $do eq "start"D= $do eq "debug"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
174 0 $self->gd_other_cmd($do, $locked);
175 }
176  
177 12 ▶ unless ($self->{gd_foreground} || $do eq 'debug') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"gd_foreground"} or $do eq "debug") 12 0 12
Condition33%1/3
$self->{"gd_foreground"} or $do eq "debug"
l!l&&r!l&&!r
0012
MC/DC0%0/2
$self->{"gd_foreground"} or $do eq "debug"
$self->{"gd_foreground"} $do eq "debug"
Truth table33%1/3
A or B
A= $self->{"gd_foreground"}B= $do eq "debug"
ABresult
1X1
011
000
178 12 $self->gd_daemonize;
179 }
180  
181 12 ▶ $locked or lock($pidfile, undef, 'nonblocking')
Branch: 1 branch
BranchTrueFalseTotal
$locked or Daemon::Generic::lock($pidfile, undef, "nonblocking") or die "Could not lock PID file ${pidfile}: $!" 12 0 12
Condition33%1/3
$locked or Daemon::Generic::lock($pidfile, undef, "nonblocking")
l!l&&r!l&&!r
1200
MC/DC0%0/2
$locked or Daemon::Generic::lock($pidfile, undef, "nonblocking")
$locked Daemon::Generic::lock($pidfile, undef, "nonblocking")
Truth table33%1/3
A or B
A= $lockedB= Daemon::Generic::lock($pidfile, undef, "nonblocking")
ABresult
1X1
011
000
182 or die "Could not lock PID file $pidfile: $!";
183  
184 12 write_file($pidfile, "$$\n");
185  
186 12 print STDERR "Starting up...\n";
187  
188 12 $self->gd_postconfig(%newconfig);
189  
190 12 $self->gd_setup_signals;
191  
192 12 $self->gd_run;
193  
194 8 unlink($pidfile);
195 8 exit(0);
196 }
197  
198 12 ▶sub gd_check {}
Subroutine gd_check: called CC=1 SCAR=0.0
Pod documented
199  
200 33 ▶sub gd_more_opt { return() }
Subroutine gd_more_opt: called CC=1 SCAR=0.0
Pod documented
201  
202 sub gd_getopt
203 33 ▶{
Subroutine gd_getopt: called CC=7 SCAR=24.4
Pod documented
204 33 my $self = shift;
205 33 Getopt::Long::Configure("auto_version");
206 GetOptions(
207 'configfile=s' => \$self->{configfile},
208 'foreground!' => \$self->{gd_foreground},
209 'debug!' => \$self->{debug},
210 $self->{gd_args}{options}
211 0 ▶ ? %{$self->{gd_args}{options}}
Branch: 2 branches
BranchTrueFalseTotal
&GetOptions("configfile=s", \$self->{"configfile"}, "foreground!", \$self->{"gd_foreground"}, "debug!", \$self->{"debug"}, $self->{"gd_args"}{"options"} ? %{$$self{"gd_args"}{"options"};} : (), $self->gd_more_opt) or exit $self->gd_usage 33 0 33
$self->{'gd_args'}{'options'} ? : 0 33 33
212 : (),
213 $self->gd_more_opt(),
214 ) or exit($self->gd_usage());
215  
216 33 ▶ if (@ARGV < ($self->{gd_args}{minimum_args} || 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV < ($self->{"gd_args"}{"minimum_args"} || 1)) 0 33 33
Condition50%1/2
$self->{'gd_args'}{'minimum_args'} || 1
l!l
033
MC/DC0%0/1
$self->{'gd_args'}{'minimum_args'} || 1
$self->{'gd_args'}{'minimum_args'}
Truth table50%1/2
A
A= $self->{'gd_args'}{'minimum_args'}
Aresult
11
00
217 0 exit($self->gd_usage());
218 }
219 33 ▶ if (@ARGV > ($self->{gd_args}{maximum_args} || 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV > ($self->{"gd_args"}{"maximum_args"} || 1)) 0 33 33
Condition50%1/2
$self->{'gd_args'}{'maximum_args'} || 1
l!l
033
MC/DC0%0/1
$self->{'gd_args'}{'maximum_args'} || 1
$self->{'gd_args'}{'maximum_args'}
Truth table50%1/2
A
A= $self->{'gd_args'}{'maximum_args'}
Aresult
11
00
220 0 exit($self->gd_usage());
221 }
222 }
223  
224 33 ▶sub gd_parse_argv { }
Subroutine gd_parse_argv: called CC=1 SCAR=0.0
Pod documented
225  
226 sub gd_help
227 0 ▶{
Subroutine gd_help: not called CC=1 SCAR=6.9
Pod documented
228 0 my $self = shift;
229 0 exit($self->gd_usage($self->{gd_args}));
230 }
231  
232 sub gd_version
233 0 ▶{
Subroutine gd_version: not called CC=5 SCAR=29.9
Pod documented
234 0 my $self = shift;
235 33 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
236 my $v = $self->{gd_args}{version}
237 0 ▶ || ${ref($self)."::VERSION"}
Condition0%0/11
$self->{'gd_args'}{'version'} || ${ref($self) . '::VERSION';} || $main::VERSION || $main::VERSION || '?'
l!l
00
$self->{"gd_args"}{"version"} or ${ref($self) . '::VERSION';} or $main::VERSION or $main::VERSION
l!l&&r!l&&!r
000
$self->{"gd_args"}{"version"} or ${ref($self) . '::VERSION';} or $main::VERSION
l!l&&r!l&&!r
000
$self->{"gd_args"}{"version"} or ${ref($self) . '::VERSION';}
l!l&&r!l&&!r
000
MC/DC0%0/4
$self->{'gd_args'}{'version'} || ${ref($self) . '::VERSION';} || $main::VERSION || $main::VERSION || '?'
$self->{"gd_args"}{"version"} ${ref($self) . '::VERSION';} $main::VERSION $main::VERSION
Truth table0%0/5
A or B or C or D
A= $self->{"gd_args"}{"version"}B= ${ref($self) . '::VERSION';}C= $main::VERSIOND= $main::VERSION
ABCDresult
1XXX1
01XX1
001X1
00011
00000
238 || $::VERSION
239 || $main::VERSION
240 || "?";
241 0 print "$self->{gd_progname} - version $v\n";;
242 0 exit;
243 }
244  
245 sub gd_pidfile
246 33 ▶{
Subroutine gd_pidfile: called CC=1 SCAR=0.0
Pod documented
247 33 my $self = shift;
248 33 my $x = $self->{configfile};
249 33 $x =~ s!/!.!g;
250 33 $self->{gd_pidfile} = "$self->{gd_pidbase}$x.pid";
251 }
252  
253 sub gd_other_cmd
254 0 ▶{
Subroutine gd_other_cmd: not called CC=1 SCAR=6.9
Pod documented
255 0 my $self = shift;
256 0 $self->gd_usage;
257 0 exit(1);
258 }
259  
260 sub gd_redirect_output
261 12 ▶{
Subroutine gd_redirect_output: called CC=5 SCAR=17.5
Pod documented
262 12 my $self = shift;
263 12 ▶ return if $self->{gd_foreground};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"gd_foreground"} 0 12 12
264 12 ▶ open(STDOUT, ">/dev/null") or die("open >/dev/null: $!");
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">/dev/null" or die "open >/dev/null: $!" 12 0 12
265 12 ▶ open(STDIN, "</dev/null") or die("open </dev/null: $!");
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "</dev/null" or die "open </dev/null: $!" 12 0 12
266 12 ▶ open(STDERR, ">&STDOUT") or tmpdie("dup stdout > stderr: $!");
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or tmpdie("dup stdout > stderr: $!") 12 0 12
267 }
268  
269 sub gd_reopen_output
270 12 ▶{
Subroutine gd_reopen_output: called CC=5 SCAR=16.6
Pod documented
271 12 my $self = shift;
272 12 ▶ return if $self->{gd_foreground};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"gd_foreground"} 0 12 12
273 12 my $logname = $self->gd_logname;
274 12 ▶ my $p = $self->{gd_logpriority} ? "-p $self->{gd_logpriority}" : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'gd_logpriority'} ? : 0 12 12
275 12 ▶ open(STDERR, "|logger $p -t '$logname'") or tmpdie("open |logger $p -t $logname: $!");
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, "|logger $p -t '${logname}'" or tmpdie("open |logger $p -t ${logname}: $!") 12 0 12
276 12 ▶ open(STDOUT, ">&STDERR") or tmpdie("dup stderr > stdout: $!");
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&STDERR" or tmpdie("dup stderr > stdout: $!") 12 0 12
277 12 select(STDERR);
278 12 $| = 1;
279 12 select(STDOUT);
280 12 $| = 1;
281 }
282  
283 sub gd_daemonize
284 12 ▶{
Subroutine gd_daemonize: called CC=5 SCAR=16.6
Pod documented
285 12 my $self = shift;
286 12 ▶ open(TMPERR, ">&STDERR") or die "dup STDERR > TMPERR: $!";
Branch: 1 branch
BranchTrueFalseTotal
open TMPERR, ">&STDERR" or die "dup STDERR > TMPERR: $!" 12 0 12
287  
288 12 print "Starting $self->{gd_progname} server\n";
289 12 $self->gd_redirect_output();
290 12 my $pid;
291 12 ▶ POSIX::_exit(0) if $pid = fork;
Branch: 1 branch
BranchTrueFalseTotal
if $pid = fork 0 12 12
292 12 ▶ tmpdie("Could not fork: $!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 12 12
293 unless defined $pid;
294  
295 12 POSIX::setsid();
296  
297 12 $self->gd_reopen_output();
298 12 ▶ print "Sucessfully daemonized\n"
Branch: 1 branch
BranchTrueFalseTotal
print "Sucessfully daemonized\n" or tmpdie("write to |logger: $!") 12 0 12
299 or tmpdie("write to |logger: $!");
300  
301 12 close(TMPERR);
302 }
303  
304 sub tmpdie
305 0 ▶{
Subroutine tmpdie: not called CC=2 SCAR=17.9
Pod undocumented
306 0 my $msg = "@_";
307 0 ▶ $msg .= sprintf(" at %s line %d\n", (caller())[1,2]) unless $msg =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n$/ 0 0 0
308 0 print TMPERR $msg;
309 0 exit 1;
310 }
311  
312 sub gd_logname
313 12 ▶{
Subroutine gd_logname: called CC=1 SCAR=0.0
Pod documented
314 12 my $self = shift;
315 12 return $self->{gd_progname}."[$$]";
316 }
317  
318 sub gd_reconfig_event
319 2 ▶{
Subroutine gd_reconfig_event: called CC=1 SCAR=0.0
Pod documented
320 2 my $self = shift;
321 2 print STDERR "Reconfiguration requested\n";
322 2 $self->gd_postconfig($self->gd_preconfig());
323 }
324  
325 sub gd_quit_event
326 0 ▶{
Subroutine gd_quit_event: not called CC=1 SCAR=6.9
Pod documented
327 0 my $self = shift;
328 0 print STDERR "Quitting...\n";
329 0 exit(0);
330 }
331  
332 sub gd_setup_signals
333 0 ▶{
Subroutine gd_setup_signals: not called CC=1 SCAR=6.9
Pod documented
334 0 my $self = shift;
335 0 ▶ $SIG{INT} = sub { $self->gd_quit_event() };
Subroutine __ANON__: not called CC=1 SCAR=6.9
336 0 ▶ $SIG{HUP} = sub { $self->gd_reconfig_event() };
Subroutine __ANON__: not called CC=1 SCAR=6.9
337 }
338  
339 0 ▶sub gd_run { die "must defined gd_run()" }
Subroutine gd_run: not called CC=1 SCAR=6.9
Pod documented
340  
341 sub gd_error
342 0 ▶{
Subroutine gd_error: not called CC=3 SCAR=24.8
Pod documented
343 0 my $self = shift;
344 0 my $e = shift;
345 0 my $do = $self->{do};
346 0 ▶ if ($do && $do eq 'stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($do and $do eq 'stop') { } 0 0 0
Condition0%0/3
$do and $do eq "stop"
!ll&&!rl&&r
000
MC/DC0%0/2
$do and $do eq "stop"
$do $do eq "stop"
Truth table0%0/3
A and B
A= $doB= $do eq "stop"
ABresult
0X0
100
111
347 0 warn $e;
348 } else {
349 0 die $e;
350 }
351 }
352  
353 0 ▶sub gd_flags_more { return () }
Subroutine gd_flags_more: not called CC=1 SCAR=6.9
Pod documented
354  
355 sub gd_flags
356 0 ▶{
Subroutine gd_flags: not called CC=1 SCAR=6.9
Pod undocumented
357 0 my $self = shift;
358 return (
359 0 '-c file' => "Specify configuration file (instead of $self->{configfile})",
360 '-f' => "Run in the foreground (don't detach)",
361 $self->gd_flags_more
362 );
363 }
364  
365 0 ▶sub gd_commands_more { return () }
Subroutine gd_commands_more: not called CC=1 SCAR=6.9
Pod documented
366  
367 sub gd_commands
368 0 ▶{
Subroutine gd_commands: not called CC=3 SCAR=24.8
Pod undocumented
369 0 my $self = shift;
370 return (
371 0 ▶ start => "Starts a new $self->{gd_progname} if there isn't one running already",
Branch: 2 branches
BranchTrueFalseTotal
$self->gd_can_install ? : 0 0 0
$self->gd_can_uninstall ? : 0 0 0
372 stop => "Stops a running $self->{gd_progname}",
373 reload => "Causes a running $self->{gd_progname} to reload it's config file. Starts a new one if none is running.",
374 restart => "Stops a running $self->{gd_progname} if one is running. Starts a new one.",
375 $self->gd_commands_more(),
376 ($self->gd_can_install()
377 ? ('install' => "Setup $self->{gd_progname} to run automatically after reboot")
378 : ()),
379 ($self->gd_can_uninstall()
380 ? ('uninstall' => "Do not run $self->{gd_progname} after reboots")
381 : ()),
382 check => "Check the configuration file and report the daemon state",
383 help => "Display this usage info",
384 version => "Display the version of $self->{gd_progname}",
385 debug => "Starts a new $self->{gd_progname} in the foreground",
386 )
387 }
388  
389 0 ▶sub gd_positional_more { return() }
Subroutine gd_positional_more: not called CC=1 SCAR=6.9
Pod documented
390  
391 sub gd_alts
392 0 ▶{
Subroutine gd_alts: not called CC=2 SCAR=17.9
Pod undocumented
393 0 my $offset = shift;
394 0 my @results;
395 0 ▶ for (my $i = $offset; $i <= $#_; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#_) 0 0 0
396 0 push(@results, $_[$i]);
397 }
398 0 return @results;
399 }
400  
401 sub gd_usage
402 0 ▶{
Subroutine gd_usage: not called CC=3 SCAR=24.8
Pod documented
403 0 my $self = shift;
404  
405 0 require Text::Wrap;
406 0 import Text::Wrap;
407  
408 0 my $col = 15;
409  
410 0 my @flags = $self->gd_flags;
411 0 my @commands = $self->gd_commands;
412 0 my @positional = $self->gd_positional_more;
413  
414 0 my $summary = "Usage: $self->{gd_progname} ";
415 0 my $details = '';
416 0 for my $i (gd_alts(0, @flags)) {
417 0 $summary .= "[ $i ] ";
418 }
419 0 $summary .= "{ ";
420 0 $summary .= join(" | ", gd_alts(0, @commands));
421 0 $summary .= " } ";
422 0 $summary .= join(" ", gd_alts(0, @positional));
423  
424 0 my (@all) = (@flags, @commands, @positional);
425 0 ▶ while (@all) {
Branch: 1 branch
BranchTrueFalseTotal
if (@all) 0 0 0
426 0 my ($key, $desc) = splice(@all, 0, 2);
427 0 local($Text::Wrap::columns) = 79;
428 0 local($Text::Wrap::unexpand) = 0;
429 0 $details .= wrap(
430 sprintf(" %-${col}s ", $key),
431 " " x ($col + 2),
432 $desc);
433 0 $details .= "\n";
434 }
435  
436 0 print "$summary\n$details";
437 0 return 0;
438 }
439  
440 0 ▶sub gd_install_pre {}
Subroutine gd_install_pre: not called CC=1 SCAR=0.0
Pod documented
441 0 ▶sub gd_install_post {}
Subroutine gd_install_post: not called CC=1 SCAR=0.0
Pod documented
442  
443 sub gd_can_install
444 0 ▶{
Subroutine gd_can_install: not called CC=7 SCAR=40.3
Pod documented
445 0 my $self = shift;
446 0 require File::Basename;
447 0 my $basename = File::Basename::basename($0);
448 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (-x "/usr/sbin/update-rc.d" and -x $0 and not $0 =~ m[^(?:/usr|/var)?/tmp/] and eval { do { symlink "", ""; 1 } } and -d "/etc/init.d" and not -e "/etc/init.d/$basename") 0 0 0
Condition0%0/15
-x "/usr/sbin/update-rc.d" and -x $0 and not $0 =~ m[^(?:/usr|/var)?/tmp/] and eval { do { symlink "", ""; 1 } } and -d "/etc/init.d" and not -e "/etc/init.d/$basename"
!ll&&!rl&&r
000
-x "/usr/sbin/update-rc.d" and -x $0 and not $0 =~ m[^(?:/usr|/var)?/tmp/] and eval { do { symlink "", ""; 1 } } and -d "/etc/init.d"
!ll&&!rl&&r
000
-x "/usr/sbin/update-rc.d" and -x $0 and not $0 =~ m[^(?:/usr|/var)?/tmp/] and eval { do { symlink "", ""; 1 } }
!ll&&!rl&&r
000
-x "/usr/sbin/update-rc.d" and -x $0 and not $0 =~ m[^(?:/usr|/var)?/tmp/]
!ll&&!rl&&r
000
-x "/usr/sbin/update-rc.d" and -x $0
!ll&&!rl&&r
000
MC/DC0%0/6
-x "/usr/sbin/update-rc.d" and -x $0 and not $0 =~ m[^(?:/usr|/var)?/tmp/] and eval { do { symlink "", ""; 1 } } and -d "/etc/init.d" and not -e "/etc/init.d/$basename"
-x "/usr/sbin/update-rc.d" -x $0 not $0 =~ m[^(?:/usr|/var)?/tmp/] eval { do { symlink "", ""; 1 } } -d "/etc/init.d" not -e "/etc/init.d/$basename"
Truth table0%0/7
A and B and not(C) and D and E and not(F)
A= -x "/usr/sbin/update-rc.d"B= -x $0C= not $0 =~ m[^(?:/usr|/var)?/tmp/]D= eval { do { symlink "", ""; 1 } }E= -d "/etc/init.d"F= not -e "/etc/init.d/$basename"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
449 -x "/usr/sbin/update-rc.d"
450 &&
451 -x $0
452 &&
453 $0 !~ m{^(?:/usr|/var)?/tmp/}
454 &&
455 0 eval { symlink("",""); 1 }
456 &&
457 -d "/etc/init.d"
458 &&
459 ! -e "/etc/init.d/$basename"
460 ) {
461 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
462 0 $self->gd_install_pre("update-rc.d");
463 0 require Cwd;
464 0 my $abs_path = Cwd::abs_path($0);
465 0 ▶ symlink($abs_path, "/etc/init.d/$basename")
Branch: 1 branch
BranchTrueFalseTotal
symlink $abs_path, "/etc/init.d/$basename" or die "Install failed: symlink /etc/init.d/$basename -> ${abs_path}: $!\n" 0 0 0
466 or die "Install failed: symlink /etc/init.d/$basename -> $abs_path: $!\n";
467 0 print "+ /usr/sbin/update-rc.d $basename defaults\n";
468 0 system("/usr/sbin/update-rc.d", $basename, "defaults");
469 0 my $exit = $? >> 8;
470 0 $self->gd_install_post("update-rc.d");
471 0 ▶ exit($exit) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 0 0
472 0 };
473 }
474  
475 0 return 0;
476 }
477  
478 sub gd_install
479 0 ▶{
Subroutine gd_install: not called CC=2 SCAR=17.9
Pod documented
480 0 my $self = shift;
481 0 my $ifunc = $self->gd_can_install();
482 0 ▶ die "Install command not supported\n" unless $ifunc;
Branch: 1 branch
BranchTrueFalseTotal
unless $ifunc 0 0 0
483 0 &$ifunc($self);
484 0 exit(0);
485 }
486  
487 0 ▶sub gd_uninstall_pre {}
Subroutine gd_uninstall_pre: not called CC=1 SCAR=0.0
Pod documented
488 0 ▶sub gd_uninstall_post {}
Subroutine gd_uninstall_post: not called CC=1 SCAR=0.0
Pod undocumented
489  
490 sub gd_can_uninstall
491 0 ▶{
Subroutine gd_can_uninstall: not called CC=5 SCAR=34.0
Pod documented
492 0 my $self = shift;
493 0 require File::Basename;
494 0 my $basename = File::Basename::basename($0);
495 0 require Cwd;
496 0 ▶ my $abs_path = Cwd::abs_path($0) || 'no abs path';
Condition0%0/2
Cwd::abs_path($0) || 'no abs path'
l!l
00
MC/DC0%0/1
Cwd::abs_path($0) || 'no abs path'
Cwd::abs_path($0)
Truth table0%0/2
A
A= Cwd::abs_path($0)
Aresult
11
00
497 0 ▶ my $link = readlink("/etc/init.d/$basename") || 'no link';
Condition0%0/2
readlink "/etc/init.d/$basename" || 'no link'
l!l
00
MC/DC0%0/1
readlink "/etc/init.d/$basename" || 'no link'
readlink "/etc/init.d/$basename"
Truth table0%0/2
A
A= readlink "/etc/init.d/$basename"
Aresult
11
00
498 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($link eq $abs_path and -x "/usr/sbin/update-rc.d") 0 0 0
Condition0%0/3
$link eq $abs_path and -x "/usr/sbin/update-rc.d"
!ll&&!rl&&r
000
MC/DC0%0/2
$link eq $abs_path and -x "/usr/sbin/update-rc.d"
$link eq $abs_path -x "/usr/sbin/update-rc.d"
Truth table0%0/3
A and B
A= $link eq $abs_pathB= -x "/usr/sbin/update-rc.d"
ABresult
0X0
100
111
499 $link eq $abs_path
500 &&
501 -x "/usr/sbin/update-rc.d"
502 ) {
503 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
504 0 $self->gd_uninstall_pre("update-rc.d");
505 0 unlink("/etc/init.d/$basename");
506 0 print "+ /usr/sbin/update-rc.d $basename remove\n";
507 0 system("/usr/sbin/update-rc.d", $basename, "remove");
508 0 my $exit = $? >> 8;
509 0 $self->gd_uninstall_post("update-rc.d");
510 0 ▶ exit($exit) if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 0 0
511 }
512 0 }
513 0 return 0;
514 }
515  
516 sub gd_uninstall
517 0 ▶{
Subroutine gd_uninstall: not called CC=2 SCAR=17.9
Pod documented
518 0 my $self = shift;
519 0 my $ufunc = $self->gd_can_uninstall();
520 0 ▶ die "Cannot uninstall\n" unless $ufunc;
Branch: 1 branch
BranchTrueFalseTotal
unless $ufunc 0 0 0
521 0 &$ufunc($self);
522 0 exit(0);
523 }
524  
525 12 ▶sub gd_kill_groups { 0 }
Subroutine gd_kill_groups: called CC=1 SCAR=0.0
Pod documented
526
527  
528 sub gd_kill
529 12 ▶{
Subroutine gd_kill: called CC=18 SCAR=47.9
Pod documented
530 12 my ($self, $pid) = @_;
531  
532 12 ▶ $pid = -abs($pid) if $self->gd_kill_groups();
Branch: 1 branch
BranchTrueFalseTotal
if $self->gd_kill_groups 0 12 12
533  
534 12 my $talkmore = 0;
535 12 my $killed = 0;
536 12 ▶ if (kill(0, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid) { } 12 0 12
537 12 $killed = 1;
538 12 kill(2,$pid);
539 12 print "Killing $pid\n";
540 12 my $t = time;
541 12 ▶ sleep(1) if kill(0, $pid);
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pid 12 0 12
542 12 ▶ if ($force_quit_delay && kill(0, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Daemon::Generic::force_quit_delay and kill 0, $pid) 0 12 12
Condition33%1/3
$Daemon::Generic::force_quit_delay and kill 0, $pid
!ll&&!rl&&r
0120
MC/DC0%0/2
$Daemon::Generic::force_quit_delay and kill 0, $pid
$Daemon::Generic::force_quit_delay kill 0, $pid
Truth table33%1/3
A and B
A= $Daemon::Generic::force_quit_delayB= kill 0, $pid
ABresult
0X0
100
111
543 0 print "Waiting for $pid to die...\n";
544 0 $talkmore = 1;
545 0 ▶ while(kill(0, $pid) && time - $t < $force_quit_delay) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid and time - $t < $Daemon::Generic::force_quit_delay) 0 0 0
Condition0%0/3
kill 0, $pid and time - $t < $Daemon::Generic::force_quit_delay
!ll&&!rl&&r
000
MC/DC0%0/2
kill 0, $pid and time - $t < $Daemon::Generic::force_quit_delay
kill 0, $pid time - $t < $Daemon::Generic::force_quit_delay
Truth table0%0/3
A and B
A= kill 0, $pidB= time - $t < $Daemon::Generic::force_quit_delay
ABresult
0X0
100
111
546 0 sleep(1);
547 }
548 }
549 12 ▶ if (kill(15, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 15, $pid) 0 12 12
550 0 print "Killing $pid with -TERM...\n";
551 0 ▶ if ($force_quit_delay) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Daemon::Generic::force_quit_delay) { } 0 0 0
552 0 ▶ while(kill(0, $pid) && time - $t < $force_quit_delay * 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid and time - $t < $Daemon::Generic::force_quit_delay * 2) 0 0 0
Condition0%0/3
kill 0, $pid and time - $t < $Daemon::Generic::force_quit_delay * 2
!ll&&!rl&&r
000
MC/DC0%0/2
kill 0, $pid and time - $t < $Daemon::Generic::force_quit_delay * 2
kill 0, $pid time - $t < $Daemon::Generic::force_quit_delay * 2
Truth table0%0/3
A and B
A= kill 0, $pidB= time - $t < $Daemon::Generic::force_quit_delay * 2
ABresult
0X0
100
111
553 0 sleep(1);
554 }
555 } else {
556 0 ▶ sleep(1) if kill(0, $pid);
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pid 0 0 0
557 }
558 }
559 12 ▶ if (kill(9, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 9, $pid) 0 12 12
560 0 print "Killing $pid with -KILL...\n";
561 0 my $k9 = time;
562 0 my $max = $force_quit_delay * 4;
563 0 ▶ $max = 60 if $max < 60;
Branch: 1 branch
BranchTrueFalseTotal
if $max < 60 0 0 0
564 0 ▶ while(kill(0, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid) 0 0 0
565 0 ▶ if (time - $k9 > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $k9 > $max) 0 0 0
566 0 print "Giving up on $pid ever dying.\n";
567 0 exit(1);
568 }
569 0 print "Waiting for $pid to die...\n";
570 0 sleep(1);
571 }
572 }
573 12 ▶ print "Process $pid is gone\n" if $talkmore;
Branch: 1 branch
BranchTrueFalseTotal
if $talkmore 0 12 12
574 } else {
575 0 print "Process $pid no longer running\n";
576 }
577 12 return $killed;
578 }
579  
580 0 ▶sub gd_preconfig { }
Subroutine gd_preconfig: not called CC=1 SCAR=0.0
Pod documented
581  
582 0 ▶sub gd_postconfig { }
Subroutine gd_postconfig: not called CC=1 SCAR=0.0
Pod documented
583  
584  
585 33 1;
↑ Summary lib/Daemon/Generic/While1.pm »