blib/lib/File/Pid.pm

filter: Statementstmt 100% 54 / 54 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 95.4% 83 / 87 CC 7 SCAR 5.5
CC
7
Cov
97%
_get_pid
3.0
write
2.0
running
2.0
CRAP 1.7
↑ Summary
1 package File::Pid;
2 # $Id: Pid.pm,v 1.1 2005/01/11 13:09:54 cwest Exp $
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 File::Pid - Pid File Manipulation
8  
9 =head1 SYNOPSIS
10  
11 use File::Pid;
12
13 my $pidfile = File::Pid->new({
14 file => '/some/file.pid',
15 });
16
17 $pidfile->write;
18
19 if ( my $num = $pidfile->running ) {
20 die "Already running: $num\n";
21 }
22  
23 $pidfile->remove;
24  
25 =cut
26  
27 2 ▶use vars qw[$VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 $VERSION = sprintf "%d.%02d", split m/\./, (qw$Revision: 1.1 $)[1];
29  
30 2 ▶use File::Spec::Functions qw[tmpdir catfile];
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use File::Basename qw[basename];
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use base qw[Class::Accessor::Fast];
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 =head1 DESCRIPTION
35  
36 This software manages a pid file for you. It will create a pid file,
37 query the process within to discover if it's still running, and remove
38 the pid file.
39  
40 =head2 new
41  
42 my $pidfile = File::Pid->new;
43  
44 my $thisfile = File::Pid->new({
45 file => '/var/run/daemon.pid',
46 });
47  
48 my $thisfileandpid = File::Pid->new({
49 file => '/var/run/daemon.pid',
50 pid => '145',
51 });
52  
53 This constructor takes two optional paramters.
54  
55 C<file> - The name of the pid file to work on. If not specified, a pid
56 file located in C<< File::Spec->tmpdir() >> will be created that matches
57 C<< (File::Basename::basename($0))[0] . '.pid' >>. So, for example, if
58 C<$0> is F<~/bin/sig.pl>, the pid file will be F</tmp/sig.pl.pid>.
59  
60 C<pid> - The pid to write to a new pidfile. If not specified, C<$$> is
61 used when the pid file doesn't exist. When the pid file does exist, the
62 pid inside it is used.
63  
64 =head2 file
65  
66 my $pidfile = $pidfile->file;
67  
68 Accessor/mutator for the filename used as the pid file.
69  
70 =head2 pid
71  
72 my $pid = $pidfile->pid;
73  
74 Accessor/mutator for the pid being saved to the pid file.
75  
76 =cut
77  
78 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
79 4 my $class = shift;
80 4 my $self = $class->SUPER::new(@_);
81 4 $self->_get_pidfile;
82 4 $self->_get_pid;
83 4 return $self;
84 }
85 2 __PACKAGE__->mk_accessors(qw[file pid]);
86  
87 =head2 write
88  
89 my $pid = $pidfile->write;
90  
91 Writes the pid file to disk, inserting the pid inside the file.
92 On success, the pid written is returned. On failure, C<undef> is
93 returned.
94  
95 =cut
96  
97 3 ▶sub write {
Subroutine write: called CC=2 SCAR=7.0
Pod documented
98 3 my $self = shift;
99 3 my $file = $self->_get_pidfile;
100 3 my $pid = $self->_get_pid;
101  
102 3 local *WRITEPID;
103 3 ▶ open WRITEPID, "> $file" or return;
Branch: 1 branch
BranchTrueFalseTotal
open WRITEPID, "> $file" or return 3 0 3
104 3 print WRITEPID "$pid\n";
105 3 close WRITEPID;
106 3 return $pid;
107 }
108  
109 =head2 running
110  
111 my $pid = $pidfile->running;
112 die "Service already running: $pid\n" if $pid;
113  
114 Checks to see if the pricess identified in the pid file is still
115 running. If the process is still running, the pid is returned. Otherwise
116 C<undef> is returned.
117  
118 =cut
119  
120 3 ▶sub running {
Subroutine running: called CC=2 SCAR=6.9
Pod documented
121 3 my $self = shift;
122 3 my $pid = $self->_get_pid_from_file;
123  
124 3 ▶ return kill(0, $pid)
Branch: 1 branch
BranchTrueFalseTotal
kill(0, $pid) ? : 2 1 3
125 ? $pid
126 : undef;
127 }
128  
129 =head2 remove
130  
131 $pidfile->remove or warn "Couldn't unlink pid file\n";
132  
133 Removes the pid file from disk. Returns true on success, false on
134 failure.
135  
136 =cut
137  
138 3 ▶sub remove { unlink shift->_get_pidfile }
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
139  
140 =head2 program_name
141  
142 This is a utility method that allows you to determine what
143 C<File::Pid> thinks the program name is. Internally this is used
144 when no pid file is specified.
145  
146 =cut
147  
148 4 ▶sub program_name {
Subroutine program_name: called CC=1 SCAR=0.0
Pod documented
149 4 my $self = shift;
150 4 my ($name) = basename($0);
151 4 return $name;
152 }
153  
154 16 ▶sub _get_pidfile {
Subroutine _get_pidfile: called CC=2 SCAR=6.9
155 16 my $self = shift;
156 16 ▶ return $self->file if $self->file;
Branch: 1 branch
BranchTrueFalseTotal
if $self->file 14 2 16
157  
158 2 my $file = catfile tmpdir, $self->program_name . '.pid';
159 2 $self->file($file);
160 2 return $self->file;
161 }
162  
163 7 ▶sub _get_pid {
Subroutine _get_pid: called CC=3 SCAR=11.0
164 7 my $self = shift;
165 7 ▶ return $self->pid if $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pid 4 3 7
166 3 ▶ $self->pid($self->_get_pid_from_file || $$);
Condition66%2/3
$self->_get_pid_from_file || $$
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->_get_pid_from_file || $$
$self->_get_pid_from_file $$
Truth table66%2/3
A || B
A= $self->_get_pid_from_fileB= $$
ABresult
1X1
011
000
167 3 return $self->pid;
168 }
169  
170 6 ▶sub _get_pid_from_file {
Subroutine _get_pid_from_file: called CC=2 SCAR=6.9
171 6 my $self = shift;
172 6 my $file = $self->_get_pidfile;
173 6 local *READPID;
174 6 ▶ open READPID, "< $file" or return;
Branch: 1 branch
BranchTrueFalseTotal
open READPID, "< $file" or return 4 2 6
175 4 chomp(my $pid = <READPID>);
176 4 close READPID;
177 4 return $pid;
178 }
179  
180 2 1;
181  
182 __END__
↑ Summary