INN commit: branches/2.5 (6 files)

INN Commit rra at isc.org
Mon May 28 18:43:20 UTC 2012


    Date: Monday, May 28, 2012 @ 11:43:20
  Author: iulius
Revision: 9409

add a releaselocks() function to INN::Utils::Shlock

Improve how to release existing locks.  lock() now keeps a trace of
acquired locks; releaselocks() releases all these locks.  Previously,
unlock() had to be called on all locks.

Modified:
  branches/2.5/backends/send-uucp.in
  branches/2.5/contrib/thdexpire.in
  branches/2.5/control/controlchan.in
  branches/2.5/frontends/mailpost.in
  branches/2.5/frontends/pullnews.in
  branches/2.5/perl/INN/Utils/Shlock.pm.in

-----------------------------+
 backends/send-uucp.in       |    5 +++
 contrib/thdexpire.in        |    5 +++
 control/controlchan.in      |    6 ++++
 frontends/mailpost.in       |    8 ++++--
 frontends/pullnews.in       |    5 +++
 perl/INN/Utils/Shlock.pm.in |   54 ++++++++++++++++++++++++++++++++++++++++--
 6 files changed, 79 insertions(+), 4 deletions(-)

Modified: backends/send-uucp.in
===================================================================
--- backends/send-uucp.in	2012-05-28 18:42:29 UTC (rev 9408)
+++ backends/send-uucp.in	2012-05-28 18:43:20 UTC (rev 9409)
@@ -43,6 +43,11 @@
 my $config_file = $INN::Config::pathetc . '/send-uucp.cf';
 my $lockfile = $INN::Config::locks . '/LOCK.send-uucp';
 
+END {
+    # In case we bail out, while holding a lock.
+    INN::Utils::Shlock::releaselocks();
+}
+
 my $use_syslog = 0;
 
 eval { require Sys::Syslog; import Sys::Syslog; $use_syslog = 1; };

Modified: contrib/thdexpire.in
===================================================================
--- contrib/thdexpire.in	2012-05-28 18:42:29 UTC (rev 9408)
+++ contrib/thdexpire.in	2012-05-28 18:43:20 UTC (rev 9409)
@@ -248,6 +248,11 @@
 
 my $lockfile = "$INN::Config::innddir/thdexpire.pid";
 
+END {
+    # In case we bail out, while holding a lock.
+    INN::Utils::Shlock::releaselocks();
+}
+
 chdir $INN::Config::spool || die "chdir $INN::Config::spool: $!";
 $opt_r=0;      # make a report
 $opt_t=30;     # check interval in minutes

Modified: control/controlchan.in
===================================================================
--- control/controlchan.in	2012-05-28 18:42:29 UTC (rev 9408)
+++ control/controlchan.in	2012-05-28 18:43:20 UTC (rev 9409)
@@ -45,6 +45,12 @@
 my $use_syslog = 0;
 my $debug = 0;
 
+END {
+    # In case we bail out, while holding a lock.
+    INN::Utils::Shlock::releaselocks();
+}
+
+
 my $usage = "usage: $0 [-ch]\n\n" .
     "Reads stdin for file names or tokens.\n\n" .
     "  -c   Disables cutoff on dates.\n" .

Modified: frontends/mailpost.in
===================================================================
--- frontends/mailpost.in	2012-05-28 18:42:29 UTC (rev 9408)
+++ frontends/mailpost.in	2012-05-28 18:43:20 UTC (rev 9409)
@@ -27,8 +27,12 @@
 my $msg ;
 
 END {
-    unlink ($tmpfile) if $tmpfile ;		# in case we die()
-    unlink ($tmpfile2) if $tmpfile2 ;		# in case we die()
+    unlink ($tmpfile) if $tmpfile;              # in case we die()
+    unlink ($tmpfile2) if $tmpfile2;            # in case we die()
+
+    # In case we bail out, typically by calling mailArtAndDie(), while
+    # holding a lock.
+    INN::Utils::Shlock::releaselocks();
 }
 
 my $usage = $0 ;

Modified: frontends/pullnews.in
===================================================================
--- frontends/pullnews.in	2012-05-28 18:42:29 UTC (rev 9408)
+++ frontends/pullnews.in	2012-05-28 18:43:20 UTC (rev 9409)
@@ -77,6 +77,11 @@
 my $defaultProgressWidth = 50;
 my $defaultMaxArts;
 
+END {
+    # In case we bail out, while holding a lock.
+    INN::Utils::Shlock::releaselocks();
+}
+
 $usage =~ s!.*/!!;
 $usage .= " [ -hnqRx -b fraction -c config -C width -d level
         -f fraction -F fakehop -g groups -G newsgroups -H headers

Modified: perl/INN/Utils/Shlock.pm.in
===================================================================
--- perl/INN/Utils/Shlock.pm.in	2012-05-28 18:42:29 UTC (rev 9408)
+++ perl/INN/Utils/Shlock.pm.in	2012-05-28 18:43:20 UTC (rev 9409)
@@ -15,7 +15,9 @@
 our @ISA = qw(Exporter);
 our $VERSION = "$INN::Config::VERSION";
 
+my %lockfiles;
 
+
 ##
 ##  Returns true if the file is properly locked.
 ##
@@ -30,6 +32,7 @@
 
     while ($locktry < $lockretrymax) {
         if (system("$INN::Config::newsbin/shlock", '-p', $$, '-f', $lockfile) == 0) {
+            $lockfiles{$lockfile} = 1;
             return 1;
         }
         $locktry++;
@@ -46,12 +49,34 @@
 ##
 sub unlock {
     my $lockfile = shift;
-    return unlink $lockfile;
+    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);
+our @EXPORT_OK = qw(lock unlock releaselocks);
 
 
 ##  That's all.
@@ -98,6 +123,16 @@
         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
@@ -106,12 +141,16 @@
 
 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
@@ -121,10 +160,21 @@
 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



More information about the inn-committers mailing list