INN commit: trunk (17 files)
INN Commit
rra at isc.org
Thu Aug 4 22:09:58 UTC 2011
Date: Thursday, August 4, 2011 @ 15:09:57
Author: iulius
Revision: 9303
add an INN::Utils::Shlock module to wrap shlock in Perl scripts
Calling shlock (a program shipped with INN) is more portable than using
flock(2) and its corresponding Perl function because this function does
not work as expected on all existing systems (for instance on Solaris).
This patch:
* contains POD documentation for INN::Utils::Shlock;
* adds support for INN::Utils::Shlock instead of flock(2) in pullnews
and mailpost.
* uses INN::Utils::Shlock instead of a subfunction calling shlock(1)
in controlchan, thdexpire and send-uucp.
Thanks to Dennis Davis for the bug report.
Added:
trunk/perl/INN/Utils/
trunk/perl/INN/Utils/Shlock.pm.in
Modified:
trunk/CONTRIBUTORS
trunk/MANIFEST
trunk/backends/send-uucp.in
trunk/contrib/thdexpire.in
trunk/control/controlchan.in
trunk/control/modules/newgroup.pl
trunk/control/modules/rmgroup.pl
trunk/doc/man/ (properties)
trunk/doc/man/Makefile
trunk/doc/pod/news.pod
trunk/doc/pod/shlock.pod
trunk/frontends/mailpost.in
trunk/frontends/pullnews.in
trunk/perl/Makefile
trunk/support/mkmanifest
-----------------------------+
CONTRIBUTORS | 2
MANIFEST | 3
backends/send-uucp.in | 36 +++-------
contrib/thdexpire.in | 13 +++
control/controlchan.in | 33 ++-------
control/modules/newgroup.pl | 11 ++-
control/modules/rmgroup.pl | 10 ++-
doc/man/Makefile | 4 -
doc/pod/news.pod | 9 ++
doc/pod/shlock.pod | 7 ++
frontends/mailpost.in | 41 +++---------
frontends/pullnews.in | 21 +-----
perl/INN/Utils/Shlock.pm.in | 140 ++++++++++++++++++++++++++++++++++++++++++
perl/Makefile | 16 +++-
support/mkmanifest | 1
15 files changed, 237 insertions(+), 110 deletions(-)
Modified: CONTRIBUTORS
===================================================================
--- CONTRIBUTORS 2011-08-04 21:10:17 UTC (rev 9302)
+++ CONTRIBUTORS 2011-08-04 22:09:57 UTC (rev 9303)
@@ -274,4 +274,4 @@
S.P. Zeidler, Nix, Florian Schlichting, Torsten Jerzembeck, Harald Dunkel,
Lars Magne Ingebrigtsen, Sam Varshavchik, Matthew Vernon, Ian Jackson,
Edmund H. Ramm, Raphael Barrois, Bo Lindbergh, Matthias Meyser,
-Dennis Preiser, Paolo Amoroso
+Dennis Preiser, Paolo Amoroso, Dennis Davis
Modified: MANIFEST
===================================================================
--- MANIFEST 2011-08-04 21:10:17 UTC (rev 9302)
+++ MANIFEST 2011-08-04 22:09:57 UTC (rev 9303)
@@ -115,6 +115,7 @@
doc/hook-python Python hook notes
doc/man nroff documentation (Directory)
doc/man/INN__Config.3pm Manpage for INN::Config Perl module
+doc/man/INN__Utils__Shlock.3pm Manpage for INN::Utils::Shlock Perl module
doc/man/Makefile Makefile for nroff documentation
doc/man/active.5 Manpage for active database
doc/man/active.times.5 Manpage for active.times file
@@ -605,6 +606,8 @@
perl Perl libraries (Directory)
perl/INN INN Perl modules (Directory)
perl/INN/Config.pm.in INN::Config module
+perl/INN/Utils INN::Utils Perl modules (Directory)
+perl/INN/Utils/Shlock.pm.in INN::Utils::Shlock module
perl/Makefile Makefile for perl libraries
samples Prototype INN config files (Directory)
samples/INN.py Stub Python functions
Modified: backends/send-uucp.in
===================================================================
--- backends/send-uucp.in 2011-08-04 21:10:17 UTC (rev 9302)
+++ backends/send-uucp.in 2011-08-04 22:09:57 UTC (rev 9303)
@@ -19,6 +19,7 @@
##############################################################################
use strict;
+use INN::Utils::Shlock;
# for compatibility with earlier versions of INN
$INN::Config::pathetc ||= '/etc/news';
@@ -75,10 +76,14 @@
chdir $INN::Config::batch or logdie("Can't access $INN::Config::batch: $!", 'crit');
-shlock($lockfile);
+# Acquire a lock.
+INN::Utils::Shlock::lock($lockfile, 60) or logdie("cannot create lockfile $lockfile");
run_site($_) foreach @sitelist;
-unlink $lockfile;
+
+# Unlock.
+INN::Utils::Shlock::unlock($lockfile) or mailArtAndDie ("cannot unlock $lockfile");
+
exit 0;
##############################################################################
@@ -224,7 +229,9 @@
my ($msg, $lvl) = @_;
logmsg($msg, $lvl || 'err');
- unlink $lockfile;
+
+ # Unlock.
+ INN::Utils::Shlock::unlock($lockfile);
exit 1;
}
@@ -236,29 +243,6 @@
logdie('ctlinnd returned status ' . ($st & 255)) if $st > 0;
}
-sub shlock {
- my $lockfile = shift;
-
- my $locktry = 0;
- while ($locktry < 60) {
- if (system("$INN::Config::newsbin/shlock", '-p', $$, '-f', $lockfile) == 0) {
- return 1;
- }
- $locktry++;
- sleep 2;
- }
-
- my $lockreason;
- if (open(LOCKFILE, $lockfile)) {
- $lockreason = 'held by ' . (<LOCKFILE> || '?');
- close LOCKFILE;
- } else {
- $lockreason = $!;
- }
- logdie("Cannot get lock $lockfile: $lockreason");
- return undef;
-}
-
__END__
=head1 NAME
Modified: contrib/thdexpire.in
===================================================================
--- contrib/thdexpire.in 2011-08-04 21:10:17 UTC (rev 9302)
+++ contrib/thdexpire.in 2011-08-04 22:09:57 UTC (rev 9303)
@@ -6,6 +6,7 @@
use POSIX ":fcntl_h";
use SDBM_File;
use Getopt::Std;
+use INN::Utils::Shlock;
# With the -M switch this program installs its own man page.
#-----------------------------------------------------------------------------
@@ -245,6 +246,8 @@
#-----------------------------------------------------------------------------
+my $lockfile = "$INN::Config::innddir/thdexpire.pid";
+
chdir $INN::Config::spool || die "chdir $INN::Config::spool: $!";
$opt_r=0; # make a report
$opt_t=30; # check interval in minutes
@@ -275,8 +278,9 @@
exit 0;
}
-(system "$INN::Config::newsbin/shlock", "-p", $$, "-f", "$INN::Config::innddir/thdexpire.pid")>>8==0
- || die "Already running";
+# Acquire a lock.
+INN::Utils::Shlock::lock($lockfile, 5) or die ("cannot create lockfile $lockfile");
+
tie(%S, SDBM_File, $sfile, O_RDWR|O_CREAT, 0664) || die "open $sfile: $!";
$SIG{'TERM'}=$SIG{'INT'}='finish';
$|=1;
@@ -641,7 +645,10 @@
sub finish
{
untie(%S);
- unlink "$INN::Config::innddir/thdexpire.pid";
+
+ # Unlock.
+ INN::Utils::Shlock::unlock($lockfile);
+
exit 0;
}
#-----------------------------------------------------------------------------
Modified: control/controlchan.in
===================================================================
--- control/controlchan.in 2011-08-04 21:10:17 UTC (rev 9302)
+++ control/controlchan.in 2011-08-04 22:09:57 UTC (rev 9303)
@@ -33,6 +33,7 @@
use Encode;
use Getopt::Std;
use MIME::Parser;
+use INN::Utils::Shlock;
use strict;
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
@@ -489,7 +490,8 @@
my $lockfile = $logfile;
$lockfile =~ s#.*/##;
$lockfile = "$INN::Config::locks/LOCK.$lockfile";
- shlock($lockfile);
+ # Acquire a lock.
+ INN::Utils::Shlock::lock($lockfile, 60) or logdie("Cannot create lockfile $lockfile");
open(LOGFILE, ">>$logfile") or logdie("Cannot open $logfile: $!");
print LOGFILE "$message\n";
@@ -497,7 +499,9 @@
print LOGFILE " $_\n";
}
close LOGFILE;
- unlink $lockfile;
+
+ # Unlock.
+ INN::Utils::Shlock::unlock($lockfile);
}
# write to syslog or errlog
@@ -591,29 +595,6 @@
return $result;
}
-sub shlock {
- my $lockfile = shift;
-
- my $locktry = 0;
- while ($locktry < 60) {
- if (system("$INN::Config::newsbin/shlock", '-p', $$, '-f', $lockfile) == 0) {
- return 1;
- }
- $locktry++;
- sleep 2;
- }
-
- my $lockreason;
- if (open(LOCKFILE, $lockfile)) {
- $lockreason = 'held by ' . (<LOCKFILE> || '?');
- close LOCKFILE;
- } else {
- $lockreason = $!;
- }
- logdie("Cannot get lock $lockfile: $lockreason");
- return undef;
-}
-
# If $body is not defined, returns a file handle which must be closed.
# Don't forget checking the return value of close().
# $addresses may be a scalar or a reference to a list of addresses.
@@ -634,7 +615,7 @@
$sm .= ' ' . join(' ', @addrs);
}
- # fork and spawn the MTA whitout using the shell
+ # fork and spawn the MTA without using the shell
my $pid = open(MTA, '|-');
logdie('Cannot fork: ' . $!) if $pid < 0;
if ($pid == 0) {
Modified: control/modules/newgroup.pl
===================================================================
--- control/modules/newgroup.pl 2011-08-04 21:10:17 UTC (rev 9302)
+++ control/modules/newgroup.pl 2011-08-04 22:09:57 UTC (rev 9303)
@@ -216,7 +216,11 @@
sub update_desc {
my ($name, $desc) = @_;
- shlock("$INN::Config::locks/LOCK.newsgroups");
+ my $lockfile = "$INN::Config::locks/LOCK.newsgroups";
+
+ # Acquire a lock.
+ INN::Utils::Shlock::lock($lockfile, 60) or logdie("Cannot create lockfile $lockfile");
+
my $tempfile = "$INN::Config::newsgroups.$$";
open(NEWSGROUPS, $INN::Config::newsgroups)
or logdie("Cannot open $INN::Config::newsgroups: $!");
@@ -237,7 +241,10 @@
close NEWSGROUPS;
rename($tempfile, $INN::Config::newsgroups)
or logdie("Cannot rename $tempfile: $!");
- unlink("$INN::Config::locks/LOCK.newsgroups", $tempfile);
+ unlink($tempfile);
+
+ # Unlock.
+ INN::Utils::Shlock::unlock($lockfile);
}
# Check the group name. This is partially derived from C News.
Modified: control/modules/rmgroup.pl
===================================================================
--- control/modules/rmgroup.pl 2011-08-04 21:10:17 UTC (rev 9302)
+++ control/modules/rmgroup.pl 2011-08-04 22:09:57 UTC (rev 9303)
@@ -73,7 +73,11 @@
} elsif ($action eq 'doit' and $status !~ /(not change|be unapproved)/) {
ctlinnd('rmgroup', $groupname);
# Update newsgroups too.
- shlock("$INN::Config::locks/LOCK.newsgroups");
+ my $lockfile = "$INN::Config::locks/LOCK.newsgroups";
+
+ # Acquire a lock.
+ INN::Utils::Shlock::lock($lockfile, 60) or logdie("Cannot create lockfile $lockfile");
+
open(NEWSGROUPS, $INN::Config::newsgroups)
or logdie("Cannot open $INN::Config::newsgroups: $!");
my $tempfile = "$INN::Config::newsgroups.$$";
@@ -85,9 +89,11 @@
close NEWSGROUPS;
rename($tempfile, $INN::Config::newsgroups)
or logdie("Cannot rename $tempfile: $!");
- unlink "$INN::Config::locks/LOCK.newsgroups";
unlink $tempfile;
+ # Unlock.
+ INN::Utils::Shlock::unlock($lockfile);
+
logger($log, "rmgroup $groupname $status $sender", $article)
if $log;
}
Property changes on: trunk/doc/man
___________________________________________________________________
Modified: svn:ignore
- active.5
active.times.5
actsync.8
archive.8
auth_krb5.8
batcher.8
buffchan.8
buffindexed.conf.5
ckpasswd.8
cnfsheadconf.8
cnfsstat.8
control.ctl.5
controlchan.8
convdate.1
ctlinnd.8
cvtbatch.8
cycbuff.conf.5
distrib.pats.5
distributions.5
docheckgroups.8
domain.8
expire.ctl.5
expire.8
expireover.8
expirerm.8
fastrm.1
getlist.1
grephistory.1
ident.8
incoming.conf.5
inews.1
inn.conf.5
INN__Config.3pm
innbind.8
inncheck.8
innconfval.1
innd.8
inndf.8
innfeed.conf.5
innfeed.8
innmail.1
innupgrade.8
innxmit.8
libauth.3
libinnhist.3
libstorage.3
list.3
mailpost.8
makedbz.8
makehistory.8
mod-active.8
moderators.5
motd.news.5
newsfeeds.5
news.daily.8
news2mail.8
newslog.5
newsgroups.5
ninpaths.8
nnrpd.8
nntpsend.8
nntpsend.ctl.5
ovdb.5
ovdb_init.8
ovdb_monitor.8
ovdb_server.8
ovdb_stat.8
overchan.8
passwd.nntp.5
perl-nocem.8
pgpverify.1
prunehistory.8
pullnews.1
qio.3
radius.8
radius.conf.5
rc.news.8
readers.conf.5
rnews.1
sasl.conf.5
scanlogs.8
scanspool.8
send-uucp.8
sendinpaths.8
shlock.1
simpleftp.1
sm.1
storage.conf.5
subscriptions.5
tally.control.8
tdx-util.8
tinyleaf.8
tst.3
uwildmat.3
+ active.5
active.times.5
actsync.8
archive.8
auth_krb5.8
batcher.8
buffchan.8
buffindexed.conf.5
ckpasswd.8
cnfsheadconf.8
cnfsstat.8
control.ctl.5
controlchan.8
convdate.1
ctlinnd.8
cvtbatch.8
cycbuff.conf.5
distrib.pats.5
distributions.5
docheckgroups.8
domain.8
expire.ctl.5
expire.8
expireover.8
expirerm.8
fastrm.1
getlist.1
grephistory.1
ident.8
incoming.conf.5
inews.1
inn.conf.5
INN__Config.3pm
INN__Utils__Shlock.3pm
innbind.8
inncheck.8
innconfval.1
innd.8
inndf.8
innfeed.conf.5
innfeed.8
innmail.1
innupgrade.8
innxmit.8
libauth.3
libinnhist.3
libstorage.3
list.3
mailpost.8
makedbz.8
makehistory.8
mod-active.8
moderators.5
motd.news.5
newsfeeds.5
news.daily.8
news2mail.8
newslog.5
newsgroups.5
ninpaths.8
nnrpd.8
nntpsend.8
nntpsend.ctl.5
ovdb.5
ovdb_init.8
ovdb_monitor.8
ovdb_server.8
ovdb_stat.8
overchan.8
passwd.nntp.5
perl-nocem.8
pgpverify.1
prunehistory.8
pullnews.1
qio.3
radius.8
radius.conf.5
rc.news.8
readers.conf.5
rnews.1
sasl.conf.5
scanlogs.8
scanspool.8
send-uucp.8
sendinpaths.8
shlock.1
simpleftp.1
sm.1
storage.conf.5
subscriptions.5
tally.control.8
tdx-util.8
tinyleaf.8
tst.3
uwildmat.3
Modified: doc/man/Makefile
===================================================================
--- doc/man/Makefile 2011-08-04 21:10:17 UTC (rev 9302)
+++ doc/man/Makefile 2011-08-04 22:09:57 UTC (rev 9303)
@@ -11,7 +11,8 @@
SEC3 = clientlib.3 dbz.3 inndcomm.3 libauth.3 libinn.3 libinnhist.3 \
libstorage.3 list.3 qio.3 tst.3 uwildmat.3
-SEC3PM = INN__Config.3pm
+SEC3PM = INN__Config.3pm \
+ INN__Utils__Shlock.3pm
SEC5 = active.5 active.times.5 buffindexed.conf.5 control.ctl.5 \
cycbuff.conf.5 distrib.pats.5 distributions.5 expire.ctl.5 history.5 incoming.conf.5 \
@@ -50,6 +51,7 @@
install-man3pm:
$(CP_MAN) INN__Config.3pm $D$(MAN3PM)/INN\:\:Config.$(MAN3PM_EXT)
+ $(CP_MAN) INN__Utils__Shlock.3pm $D$(MAN3PM)/INN\:\:Utils\:\:Shlock.$(MAN3PM_EXT)
# We also create symbolic links between config files and programs.
# We try to use relative symbolic links, when possible. As '-ef' is
Modified: doc/pod/news.pod
===================================================================
--- doc/pod/news.pod 2011-08-04 21:10:17 UTC (rev 9302)
+++ doc/pod/news.pod 2011-08-04 22:09:57 UTC (rev 9303)
@@ -215,6 +215,15 @@
=item *
+Fixed an issue on systems which do not have a working flock(2) function
+(Solaris, for instance). B<mailpost> and B<pullnews> are reported not to
+be usable on such systems. Many thanks to Dennis Davis for the bug report.
+
+A wrapper around B<shlock> is now called in Perl scripts. The
+INN::Utils::Shlock module has been added for that use.
+
+=item *
+
Fixed an issue in the Python access hook for B<nnrpd>: it has not been
working since S<Python 2.5> on 64-bit platforms, owing to a change to
Python's C API, using a new Py_ssize_t type definition instead of int.
Modified: doc/pod/shlock.pod
===================================================================
--- doc/pod/shlock.pod 2011-08-04 21:10:17 UTC (rev 9302)
+++ doc/pod/shlock.pod 2011-08-04 22:09:57 UTC (rev 9303)
@@ -16,6 +16,9 @@
create the lock file, or non-zero if the file refers to a currently
active process.
+A Perl wrapper around B<shlock> can be used via the C<INN::Utils::Shlock>
+module.
+
=head1 OPTIONS
=over 4
@@ -72,4 +75,8 @@
$Id$
+=head1 SEE ALSO
+
+INN::Utils::Shlock(3pm).
+
=cut
Modified: frontends/mailpost.in
===================================================================
--- frontends/mailpost.in 2011-08-04 21:10:17 UTC (rev 9302)
+++ frontends/mailpost.in 2011-08-04 22:09:57 UTC (rev 9303)
@@ -18,6 +18,7 @@
use IPC::Open3;
use IO::Select;
use POSIX qw(setsid);
+use INN::Utils::Shlock;
use strict;
my $debugging = 0 ;
@@ -30,11 +31,6 @@
unlink ($tmpfile2) if $tmpfile2 ; # in case we die()
}
-my $LOCK_SH = 1;
-my $LOCK_EX = 2;
-my $LOCK_NB = 4;
-my $LOCK_UN = 8;
-
my $usage = $0 ;
$usage =~ s!.*/!! ;
my $prog = $usage ;
@@ -377,22 +373,13 @@
$message_id = "${lhs}\@${rhs}";
-push @errorText, "(TAS Message-ID database for $message_id)\n";
+push @errorText, "(TAS message-ID database for $message_id)\n";
my $lockfile = sprintf("%s.lock", $Database);
-open(LOCKFILE, "<$lockfile") ||
- open(LOCKFILE, ">$lockfile") ||
- mailArtAndDie ("can't open $lockfile: $!") ;
+# Acquire a lock.
+INN::Utils::Shlock::lock($lockfile, 5) or mailArtAndDie ("cannot create lockfile $lockfile");
-my $i ;
-for ($i = 0 ; $i < 5 ; $i++) {
- flock(LOCKFILE, $LOCK_EX) && last ;
- sleep 1 ;
-}
-
-mailArtAndDie ("can't lock $lockfile: $!") if ($i == 5) ;
-
my %DATABASE ;
dbmopen(%DATABASE, $Database, 0666) || mailArtAndDie ("can't dbmopen $Database: $!");
@@ -416,8 +403,8 @@
dbmclose(%DATABASE) || mailArtAndDie ("can't dbmclose $Database: $!") ;
-flock(LOCKFILE, $LOCK_UN) || mailArtAndDie ("can't unlock $lockfile: $!");
-close LOCKFILE ;
+# Unlock.
+INN::Utils::Shlock::unlock($lockfile) or mailArtAndDie ("cannot unlock $lockfile");
## For crosspost.
@@ -442,17 +429,9 @@
sleep $opt_c ;
- open(LOCKFILE, "<$lockfile") ||
- open(LOCKFILE, ">$lockfile") ||
- mailArtAndDie ("can't open $lockfile: $!") ;
+ # Acquire a lock.
+ INN::Utils::Shlock::lock($lockfile, 5) or mailArtAndDie ("cannot create lockfile $lockfile");
- my $i ;
- for ($i = 0 ; $i < 5 ; $i++) {
- flock(LOCKFILE, $LOCK_EX) && last ;
- sleep 1 ;
- }
- mailArtAndDie ("can't lock $lockfile: $!") if ($i == 5) ;
-
my $umask_bak = umask();
umask(000);
dbmopen(%DATABASE, $Database, 0666) || mailArtAndDie ("can't dbmopen $Database: $!");
@@ -464,8 +443,8 @@
dbmclose(%DATABASE) || mailArtAndDie ("can't dbmclose $Database: $!") ;
- flock(LOCKFILE, $LOCK_UN) || mailArtAndDie ("can't unlock $lockfile: $!");
- close LOCKFILE ;
+ # Unlock.
+ INN::Utils::Shlock::unlock($lockfile) or mailArtAndDie ("cannot unlock $lockfile");
if (defined($dup)) {
syslog("err", "mismatch $newsgroups") if $debugging && $use_syslog;
Modified: frontends/pullnews.in
===================================================================
--- frontends/pullnews.in 2011-08-04 21:10:17 UTC (rev 9302)
+++ frontends/pullnews.in 2011-08-04 22:09:57 UTC (rev 9303)
@@ -68,8 +68,7 @@
use Getopt::Std;
use IO::Handle;
use POSIX qw(ceil floor);
-use Fcntl;
-use Fcntl qw(:flock);
+use INN::Utils::Shlock;
use strict;
my $usage = $0;
@@ -275,23 +274,15 @@
}
open(LOG, $logFile) || die "can't open logfile ($logFile)!: $!\n";
+# Forces a flush after each write or print.
my $oldfh = select;
$| = 1; select LOG; $| = 1; select $oldfh;
my $lockfile = $groupFile . '.pid';
-sysopen (LOCK, "$lockfile", O_RDWR | O_CREAT, 0700) ||
- die "can't create lock file ($lockfile): $!\n";
-$oldfh = select; select LOCK; $| = 1; select $oldfh;
-if (!flock (LOCK, LOCK_EX | LOCK_NB)) {
- seek LOCK, 0, 0;
- my $otherpid = <LOCK>;
- chomp $otherpid;
- die "Another pullnews (pid: $otherpid) seems to be running.\n";
-}
+# Acquire a lock.
+INN::Utils::Shlock::lock($lockfile) or die "cannot create lockfile $lockfile\n";
-print LOCK "$$\n";
-
print LOG scalar(localtime(time)), " start\n\n" unless $quiet;
if (@groupsToGet && ! $quiet) {
@@ -550,8 +541,8 @@
}
sub cleanLock {
- flock (LOCK, LOCK_UN);
- unlink $lockfile if defined $lockfile;
+ # Unlock.
+ INN::Utils::Shlock::unlock($lockfile) if defined $lockfile;
}
sub bail {
Property changes on: trunk/perl/INN/Utils
___________________________________________________________________
Added: svn:ignore
+ Shlock.pm
Added: perl/INN/Utils/Shlock.pm.in
===================================================================
--- perl/INN/Utils/Shlock.pm.in (rev 0)
+++ perl/INN/Utils/Shlock.pm.in 2011-08-04 22:09:57 UTC (rev 9303)
@@ -0,0 +1,140 @@
+#! /usr/bin/perl -w
+# fixscript will replace this line with code to load INN::Config
+
+## $Id$
+##
+## 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";
+
+
+##
+## 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) {
+ return 1;
+ }
+ $locktry++;
+ sleep $lockwait;
+ }
+
+ # Failed to lock.
+ return 0;
+}
+
+
+##
+## Returns true if the file is properly unlocked.
+##
+sub unlock {
+ my $lockfile = shift;
+ return unlink $lockfile;
+}
+
+
+## This array will contain what it is possible to export.
+our @EXPORT_OK = qw(lock unlock);
+
+
+## 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";
+ }
+
+=head1 INTERFACE
+
+=over 4
+
+=item lock(I<lockfile>)
+
+Tries to create a lock file named I<lockfile>.
+
+=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).
+
+=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) >>.
+
+=item unlock(I<lockfile>)
+
+Removes the file named I<lockfile>.
+
+=back
+
+=head1 HISTORY
+
+Documentation written by Julien Elie for InterNetNews.
+
+$Id$
+
+=head1 SEE ALSO
+
+perl(1), shlock(1).
+
+=cut
Property changes on: trunk/perl/INN/Utils/Shlock.pm.in
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision
Added: svn:eol-style
+ native
Modified: perl/Makefile
===================================================================
--- perl/Makefile 2011-08-04 21:10:17 UTC (rev 9302)
+++ perl/Makefile 2011-08-04 22:09:57 UTC (rev 9303)
@@ -4,7 +4,8 @@
top = ..
-ALL =
+ALL = INN/Utils/Shlock.pm
+
EXTRA = INN/Config.pm
## The double underscore '__' will be converted into two colons '::'
@@ -12,12 +13,13 @@
## file, and we cannot generate the right file with '::' in the
## Subversion man directory because it would always be regenerated:
## the file containing '__' would indeed not be found).
-MAN = ../doc/man/INN__Config.3pm
+MAN = ../doc/man/INN__Config.3pm \
+ ../doc/man/INN__Utils__Shlock.3pm
all: $(ALL) $(EXTRA) $(MAN)
install: all
- for F in $(EXTRA) ; do \
+ for F in $(ALL) $(EXTRA) ; do \
$(CP_RPUB) $$F $D$(PATHLIBPERL)/$$F ; \
done
@@ -40,6 +42,14 @@
@echo Run configure before running make. See INSTALL for details.
@exit 1
+## Compilation rules.
+
+FIX = $(FIXSCRIPT)
+
+INN/Utils/Shlock.pm: INN/Utils/Shlock.pm.in $(FIX) ; $(FIX) INN/Utils/Shlock.pm.in
+
../doc/man/INN__Config.3pm: INN/Config.pm.in
$(POD2MAN) -s '$(MAN3PM_EXT)' -n "INN::Config" $? > $@
+../doc/man/INN__Utils__Shlock.3pm: INN/Utils/Shlock.pm.in
+ $(POD2MAN) -s '$(MAN3PM_EXT)' -n "INN::Utils::Shlock" $? > $@
Modified: support/mkmanifest
===================================================================
--- support/mkmanifest 2011-08-04 21:10:17 UTC (rev 9302)
+++ support/mkmanifest 2011-08-04 22:09:57 UTC (rev 9303)
@@ -175,6 +175,7 @@
innfeed/version.c
nnrpd/nnrpd
perl/INN/Config.pm
+perl/INN/Utils/Shlock.pm
samples/buffindexed.conf
samples/inn.conf
samples/innreport.conf
More information about the inn-committers
mailing list