class Puppet::Util::Pidlock

Public Class Methods

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

Public Instance Methods

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

Private Instance Methods

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