/usr/share/perl5/INN/Utils/Shlock.pm is in inn2 2.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | #! /usr/bin/perl -w
use lib '/usr/share/perl5'; use INN::Config;
## $Id: Shlock.pm.in 9408 2012-05-28 18:42:29Z iulius $
##
## Perl module for wrapping the shlock program shipped with INN.
##
package INN::Utils::Shlock;
use strict;
use warnings;
use Exporter;
our @ISA = qw(Exporter);
our $VERSION = "$INN::Config::VERSION";
my %lockfiles;
##
## Returns true if the file is properly locked.
##
sub lock {
my $lockfile = shift;
my $lockretrymax = shift;
my $lockwait = shift;
my $locktry = 0;
$lockretrymax = 1 if not defined $lockretrymax;
$lockwait = 2 if not defined $lockwait;
while ($locktry < $lockretrymax) {
if (system("$INN::Config::newsbin/shlock", '-p', $$, '-f', $lockfile) == 0) {
$lockfiles{$lockfile} = 1;
return 1;
}
$locktry++;
sleep $lockwait;
}
# Failed to lock.
return 0;
}
##
## Returns true if the file is properly unlocked.
##
sub unlock {
my $lockfile = shift;
if (unlink $lockfile) {
delete $lockfiles{$lockfile};
return 1;
} else {
return 0;
}
}
##
## Attempts to unlock any leftover locks.
## Returns the number of removed locks.
##
sub releaselocks {
my $key;
my $count = 0;
foreach $key (keys(%lockfiles)) {
$count += unlock($key);
}
undef(%lockfiles);
return $count;
}
## This array will contain what it is possible to export.
our @EXPORT_OK = qw(lock unlock releaselocks);
## That's all.
1;
__END__
=head1 NAME
INN::Utils::Shlock - Wrapper around the shlock program
=head1 DESCRIPTION
This Perl module wraps the shlock(1) program so that it can easily be used.
Calling B<shlock> is more portable than using flock(2) and its corresponding
Perl function because this function does not work as expected on all
existing systems.
See the shlock(1) documentation for more information.
Using INN::Utils::Shlock is straight-forward:
use lib '<pathnews>/lib/perl';
use INN::Utils::Shlock;
my $lockfile = "myprogram.LOCK";
# Acquire a lock.
INN::Utils::Shlock::lock($lockfile);
# Do whatever you want. The lock prevents concurrent accesses.
# Unlock.
INN::Utils::Shlock::unlock($lockfile);
These two functions return C<1> on success, C<0> on failure. For example,
the success of (un)locking can be checked as:
INN::Utils::Shlock::lock($lockfile) or die "cannot create lock file";
or:
if (! INN::Utils::Shlock::lock($lockfile, 4)) {
die "giving up after 4 unsuccessful attempts to create lock file";
}
Instead of calling C<< unlock(I<lockfile>) >>, the C<releaselocks()>
function can be called. It removes any leftover locks, which is useful
when several different locks are used. Another possible use is to call
it in an END code block:
END {
# In case we bail out, while holding a lock.
INN::Utils::Shlock::releaselocks();
}
=head1 INTERFACE
=over 4
=item lock(I<lockfile>)
Tries to create a lock file named I<lockfile>.
This function returns C<1> on success, C<0> on failure.
=item lock(I<lockfile>, I<tries>)
Tries to create a lock file named I<lockfile>. If it fails, locking
attempts are repeated once every 2 seconds for at most I<tries> times
(including the first unsuccessful attempt).
This function returns C<1> on success, C<0> on failure.
=item lock(I<lockfile>, I<tries>, I<delay>)
Tries to create a lock file named F<lockfile>. If it fails, locking
attempts are repeated once every I<delay> seconds for at most I<tries>
times (including the first unsuccessful attempt).
Note that C<< lock(I<lockfile>) >> is equivalent to C<< lock(I<lockfile>,
1, 2) >>.
This function returns C<1> on success, C<0> on failure.
=item releaselocks()
Removes all the lock files previously created by calling the C<<
lock(I<lockfile>) >> function.
This function returns the number of removed lock files.
=item unlock(I<lockfile>)
Removes the file named I<lockfile>.
This function returns C<1> on success, C<0> on failure.
=back
=head1 HISTORY
Documentation written by Julien Elie for InterNetNews.
$Id: Shlock.pm.in 9408 2012-05-28 18:42:29Z iulius $
=head1 SEE ALSO
perl(1), shlock(1).
=cut
|