Module: Facter::Util::Linux

Defined in:
lib/facter/util/linux.rb,
lib/facter/util/linux/dhcp.rb,
lib/facter/util/linux/proc.rb,
lib/facter/util/linux/if_inet6.rb,
lib/facter/util/linux/routing_table.rb,
lib/facter/util/linux/socket_parser.rb

Defined Under Namespace

Classes: Dhcp, IfInet6, Proc, RoutingTable, SocketParser

Class Method Summary collapse

Class Method Details

.proc_cmdline(pid) ⇒ Object



43
44
45
46
# File 'lib/facter/util/linux.rb', line 43

def self.proc_cmdline(pid)
  raw = Facter::Util::FileHelper.safe_read("/proc/#{pid}/cmdline", nil)
  raw&.tr("\0", ' ')
end

.proc_comm(pid) ⇒ Object



39
40
41
# File 'lib/facter/util/linux.rb', line 39

def self.proc_comm(pid)
  Facter::Util::FileHelper.safe_read("/proc/#{pid}/comm", nil)&.strip
end

.process_running?(process_name) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/facter/util/linux.rb', line 6

def self.process_running?(process_name)
  pidfiles = Dir.glob("{/run,/var/run}/#{process_name}{,*,/}*.pid")
  pidfiles.each do |pf|
    next unless File.file?(pf)

    pid = begin
      Integer(Facter::Util::FileHelper.safe_read(pf, '').strip, 10)
    rescue StandardError
      nil
    end
    next unless pid&.positive?

    begin
      # Doesn't actually kill, just detects if the process exists
      Process.kill(0, pid)
      return true if proc_comm(pid) == process_name || proc_cmdline(pid)&.match?(%r{(^|\s|/)#{process_name}(\s|$)})
    rescue Errno::ESRCH
      # If we can't confirm identity, still treat it as not running to be safe.
      next
    rescue Errno::EPERM
      # Exists but we can't inspect it; assume it's running.
      return true
    end
  end

  # Fallback: Try to find it in /proc
  return false unless Dir.exist?('/proc')

  Dir.glob('/proc/[0-9]*/comm').any? do |path|
    Facter::Util::FileHelper.safe_read(path, nil)&.strip == process_name
  end
end