| 1 | # -*- Perl -*- | ||||||||||||||||||||||||||||
| 2 | # Proc::PID::File::Fcntl - pidfile manager | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Proc::PID::File::Fcntl; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Proc::PID::File::Fcntl - Manage PID files using fcntl() locks | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | use Proc::PID::File::Fcntl; | ||||||||||||||||||||||||||||
| 14 | my $pidfile = new Proc::PID::File::Fcntl; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | This Perl module permits callers to prevent multiple simultaneous | ||||||||||||||||||||||||||||
| 19 | instantiations of themselves. The module accomplishes this using a | ||||||||||||||||||||||||||||
| 20 | I<pidfile>, which is used to obtain a lock. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Unlike the traditional I<pidfile> locking protocol, which uses the | ||||||||||||||||||||||||||||
| 23 | existence and content of the file to indicate the lock, this module | ||||||||||||||||||||||||||||
| 24 | uses fcntl() locking of the file. This locking protocol is free of | ||||||||||||||||||||||||||||
| 25 | races and, assuming the lock file is not in a networked filesystem, is | ||||||||||||||||||||||||||||
| 26 | safe across OS crashes. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 METHODS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use POSIX qw(F_SETLK F_GETLK O_RDWR O_CREAT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | our $VERSION = "1.01"; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | our $_flock_init = "\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; | |||||||||||||||||||||||||||
| 39 | 1 | our $_flock_pid_template = "x24L"; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 $pidfile = Proc::PID::File::Fcntl->new(opt => val, ... ) | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Creates and locks a new I<pidfile>, returning an object holding the | ||||||||||||||||||||||||||||
| 44 | lock. When the returned object goes out of scope, the lock is | ||||||||||||||||||||||||||||
| 45 | released. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Calls C<die> if the file cannot be opened or is locked by another process. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Must not be called when the calling process already has a lock on the | ||||||||||||||||||||||||||||
| 50 | I<pidfile>, otherwise that lock will be released. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | The options available include the following: | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =over | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item I<path> | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Specifies the path name of the I<pidfile>. Defaults to I<$0>. If the | ||||||||||||||||||||||||||||
| 59 | value does not start with F</> then F</var/run/> is prepended. If the | ||||||||||||||||||||||||||||
| 60 | value does not end with F<.pid> then F<.pid> is appended. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =back | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub new ($) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 1 | my $self = bless({}, shift); | |||||||||||||||||||||||||||
| 68 | 1 | %$self = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | $self->{path} = $0 unless defined($self->{path}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 1 | ▶ | $self->{path} = "/var/run/".$self->{path} unless $self->{path} =~ m|^/|; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | $self->{path} .= '.pid' unless $self->{path} =~ /\.pid$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | my $fd; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | do { | ||||||||||||||||||||||||||||
| 77 | 1 | ▶ | sysopen($fd, $self->{path}, O_RDWR|O_CREAT) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | || die "Cannot open pid file $self->{path}: $!\n"; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | my $flock = $_flock_init; | |||||||||||||||||||||||||||
| 81 | 1 | ▶ | fcntl($fd, F_SETLK, $flock) || die "pidfile ".$self->{path}." already locked\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | } while ((! -f $self->{path}) || (stat _)[1] != (stat $fd)[1] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not -f $self->{"path"} or (stat _)[1] != (stat $fd)[1]
MC/DC0%0/2
not -f $self->{"path"} or (stat _)[1] != (stat $fd)[1]
not -f $self->{"path"}
(stat _)[1] != (stat $fd)[1]
Truth table33%1/3
not(A) or B
A= not -f $self->{"path"}B= (stat _)[1] != (stat $fd)[1]
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | my $data = "$$\n"; | |||||||||||||||||||||||||||
| 85 | 1 | syswrite $fd, $data, length($data); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | $self->{fd} = $fd; | |||||||||||||||||||||||||||
| 88 | 1 | $self->{pid} = $$; | |||||||||||||||||||||||||||
| 89 | 1 | return $self; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 $pid = Proc::PID::File::Fcntl->getlocker(opt => val, ... ) | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Returns the PID of the process holding the lock on a I<pidfile> or | ||||||||||||||||||||||||||||
| 95 | C<0> if there is no such process. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Must not be called when the calling process already has a lock on the | ||||||||||||||||||||||||||||
| 98 | I<pidfile>, otherwise that lock will be released. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | The options available include the following: | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =over | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =item I<path> | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Specifies the path name of the I<pidfile>. Defaults to I<$0>. If the | ||||||||||||||||||||||||||||
| 107 | value does not start with F</> then F</var/run/> is prepended. If the | ||||||||||||||||||||||||||||
| 108 | value does not end with F<.pid> then F<.pid> is appended. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =back | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub getlocker { | ||||||||||||||||||||||||||
|
Subroutine
getlocker: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | die "Must be called as class method" if ref($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | my %opts = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $opts{path} = $0 unless defined($opts{path}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | $opts{path} = "/var/run/".$opts{path} unless $opts{path} =~ m|^/|; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | $opts{path} .= '.pid' unless $opts{path} =~ /\.pid$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sysopen(my $fd, $opts{path}, O_RDWR) || return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | my $flock = $_flock_init; | |||||||||||||||||||||||||||
| 127 | 0 | fcntl($fd, F_GETLK, $flock); | |||||||||||||||||||||||||||
| 128 | 0 | return unpack $_flock_pid_template, $flock; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 132 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 133 | 1 | ▶ | if ($self->{fd} && ($$ == $self->{pid})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"fd"} and $$ == $self->{"pid"}
MC/DC0%0/2
$self->{"fd"} and $$ == $self->{"pid"}
$self->{"fd"}
$$ == $self->{"pid"}
Truth table33%1/3
A and B
A= $self->{"fd"}B= $$ == $self->{"pid"}
| |||||||||||||||||||||||||||||
| 134 | # Must unlink BEFORE releasing lock, otherwise we might unlink | ||||||||||||||||||||||||||||
| 135 | # some other process's file | ||||||||||||||||||||||||||||
| 136 | 1 | unlink $self->{path}; | |||||||||||||||||||||||||||
| 137 | 1 | delete $self->{fd}; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | John Gardiner Myers <jgmyers@proofpoint.com> | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | For help and thank you notes, e-mail the author directly. To report a | ||||||||||||||||||||||||||||
| 148 | bug, submit a patch, or add to the wishlist please visit the CPAN bug | ||||||||||||||||||||||||||||
| 149 | manager at: F<http://rt.cpan.org> | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Copyright (C) 2005, 2006 Proofpoint, Inc. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 156 | the same terms as Perl itself | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 1 | 1; | |||||||||||||||||||||||||||