lib/Beekeeper/WorkerPool/Daemon.pm

filter: Statementstmt 7.2% 19 / 262 Branchbran 0.0% 0 / 148 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 14.2% 5 / 35 Podpod 0.0% 0 / 26 totaltotal 4.8% 24 / 499 CC 86 SCAR 40.9
CC
86
Cov
4%
write_pid_file
132.0
stop_daemon
132.0
run
90.0
CRAP 59.9
« blib/lib/auto/share/dist/Beekeeper/dashboard/lib/Beekeeper/Service/Dashboard/Worker.pm ↑ Summary lib/Beekeeper/Service/Supervisor/Worker.pm »
1 package Beekeeper::WorkerPool::Daemon;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.10';
7  
8 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 my $PID_FILE_DIR = "/var/run";
13 1 my $LOG_FILE_DIR = "/var/log";
14  
15  
16 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
17 0 my $class = shift;
18 0 my $self = {
19 options => {},
20 config => {},
21 daemonized => 0,
22 };
23 0 bless $self, $class;
24 0 $self->configure(@_);
25 0 return $self;
26 }
27  
28 0 ▶sub configure {
Subroutine configure: not called CC=2 SCAR=17.9
Pod undocumented
29 0 my ($self, %config) = @_;
30 0 foreach (keys %config) {
31 0 $self->{config}->{$_} = $config{$_};
32 }
33 }
34  
35 0 ▶sub config {
Subroutine config: not called CC=1 SCAR=6.9
Pod undocumented
36 0 my ($self, $key) = @_;
37 0 $self->{config}->{$key};
38 }
39  
40 0 ▶sub parse_options {
Subroutine parse_options: not called CC=6 SCAR=37.4
Pod undocumented
41 0 my $self = shift;
42  
43 # Parse command line options using Getopt::Long
44  
45 0 my @options_spec = ( "foreground", "user=s", "group=s", "help" );
46  
47 0 my $extra_options = $self->{config}->{get_options};
48 0 ▶ if ($extra_options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_options) 0 0 0
49 0 foreach my $opt (@$extra_options) {
50 0 ▶ next if (grep { $opt eq $_ } @options_spec);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$opt eq $_;} @options_spec 0 0 0
51 0 push @options_spec, $opt;
52 }
53 }
54  
55 # GetOptions cannot be done twice
56 0 ▶ return if keys %{$self->{options}};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"options"};} 0 0 0
57  
58 0 my %options;
59 0 ▶ GetOptions(\%options, @options_spec) or CORE::exit(1);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%options, @options_spec) or exit 1 0 0 0
60 0 $self->{options} = \%options;
61 }
62  
63 0 ▶sub option {
Subroutine option: not called CC=1 SCAR=6.9
Pod undocumented
64 0 my ($self, $option) = @_;
65 0 $self->{options}->{$option};
66 }
67  
68 0 ▶sub daemon_name {
Subroutine daemon_name: not called CC=2 SCAR=17.9
Pod undocumented
69 0 my $self = shift;
70 0 my $daemon_name = $self->{config}->{daemon_name};
71 0 ▶ unless ($daemon_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($daemon_name) 0 0 0
72 0 $daemon_name = $0;
73 0 $daemon_name =~ s|.*/||;
74 }
75 0 return $daemon_name;
76 }
77  
78 0 ▶sub daemon_description {
Subroutine daemon_description: not called CC=2 SCAR=17.9
Pod undocumented
79 0 my $self = shift;
80 0 ▶ $self->{config}->{description} || 'daemon';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"config"}{"description"} or "daemon" 0 0 0
81 }
82  
83  
84 #------------------------------------------------------------------------------
85  
86 0 ▶sub run {
Subroutine run: not called CC=9 SCAR=45.0
Pod undocumented
87 0 my $self = shift;
88  
89 0 $self->parse_options;
90  
91 0 my $cmd = $ARGV[0];
92  
93 0 ▶ $cmd = 'help' if (!$cmd || $self->{options}->{help});
Branch: 1 branch
BranchTrueFalseTotal
if not $cmd or $self->{"options"}{"help"} 0 0 0
Condition0%0/3
not $cmd or $self->{"options"}{"help"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $cmd or $self->{"options"}{"help"}
not $cmd $self->{"options"}{"help"}
Truth table0%0/3
not(A) or B
A= not $cmdB= $self->{"options"}{"help"}
ABresult
1X1
011
000
94  
95 0 ▶ if ($cmd eq 'start') {
Branch: 6 branches
BranchTrueFalseTotal
if ($cmd eq 'start') { } 0 0 0
elsif ($cmd eq 'stop') { } 0 0 0
elsif ($cmd eq 'restart') { } 0 0 0
elsif ($cmd eq 'reload') { } 0 0 0
elsif ($cmd eq 'check') { } 0 0 0
elsif ($cmd eq 'help') { } 0 0 0
96 0 $self->cmd_start;
97 #goto &cmd_start;
98 }
99 elsif ($cmd eq 'stop') {
100 0 $self->cmd_stop;
101 }
102 elsif ($cmd eq 'restart') {
103 0 $self->cmd_stop;
104 0 $self->cmd_start;
105 #goto &cmd_start;
106 }
107 elsif ($cmd eq 'reload') {
108 0 $self->cmd_reload;
109 }
110 elsif ($cmd eq 'check') {
111 0 $self->cmd_check;
112 }
113 elsif ($cmd eq 'help') {
114 0 $self->cmd_help;
115 }
116 else {
117 0 print "Unknown command '$cmd'.\n";
118 0 $self->cmd_help;
119 }
120 }
121  
122 0 ▶sub cmd_start {
Subroutine cmd_start: not called CC=3 SCAR=24.8
Pod undocumented
123 0 my $self = shift;
124  
125 0 print "Starting " . $self->daemon_description . ": " . $self->daemon_name;
126  
127 0 ▶ if ($self->daemon_is_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->daemon_is_running) 0 0 0
128 0 print " is already running.\n";
129 0 return;
130 }
131  
132 0 ▶ print ".\n" if ($self->{options}->{foreground});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"foreground"} 0 0 0
133  
134 0 $self->daemonize;
135 #goto &daemonize;
136  
137 0 print ".\n";
138 }
139  
140 0 ▶sub cmd_stop {
Subroutine cmd_stop: not called CC=2 SCAR=17.9
Pod undocumented
141 0 my $self = shift;
142  
143 0 print "Stopping " . $self->daemon_description . ": " . $self->daemon_name;
144  
145 0 ▶ unless ($self->daemon_is_running) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->daemon_is_running) 0 0 0
146 0 print " was not running.\n";
147 0 return;
148 }
149  
150 0 $self->stop_daemon;
151 0 print ".\n";
152 }
153  
154 0 ▶sub cmd_reload {
Subroutine cmd_reload: not called CC=1 SCAR=6.9
Pod undocumented
155 0 my $self = shift;
156  
157 0 print "Reloading " . $self->daemon_description . ": " . $self->daemon_name;
158  
159 0 $self->hup_daemon;
160 0 print ".\n";
161 }
162  
163 0 ▶sub cmd_check {
Subroutine cmd_check: not called CC=2 SCAR=17.9
Pod undocumented
164 0 my $self = shift;
165  
166 0 print $self->daemon_name;
167  
168 0 ▶ if ($self->daemon_is_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->daemon_is_running) { } 0 0 0
169 0 print " is running.\n";
170 }
171 else {
172 0 print " is not running.\n";
173 }
174 }
175  
176 0 ▶sub cmd_help {
Subroutine cmd_help: not called CC=1 SCAR=6.9
Pod undocumented
177 0 my $self = shift;
178  
179 0 my $progname = $0;
180 0 $progname =~ s|.*/||;
181  
182 0 print "Usage: $progname [options] {start|stop|restart|reload|check}\n";
183 0 print " --foreground Run in foreground (do not daemonize)\n";
184 0 print " --user Run as specified user\n";
185 0 print " --group Run as specified group\n";
186 0 print " --help Shows this message\n";
187 }
188  
189  
190 #------------------------------------------------------------------------------
191  
192 # DAEMONIZE
193  
194 0 ▶sub daemonize {
Subroutine daemonize: not called CC=8 SCAR=42.8
Pod undocumented
195 0 my $self = shift;
196  
197 0 ▶ unless ($self->{options}->{foreground}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"options"}{"foreground"}) 0 0 0
198  
199 # Fork and exit parent
200 0 ▶ _fork() && return;
Branch: 1 branch
BranchTrueFalseTotal
_fork() and return 0 0 0
201  
202 # Detach ourselves from the terminal
203 0 ▶ POSIX::setsid() or die("Cannot detach from controlling terminal");
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() or die "Cannot detach from controlling terminal" 0 0 0
204  
205 # Prevent possibility of acquiring a controling terminal
206 0 $SIG{'HUP'} = 'IGNORE';
207 0 ▶ _fork() && CORE::exit(0);
Branch: 1 branch
BranchTrueFalseTotal
_fork() and exit 0 0 0 0
208  
209 # Change working directory
210 0 chdir "/";
211  
212 # Clear file creation mask
213 0 umask 0;
214  
215 # Close open file descriptors
216 0 my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX );
217 0 ▶ $openmax = 64 if (!defined($openmax) || $openmax < 0);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $openmax or $openmax < 0 0 0 0
Condition0%0/3
not defined $openmax or $openmax < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $openmax or $openmax < 0
not defined $openmax $openmax < 0
Truth table0%0/3
not(A) or B
A= not defined $openmaxB= $openmax < 0
ABresult
1X1
011
000
218 0 foreach my $i (0..$openmax) { POSIX::close($i); }
219  
220 0 $self->redirect_output;
221  
222 0 $self->{daemonized} = 1;
223 }
224  
225 0 $self->write_pid_file;
226  
227 0 $self->change_effective_user;
228  
229 0 $self->main;
230  
231 0 CORE::exit(0);
232 }
233  
234 0 ▶sub _fork {
Subroutine _fork: not called CC=3 SCAR=24.8
235 FORK: {
236 0 ▶ if (defined(my $pid = fork())) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $pid = fork)) { } 0 0 0
elsif ($! =~ /No more process/) { } 0 0 0
237 0 return $pid;
238 }
239 elsif ($! =~ /No more process/) {
240 0 sleep(5);
241 0 redo FORK;
242 }
243 else {
244 0 die("Can't fork: $!");
245 }
246 }
247 }
248  
249  
250 0 ▶sub redirect_output {
Subroutine redirect_output: not called CC=9 SCAR=45.0
Pod undocumented
251 0 my $self = shift;
252  
253 0 my $logfile = $self->{config}->{log_file};
254  
255 0 ▶ unless ($logfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($logfile) 0 0 0
256 0 my $dir = $LOG_FILE_DIR;
257 0 my $user = getpwuid($<);
258 0 my $file = $self->daemon_name . '.log';
259 0 ▶ $logfile = (-d "$dir/$user") ? "$dir/$user/$file" : "$dir/$file";
Branch: 1 branch
BranchTrueFalseTotal
-d "$dir/$user" ? : 0 0 0
260 }
261  
262 0 ▶ die unless ($logfile =~ m/\.log$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $logfile =~ /\.log$/ 0 0 0
263  
264 0 ▶ open(LOG, '>>', $logfile) or die("Can't open log file '$logfile': $!");
Branch: 1 branch
BranchTrueFalseTotal
open LOG, ">>", $logfile or die "Can't open log file '${logfile}': $!" 0 0 0
265  
266 0 ▶ open(STDERR, '>&', \*LOG) or (print "Can't redirect STDERR to log file: $!" && CORE::exit(1));
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", \*LOG or print "Can't redirect STDERR to log file: $!" && exit 1 0 0 0
Condition0%0/2
"Can't redirect STDERR to log file: $!" && exit 1
!ll
00
MC/DC0%0/1
"Can't redirect STDERR to log file: $!" && exit 1
"Can't redirect STDERR to log file: $!"
Truth table0%0/2
A
A= "Can't redirect STDERR to log file: $!"
Aresult
00
11
267 0 ▶ open(STDOUT, '>&', \*LOG) or die("Can't redirect STDOUT to log file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", \*LOG or die "Can't redirect STDOUT to log file: $!" 0 0 0
268 0 ▶ open(STDIN, '<', '/dev/null') or die("Can't reopen STDIN to /dev/null: $!");
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", "/dev/null" or die "Can't reopen STDIN to /dev/null: $!" 0 0 0
269  
270 # Autoflush after each write
271 0 $| = 1;
272 }
273  
274  
275 0 ▶sub change_effective_user {
Subroutine change_effective_user: not called CC=9 SCAR=45.0
Pod undocumented
276 0 my $self = shift;
277  
278 # Note that privileges are not permanently dropped and can be restored.
279 # If you need to drop privileges permanently, override this method and
280 # use the module Unix::SetUser which allows to do that (or think about
281 # using 'su' to start your daemon as a non root user)
282  
283 # Only root can swith user
284 0 ▶ return unless ($> == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $> == 0 0 0 0
285  
286 0 ▶ my $as_user = $self->{options}->{user} || "nobody";
Condition0%0/2
$self->{'options'}{'user'} || 'nobody'
l!l
00
MC/DC0%0/1
$self->{'options'}{'user'} || 'nobody'
$self->{'options'}{'user'}
Truth table0%0/2
A
A= $self->{'options'}{'user'}
Aresult
11
00
287 0 ▶ my $as_group = $self->{options}->{group} || "nogroup";
Condition0%0/2
$self->{'options'}{'group'} || 'nogroup'
l!l
00
MC/DC0%0/1
$self->{'options'}{'group'} || 'nogroup'
$self->{'options'}{'group'}
Truth table0%0/2
A
A= $self->{'options'}{'group'}
Aresult
11
00
288  
289 0 my $uid = getpwnam($as_user);
290 0 my $gid = getgrnam($as_group);
291  
292 0 ▶ unless (defined $uid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uid) 0 0 0
293 0 die("Cannot switch to a non existent user '$as_user'");
294 }
295 0 ▶ unless (defined $gid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gid) 0 0 0
296 0 die("Cannot switch to a non existent group '$as_group'");
297 }
298 0 ▶ unless ($uid > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uid > 0) 0 0 0
299 0 die("Cannot run daemon as root");
300 }
301  
302 # Change the effective gid
303 0 ▶ $) = $gid or die("Cannot switch to group '$as_group': $!");
Branch: 1 branch
BranchTrueFalseTotal
$) = $gid or die "Cannot switch to group '${as_group}': $!" 0 0 0
304  
305 # Change the effective uid
306 0 ▶ $> = $uid or die("Cannot switch to user '$as_user': $!");
Branch: 1 branch
BranchTrueFalseTotal
$> = $uid or die "Cannot switch to user '${as_user}': $!" 0 0 0
307 }
308  
309 0 ▶sub restore_effective_user {
Subroutine restore_effective_user: not called CC=2 SCAR=17.9
Pod undocumented
310 0 my $self = shift;
311  
312 # Only root can swith user
313 0 ▶ return unless ($< == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $< == 0 0 0 0
314
315 # Restore the effective uid to the real uid
316 0 $> = $<;
317  
318 # Restore the effective gid to the real gid
319 0 $) = $(;
320 }
321  
322  
323 #------------------------------------------------------------------------------
324  
325 # PIDFILE HANDLING
326  
327 0 ▶sub pid_file {
Subroutine pid_file: not called CC=3 SCAR=24.8
Pod undocumented
328 0 my $self = shift;
329  
330 0 my $pidfile = $self->{config}->{pidfile};
331  
332 0 ▶ unless ($pidfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pidfile) 0 0 0
333 0 my $dir = $PID_FILE_DIR;
334 0 my $user = getpwuid($<);
335 0 my $file = $self->daemon_name . '.pid';
336 0 ▶ $pidfile = (-d "$dir/$user") ? "$dir/$user/$file" : "$dir/$file";
Branch: 1 branch
BranchTrueFalseTotal
-d "$dir/$user" ? : 0 0 0
337 }
338  
339 0 return $pidfile;
340 }
341  
342 0 ▶sub write_pid_file {
Subroutine write_pid_file: not called CC=11 SCAR=48.8
Pod undocumented
343 0 my $self = shift;
344 0 my $pidfile = $self->pid_file;
345  
346 0 ▶ die unless ($pidfile =~ m/\.pid$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $pidfile =~ /\.pid$/ 0 0 0
347  
348 # Open the pidfile in exclusive mode, to avoid race conditions
349 0 ▶ sysopen(my $fh, $pidfile, O_RDWR|O_CREAT) or die("Cannot open pid file '$pidfile': $!");
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $pidfile, 66 or die "Cannot open pid file '${pidfile}': $!" 0 0 0
350 0 ▶ flock($fh, LOCK_EX | LOCK_NB) or die("Pid file '$pidfile' is already locked");
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 6 or die "Pid file '${pidfile}' is already locked" 0 0 0
351  
352 # Read the content of the pidfile
353 0 my $pid = <$fh>;
354  
355 0 ▶ if ($pid && $pid =~ m/^(\d+)/ && $pid != $$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid and $pid =~ /^(\d+)/ and $pid != $$) 0 0 0
Condition0%0/6
$pid and $pid =~ /^(\d+)/ and $pid != $$
!ll&&!rl&&r
000
$pid and $pid =~ /^(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/3
$pid and $pid =~ /^(\d+)/ and $pid != $$
$pid $pid =~ /^(\d+)/ $pid != $$
Truth table0%0/4
A and B and C
A= $pidB= $pid =~ /^(\d+)/C= $pid != $$
ABCresult
0XX0
10X0
1100
1111
356 # File already exists and contains a process id. Check then if that
357 # process id actually belong to a running instance of this daemon
358 0 ▶ if ($self->verify_daemon_process($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verify_daemon_process($pid)) 0 0 0
359 0 close($fh);
360 0 die("Cannot write pid file: alredy running");
361 }
362 }
363  
364 # Write our process id to the file
365 0 ▶ sysseek($fh, 0, 0) or die("Cannot seek in pid file '$pidfile': $!");
Branch: 1 branch
BranchTrueFalseTotal
sysseek $fh, 0, 0 or die "Cannot seek in pid file '${pidfile}': $!" 0 0 0
366 0 ▶ truncate($fh, 0) or die("Cannot truncate pid file '$pidfile': $!");
Branch: 1 branch
BranchTrueFalseTotal
truncate $fh, 0 or die "Cannot truncate pid file '${pidfile}': $!" 0 0 0
367 0 ▶ syswrite($fh, "$$\n", length("$$\n")) or die("Cannot write to pid file '$pidfile': $!");
Branch: 1 branch
BranchTrueFalseTotal
syswrite $fh, "${$}\n", length "${$}\n" or die "Cannot write to pid file '${pidfile}': $!" 0 0 0
368 0 close($fh);
369 }
370  
371 0 ▶sub read_pid_file {
Subroutine read_pid_file: not called CC=3 SCAR=24.8
Pod undocumented
372 0 my $self = shift;
373 0 my $pidfile = $self->pid_file;
374  
375 0 ▶ unless (-e $pidfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $pidfile) 0 0 0
376 # Pidfile does not exists
377 0 return;
378 }
379  
380 # Read the content of the pidfile
381 0 ▶ open(my $fh, '<', $pidfile) or die("Cannot open pid file '$pidfile': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $pidfile or die "Cannot open pid file '${pidfile}': $!" 0 0 0
382 0 my ($pid) = <$fh> =~ /^(\d+)/;
383 0 close($fh);
384  
385 0 return $pid;
386 }
387  
388 0 ▶sub delete_pid_file {
Subroutine delete_pid_file: not called CC=5 SCAR=34.0
Pod undocumented
389 0 my $self = shift;
390  
391 0 my $pid = $self->read_pid_file;
392  
393 0 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
394 # Do not delete file, it does not exist or does not contain a process id
395 0 return;
396 }
397  
398 0 ▶ unless ($pid == $$) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid == $$) 0 0 0
399 # Do not delete file, it was not created by this process
400 0 return;
401 }
402  
403 0 my $pidfile = $self->pid_file;
404 0 ▶ die unless ($pidfile =~ m/\.pid$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $pidfile =~ /\.pid$/ 0 0 0
405 0 ▶ unlink($pidfile) or warn("Cannot unlink pid file '$pidfile' : $!");
Branch: 1 branch
BranchTrueFalseTotal
unlink $pidfile or warn "Cannot unlink pid file '${pidfile}' : $!" 0 0 0
406 }
407  
408 0 ▶sub verify_daemon_process {
Subroutine verify_daemon_process: not called CC=8 SCAR=42.8
Pod undocumented
409 0 my ($self, $pid) = @_;
410  
411 # Verify that the process identifed by the pid is actually running and
412 # is an instance of this daemon. This is necessary because the process id
413 # written to the pidfile by an instance of the daemon may coincidentally
414 # be reused by another process after a system restart, thus making the
415 # daemon think it's already running and preventing it from start at boot
416 # time. This implementation checks the 'ps' output.
417  
418 0 ▶ unless (kill(0, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (kill 0, $pid) 0 0 0
419 # Process is not running
420 0 return 0;
421 }
422  
423 0 ▶ unless ($^O =~ m/linux|freebsd/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /linux|freebsd/i) 0 0 0
424 # The ps verification will only work for Linux and FreeBSD
425 0 return 1;
426 }
427  
428 0 my $me = $0;
429 0 $me =~ s|.*/||;
430  
431 0 ▶ die unless ($pid =~ m/^\d+$/); # paranoid security check
Branch: 1 branch
BranchTrueFalseTotal
unless $pid =~ /^\d+$/ 0 0 0
432 0 ▶ my $ps_output = `ps -fp $pid` or die("ps utility not available: $!");
Branch: 1 branch
BranchTrueFalseTotal
my $ps_output = `ps -fp $pid` or die "ps utility not available: $!" 0 0 0
433  
434 0 my @ps_lines = split("$/", $ps_output);
435 0 ▶ return 0 unless (scalar @ps_lines == 2);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @ps_lines == 2 0 0 0
436 0 s/^\s+// foreach (@ps_lines); # trim leading spaces
437 0 my @ps_header = split(/\s+/, $ps_lines[0]);
438 0 my $columns_count = scalar @ps_header;
439 0 my @ps_cols = split(/\s+/, $ps_lines[1], $columns_count);
440 0 my $command = $ps_cols[$columns_count - 1]; # last column
441  
442 0 my $me_regex = quotemeta($me);
443 0 ▶ return ($command =~ m/$me_regex/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$command =~ /$me_regex/ ? : 0 0 0
444 }
445  
446 0 ▶sub daemon_is_running {
Subroutine daemon_is_running: not called CC=3 SCAR=24.8
Pod undocumented
447 0 my $self = shift;
448  
449 0 my $pid = $self->read_pid_file;
450  
451 # Daemon is not running if not pidfile exist
452 0 ▶ return 0 unless ($pid);
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
453  
454 # Verify that the process identifed by the readed pid is actually
455 # running and is an instance of this daemon
456 0 ▶ return 0 unless ($self->verify_daemon_process($pid));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->verify_daemon_process($pid) 0 0 0
457  
458 0 return $pid;
459 }
460  
461  
462 #------------------------------------------------------------------------------
463  
464 # PROCESS TERMINATION
465  
466 0 ▶sub stop_daemon {
Subroutine stop_daemon: not called CC=11 SCAR=48.8
Pod undocumented
467 0 my $self = shift;
468  
469 0 my $pid = $self->daemon_is_running;
470  
471 # Nothing to do if daemon is not running
472 0 ▶ return unless ($pid);
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
473  
474 0 my $send_SIGINT = 120; # seconds #TODO: This should be configurable
475 0 my $send_SIGKILL = 130; # seconds
476 0 my $give_up = 140; # seconds
477  
478 0 my $start_time = time();
479 0 local $| = 1;
480  
481 # Send SIGTERM (terminate request) signal
482 0 ▶ if (kill( SIGTERM, $pid )) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 15, $pid) 0 0 0
483 WAIT: {
484 0 sleep(1);
485 0 ▶ return unless kill(0, $pid);
Branch: 1 branch
BranchTrueFalseTotal
unless kill 0, $pid 0 0 0
486 0 my $elapsed = time() - $start_time;
487 0 ▶ redo if ($elapsed < $send_SIGINT);
Branch: 1 branch
BranchTrueFalseTotal
if $elapsed < $send_SIGINT 0 0 0
488 }
489 }
490  
491 # Send SIGINT (interrupt request) signal
492 0 ▶ if (kill( SIGINT, $pid )) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 2, $pid) 0 0 0
493 0 print "\nSending SIGINT to process $pid...";
494 WAIT: {
495 0 sleep(1);
496 0 ▶ return unless kill(0, $pid);
Branch: 1 branch
BranchTrueFalseTotal
unless kill 0, $pid 0 0 0
497 0 my $elapsed = time() - $start_time;
498 0 ▶ redo if ($elapsed < $send_SIGKILL);
Branch: 1 branch
BranchTrueFalseTotal
if $elapsed < $send_SIGKILL 0 0 0
499 }
500 }
501  
502 # Send SIGKILL (terminate immediately) signal
503 0 ▶ if (kill( SIGKILL, $pid )) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 9, $pid) 0 0 0
504 0 print "\nSending SIGKILL to process $pid...";
505 WAIT: {
506 0 sleep(1);
507 0 ▶ return unless kill(0, $pid);
Branch: 1 branch
BranchTrueFalseTotal
unless kill 0, $pid 0 0 0
508 0 my $elapsed = time() - $start_time;
509 0 ▶ redo if ($elapsed < $give_up);
Branch: 1 branch
BranchTrueFalseTotal
if $elapsed < $give_up 0 0 0
510 }
511 }
512  
513 0 print "\nGiving up, cannot kill process $pid.\n";
514 0 CORE::exit(1);
515 }
516  
517 0 ▶sub hup_daemon {
Subroutine hup_daemon: not called CC=2 SCAR=17.9
Pod undocumented
518 0 my $self = shift;
519  
520 0 my $pid = $self->daemon_is_running;
521  
522 # Nothing to do if daemon is not running
523 0 ▶ return unless ($pid);
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
524  
525 0 kill( SIGHUP, $pid );
526 }
527  
528 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
529 0 my $self = shift;
530  
531 0 $self->restore_effective_user;
532  
533 0 $self->delete_pid_file;
534 }
535  
536  
537 #------------------------------------------------------------------------------
538  
539 # main() method is intended to be overrided, this is just a placeholder
540  
541 0 ▶sub main {
Subroutine main: not called CC=2 SCAR=17.9
Pod undocumented
542 0 my $self = shift;
543  
544 0 print "\nStarted...\n";
545  
546 0 my $quit = 0;
547  
548 0 ▶ $SIG{'TERM'} = sub { $quit = 1 }; # SIGTERM terminate request
Subroutine __ANON__: not called CC=1 SCAR=6.9
549 0 ▶ $SIG{'INT'} = sub { $quit = 1 }; # SIGINT interrupt request, Ctrl-C
Subroutine __ANON__: not called CC=1 SCAR=6.9
550  
551 0 ▶ while (!$quit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quit) 0 0 0
552 # Do something here...
553 0 sleep 1;
554 }
555  
556 0 print "Stopped\n";
557 }
558  
559 1 1;
560  
561 __END__
« blib/lib/auto/share/dist/Beekeeper/dashboard/lib/Beekeeper/Service/Dashboard/Worker.pm ↑ Summary lib/Beekeeper/Service/Supervisor/Worker.pm »