summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/pidlock.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet/util/pidlock.rb')
-rw-r--r--lib/puppet/util/pidlock.rb118
1 files changed, 59 insertions, 59 deletions
diff --git a/lib/puppet/util/pidlock.rb b/lib/puppet/util/pidlock.rb
index fae8134a5..d24b8378e 100644
--- a/lib/puppet/util/pidlock.rb
+++ b/lib/puppet/util/pidlock.rb
@@ -1,68 +1,68 @@
require 'fileutils'
class Puppet::Util::Pidlock
- attr_reader :lockfile
-
- def initialize(lockfile)
- @lockfile = lockfile
- end
-
- def locked?
- clear_if_stale
- File.exists? @lockfile
- end
-
- def mine?
- Process.pid == lock_pid
- end
+ attr_reader :lockfile
- def anonymous?
- return false unless File.exists?(@lockfile)
- File.read(@lockfile) == ""
- end
-
- def lock(opts = {})
- opts = {:anonymous => false}.merge(opts)
+ def initialize(lockfile)
+ @lockfile = lockfile
+ end
- if locked?
- mine?
- else
- if opts[:anonymous]
- File.open(@lockfile, 'w') { |fd| true }
- else
- File.open(@lockfile, "w") { |fd| fd.write(Process.pid) }
- end
- true
- end
- end
-
- def unlock(opts = {})
- opts = {:anonymous => false}.merge(opts)
+ def locked?
+ clear_if_stale
+ File.exists? @lockfile
+ end
- if mine? or (opts[:anonymous] and anonymous?)
- File.unlink(@lockfile)
- true
- else
- false
- end
- end
+ def mine?
+ Process.pid == lock_pid
+ end
- private
- def lock_pid
- if File.exists? @lockfile
- File.read(@lockfile).to_i
- else
- nil
- end
- end
+ def anonymous?
+ return false unless File.exists?(@lockfile)
+ File.read(@lockfile) == ""
+ end
- def clear_if_stale
- return if lock_pid.nil?
-
- begin
- Process.kill(0, lock_pid)
- rescue Errno::ESRCH
- File.unlink(@lockfile)
- end
- end
+ def lock(opts = {})
+ opts = {:anonymous => false}.merge(opts)
+
+ if locked?
+ mine?
+ else
+ if opts[:anonymous]
+ File.open(@lockfile, 'w') { |fd| true }
+ else
+ File.open(@lockfile, "w") { |fd| fd.write(Process.pid) }
+ end
+ true
+ end
+ end
+
+ def unlock(opts = {})
+ opts = {:anonymous => false}.merge(opts)
+
+ if mine? or (opts[:anonymous] and anonymous?)
+ File.unlink(@lockfile)
+ true
+ else
+ false
+ end
+ end
+
+ private
+ def lock_pid
+ if File.exists? @lockfile
+ File.read(@lockfile).to_i
+ else
+ nil
+ end
+ end
+
+ def clear_if_stale
+ return if lock_pid.nil?
+
+ begin
+ Process.kill(0, lock_pid)
+ rescue Errno::ESRCH
+ File.unlink(@lockfile)
+ end
+ end
end