« 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
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | foreach my $opt (@$extra_options) { | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | next if (grep { $opt eq $_ } @options_spec); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my %options; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | GetOptions(\%options, @options_spec) or CORE::exit(1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
not $cmd or $self->{"options"}{"help"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($cmd eq 'start') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | print " is already running.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | print ".\n" if ($self->{options}->{foreground}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | # Fork and exit parent | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | _fork() && return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | # Detach ourselves from the terminal | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | POSIX::setsid() or die("Cannot detach from controlling terminal"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | # Prevent possibility of acquiring a controling terminal | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | $SIG{'HUP'} = 'IGNORE'; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | _fork() && CORE::exit(0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
not defined $openmax or $openmax < 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | die unless ($logfile =~ m/\.log$/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | open(LOG, '>>', $logfile) or die("Can't open log file '$logfile': $!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | open(STDERR, '>&', \*LOG) or (print "Can't redirect STDERR to log file: $!" && CORE::exit(1)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
"Can't redirect STDERR to log file: $!" && exit 1
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: $!"
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | open(STDOUT, '>&', \*LOG) or die("Can't redirect STDOUT to log file: $!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | open(STDIN, '<', '/dev/null') or die("Can't reopen STDIN to /dev/null: $!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | my $as_user = $self->{options}->{user} || "nobody"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'options'}{'user'} || 'nobody'
MC/DC0%0/1
$self->{'options'}{'user'} || 'nobody'
$self->{'options'}{'user'}
Truth table0%0/2
A
A= $self->{'options'}{'user'}
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | my $as_group = $self->{options}->{group} || "nogroup"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'options'}{'group'} || 'nogroup'
MC/DC0%0/1
$self->{'options'}{'group'} || 'nogroup'
$self->{'options'}{'group'}
Truth table0%0/2
A
A= $self->{'options'}{'group'}
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | my $uid = getpwnam($as_user); | |||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | my $gid = getgrnam($as_group); | |||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | unless (defined $uid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | die("Cannot switch to a non existent user '$as_user'"); | |||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | unless (defined $gid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | die("Cannot switch to a non existent group '$as_group'"); | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | unless ($uid > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | # Change the effective uid | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | ▶ | $> = $uid or die("Cannot switch to user '$as_user': $!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | flock($fh, LOCK_EX | LOCK_NB) or die("Pid file '$pidfile' is already locked"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$pid and $pid =~ /^(\d+)/ and $pid != $$
$pid and $pid =~ /^(\d+)/
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 != $$
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | ▶ | truncate($fh, 0) or die("Cannot truncate pid file '$pidfile': $!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | ▶ | syswrite($fh, "$$\n", length("$$\n")) or die("Cannot write to pid file '$pidfile': $!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | unlink($pidfile) or warn("Cannot unlink pid file '$pidfile' : $!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | # Process is not running | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | ▶ | unless ($^O =~ m/linux|freebsd/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | ▶ | my $ps_output = `ps -fp $pid` or die("ps utility not available: $!"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | my @ps_lines = split("$/", $ps_output); | |||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | return 0 unless (scalar @ps_lines == 2); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 483 | WAIT: { | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | sleep(1); | |||||||||||||||||||||||||||||||||||||||||
| 485 | 0 | ▶ | return unless kill(0, $pid); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | my $elapsed = time() - $start_time; | |||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | ▶ | redo if ($elapsed < $send_SIGINT); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | # Send SIGINT (interrupt request) signal | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 0 | ▶ | if (kill( SIGINT, $pid )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 493 | 0 | print "\nSending SIGINT to process $pid..."; | |||||||||||||||||||||||||||||||||||||||||
| 494 | WAIT: { | ||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | sleep(1); | |||||||||||||||||||||||||||||||||||||||||
| 496 | 0 | ▶ | return unless kill(0, $pid); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | my $elapsed = time() - $start_time; | |||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | ▶ | redo if ($elapsed < $send_SIGKILL); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | # Send SIGKILL (terminate immediately) signal | ||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | ▶ | if (kill( SIGKILL, $pid )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | print "\nSending SIGKILL to process $pid..."; | |||||||||||||||||||||||||||||||||||||||||
| 505 | WAIT: { | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | sleep(1); | |||||||||||||||||||||||||||||||||||||||||
| 507 | 0 | ▶ | return unless kill(0, $pid); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 508 | 0 | my $elapsed = time() - $start_time; | |||||||||||||||||||||||||||||||||||||||||
| 509 | 0 | ▶ | redo if ($elapsed < $give_up); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | # Do something here... | ||||||||||||||||||||||||||||||||||||||||||
| 553 | 0 | sleep 1; | |||||||||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | 0 | print "Stopped\n"; | |||||||||||||||||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||
| 559 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | __END__ | ||||||||||||||||||||||||||||||||||||||||||