[LON-CAPA-cvs] cvs: loncom / LONCAPA.pm apachereload lciptables loncron lond

raeburn raeburn@source.lon-capa.org
Sat, 14 May 2011 16:12:53 -0000


raeburn		Sat May 14 16:12:53 2011 EDT

  Modified files:              
    /loncom	lond loncron apachereload lciptables LONCAPA.pm 
  Log:
  - Duplicated &try_to_lock() routine moved to one location (in LONCAPA.pm).
  - Try to get locks for lock_apachereload and lock_lciptables before 
    making system calls (in lond and loncron respectively).
  
  
Index: loncom/lond
diff -u loncom/lond:1.473 loncom/lond:1.474
--- loncom/lond:1.473	Sat May 14 15:47:01 2011
+++ loncom/lond	Sat May 14 16:12:53 2011
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.473 2011/05/14 15:47:01 raeburn Exp $
+# $Id: lond,v 1.474 2011/05/14 16:12:53 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -60,7 +60,7 @@
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.473 $'; #' stupid emacs
+my $VERSION='$Revision: 1.474 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid="default";
 my $currentdomainid;
@@ -6093,9 +6093,12 @@
 #
 sub ReloadApache {
 # --------------------------- Handle case of another apachereload process (locking)
-    my $execdir = $perlvar{'lonDaemons'};
-    my $script  = $execdir."/apachereload";
-    system($script);
+    if (&LONCAPA::try_to_lock('/tmp/lock_apachereload')) {
+        my $execdir = $perlvar{'lonDaemons'};
+        my $script  = $execdir."/apachereload";
+        system($script);
+        unlink('/tmp/lock_apachereload'); #  Remove the lock file.
+    }
 }
 
 #
Index: loncom/loncron
diff -u loncom/loncron:1.88 loncom/loncron:1.89
--- loncom/loncron:1.88	Sat Apr 23 16:18:49 2011
+++ loncom/loncron	Sat May 14 16:12:53 2011
@@ -2,7 +2,7 @@
 
 # Housekeeping program, started by cron, loncontrol and loncron.pl
 #
-# $Id: loncron,v 1.88 2011/04/23 16:18:49 raeburn Exp $
+# $Id: loncron,v 1.89 2011/05/14 16:12:53 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -32,6 +32,7 @@
 
 use lib '/home/httpd/lib/perl/';
 use LONCAPA::Configuration;
+use LONCAPA;
 use Apache::lonnet;
 use Apache::loncommon;
 
@@ -821,8 +822,11 @@
                 print $fh "$key\n";
             }
             close($fh);
-            my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
-            system("$execpath $tmpfile");
+            if (&LONCAPA::try_to_lock('/tmp/lock_lciptables')) {
+                my $execpath = $perlvar{'lonDaemons'}.'/lciptables';
+                system("$execpath $tmpfile");
+                unlink('/tmp/lock_lciptables');  # Remove the lock file. 
+            }
             unlink($tmpfile);
         }
     }
Index: loncom/apachereload
diff -u loncom/apachereload:1.6 loncom/apachereload:1.7
--- loncom/apachereload:1.6	Sat Jan 28 00:52:27 2006
+++ loncom/apachereload	Sat May 14 16:12:53 2011
@@ -3,7 +3,7 @@
 #
 # apachereload - setuid script that reloads the apache daemon.
 #
-# $Id: apachereload,v 1.6 2006/01/28 00:52:27 albertel Exp $
+# $Id: apachereload,v 1.7 2011/05/14 16:12:53 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -68,14 +68,6 @@
 # ----------------------------------- Start running script with www permissions
 &disable_root_capability;
 
-# --------------------------- Handle case of another apachereload process (locking)
-unless (&try_to_lock('/tmp/lock_apachereload')) {
-    print "Error. Too many other simultaneous password change requests being ".
-	"made.\n" unless $noprint;
-    exit 4;
-}
-
-
 &enable_root_capability;
 ($>,$<)=(0,0);
 
@@ -85,12 +77,7 @@
 
 system($command);
 
-#  Remove the lock file.
-
-
-
 &disable_root_capability;
-unlink('/tmp/lock_apachereload');
 exit 0;
 
 # ---------------------------------------------- have setuid script run as root
@@ -116,44 +103,6 @@
     }
 }
 
