| 1 | package Ubic::Daemon::PidState; | ||||||||||||||||||||||||||||
| 2 | 163 | $Ubic::Daemon::PidState::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: internal object representing process info stored on disk | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 163 | ▶ | use Params::Validate qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 163 | ▶ | use Ubic::Lockf; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 163 | ▶ | use Ubic::AtomicFile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | use overload '""' => sub { | ||||||||||||||||||||||||||||
| 14 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 15 | 0 | return $self->{dir}; | |||||||||||||||||||||||||||
| 16 | 163 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub new { | ||||||||||||||||||||||||||||
| 19 | 1074 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 1074 | my ($dir) = validate_pos(@_, { type => SCALAR }); | |||||||||||||||||||||||||||
| 21 | 1074 | return bless { dir => $dir } => $class; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub is_empty { | ||||||||||||||||||||||||||||
| 25 | 894 | ▶ | my ($self) = validate_pos(@_, 1); | ||||||||||||||||||||||||||
|
Subroutine
is_empty: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 894 | my $dir = $self->{dir}; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 894 | ▶ | return if not -d $dir and -s $dir; # old-style pidfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not -d $dir and -s $dir
MC/DC0%0/2
not -d $dir and -s $dir
not -d $dir
-s $dir
Truth table66%2/3
not(A) and B
A= not -d $dirB= -s $dir
| |||||||||||||||||||||||||||||
| 29 | 894 | ▶ | return if -d $dir and -s "$dir/pid"; # non-empty new-style pidfile | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
-d $dir and -s "$dir/pid"
MC/DC100%2/2
-d $dir and -s "$dir/pid"
-d $dir
-s "$dir/pid"
Truth table100%3/3
A and B
A= -d $dirB= -s "$dir/pid"
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 447 | return 1; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub init { | ||||||||||||||||||||||||||||
| 35 | 180 | ▶ | my ($self) = validate_pos(@_, 1); | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 180 | my $dir = $self->{dir}; | |||||||||||||||||||||||||||
| 37 | 180 | ▶ | if (-e $dir and not -d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $dir and not -d $dir
MC/DC0%0/2
-e $dir and not -d $dir
-e $dir
not -d $dir
Truth table66%2/3
A and not(B)
A= -e $dirB= not -d $dir
| |||||||||||||||||||||||||||||
| 38 | 0 | print "converting $dir to dir\n"; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | unlink $dir or die "Can't unlink $dir: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 180 | ▶ | unless (-d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 121 | ▶ | mkdir $dir or die "Can't create $dir: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | sub read { | ||||||||||||||||||||||||||||
| 48 | 447 | ▶ | my ($self) = validate_pos(@_, 1); | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=8 SCAR=31.5
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 447 | my $dir = $self->{dir}; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 447 | my $content; | |||||||||||||||||||||||||||
| 53 | my $parse_content = sub { | ||||||||||||||||||||||||||||
| 54 | 447 | ▶ | if ($content =~ /\A pid \s+ (\d+) \n guid \s+ (.+) (?: \n daemon \s+ (\d+) )? \Z/x) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=16.5
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 55 | # new format | ||||||||||||||||||||||||||||
| 56 | 447 | return { pid => $1, guid => $2, daemon => $3, format => 'new' }; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | elsif ($content eq '') { | ||||||||||||||||||||||||||||
| 59 | # file is empty, probably lost after reboot - we didn't sync() pidfile to disk in old versions | ||||||||||||||||||||||||||||
| 60 | 0 | return; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | else { | ||||||||||||||||||||||||||||
| 63 | # We consider invalid pidfile content as the fatal, unrecoverable error. | ||||||||||||||||||||||||||||
| 64 | # Please report all cases of invalid pidfile as critical issues. | ||||||||||||||||||||||||||||
| 65 | # | ||||||||||||||||||||||||||||
| 66 | # (This policy will be changed if ubic become popular enough for this to annoy enough people, | ||||||||||||||||||||||||||||
| 67 | # but collecting bugreports about weird stuff happening is more important by now.) | ||||||||||||||||||||||||||||
| 68 | 0 | die "invalid pidfile content in pidfile $dir"; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 447 | }; | |||||||||||||||||||||||||||
| 71 | 447 | ▶ | if (-d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | # pidfile as dir | ||||||||||||||||||||||||||||
| 73 | 447 | my $open_success = open my $fh, '<', "$dir/pid"; | |||||||||||||||||||||||||||
| 74 | 447 | ▶ | unless ($open_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($!{ENOENT}) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | return; # pidfile not found, daemon is not running | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 0 | die "Failed to open '$dir/pid': $!"; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 447 | $content = join '', <$fh>; | |||||||||||||||||||||||||||
| 83 | 447 | return $parse_content->(); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | # deprecated - single pidfile without dir | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if (-f $dir and not -s $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $dir and not -s $dir
MC/DC0%0/2
-f $dir and not -s $dir
-f $dir
not -s $dir
Truth table0%0/3
A and not(B)
A= -f $dirB= not -s $dir
| |||||||||||||||||||||||||||||
| 88 | 0 | return; # empty pidfile - old way to stop services | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | open my $fh, '<', $dir or die "Failed to open $dir: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $content = join '', <$fh>; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($content =~ /\A (\d+) \Z/x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | # old format | ||||||||||||||||||||||||||||
| 94 | 0 | return { pid => $1, format => 'old' }; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 0 | return $parse_content->(); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub lock { | ||||||||||||||||||||||||||||
| 103 | 398 | ▶ | my ($self, $timeout) = validate_pos(@_, 1, { type => SCALAR, default => 0 }); | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 398 | my $dir = $self->{dir}; | |||||||||||||||||||||||||||
| 106 | 398 | ▶ | if (-d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | # new-style pidfile | ||||||||||||||||||||||||||||
| 108 | 398 | return lockf("$dir/lock", { timeout => $timeout }); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||
| 111 | 0 | return lockf($dir, { blocking => 0 }); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub remove { | ||||||||||||||||||||||||||||
| 116 | 34 | ▶ | my ($self) = validate_pos(@_, 1); | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=5 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 34 | my $dir = $self->{dir}; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 34 | ▶ | if (-d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 34 | ▶ | if (-e "$dir/pid") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 5 | ▶ | unlink "$dir/pid" or die "Can't remove $dir/pid: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | unlink $dir or die "Can't remove $dir: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub write { | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
write: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 0 | my $dir = $self->{dir}; | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | unless (-d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | die "piddir $dir not initialized"; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 0 | my $params = validate(@_, { | |||||||||||||||||||||||||||
| 136 | pid => 1, | ||||||||||||||||||||||||||||
| 137 | guid => 1, | ||||||||||||||||||||||||||||
| 138 | }); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | my ($pid, $guid) = @$params{qw/ pid guid /}; | |||||||||||||||||||||||||||
| 141 | 0 | my $self_pid = $$; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my $content = | |||||||||||||||||||||||||||
| 144 | "pid $self_pid\n". | ||||||||||||||||||||||||||||
| 145 | "guid $guid\n". | ||||||||||||||||||||||||||||
| 146 | "daemon $pid\n"; | ||||||||||||||||||||||||||||
| 147 | 0 | Ubic::AtomicFile::store( $content => "$dir/pid" ); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 163 | 1; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||||||||||