blib/lib/Daemon/Control.pm

filter: Statementstmt 68.2% 236 / 346 Branchbran 50.9% 112 / 220 Conditioncond 39.4% 15 / 38 MC/DCmcdc 8.7% 2 / 23 Subroutinesub 86.0% 37 / 43 Podpod 69.2% 18 / 26 totaltotal 60.3% 420 / 696 CC 130 SCAR 26.5
CC
130
Cov
60%
process_running
72.0
with_plugins
30.0
_double_fork
23.2
CRAP 14.2
↑ Summary
1 package Daemon::Control;
2  
3 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 46 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use POSIX qw(_exit setsid setuid setgid getuid getgid);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 46 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 46 ▶use File::Path qw( make_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 46 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 46 require 5.008001; # Supporting 5.8.1+
10  
11 46 our $VERSION = '0.001009'; # 0.1.9
12 46 $VERSION = eval $VERSION;
13  
14 46 my @accessors = qw(
15 pid color_map name program program_args directory quiet
16 path scan_name stdout_file stderr_file pid_file fork data
17 lsb_start lsb_stop lsb_sdesc lsb_desc redirect_before_fork init_config
18 kill_timeout umask resource_dir help init_code
19 prereq_no_process foreground reload_signal stop_signals
20 );
21  
22 46 my $cmd_opt = "[start|stop|restart|reload|status|foreground|show_warnings|get_init_file|help]";
23  
24 # Accessor building
25  
26 46 for my $method ( @accessors ) {
27 my $accessor = sub {
28 1183 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
29 1183 ▶ $self->{$method} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 104 1079 1183
30 1183 return $self->{$method};
31 1334 };
32 {
33 46 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1334 *$method = $accessor;
35 }
36 }
37  
38 # As a result of not using a real object system for
39 # this, I don't get after user => sub { } style things,
40 # so I'm making my own triggers for user and group.
41  
42 sub user {
43 35 ▶ my $self = shift;
Subroutine user: called CC=2 SCAR=8.4
Pod documented
44  
45 35 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 35 35
46 0 $self->{user} = shift;
47 0 delete $self->{uid};
48 }
49  
50 35 return $self->{user};
51 }
52  
53 sub group {
54 22 ▶ my $self = shift;
Subroutine group: called CC=2 SCAR=8.4
Pod documented
55  
56 22 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 22 22
57 0 $self->{group} = shift;
58 0 delete $self->{gid};
59 }
60  
61 22 return $self->{group};
62 }
63  
64 sub uid {
65 33 ▶ my $self = shift;
Subroutine uid: called CC=3 SCAR=11.4
Pod documented
66  
67 33 ▶ return $self->{uid} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 33 33
68  
69 33 ▶ $self->_set_uid_from_name unless exists $self->{uid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"uid"} 33 0 33
70  
71 return $self->{uid}
72 32 }
73  
74 sub gid {
75 10 ▶ my $self = shift;
Subroutine gid: called CC=3 SCAR=11.4
Pod documented
76  
77 10 ▶ return $self->{gid} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 10 10
78  
79 10 ▶ $self->_set_gid_from_name unless exists $self->{gid};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"gid"} 10 0 10
80  
81 return $self->{gid}
82 9 }
83  
84 sub new {
85 45 ▶ my ( $class, @in ) = @_;
Subroutine new: called CC=7 SCAR=20.8
Pod undocumented
86  
87 45 ▶ my $args = ref $in[0] eq 'HASH' ? $in[0] : { @in };
Branch: 1 branch
BranchTrueFalseTotal
ref $in[0] eq 'HASH' ? : 36 9 45
88  
89 # Create the object with defaults.
90 45 my $self = bless {
91 color_map => { red => 31, green => 32 },
92 redirect_before_fork => 1,
93 kill_timeout => 1,
94 quiet => 0,
95 umask => 0,
96 foreground => 0,
97 reload_signal => 'HUP',
98 stop_signals => [ qw(TERM TERM INT KILL) ],
99 }, $class;
100  
101 45 for my $accessor ( @accessors, qw(uid gid user group) ) {
102 1485 ▶ if ( exists $args->{$accessor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$accessor}) 532 953 1485
103 532 $self->{$accessor} = delete $args->{$accessor};
104 }
105 }
106  
107 # Shortcut caused by setting foreground or using the ENV to do it.
108 45 ▶ if ( ( $self->foreground == 1 ) || ( $ENV{DC_FOREGROUND} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->foreground == 1 or $ENV{"DC_FOREGROUND"}) 0 45 45
Condition33%1/3
$self->foreground == 1 or $ENV{"DC_FOREGROUND"}
l!l&&r!l&&!r
0045
MC/DC0%0/2
$self->foreground == 1 or $ENV{"DC_FOREGROUND"}
$self->foreground == 1 $ENV{"DC_FOREGROUND"}
Truth table33%1/3
A or B
A= $self->foreground == 1B= $ENV{"DC_FOREGROUND"}
ABresult
1X1
011
000
109 0 $self->fork( 0 );
110 0 $self->quiet( 1 );
111 }
112  
113 45 ▶ die "Unknown arguments to the constructor: " . join( " ", keys %$args )
Branch: 1 branch
BranchTrueFalseTotal
if keys %$args 0 45 45
114 if keys( %$args );
115  
116 45 return $self;
117 }
118  
119 sub with_plugins {
120 0 ▶ my ( $class, @in ) = @_;
Subroutine with_plugins: not called CC=5 SCAR=34.0
Pod documented
121  
122 # ->with_plugins()->new is just ->new...
123 0 ▶ return $class unless @in;
Branch: 1 branch
BranchTrueFalseTotal
unless @in 0 0 0
124  
125 # Make sure we have Role::Tiny installed.
126 0 local $@;
127 0 eval "require Role::Tiny";
128 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
129 0 die "Error: Role::Tiny is required for with_plugins to function.\n";
130 }
131  
132 # Take an array or arrayref as an argument
133 # and mutate it into a list like this:
134 # 'Module' -> Becomes -> 'Root::Module'
135 # '+Module' -> Becomes -> 'Module'
136 my @plugins = map {
137 0 ▶ substr( $_, 0, 1 ) eq '+'
Branch: 2 branches
BranchTrueFalseTotal
substr($_, 0, 1) eq '+' ? : 0 0 0
ref $in[0] eq 'ARRAY' ? : 0 0 0
138 ? substr( $_, 1 )
139 : "Daemon::Control::Plugin::$_"
140 0 } ref $in[0] eq 'ARRAY' ? @{ $in[0] } : @in;
141  
142  
143 # Compose the plugins into our class, and return for the user
144 # to call ->new().
145 0 return Role::Tiny->create_class_with_roles( $class, @plugins );
146 }
147  
148 # Set the uid, triggered from getting the uid if the user has changed.
149 sub _set_uid_from_name {
150 33 ▶ my ( $self ) = @_;
Subroutine _set_uid_from_name: called CC=3 SCAR=11.8
151 33 ▶ return unless defined $self->user;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->user 32 1 33
152  
153 1 my $uid = getpwnam( $self->user );
154 1 ▶ die "Error: Couldn't get uid for non-existent user " . $self->user
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uid 1 0 1
155 unless defined $uid;
156 0 $self->trace( "Set UID => $uid" );
157 0 $self->uid( $uid );
158 }
159  
160 # Set the uid, triggered from getting the gid if the group has changed.
161 sub _set_gid_from_name {
162 10 ▶ my ( $self ) = @_;
Subroutine _set_gid_from_name: called CC=5 SCAR=19.8
163  
164 # Grab the GID if we have a UID but no GID.
165 10 ▶ if ( !defined $self->group && defined $self->uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->group and defined $self->uid) 0 10 10
Condition66%2/3
not defined $self->group and defined $self->uid
!ll&&!rl&&r
190
MC/DC0%0/2
not defined $self->group and defined $self->uid
not defined $self->group defined $self->uid
Truth table66%2/3
not(A) and B
A= not defined $self->groupB= defined $self->uid
ABresult
0X0
100
111
166 0 my ( $gid ) = ( (getpwuid( $self->uid ))[3] );
167 0 $self->gid( $gid );
168 0 $self->trace( "Implicit GID => $gid" );
169 0 return $gid;
170 }
171  
172 10 ▶ return unless defined $self->group;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->group 9 1 10
173  
174 1 my $gid = getgrnam( $self->group );
175 1 ▶ die "Error: Couldn't get gid for non-existent group " . $self->group
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gid 1 0 1
176 unless defined $gid;
177 0 $self->trace( "Set GID => $gid" );
178 0 $self->gid( $gid );
179  
180 }
181  
182 sub redirect_filehandles {
183 8 ▶ my ( $self ) = @_;
Subroutine redirect_filehandles: called CC=13 SCAR=26.5
Pod undocumented
184  
185 8 ▶ if ( $self->stdout_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stdout_file) 8 0 8
186 8 my $file = $self->stdout_file;
187 8 ▶ $file = $file eq '/dev/null' ? File::Spec->devnull : $file;
Branch: 1 branch
BranchTrueFalseTotal
$file eq '/dev/null' ? : 5 3 8
188  
189 8 ▶ if ( ref $file eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'ARRAY') { } 2 6 8
190 2 my $mode = shift @$file;
191 2 ▶ open STDOUT, $mode, @$file ? @$file : ()
Branch: 2 branches
BranchTrueFalseTotal
open STDOUT, $mode, @$file ? @$file : () or die "Failed to open STDOUT with args $mode @${file}: $!" 0 2 2
@$file ? : 0 2 2
192 or die "Failed to open STDOUT with args $mode @$file: $!";
193  
194 2 $self->trace("STDOUT redirected to open(STDOUT $mode @$file)");
195 }
196 else {
197 6 ▶ open STDOUT, ">>", $file
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">>", $file or die "Failed to open STDOUT to ${file}: $!" 0 6 6
198 or die "Failed to open STDOUT to $file: $!";
199 6 $self->trace( "STDOUT redirected to $file" );
200 }
201 }
202 8 ▶ if ( $self->stderr_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stderr_file) 8 0 8
203 8 my $file = $self->stderr_file;
204 8 ▶ $file = $file eq '/dev/null' ? File::Spec->devnull : $file;
Branch: 1 branch
BranchTrueFalseTotal
$file eq '/dev/null' ? : 5 3 8
205  
206 8 ▶ if ( ref $file eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'ARRAY') { } 2 6 8
207 2 my $mode = shift @$file;
208 2 ▶ open STDERR, $mode, @$file ? @$file : ()
Branch: 2 branches
BranchTrueFalseTotal
open STDERR, $mode, @$file ? @$file : () or die "Failed to open STDERR with args $mode @${file}: $!" 0 2 2
@$file ? : 2 0 2
209 or die "Failed to open STDERR with args $mode @$file: $!";
210  
211 2 $self->trace("STDERR redirected to open(STDERR $mode @$file)");
212 }
213 else {
214 6 ▶ open STDERR, ">>", $file
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">>", $file or die "Failed to open STDERR to ${file}: $!" 0 6 6
215 or die "Failed to open STDERR to $file: $!";
216 6 $self->trace("STDERR redirected to $file");
217 }
218 }
219 }
220  
221 sub _create_resource_dir {
222 22 ▶ my ( $self ) = @_;
Subroutine _create_resource_dir: called CC=1 SCAR=0.0
223 22 $self->_create_dir($self->resource_dir);
224 }
225  
226 sub _create_dir {
227 36 ▶ my ( $self, $dir ) = @_;
Subroutine _create_dir: called CC=7 SCAR=28.0
228  
229 36 ▶ return 0 unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 22 14 36
230 14 ▶ return 1 unless length($dir);
Branch: 1 branch
BranchTrueFalseTotal
unless length $dir 12 2 14
231  
232 2 ▶ if ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 2 0 2
233 2 $self->trace( "Dir exists (" . $dir . ") - no need to create" );
234 2 return 1;
235 }
236  
237 0 my ( $created ) = make_path(
238 $dir,
239 {
240 uid => $self->uid,
241 group => $self->gid,
242 error => \my $errors,
243 }
244 );
245  
246 0 ▶ if ( @$errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$errors) 0 0 0
247 0 for my $error ( @$errors ) {
248 0 my ( $file, $msg ) = %$error;
249 0 die "Error creating $file: $msg";
250 }
251 }
252  
253 0 ▶ if ( $created eq $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($created eq $dir) 0 0 0
254 0 $self->trace( "Created dir (" . $dir . ")" );
255 0 return 1;
256 }
257  
258 0 $self->trace( "_create_dir() for $dir failed and I don't know why" );
259 0 return 0;
260 }
261  
262 sub _double_fork {
263 22 ▶ my ( $self ) = @_;
Subroutine _double_fork: called CC=10 SCAR=31.5
264 22 my $pid = fork();
265  
266 22 $self->trace( "_double_fork()" );
267 22 ▶ if ( $pid == 0 ) { # Child, launch the process here.
Branch: 2 branches
BranchTrueFalseTotal
if ($pid == 0) { } 9 13 22
elsif (not defined $pid) { } 0 13 13
268 9 setsid(); # Become the process leader.
269 9 my $new_pid = fork();
270 9 ▶ if ( $new_pid == 0 ) { # Our double fork.
Branch: 2 branches
BranchTrueFalseTotal
if ($new_pid == 0) { } 9 0 9
elsif (not defined $new_pid) { } 0 0 0
271  
272 9 ▶ if ( $self->gid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->gid) 0 9 9
273 0 setgid( $self->gid );
274 0 $self->trace( "setgid(" . $self->gid . ")" );
275 }
276  
277 9 ▶ if ( $self->uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->uid) 0 9 9
278 0 setuid( $self->uid );
279  
280 0 ▶ $ENV{USER} = $self->user || getpwuid($self->uid);
Condition0%0/3
$self->user || getpwuid $self->uid
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->user || getpwuid $self->uid
$self->user getpwuid $self->uid
Truth table0%0/3
A || B
A= $self->userB= getpwuid $self->uid
ABresult
1X1
011
000
281 0 $ENV{HOME} = ((getpwuid($self->uid))[7]);
282  
283 0 $self->trace( "setuid(" . $self->uid . ")" );
284 0 $self->trace( "\$ENV{USER} => " . $ENV{USER} );
285 0 $self->trace( "\$ENV{HOME} => " . $ENV{HOME} );
286 }
287  
288 9 ▶ if ( $self->umask ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->umask) 0 9 9
289 0 umask( $self->umask);
290 0 $self->trace( "umask(" . $self->umask . ")" );
291 }
292  
293 9 open( STDIN, "<", File::Spec->devnull );
294  
295 9 ▶ if ( $self->redirect_before_fork ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->redirect_before_fork) 8 1 9
296 8 $self->redirect_filehandles;
297 }
298  
299 9 $self->_launch_program;
300 } elsif ( not defined $new_pid ) {
301 0 warn "Cannot fork: $!";
302 } else {
303 0 $self->pid( $new_pid );
304 0 $self->trace("Set PID => $new_pid" );
305 0 $self->write_pid;
306 0 _exit 0;
307 }
308 } elsif ( not defined $pid ) { # We couldn't fork. =(
309 0 warn "Cannot fork: $!";
310 } else { # In the parent, $pid = child's PID, return it.
311 13 waitpid( $pid, 0 );
312 }
313 19 return $self;
314 }
315  
316 0 ▶sub _foreground { shift->_launch_program }
Subroutine _foreground: not called CC=1 SCAR=6.9
317  
318 sub _fork {
319 0 ▶ my ( $self ) = @_;
Subroutine _fork: not called CC=3 SCAR=24.8
320 0 my $pid = fork();
321  
322 0 $self->trace( "_fork()" );
323 0 ▶ if ( $pid == 0 ) { # Child, launch the process here.
Branch: 2 branches
BranchTrueFalseTotal
if ($pid == 0) { } 0 0 0
elsif (not defined $pid) { } 0 0 0
324 0 $self->_launch_program;
325 } elsif ( not defined $pid ) {
326 0 warn "Cannot fork: $!";
327 } else { # In the parent, $pid = child's PID, return it.
328 0 $self->pid( $pid );
329 0 $self->trace("Set PID => $pid" );
330 0 $self->write_pid;
331 }
332 0 return $self;
333 }
334  
335 sub _launch_program {
336 9 ▶ my ($self) = @_;
Subroutine _launch_program: called CC=5 SCAR=17.1
337  
338 9 ▶ if ( $self->directory ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->directory) 0 9 9
339 0 chdir( $self->directory );
340 0 $self->trace( "chdir(" . $self->directory . ")" );
341 }
342  
343 9 ▶ my @args = @{$self->program_args || [ ]};
Branch: 1 branch
BranchTrueFalseTotal
$self->program_args or [] 3 6 9
344  
345 9 ▶ if ( ref $self->program eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->program eq 'CODE') { } 6 3 9
346 6 $self->program->( $self, @args );
347 } else {
348 3 ▶ exec ( $self->program, @args )
Branch: 1 branch
BranchTrueFalseTotal
exec $self->program, @args or die 'Failed to exec ' . $self->program . ' ' . join(" ", @args) . ": $!" 0 0 0
349 or die "Failed to exec " . $self->program . " "
350 . join( " ", @args ) . ": $!";
351 }
352 6 return 0;
353 }
354  
355 sub write_pid {
356 14 ▶ my ( $self ) = @_;
Subroutine write_pid: called CC=3 SCAR=14.2
Pod documented
357  
358 # Create the PID file as the user we currently are,
359 # and change the permissions to our target UID/GID.
360  
361 14 $self->_write_pid;
362  
363 14 ▶ if ( $self->uid && $self->gid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->uid and $self->gid) 0 14 14
Condition33%1/3
$self->uid and $self->gid
!ll&&!rl&&r
1400
MC/DC0%0/2
$self->uid and $self->gid
$self->uid $self->gid
Truth table33%1/3
A and B
A= $self->uidB= $self->gid
ABresult
0X0
100
111
364 0 chown $self->uid, $self->gid, $self->pid_file;
365 0 $self->trace("PID => chown(" . $self->uid . ", " . $self->gid .")");
366 }
367 }
368  
369 sub _write_pid {
370 14 ▶ my ( $self ) = @_;
Subroutine _write_pid: called CC=3 SCAR=11.1
371  
372 14 my ($volume, $dir, $file) = File::Spec->splitpath($self->pid_file);
373 14 ▶ return 0 if not $self->_create_dir($dir);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_create_dir($dir) 0 14 14
374  
375 14 ▶ open my $sf, ">", $self->pid_file
Branch: 1 branch
BranchTrueFalseTotal
open my $sf, ">", $self->pid_file or die 'Failed to write ' . $self->pid_file . ": $!" 0 14 14
376 or die "Failed to write " . $self->pid_file . ": $!";
377 14 print $sf $self->pid;
378 14 close $sf;
379 14 $self->trace( "Wrote pid (" . $self->pid . ") to pid file (" . $self->pid_file . ")" );
380 14 return $self;
381 }
382  
383 sub read_pid {
384 82 ▶ my ( $self ) = @_;
Subroutine read_pid: called CC=3 SCAR=11.0
Pod documented
385  
386 # If we don't have a PID file, we're going to set it
387 # to 0 -- this will prevent killing normal processes,
388 # and make is_running return false.
389 82 ▶ if ( ! -f $self->pid_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->pid_file) 2 80 82
390 2 $self->pid( 0 );
391 2 return 0;
392 }
393  
394 80 ▶ open my $lf, "<", $self->pid_file
Branch: 1 branch
BranchTrueFalseTotal
open my $lf, "<", $self->pid_file or die 'Failed to read ' . $self->pid_file . ": $!" 0 80 80
395 or die "Failed to read " . $self->pid_file . ": $!";
396 80 my $pid = do { local $/; <$lf> };
397 80 close $lf;
398 80 $self->pid( $pid );
399 80 return $pid;
400 }
401  
402 sub pid_running {
403 55 ▶ my ( $self, $pid ) = @_;
Subroutine pid_running: called CC=8 SCAR=28.3
Pod undocumented
404  
405 55 ▶ $pid ||= $self->read_pid;
Condition66%2/3
$pid ||= $self->read_pid
l!l&&r!l&&!r
28270
MC/DC0%0/2
$pid ||= $self->read_pid
$pid $self->read_pid
Truth table66%2/3
A ||= B
A= $pidB= $self->read_pid
ABresult
1X1
011
000
406  
407 55 ▶ return 0 unless $self->pid >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pid >= 1 0 55 55
408 55 ▶ return 0 unless kill 0, $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless kill 0, $self->pid 28 27 55
409  
410 27 ▶ if ( $self->scan_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scan_name) 0 27 27
411 0 ▶ open my $lf, "-|", "ps", "-w", "-w", "-p", $self->pid, "-o", "command="
Branch: 1 branch
BranchTrueFalseTotal
open my $lf, "-|", "ps", "-w", "-w", "-p", $self->pid, "-o", "command=" or die "Failed to get pipe to ps for scan_name." 0 0 0
412 or die "Failed to get pipe to ps for scan_name.";
413 0 ▶ while ( my $line = <$lf> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $lf)) 0 0 0
414 0 ▶ return 1 if $line =~ $self->scan_name;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ $self->scan_name 0 0 0
415 }
416 0 return 0;
417 }
418 # Scan name wasn't used, testing normal PID.
419 27 return kill 0, $self->pid;
420 }
421  
422 sub process_running {
423 0 ▶ my ( $self, $pattern ) = @_;
Subroutine process_running: not called CC=8 SCAR=42.8
Pod undocumented
424  
425 0 ▶ my $psopt = $^O =~ m/bsd$/ ? '-ax' : '-u ' . $self->user;
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /bsd$/ ? : 0 0 0
426 0 my $ps = `LC_ALL=C command ps $psopt -o pid,args`;
427 0 $ps =~ s/^\s+//mg;
428 0 my @pids;
429 0 for my $line (split /\n/, $ps)
430 {
431 0 ▶ next if $line =~ m/^\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\D/ 0 0 0
432 0 my ($pid, $command, $args) = split /\s+/, $line, 3;
433  
434 0 ▶ next if $pid eq $$;
Branch: 1 branch
BranchTrueFalseTotal
if $pid eq $$ 0 0 0
435 0 ▶ push @pids, $pid
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /$pattern/ or defined $args and $args =~ /$pattern/ 0 0 0
Condition0%0/6
$command =~ /$pattern/ or defined $args and $args =~ /$pattern/
l!l&&r!l&&!r
000
defined $args and $args =~ /$pattern/
!ll&&!rl&&r
000
MC/DC0%0/3
$command =~ /$pattern/ or defined $args and $args =~ /$pattern/
$command =~ /$pattern/ defined $args $args =~ /$pattern/
Truth table0%0/4
A or B and C
A= $command =~ /$pattern/B= defined $argsC= $args =~ /$pattern/
ABCresult
1XX1
0111
00X0
0100
436 if $command =~ $pattern
437 or defined $args and $args =~ $pattern;
438 }
439 0 return @pids;
440 }
441  
442 sub pretty_print {
443 41 ▶ my ( $self, $message, $color ) = @_;
Subroutine pretty_print: called CC=4 SCAR=14.0
Pod documented
444  
445 41 ▶ return if $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->quiet 0 41 41
446  
447 41 ▶ $color ||= "green"; # Green is no color.
Condition100%2/2
$color ||= "green"
l!l
635
MC/DC100%1/1
$color ||= "green"
$color
Truth table100%2/2
A
A= $color
Aresult
11
00
448 41 ▶ my $code = $self->color_map->{$color} ||= "32"; # Green is invalid.
Condition50%1/2
$self->color_map->{$color} ||= 32
l!l
410
MC/DC0%0/1
$self->color_map->{$color} ||= 32
$self->color_map->{$color}
Truth table50%1/2
A
A= $self->color_map->{$color}
Aresult
11
00
449 41 local $| = 1;
450 41 printf( "%-49s %30s\n", $self->name, "\033[$code" ."m[$message]\033[0m" );
451 }
452  
453 # Callable Functions
454  
455 sub do_foreground {
456 0 ▶ my ( $self ) = @_;
Subroutine do_foreground: not called CC=1 SCAR=6.9
Pod documented
457  
458 # Short cut to...
459 0 $self->fork( 0 );
460 0 $self->quiet( 1 );
461 0 return $self->do_start;
462 }
463  
464 sub do_start {
465 22 ▶ my ( $self ) = @_;
Subroutine do_start: called CC=11 SCAR=30.7
Pod documented
466  
467 # Optionally check if a process is already running with the same name
468 22 ▶ if ($self->prereq_no_process)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prereq_no_process) 0 22 22
469 {
470 0 my $program = $self->program;
471 0 ▶ my $pattern = $self->prereq_no_process eq '1'
Branch: 1 branch
BranchTrueFalseTotal
$self->prereq_no_process eq '1' ? : 0 0 0
472 ? qr/\b${program}\b/
473 : $self->prereq_no_process;
474 0 my @pids = $self->process_running($pattern);
475 0 ▶ if (@pids)
Branch: 1 branch
BranchTrueFalseTotal
if (@pids) 0 0 0
476 {
477 0 $self->pretty_print( 'Duplicate Running? (pid ' . join(', ', @pids) . ')', "red" );
478 0 return 1;
479 }
480 }
481  
482 # Make sure the PID file exists.
483 22 ▶ if ( ! -f $self->pid_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->pid_file) 14 8 22
484 14 $self->pid( 0 ); # Make PID invalid.
485 14 $self->write_pid();
486 }
487  
488 # Duplicate Check
489 22 $self->read_pid;
490 22 ▶ if ( $self->pid && $self->pid_running ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid and $self->pid_running) 0 22 22
Condition66%2/3
$self->pid and $self->pid_running
!ll&&!rl&&r
1480
MC/DC0%0/2
$self->pid and $self->pid_running
$self->pid $self->pid_running
Truth table66%2/3
A and B
A= $self->pidB= $self->pid_running
ABresult
0X0
100
111
491 0 $self->pretty_print( "Duplicate Running", "red" );
492 0 return 1;
493 }
494  
495 22 $self->_create_resource_dir;
496  
497 22 ▶ $self->fork( 2 ) unless defined $self->fork;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->fork 0 22 22
498 22 ▶ $self->_double_fork if $self->fork == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fork == 2 22 0 22
499 19 ▶ $self->_fork if $self->fork == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fork == 1 0 19 19
500 19 ▶ $self->_foreground if $self->fork == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fork == 0 0 19 19
501 19 $self->pretty_print( "Started" );
502 19 return 0;
503 }
504  
505 sub do_show_warnings {
506 2 ▶ my ( $self ) = @_;
Subroutine do_show_warnings: called CC=4 SCAR=13.9
Pod undocumented
507  
508 2 ▶ if ( ! $self->fork ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->fork) 1 1 2
509 1 warn "Fork undefined. Defaulting to fork => 2.\n";
510 }
511  
512 2 ▶ if ( ! $self->stdout_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->stdout_file) 1 1 2
513 1 warn "stdout_file undefined. Will not redirect file handle.\n";
514 }
515  
516 2 ▶ if ( ! $self->stderr_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->stderr_file) 1 1 2
517 1 warn "stderr_file undefined. Will not redirect file handle.\n";
518 }
519 }
520  
521 sub do_stop {
522 7 ▶ my ( $self ) = @_;
Subroutine do_stop: called CC=10 SCAR=25.0
Pod documented
523  
524 7 $self->read_pid;
525 7 my $start_pid = $self->pid;
526  
527 # Probably don't want to send anything to init(1).
528 7 ▶ return 1 unless $start_pid > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $start_pid > 1 0 7 7
529  
530 7 ▶ if ( $self->pid_running($start_pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid_running($start_pid)) { } 7 0 7
531 SIGNAL:
532 7 foreach my $signal (@{ $self->stop_signals }) {
533 7 $self->trace( "Sending $signal signal to pid $start_pid..." );
534 7 kill $signal => $start_pid;
535  
536 7 for (1..$self->kill_timeout)
537 {
538 # abort early if the process is now stopped
539 7 $self->trace("checking if pid $start_pid is still running...");
540 7 ▶ last if not $self->pid_running($start_pid);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pid_running($start_pid) 0 7 7
541 7 sleep 1;
542 }
543 7 ▶ last unless $self->pid_running($start_pid);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pid_running($start_pid) 7 0 7
544 }
545 7 ▶ if ( $self->pid_running($start_pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid_running($start_pid)) 0 7 7
546 0 $self->pretty_print( "Failed to Stop", "red" );
547 0 return 1;
548 }
549 7 $self->pretty_print( "Stopped" );
550 } else {
551 0 $self->pretty_print( "Not Running", "red" );
552 }
553  
554 # Clean up the PID file on stop, unless the pid
555 # doesn't match $start_pid (perhaps a standby
556 # worker stepped in to take over from the one
557 # that was just terminated).
558  
559 7 ▶ if ( $self->pid_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid_file) 7 0 7
560 7 ▶ unlink($self->pid_file) if $self->read_pid == $start_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->read_pid == $start_pid 7 0 7
561 }
562 7 return 0;
563 }
564  
565 sub do_restart {
566 4 ▶ my ( $self ) = @_;
Subroutine do_restart: called CC=2 SCAR=7.0
Pod documented
567 4 $self->read_pid;
568  
569 4 ▶ if ( $self->pid_running ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid_running) 4 0 4
570 4 $self->do_stop;
571 }
572 4 $self->do_start;
573 3 return 0;
574 }
575  
576 sub do_status {
577 15 ▶ my ( $self ) = @_;
Subroutine do_status: called CC=3 SCAR=11.0
Pod documented
578 15 $self->read_pid;
579  
580 15 ▶ if ( $self->pid && $self->pid_running ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid and $self->pid_running) { } 9 6 15
Condition66%2/3
$self->pid and $self->pid_running
!ll&&!rl&&r
069
MC/DC50%1/2
$self->pid and $self->pid_running
$self->pid $self->pid_running
Truth table66%2/3
A and B
A= $self->pidB= $self->pid_running
ABresult
0X0
100
111
581 9 $self->pretty_print( "Running" );
582 9 return 0;
583 } else {
584 6 $self->pretty_print( "Not Running", "red" );
585 6 return 3;
586 }
587 }
588  
589 sub do_reload {
590 0 ▶ my ( $self ) = @_;
Subroutine do_reload: not called CC=3 SCAR=24.8
Pod documented
591 0 $self->read_pid;
592  
593 0 ▶ if ( $self->pid && $self->pid_running ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid and $self->pid_running) { } 0 0 0
Condition0%0/3
$self->pid and $self->pid_running
!ll&&!rl&&r
000
MC/DC0%0/2
$self->pid and $self->pid_running
$self->pid $self->pid_running
Truth table0%0/3
A and B
A= $self->pidB= $self->pid_running
ABresult
0X0
100
111
594 0 kill $self->reload_signal, $self->pid;
595 0 $self->pretty_print( "Reloaded" );
596 0 return 0;
597 } else {
598 0 $self->pretty_print( "Not Running", "red" );
599 0 return 1;
600 }
601 }
602  
603 sub do_get_init_file {
604 4 ▶ shift->dump_init_script;
Subroutine do_get_init_file: called CC=1 SCAR=0.0
Pod documented
605 4 return 0;
606 }
607  
608 sub do_help {
609 1 ▶ my ( $self ) = @_;
Subroutine do_help: called CC=2 SCAR=7.0
Pod undocumented
610  
611 1 print "Syntax: $0 $cmd_opt\n\n";
612 1 ▶ print $self->help if $self->help;
Branch: 1 branch
BranchTrueFalseTotal
if $self->help 0 1 1
613 1 return 0;
614 }
615  
616 sub dump_init_script {
617 4 ▶ my ( $self ) = @_;
Subroutine dump_init_script: called CC=13 SCAR=27.8
Pod documented
618 4 ▶ if ( ! $self->data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->data) 4 0 4
619 4 my $data;
620 4 ▶ while ( my $line = <DATA> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline DATA)) 104 0 104
621 104 ▶ last if $line =~ /^__END__$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^__END__$/ 4 100 104
622 100 $data .= $line;
623 }
624 4 $self->data( $data );
625 }
626  
627 # So, instead of expanding run_template to use a real DSL
628 # or making TT a dependancy, I'm just going to fake template
629 # IF logic.
630 4 ▶ my $init_source_file = $self->init_config
Branch: 1 branch
BranchTrueFalseTotal
$self->init_config ? : 1 3 4
631 ? $self->run_template(
632 '[ -r [% FILE %] ] && . [% FILE %]',
633 { FILE => $self->init_config } )
634 : "";
635  
636 4 ▶ $self->data( $self->run_template(
Branch: 7 branches
BranchTrueFalseTotal
$self->name ? : 4 0 4
$self->lsb_start ? : 4 0 4
$self->lsb_stop ? : 4 0 4
$self->lsb_sdesc ? : 4 0 4
$self->lsb_desc ? : 4 0 4
$self->path ? : 4 0 4
$self->init_code ? : 1 3 4
Condition50%1/2
$self->VERSION || 'DEV'
l!l
40
MC/DC0%0/1
$self->VERSION || 'DEV'
$self->VERSION
Truth table50%1/2
A
A= $self->VERSION
Aresult
11
00
637 $self->data,
638 {
639 HEADER => 'Generated at ' . scalar(localtime)
640 . ' with Daemon::Control ' . ($self->VERSION || 'DEV'),
641 NAME => $self->name ? $self->name : "",
642 REQUIRED_START => $self->lsb_start ? $self->lsb_start : "",
643 REQUIRED_STOP => $self->lsb_stop ? $self->lsb_stop : "",
644 SHORT_DESCRIPTION => $self->lsb_sdesc ? $self->lsb_sdesc : "",
645 DESCRIPTION => $self->lsb_desc ? $self->lsb_desc : "",
646 SCRIPT => $self->path ? $self->path : abs_path($0),
647 INIT_SOURCE_FILE => $init_source_file,
648 INIT_CODE_BLOCK => $self->init_code ? $self->init_code : "",
649 }
650 ));
651 4 print $self->data;
652 }
653  
654 sub run_template {
655 5 ▶ my ( $self, $content, $config ) = @_;
Subroutine run_template: called CC=1 SCAR=0.0
Pod undocumented
656  
657 5 $content =~ s/\[% (.*?) %\]/$config->{$1}/g;
658  
659 5 return $content;
660 }
661  
662  
663  
664 sub run_command {
665 44 ▶ my ( $self, $arg ) = @_;
Subroutine run_command: called CC=8 SCAR=26.2
Pod documented
666  
667 # Error Checking.
668 44 ▶ if ( ! $self->program ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->program) 0 44 44
669 0 die "Error: program must be defined.";
670 }
671 44 ▶ if ( ! $self->pid_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->pid_file) 0 44 44
672 0 die "Error: pid_file must be defined.";
673 }
674 44 ▶ if ( ! $self->name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->name) 0 44 44
675 0 die "Error: name must be defined.";
676 }
677  
678 44 ▶ my $called_with = $arg || "help";
Condition50%1/2
$arg || 'help'
l!l
440
MC/DC0%0/1
$arg || 'help'
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
679 44 $called_with =~ s/^[-]+//g; # Allow people to do --command too.
680  
681 44 ▶ my $action = "do_" . ($called_with ? $called_with : "" );
Branch: 1 branch
BranchTrueFalseTotal
$called_with ? : 44 0 44
682  
683 44 my $allowed_actions = "Must be called with an action: $cmd_opt";
684  
685 44 ▶ if ( $self->can($action) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->can($action)) { } 44 0 44
elsif (not $called_with) { } 0 0 0
686 44 return $self->$action;
687 } elsif ( ! $called_with ) {
688 0 die $allowed_actions
689 } else {
690 0 die "Error: undefined action $called_with. $allowed_actions";
691 }
692  
693 }
694  
695 # Application Code.
696 sub run {
697 44 ▶ exit shift->run_command( @ARGV );
Subroutine run: called CC=1 SCAR=0.0
Pod documented
698 }
699  
700 sub trace {
701 68 ▶ my ( $self, $message ) = @_;
Subroutine trace: called CC=4 SCAR=22.5
Pod undocumented
702  
703 68 ▶ return unless $ENV{DC_TRACE};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"DC_TRACE"} 68 0 68
704  
705 0 ▶ print "[TRACE] $message\n" if $ENV{DC_TRACE} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DC_TRACE"} == 1 0 0 0
706 0 ▶ print STDERR "[TRACE] $message\n" if $ENV{DC_TRACE} == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DC_TRACE"} == 2 0 0 0
707 }
708  
709 46 1;
710  
711 __DATA__
↑ Summary