| 1 | package Ubic::Daemon; | ||||||||||||||||||||||||||||
| 2 | 163 | $Ubic::Daemon::VERSION = '1.59'; | |||||||||||||||||||||||||||
| 3 | 163 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 163 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: daemon management utilities | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 163 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 163 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 163 | ▶ | use POSIX qw(setsid :sys_wait_h); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 163 | ▶ | use Time::HiRes qw(sleep); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 163 | ▶ | use Params::Validate qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 163 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 163 | ▶ | use Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 163 | ▶ | use Ubic::Lockf; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 163 | ▶ | use Ubic::AccessGuard; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 163 | ▶ | use Ubic::Daemon::Status; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 163 | ▶ | use Ubic::Daemon::PidState; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 163 | ▶ | use parent qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 163 | our @EXPORT_OK = qw(start_daemon stop_daemon check_daemon); | |||||||||||||||||||||||||||
| 24 | 163 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 25 | all => \@EXPORT_OK, | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 163 | our $OS; | |||||||||||||||||||||||||||
| 29 | sub import { | ||||||||||||||||||||||||||||
| 30 | 373 | ▶ | my %module = ( | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=15.5
| |||||||||||||||||||||||||||||
| 31 | linux => 'Linux', | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # UBIC_DAEMON_OS support is here only for tests | ||||||||||||||||||||||||||||
| 35 | 373 | ▶ | my $module = $ENV{UBIC_DAEMON_OS} || $module{$^O} || 'POSIX'; | ||||||||||||||||||||||||||
|
Condition40%2/5
$ENV{'UBIC_DAEMON_OS'} || $module{$^O} || 'POSIX'
$ENV{"UBIC_DAEMON_OS"} or $module{$^O}
MC/DC0%0/2
$ENV{'UBIC_DAEMON_OS'} || $module{$^O} || 'POSIX'
$ENV{"UBIC_DAEMON_OS"}
$module{$^O}
Truth table33%1/3
A or B
A= $ENV{"UBIC_DAEMON_OS"}B= $module{$^O}
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 373 | require "Ubic/Daemon/OS/$module.pm"; | |||||||||||||||||||||||||||
| 38 | 373 | $OS = eval "Ubic::Daemon::OS::$module->new"; | |||||||||||||||||||||||||||
| 39 | 373 | ▶ | unless ($OS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | die "failed to initialize OS-specific module $module: $@"; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 373 | __PACKAGE__->export_to_level(1, @_); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||
| 46 | 163 | my @signame; | |||||||||||||||||||||||||||
| 47 | sub _signame { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $signum = shift; | ||||||||||||||||||||||||||
|
Subroutine
_signame: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | unless (@signame) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | @signame = split /\s+/, $Config{sig_name}; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 0 | return $signame[$signum]; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 163 | sub _log { | |||||||||||||||||||||||||||
| 58 | 87 | ▶ | my $fh = shift; | ||||||||||||||||||||||||||
|
Subroutine
_log: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 87 | ▶ | return unless defined $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 39 | print {$fh} '[', scalar(localtime), "]\t$$\t", @_, "\n"; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub _log_exit_code { | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my ($fh, $code, $pid) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_log_exit_code: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($code == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | _log($fh, "daemon $pid exited"); | |||||||||||||||||||||||||||
| 67 | 0 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $msg = "daemon $pid failed with \$? = $?"; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (my $signal = $? & 127) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 72 | 0 | my $signame = _signame($signal); | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (defined $signame) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $msg = "daemon $pid failed with signal $signame ($signal)"; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||
| 77 | 0 | $msg = "daemon $pid failed with signal $signal"; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | elsif ($? & 128) { | ||||||||||||||||||||||||||||
| 81 | 0 | $msg = "daemon $pid failed, core dumped"; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | elsif (my $code = $? >> 8) { | ||||||||||||||||||||||||||||
| 84 | 0 | $msg = "daemon $pid failed, exit code $code"; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 0 | _log($fh, $msg); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub stop_daemon($;@) { | ||||||||||||||||||||||||||||
| 90 | 98 | ▶ | my ($pidfile, @tail) = validate_pos(@_, { type => SCALAR }, 0); | ||||||||||||||||||||||||||
|
Subroutine
stop_daemon: called CC=9 SCAR=22.4
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 95 | my $options = validate(@tail, { | |||||||||||||||||||||||||||
| 92 | timeout => { default => 30, regex => qr/^\d+$/ }, | ||||||||||||||||||||||||||||
| 93 | }); | ||||||||||||||||||||||||||||
| 94 | 92 | ▶ | my $timeout = $options->{timeout} if defined $options->{timeout}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # TODO - move this check into Ubic::Daemon::PidState | ||||||||||||||||||||||||||||
| 97 | 92 | my $pid_state = Ubic::Daemon::PidState->new($pidfile); | |||||||||||||||||||||||||||
| 98 | 92 | ▶ | return 'not running' if $pid_state->is_empty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 78 | my $piddata = $pid_state->read; | |||||||||||||||||||||||||||
| 101 | 78 | ▶ | unless ($piddata) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | return 'not running'; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 78 | my $pid = $piddata->{pid}; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 78 | ▶ | unless (check_daemon($pidfile)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | return 'not running'; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 78 | kill 15 => $pid; | |||||||||||||||||||||||||||
| 110 | 78 | my $trial = 1; | |||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 78 | my $sleep = 0.1; | |||||||||||||||||||||||||||
| 113 | 78 | my $total_sleep = 0; | |||||||||||||||||||||||||||
| 114 | 78 | while (1) { | |||||||||||||||||||||||||||
| 115 | 206 | ▶ | unless (check_daemon($pidfile)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 75 | return 'stopped'; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 131 | ▶ | last if $total_sleep >= $timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 128 | sleep($sleep); | |||||||||||||||||||||||||||
| 120 | 128 | $total_sleep += $sleep; | |||||||||||||||||||||||||||
| 121 | 128 | ▶ | $sleep += 0.1 * $trial if $sleep < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 128 | $trial++; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 3 | ▶ | unless (check_daemon($pidfile)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | return 'stopped'; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 3 | die "failed to stop daemon with pidfile '$pidfile' (pid $pid, timeout $timeout, trials $trial)"; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub start_daemon($) { | ||||||||||||||||||||||||||||
| 132 | 193 | ▶ | my %options = validate(@_, { | ||||||||||||||||||||||||||
|
Subroutine
start_daemon: called CC=65 SCAR=65.0
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | bin => { type => SCALAR | ARRAYREF, optional => 1 }, | ||||||||||||||||||||||||||||
| 134 | function => { type => CODEREF, optional => 1 }, | ||||||||||||||||||||||||||||
| 135 | name => { type => SCALAR, optional => 1 }, | ||||||||||||||||||||||||||||
| 136 | pidfile => { type => SCALAR }, | ||||||||||||||||||||||||||||
| 137 | stdout => { type => SCALAR, default => '/dev/null' }, | ||||||||||||||||||||||||||||
| 138 | stderr => { type => SCALAR, default => '/dev/null' }, | ||||||||||||||||||||||||||||
| 139 | ubic_log => { type => SCALAR, optional => 1 }, | ||||||||||||||||||||||||||||
| 140 | term_timeout => { type => SCALAR, default => 10, regex => qr/^\d+$/ }, | ||||||||||||||||||||||||||||
| 141 | cwd => { type => SCALAR, optional => 1 }, | ||||||||||||||||||||||||||||
| 142 | env => { type => HASHREF, optional => 1 }, | ||||||||||||||||||||||||||||
| 143 | proxy_logs => { type => BOOLEAN, optional => 1 }, | ||||||||||||||||||||||||||||
| 144 | credentials => { isa => 'Ubic::Credentials', optional => 1 }, | ||||||||||||||||||||||||||||
| 145 | start_hook => { type => CODEREF, optional => 1 }, | ||||||||||||||||||||||||||||
| 146 | }); | ||||||||||||||||||||||||||||
| 147 | my ($bin, $function, $name, $pidfile, $stdout, $stderr, $ubic_log, $term_timeout, $cwd, $env, $credentials, $start_hook, $proxy_logs) | ||||||||||||||||||||||||||||
| 148 | 192 | = @options{qw/ bin function name pidfile stdout stderr ubic_log term_timeout cwd env credentials start_hook proxy_logs /}; | |||||||||||||||||||||||||||
| 149 | 192 | ▶ | if (not defined $bin and not defined $function) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bin or defined $function
MC/DC0%0/2
defined $bin or defined $function
defined $bin
defined $function
Truth table66%2/3
A or B
A= defined $binB= defined $function
| |||||||||||||||||||||||||||||
| 150 | 0 | croak "One of 'bin' and 'function' should be specified"; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 192 | ▶ | if (defined $bin and defined $function) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bin and defined $function
MC/DC0%0/2
defined $bin and defined $function
defined $bin
defined $function
Truth table66%2/3
A and B
A= defined $binB= defined $function
| |||||||||||||||||||||||||||||
| 153 | 0 | croak "Only one of 'bin' and 'function' should be specified"; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 192 | ▶ | unless (defined $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 154 | ▶ | if (ref $bin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 83 | $name = join ' ', @$bin; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | else { | ||||||||||||||||||||||||||||
| 160 | 71 | ▶ | $name = $bin || 'anonymous'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$bin || 'anonymous'
MC/DC0%0/1
$bin || 'anonymous'
$bin
Truth table50%1/2
A
A= $bin
| |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 192 | ▶ | if (check_daemon($pidfile)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 12 | croak "Daemon with pidfile $pidfile already running, can't start"; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 180 | my $pid_state = Ubic::Daemon::PidState->new($pidfile); | |||||||||||||||||||||||||||
| 169 | 180 | $pid_state->init; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 180 | ▶ | pipe my ($read_pipe, $write_pipe) or die "pipe failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 180 | my $child; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 180 | ▶ | unless ($child = fork) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 29 | ▶ | die "fork failed" unless defined $child; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 29 | my $ubic_fh; | |||||||||||||||||||||||||||
| 178 | my $lock; | ||||||||||||||||||||||||||||
| 179 | my $instant_exit = sub { | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | close($ubic_fh) if $ubic_fh; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | STDOUT->flush; | |||||||||||||||||||||||||||
| 182 | 0 | STDERR->flush; | |||||||||||||||||||||||||||
| 183 | 0 | undef $lock; | |||||||||||||||||||||||||||
| 184 | 0 | POSIX::_exit(0); # don't allow any cleanup to happen - this process was forked from unknown environment, don't want to run unknown destructors | |||||||||||||||||||||||||||
| 185 | 29 | }; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 29 | eval { | |||||||||||||||||||||||||||
| 188 | 29 | ▶ | close($read_pipe) or die "Can't close read pipe: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | # forking child - will reopen standard streams, daemonize itself, fork into daemon binary and wait for it | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | { | ||||||||||||||||||||||||||||
| 192 | 29 | ▶ | my $tmp_pid = fork() and POSIX::_exit(0); # detach from parent process | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 29 | ▶ | die "fork failed" unless defined $tmp_pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # Close all inherited filehandles except $write_pipe (it will be closed explicitly). | ||||||||||||||||||||||||||||
| 197 | # Do not close fh if uses 'function' option instead of 'bin' | ||||||||||||||||||||||||||||
| 198 | # ('function' support should be removed altogether because of this, actually; it's evil). | ||||||||||||||||||||||||||||
| 199 | 29 | ▶ | if ($bin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 27 | my $write_pipe_fd_num = fileno($write_pipe); | |||||||||||||||||||||||||||
| 201 | 27 | $OS->close_all_fh($write_pipe_fd_num); # except pipe | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | my $open_handles_sub = sub { | ||||||||||||||||||||||||||||
| 205 | 29 | ▶ | my $guard; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.5
| |||||||||||||||||||||||||||||
| 206 | 29 | ▶ | $guard = Ubic::AccessGuard->new($credentials) if $credentials; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 29 | ▶ | open STDOUT, ">>", $stdout or die "Can't write to '$stdout': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 29 | ▶ | open STDERR, ">>", $stderr or die "Can't write to '$stderr': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 29 | STDOUT->autoflush(1); | |||||||||||||||||||||||||||
| 210 | 29 | STDERR->autoflush(1); | |||||||||||||||||||||||||||
| 211 | 29 | ▶ | if (defined $ubic_log) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 13 | ▶ | open $ubic_fh, ">>", $ubic_log or die "Can't write to '$ubic_log': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 13 | $ubic_fh->autoflush(1); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | 29 | }; | |||||||||||||||||||||||||||
| 216 | 29 | $open_handles_sub->(); | |||||||||||||||||||||||||||
| 217 | 29 | my $stdin = '/dev/null'; | |||||||||||||||||||||||||||
| 218 | 29 | ▶ | open STDIN, "<", $stdin or die "Can't read from '$stdin': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 29 | $SIG{HUP} = 'ignore'; | |||||||||||||||||||||||||||
| 221 | 29 | $0 = "ubic-guardian $name"; | |||||||||||||||||||||||||||
| 222 | 29 | setsid; # ubic-daemon gets it's own session | |||||||||||||||||||||||||||
| 223 | 29 | _log($ubic_fh, "guardian name: $0"); | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 29 | _log($ubic_fh, "obtaining lock..."); | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # We're passing 'timeout' option to lockf call to get rid of races. | ||||||||||||||||||||||||||||
| 228 | # There should be no races when Ubic::Daemon is used in context of | ||||||||||||||||||||||||||||
| 229 | # ubic service, because services have an additional lock, but | ||||||||||||||||||||||||||||
| 230 | # Ubic::Daemon can be useful without services as well. | ||||||||||||||||||||||||||||
| 231 | 29 | ▶ | $lock = $pid_state->lock(5) or die "Can't lock $pid_state"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 29 | $pid_state->remove; | |||||||||||||||||||||||||||
| 234 | 29 | _log($ubic_fh, "got lock"); | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 29 | my %daemon_pipes; | |||||||||||||||||||||||||||
| 237 | 29 | ▶ | if (defined $proxy_logs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 2 | for my $handle (qw/stdout stderr/) { | |||||||||||||||||||||||||||
| 239 | 4 | ▶ | pipe my ($read, $write) or die "pipe for daemon $handle failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 4 | $daemon_pipes{$handle} = {read => $read, write => $write}; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | 29 | my $child; | |||||||||||||||||||||||||||
| 244 | 29 | ▶ | if ($child = fork) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | # guardian | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | _log($ubic_fh, "guardian pid: $$"); | |||||||||||||||||||||||||||
| 248 | 0 | _log($ubic_fh, "daemon pid: $child"); | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 0 | my $child_guid = $OS->pid2guid($child); | |||||||||||||||||||||||||||
| 251 | 0 | ▶ | unless ($child_guid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | ▶ | if ($OS->pid_exists($child)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 0 | die "Can't detect guid"; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | 0 | $? = 0; | |||||||||||||||||||||||||||
| 256 | 0 | ▶ | unless (waitpid($child, WNOHANG) == $child) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 0 | die "No pid $child but waitpid didn't collect $child status"; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | 0 | _log_exit_code($ubic_fh, $?, $child); | |||||||||||||||||||||||||||
| 260 | 0 | $pid_state->remove(); | |||||||||||||||||||||||||||
| 261 | 0 | die "daemon exited immediately"; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | 0 | _log($ubic_fh, "child guid: $child_guid"); | |||||||||||||||||||||||||||
| 264 | 0 | $pid_state->write({ pid => $child, guid => $child_guid }); | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | my $kill_sub = sub { | ||||||||||||||||||||||||||||
| 267 | 0 | ▶ | if ($term_timeout) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 0 | _log($ubic_fh, "SIGTERM timeouted after $term_timeout second(s)"); | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | 0 | _log($ubic_fh, "sending SIGKILL to $child"); | |||||||||||||||||||||||||||
| 271 | 0 | kill -9 => $child; | |||||||||||||||||||||||||||
| 272 | 0 | _log($ubic_fh, "daemon $child probably killed by SIGKILL"); | |||||||||||||||||||||||||||
| 273 | 0 | $pid_state->remove(); | |||||||||||||||||||||||||||
| 274 | 0 | $instant_exit->(); | |||||||||||||||||||||||||||
| 275 | 0 | }; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | my $sigterm_sent; | |||||||||||||||||||||||||||
| 278 | $SIG{TERM} = sub { | ||||||||||||||||||||||||||||
| 279 | 0 | ▶ | if ($term_timeout > 0) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 0 | $SIG{ALRM} = $kill_sub; | |||||||||||||||||||||||||||
| 281 | 0 | alarm($term_timeout); | |||||||||||||||||||||||||||
| 282 | 0 | _log($ubic_fh, "sending SIGTERM to $child"); | |||||||||||||||||||||||||||
| 283 | 0 | kill -15 => $child; | |||||||||||||||||||||||||||
| 284 | 0 | $sigterm_sent = 1; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | else { | ||||||||||||||||||||||||||||
| 287 | 0 | $kill_sub->(); | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | 0 | }; | |||||||||||||||||||||||||||
| 290 | 0 | ▶ | print {$write_pipe} "pidfile written\n" or die "Can't write to pipe: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | 0 | ▶ | close $write_pipe or die "Can't close pipe: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | undef $write_pipe; | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 0 | ▶ | if (defined $proxy_logs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | $SIG{HUP} = sub { | ||||||||||||||||||||||||||||
| 296 | 0 | ▶ | eval { $open_handles_sub->() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | _log($ubic_fh, "failed to reopen stdout/stderr handles: $@"); | |||||||||||||||||||||||||||
| 299 | 0 | $kill_sub->(); | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | else { | ||||||||||||||||||||||||||||
| 302 | 0 | _log($ubic_fh, "reopened stdout/stderr"); | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | 0 | }; | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 0 | for my $handle (qw/stdout stderr/) { | |||||||||||||||||||||||||||
| 307 | 0 | ▶ | close($daemon_pipes{$handle}{write}) or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 0 | _log($ubic_fh, "Can't close $handle write: $!"); | |||||||||||||||||||||||||||
| 309 | 0 | die "Can't close $handle write: $!" | |||||||||||||||||||||||||||
| 310 | }; | ||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | 0 | my $sel = IO::Select->new(); | |||||||||||||||||||||||||||
| 313 | 0 | $sel->add($daemon_pipes{stdout}{read}, $daemon_pipes{stderr}{read}); | |||||||||||||||||||||||||||
| 314 | 0 | my $BUFF_SIZE = 4096; | |||||||||||||||||||||||||||
| 315 | READ: | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | while ($OS->pid_exists($child)) { # this loop is needed because of timeout in can_read | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | 0 | ▶ | while (my @ready = $sel->can_read(1)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 0 | my $exhausted = 0; | |||||||||||||||||||||||||||
| 319 | 0 | for my $handle (@ready) { | |||||||||||||||||||||||||||
| 320 | 0 | my $data; | |||||||||||||||||||||||||||
| 321 | 0 | my $bytes_read = sysread($handle, $data, $BUFF_SIZE); | |||||||||||||||||||||||||||
| 322 | 0 | ▶ | die "Can't poll $handle: $!" unless defined $bytes_read; # handle EWOULDBLOCK? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 0 | ▶ | $exhausted += 1 if $bytes_read == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 0 | ▶ | if (fileno $handle == fileno $daemon_pipes{stdout}{read}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 0 | print STDOUT $data; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | 0 | ▶ | if (fileno $handle == fileno $daemon_pipes{stderr}{read}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 0 | print STDERR $data; | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | 0 | ▶ | last READ if $exhausted == @ready; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 0 | $? = 0; | |||||||||||||||||||||||||||
| 337 | 0 | waitpid($child, 0); | |||||||||||||||||||||||||||
| 338 | 0 | my $code = $?; | |||||||||||||||||||||||||||
| 339 | 0 | ▶ | if ($sigterm_sent and ($code & 127) == &POSIX::SIGTERM) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$sigterm_sent and ($code & 127) == &POSIX::SIGTERM
MC/DC0%0/2
$sigterm_sent and ($code & 127) == &POSIX::SIGTERM
$sigterm_sent
($code & 127) == &POSIX::SIGTERM
Truth table0%0/3
A and B
A= $sigterm_sentB= ($code & 127) == &POSIX::SIGTERM
| |||||||||||||||||||||||||||||
| 340 | # it's ok, we probably sent this signal ourselves | ||||||||||||||||||||||||||||
| 341 | 0 | _log($ubic_fh, "daemon $child exited by sigterm"); | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | else { | ||||||||||||||||||||||||||||
| 344 | 0 | _log_exit_code($ubic_fh, $code, $child); | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | 0 | $pid_state->remove; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | else { | ||||||||||||||||||||||||||||
| 349 | # daemon | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 29 | ▶ | die "fork failed" unless defined $child; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | # start new process group - become immune to kills at parent group and at the same time be able to kill all processes below | ||||||||||||||||||||||||||||
| 354 | 29 | setpgrp; | |||||||||||||||||||||||||||
| 355 | 29 | $0 = "ubic-daemon $name"; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 29 | ▶ | if (defined $cwd) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | 1 | ▶ | chdir $cwd or die "chdir to '$cwd' failed: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | 29 | ▶ | if (defined $env) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | 0 | for my $key (keys %{ $env }) { | |||||||||||||||||||||||||||
| 362 | 0 | $ENV{$key} = $env->{$key}; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | 29 | ▶ | $start_hook->() if $start_hook; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 29 | ▶ | $credentials->set() if $credentials; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 29 | ▶ | close($ubic_fh) if defined $ubic_fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 29 | $lock->dissolve; | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 29 | ▶ | print {$write_pipe} "execing into daemon\n" or die "Can't write to pipe: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 29 | ▶ | close($write_pipe) or die "Can't close pipe: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 29 | undef $write_pipe; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 29 | ▶ | if (defined $proxy_logs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | # redirecting standard streams to pipes | ||||||||||||||||||||||||||||
| 377 | 2 | ▶ | close($daemon_pipes{$_}{read}) or die "Can't close $_ read: $!" for qw/stdout stderr/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | 2 | ▶ | open STDOUT, '>&=', $daemon_pipes{stdout}{write} or die "Can't open stdout write: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 2 | ▶ | open STDERR, '>&=', $daemon_pipes{stderr}{write} or die "Can't open stderr write: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | # finally, run underlying binary | ||||||||||||||||||||||||||||
| 383 | 29 | ▶ | if (ref $bin) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 384 | 13 | ▶ | exec(@$bin) or die "exec failed: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | elsif ($bin) { | ||||||||||||||||||||||||||||
| 387 | 14 | ▶ | exec($bin) or die "exec failed: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | else { | ||||||||||||||||||||||||||||
| 390 | 2 | $function->(); | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | }; | ||||||||||||||||||||||||||||
| 394 | 0 | ▶ | if ($write_pipe) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 0 | print {$write_pipe} "Error: $@\n"; | |||||||||||||||||||||||||||
| 396 | 0 | $write_pipe->flush; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | 0 | $instant_exit->(); | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | 151 | waitpid($child, 0); # child should've exited immediately | |||||||||||||||||||||||||||
| 401 | 151 | ▶ | close($write_pipe) or die "Can't close write_pipe: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 151 | my $out = ''; | |||||||||||||||||||||||||||
| 404 | 151 | ▶ | while ( my $data = <$read_pipe>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | 302 | $out .= $data; | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | 151 | ▶ | close($read_pipe) or die "Can't close read_pipe: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | 151 | ▶ | if ($out =~ /^execing into daemon$/m and $out =~ /^pidfile written$/m) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$out =~ /^execing into daemon$/m and $out =~ /^pidfile written$/m
MC/DC50%1/2
$out =~ /^execing into daemon$/m and $out =~ /^pidfile written$/m
$out =~ /^execing into daemon$/m
$out =~ /^pidfile written$/m
Truth table66%2/3
A and B
A= $out =~ /^execing into daemon$/mB= $out =~ /^pidfile written$/m
| |||||||||||||||||||||||||||||
| 409 | # TODO - check daemon's name to make sure that exec happened | ||||||||||||||||||||||||||||
| 410 | 140 | return; | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | 11 | die "Failed to create daemon: '$out'"; | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | sub check_daemon { | ||||||||||||||||||||||||||||
| 416 | 802 | ▶ | my $pidfile = shift; | ||||||||||||||||||||||||||
|
Subroutine
check_daemon: called CC=10 SCAR=27.0
Pod
documented
| |||||||||||||||||||||||||||||
| 417 | 802 | my $options = validate(@_, { | |||||||||||||||||||||||||||
| 418 | quiet => { optional => 1 }, | ||||||||||||||||||||||||||||
| 419 | }); | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | my $print = sub { | ||||||||||||||||||||||||||||
| 422 | 10 | ▶ | print @_, "\n" unless $options->{quiet}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 423 | 802 | }; | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 802 | my $pid_state = Ubic::Daemon::PidState->new($pidfile); | |||||||||||||||||||||||||||
| 426 | 802 | ▶ | return undef if $pid_state->is_empty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | 369 | my $lock = $pid_state->lock; | |||||||||||||||||||||||||||
| 429 | 369 | my $piddata = $pid_state->read; | |||||||||||||||||||||||||||
| 430 | 369 | ▶ | unless ($lock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 431 | # locked => daemon is alive | ||||||||||||||||||||||||||||
| 432 | 364 | return Ubic::Daemon::Status->new({ pid => $piddata->{daemon}, guardian_pid => $piddata->{pid} }); | |||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 5 | ▶ | unless ($piddata) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | 0 | return undef; | |||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | # acquired lock when pidfile exists | ||||||||||||||||||||||||||||
| 440 | # checking whether just ubic-guardian died or whole process group | ||||||||||||||||||||||||||||
| 441 | 5 | ▶ | if ($piddata->{format} and $piddata->{format} eq 'old') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$piddata->{"format"} and $piddata->{"format"} eq "old"
MC/DC0%0/2
$piddata->{"format"} and $piddata->{"format"} eq "old"
$piddata->{"format"}
$piddata->{"format"} eq "old"
Truth table33%1/3
A and B
A= $piddata->{"format"}B= $piddata->{"format"} eq "old"
| |||||||||||||||||||||||||||||
| 442 | 0 | die "deprecated pidfile format detected\n"; | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | 5 | ▶ | unless ($piddata->{daemon}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 445 | 163 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 446 | 0 | die "pidfile $pidfile exists, but daemon pid is not saved in it, so existing unguarded daemon can't be killed (piddata: ".Dumper($piddata).")"; | |||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | 5 | ▶ | unless ($OS->pid_exists($piddata->{daemon})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 449 | 0 | $pid_state->remove; | |||||||||||||||||||||||||||
| 450 | 0 | $print->("pidfile $pidfile removed - daemon with cached pid $piddata->{daemon} not found"); | |||||||||||||||||||||||||||
| 451 | 0 | return undef; | |||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | # TODO - wrap in eval and return undef if pid2cmd fails? | ||||||||||||||||||||||||||||
| 455 | 5 | my $daemon_cmd = $OS->pid2cmd($piddata->{daemon}); | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 5 | my $guid = $OS->pid2guid($piddata->{daemon}); | |||||||||||||||||||||||||||
| 458 | 5 | ▶ | unless ($guid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 459 | 0 | $print->("daemon '$daemon_cmd' from $pidfile just disappeared"); | |||||||||||||||||||||||||||
| 460 | 0 | return undef; | |||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | 5 | ▶ | if ($guid eq $piddata->{guid}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 463 | 5 | $print->("killing unguarded daemon '$daemon_cmd' with pid $piddata->{daemon} from $pidfile"); | |||||||||||||||||||||||||||
| 464 | 5 | kill -9 => $piddata->{daemon}; | |||||||||||||||||||||||||||
| 465 | 5 | $pid_state->remove; | |||||||||||||||||||||||||||
| 466 | 5 | $print->("pidfile $pidfile removed"); | |||||||||||||||||||||||||||
| 467 | 5 | return undef; | |||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | 0 | $print->("daemon pid $piddata->{daemon} cached in pidfile $pidfile, ubic-guardian not found"); | |||||||||||||||||||||||||||
| 470 | 0 | $print->("current process '$daemon_cmd' with pid $piddata->{daemon} has wrong guid ($piddata->{guid}, expected $guid) and will not be killed"); | |||||||||||||||||||||||||||
| 471 | 0 | $print->("removing pidfile $pidfile"); | |||||||||||||||||||||||||||
| 472 | 0 | $pid_state->remove; | |||||||||||||||||||||||||||
| 473 | 0 | return undef; | |||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | 163 | 1; | |||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | __END__ | ||||||||||||||||||||||||||||