class Puppet::Util::Pidlock

Public Class Methods

new(lockfile) click to toggle source
  # File lib/puppet/util/pidlock.rb
7 def initialize(lockfile)
8   @lockfile = Puppet::Util::Lockfile.new(lockfile)
9 end

Public Instance Methods

file_path() click to toggle source
   # File lib/puppet/util/pidlock.rb
43 def file_path
44   @lockfile.file_path
45 end
lock() click to toggle source
   # File lib/puppet/util/pidlock.rb
20 def lock
21   return mine? if locked?
22 
23   @lockfile.lock(Process.pid)
24 end
lock_pid() click to toggle source
   # File lib/puppet/util/pidlock.rb
34 def lock_pid
35   pid = @lockfile.lock_data
36   begin
37     Integer(pid)
38   rescue ArgumentError, TypeError
39     nil
40   end
41 end
locked?() click to toggle source
   # File lib/puppet/util/pidlock.rb
11 def locked?
12   clear_if_stale
13   @lockfile.locked?
14 end
mine?() click to toggle source
   # File lib/puppet/util/pidlock.rb
16 def mine?
17   Process.pid == lock_pid
18 end
unlock() click to toggle source
   # File lib/puppet/util/pidlock.rb
26 def unlock
27   if mine?
28     return @lockfile.unlock
29   else
30     false
31   end
32 end

Private Instance Methods

clear_if_stale() click to toggle source
    # File lib/puppet/util/pidlock.rb
 60 def clear_if_stale
 61   pid = lock_pid
 62   return @lockfile.unlock if pid == nil
 63   return if Process.pid == pid
 64 
 65   errors = [Errno::ESRCH]
 66   # Win32::Process now throws SystemCallError. Since this could be
 67   # defined anywhere, only add when on Windows.
 68   errors << SystemCallError if Puppet::Util::Platform.windows?
 69 
 70   begin
 71     Process.kill(0, pid)
 72   rescue *errors
 73     return @lockfile.unlock
 74   end
 75 
 76   # Ensure the process associated with this pid is our process. If
 77   # not, we can unlock the lockfile. CLI arguments used for identifying
 78   # on POSIX depend on the os and sometimes even version.
 79   if Puppet.features.posix?
 80     ps_argument = ps_argument_for_current_kernel
 81 
 82     # Check, obtain and use the right ps argument
 83     begin
 84       procname = Puppet::Util::Execution.execute(["ps", ps_argument, pid, "-o", "comm="]).strip
 85     rescue Puppet::ExecutionFailure
 86       ps_argument = "-p"
 87       procname = Puppet::Util::Execution.execute(["ps", ps_argument, pid, "-o", "comm="]).strip
 88     end
 89 
 90     args = Puppet::Util::Execution.execute(["ps", ps_argument, pid, "-o", "args="]).strip
 91     @lockfile.unlock unless procname =~ /ruby/ && args =~ /puppet/ || procname =~ /puppet(-.*)?$/
 92   elsif Puppet.features.microsoft_windows?
 93     # On Windows, we're checking if the filesystem path name of the running
 94     # process is our vendored ruby:
 95     begin
 96       exe_path = Puppet::Util::Windows::Process::get_process_image_name_by_pid(pid)
 97       @lockfile.unlock unless exe_path =~ /\\bin\\ruby.exe$/
 98     rescue Puppet::Util::Windows::Error => e
 99       Puppet.debug("Failed to read pidfile #{file_path}: #{e.message}")
100     end
101   end
102 end
ps_argument_for_current_kernel() click to toggle source
   # File lib/puppet/util/pidlock.rb
49 def ps_argument_for_current_kernel
50   case Puppet.runtime[:facter].value(:kernel)
51     when "Linux"
52       "-eq"
53     when "AIX"
54       "-T"
55     else
56       "-p"
57   end
58 end