--- loncom/LONCAPA.pm 2010/06/15 05:31:43 1.30 +++ loncom/LONCAPA.pm 2011/05/14 16:12:53 1.31 @@ -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 @@ sub clean_handle { 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=; + 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 {