blib/lib/Ubic/Daemon/PidState.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 22 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 152 SCAR 56.1
CC
16
Cov
0%
read
72.0
is_empty
12.0
init
12.0
CRAP 272.0
1 package Ubic::Daemon::PidState;
2 $Ubic::Daemon::PidState::VERSION = '1.59';
3 use strict;
4 use warnings;
5  
6 # ABSTRACT: internal object representing process info stored on disk
7  
8  
9 use Params::Validate qw(:all);
10 use Ubic::Lockf;
11 use Ubic::AtomicFile;
12  
13 use overload '""' => sub {
14 my $self = shift;
15 return $self->{dir};
16 };
17  
18 sub new {
19 my $class = shift;
20 my ($dir) = validate_pos(@_, { type => SCALAR });
21 return bless { dir => $dir } => $class;
22 }
23  
24 sub is_empty {
25 my ($self) = validate_pos(@_, 1);
26 my $dir = $self->{dir};
27  
28 return if not -d $dir and -s $dir; # old-style pidfile
29 return if -d $dir and -s "$dir/pid"; # non-empty new-style pidfile
30  
31 return 1;
32 }
33  
34 sub init {
35 my ($self) = validate_pos(@_, 1);
36 my $dir = $self->{dir};
37 if (-e $dir and not -d $dir) {
38 print "converting $dir to dir\n";
39 unlink $dir or die "Can't unlink $dir: $!";
40 }
41 unless (-d $dir) {
42 mkdir $dir or die "Can't create $dir: $!";
43 }
44  
45 }
46  
47 sub read {
48 my ($self) = validate_pos(@_, 1);
49  
50 my $dir = $self->{dir};
51  
52 my $content;
53 my $parse_content = sub {
54 if ($content =~ /\A pid \s+ (\d+) \n guid \s+ (.+) (?: \n daemon \s+ (\d+) )? \Z/x) {
55 # new format
56 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 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 die "invalid pidfile content in pidfile $dir";
69 }
70 };
71 if (-d $dir) {
72 # pidfile as dir
73 my $open_success = open my $fh, '<', "$dir/pid";
74 unless ($open_success) {
75 if ($!{ENOENT}) {
76 return; # pidfile not found, daemon is not running
77 }
78 else {
79 die "Failed to open '$dir/pid': $!";
80 }
81 }
82 $content = join '', <$fh>;
83 return $parse_content->();
84 }
85 else {
86 # deprecated - single pidfile without dir
87 if (-f $dir and not -s $dir) {
88 return; # empty pidfile - old way to stop services
89 }
90 open my $fh, '<', $dir or die "Failed to open $dir: $!";
91 $content = join '', <$fh>;
92 if ($content =~ /\A (\d+) \Z/x) {
93 # old format
94 return { pid => $1, format => 'old' };
95 }
96 else {
97 return $parse_content->();
98 }
99 }
100 }
101  
102 sub lock {
103 my ($self, $timeout) = validate_pos(@_, 1, { type => SCALAR, default => 0 });
104  
105 my $dir = $self->{dir};
106 if (-d $dir) {
107 # new-style pidfile
108 return lockf("$dir/lock", { timeout => $timeout });
109 }
110 else {
111 return lockf($dir, { blocking => 0 });
112 }
113 }
114  
115 sub remove {
116 my ($self) = validate_pos(@_, 1);
117 my $dir = $self->{dir};
118  
119 if (-d $dir) {
120 if (-e "$dir/pid") {
121 unlink "$dir/pid" or die "Can't remove $dir/pid: $!";
122 }
123 }
124 else {
125 unlink $dir or die "Can't remove $dir: $!";
126 }
127 }
128  
129 sub write {
130 my $self = shift;
131 my $dir = $self->{dir};
132 unless (-d $dir) {
133 die "piddir $dir not initialized";
134 }
135 my $params = validate(@_, {
136 pid => 1,
137 guid => 1,
138 });
139  
140 my ($pid, $guid) = @$params{qw/ pid guid /};
141 my $self_pid = $$;
142  
143 my $content =
144 "pid $self_pid\n".
145 "guid $guid\n".
146 "daemon $pid\n";
147 Ubic::AtomicFile::store( $content => "$dir/pid" );
148 }
149  
150  
151 1;
152  
153 __END__