lib/Ubic/Daemon/PidState.pm

filter: Statementstmt 67.4% 56 / 83 Branchbran 37.5% 15 / 40 Conditioncond 58.3% 7 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 87.5% 14 / 16 Podpod 100% 7 / 7 totaltotal 60.8% 101 / 166 SCAR 42.8
CC
25
Cov
58%
read
23.4
init
8.1
remove
6.1
CRAP 72.0
« blib/lib/Ubic/Cmd/Results.pm ↑ Summary blib/lib/Ubic/Daemon/OS/Linux.pm »
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
BranchTrueFalseTotal
if not -d $dir and -s $dir 0 894 894
Condition66%2/3
not -d $dir and -s $dir
!ll&&!rl&&r
6951990
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
ABresult
0X0
100
111
29 894 ▶ return if -d $dir and -s "$dir/pid"; # non-empty new-style pidfile
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir and -s "$dir/pid" 447 447 894
Condition100%3/3
-d $dir and -s "$dir/pid"
!ll&&!rl&&r
199248447
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (-e $dir and not -d $dir) 0 180 180
Condition66%2/3
-e $dir and not -d $dir
!ll&&!rl&&r
121590
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
ABresult
0X0
100
111
38 0 print "converting $dir to dir\n";
39 0 ▶ unlink $dir or die "Can't unlink $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $dir or die "Can't unlink ${dir}: $!" 0 0 0
40 }
41 180 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 121 59 180
42 121 ▶ mkdir $dir or die "Can't create $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or die "Can't create ${dir}: $!" 0 121 121
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
BranchTrueFalseTotal
if ($content =~ /\A pid \s+ (\d+) \n guid \s+ (.+) (?: \n daemon \s+ (\d+) )? \Z/x) { } 447 0 447
elsif ($content eq '') { } 0 0 0
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
BranchTrueFalseTotal
if (-d $dir) { } 447 0 447
72 # pidfile as dir
73 447 my $open_success = open my $fh, '<', "$dir/pid";
74 447 ▶ unless ($open_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($open_success) 0 447 447
75 0 ▶ if ($!{ENOENT}) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if ($!{'ENOENT'}) { } 0 0 0
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
BranchTrueFalseTotal
if (-f $dir and not -s $dir) 0 0 0
Condition0%0/3
-f $dir and not -s $dir
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
open my $fh, "<", $dir or die "Failed to open ${dir}: $!" 0 0 0
91 0 $content = join '', <$fh>;
92 0 ▶ if ($content =~ /\A (\d+) \Z/x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\A (\d+) \Z/x) { } 0 0 0
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
BranchTrueFalseTotal
if (-d $dir) { } 398 0 398
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
BranchTrueFalseTotal
if (-d $dir) { } 34 0 34
120 34 ▶ if (-e "$dir/pid") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$dir/pid") 5 29 34
121 5 ▶ unlink "$dir/pid" or die "Can't remove $dir/pid: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink "$dir/pid" or die "Can't remove $dir/pid: $!" 0 5 5
122 }
123 }
124 else {
125 0 ▶ unlink $dir or die "Can't remove $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $dir or die "Can't remove ${dir}: $!" 0 0 0
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
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
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__
« blib/lib/Ubic/Cmd/Results.pm ↑ Summary blib/lib/Ubic/Daemon/OS/Linux.pm »