-# ----------------------- make sure that another apachereload process isn't running
-sub try_to_lock {
-    my ($lockfile)=@_;
-    my $currentpid;
-    my $lastpid;
-    # Do not manipulate lock file as root
-    if ($>==0) {
-	return 0;
-    }
-    # Try to generate lock file.
-    # Wait 3 seconds.  If same process id is in
-    # lock file, then assume lock file is stale, and
-    # go ahead.  If process id's fluctuate, try
-    # for a maximum of 10 times.
-    for (0..10) {
-	if (-e $lockfile) {
-	    open(LOCK,"<$lockfile");
-	    $currentpid=<LOCK>;
-	    close LOCK;
-	    if ($currentpid==$lastpid) {
-		last;
-	    }
-	    sleep 3;
-	    $lastpid=$currentpid;
-	}
-	else {
-	    last;
-	}
-	if ($_==10) {
-	    return 0;
-	}
-    }
-    open(LOCK,">$lockfile");
-    print LOCK $$;
-    close LOCK;
-    return 1;
-}
-
 =head1 NAME
 
 apachereload -setuid script to reload the apache web server.
Index: loncom/lciptables
diff -u loncom/lciptables:1.4 loncom/lciptables:1.5
--- loncom/lciptables:1.4	Thu Dec 30 18:40:19 2010
+++ loncom/lciptables	Sat May 14 16:12:53 2011
@@ -2,7 +2,7 @@
 #
 # The Learning Online Network with CAPA
 #
-# $Id: lciptables,v 1.4 2010/12/30 18:40:19 raeburn Exp $
+# $Id: lciptables,v 1.5 2011/05/14 16:12:53 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -88,13 +88,6 @@
     &Exit(3);
 }
 
-# --------------------------- Handle case of another lciptables process (locking)
-unless (&try_to_lock("/tmp/lock_lciptables")) {
-    print "Error. Too many other simultaneous iptables manipulation requests being ".
-        "made.\n" unless $noprint;
-    &Exit(4);
-}
-
 my $lond_port = &LONCAPA::Firewall::get_lond_port();
 
 
@@ -115,7 +108,6 @@
 # -------------------------------------------------------- Exit script
 print "lciptables Exiting\n" unless $noprint;
 &DisableRoot;
-unlink('/tmp/lock_lciptables');
 &Exit(0);
 
 
@@ -140,42 +132,6 @@
     }
 }
 
-sub try_to_lock {
-    my ($lockfile)=@_;
-    my $currentpid;
-    my $lastpid;
-    # Do not manipulate lock file as root
-    if ($>==0) {
-        return 0;
-    }
-    # Try to generate lock file.
-    # Wait 3 seconds.  If same process id is in
-    # lock file, then assume lock file is stale, and
-    # go ahead.  If process id's fluctuate, try
-    # for a maximum of 10 times.
-    for (0..10) {
-        if (-e $lockfile) {
-            open(LOCK,"<$lockfile");
-            $currentpid=<LOCK>;
-            close LOCK;
-            if ($currentpid==$lastpid) {
-                last;
-            }
-            sleep 3;
-            $lastpid=$currentpid;
-        } else {
-            last;
-        }
-        if ($_==10) {
-            return 0;
-        }
-    }
-    open(LOCK,">$lockfile");
-    print LOCK $$;
-    close LOCK;
-    return 1;
-}
-
 sub Exit {
     my ($code) = @_;
     &DisableRoot();
Index: loncom/LONCAPA.pm
diff -u loncom/LONCAPA.pm:1.30 loncom/LONCAPA.pm:1.31
--- loncom/LONCAPA.pm:1.30	Tue Jun 15 05:31:43 2010
+++ loncom/LONCAPA.pm	Sat May 14 16:12:53 2011
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Base routines
 #
-# $Id: LONCAPA.pm,v 1.30 2010/06/15 05:31:43 raeburn Exp $
+# $Id: LONCAPA.pm,v 1.31 2011/05/14 16:12:53 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -155,6 +155,47 @@
     return $handle;
 }
 
+#
+# -- Ensure another process for same filesystem action is not running.
+#    lond uses for: apachereload; loncron uses for: lciptables
+#
+
+sub try_to_lock {
+    my ($lockfile)=@_;
+    my $currentpid;
+    my $lastpid;
+    # Do not manipulate lock file as root
+    if ($>==0) {
+        return 0;
+    }
+    # Try to generate lock file.
+    # Wait 3 seconds.  If same process id is in
+    # lock file, then assume lock file is stale, and
+    # go ahead.  If process id's fluctuate, try
+    # for a maximum of 10 times.
+    for (0..10) {
+        if (-e $lockfile) { 
+            open(LOCK,"<$lockfile");
+            $currentpid=<LOCK>;
+            close LOCK;
+            if ($currentpid==$lastpid) {
+                last;
+            }
+            sleep 3;
+            $lastpid=$currentpid;
+        } else {
+            last;
+        }
+        if ($_==10) {
+            return 0;
+        }
+    }
+    open(LOCK,">$lockfile");
+    print LOCK $$;
+    close LOCK;
+    return 1;
+}
+
 # -------------------------------------------- Return path to profile directory
 
 sub propath {