Skip to content

Commit 7382d6b

Browse files
committed
Refactor into process_running?
1 parent 637506a commit 7382d6b

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

lib/facter/util/linux.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
module Facter
4+
module Util
5+
module Linux
6+
def self.process_running?(process_name)
7+
pidfiles = Dir.glob("{/run,/var/run}/#{process_name}{,*,/}*.pid")
8+
pidfiles.each do |pf|
9+
next unless File.file?(pf)
10+
11+
pid = begin
12+
Integer(Facter::Util::FileHelper.safe_read(pf, '').strip, 10)
13+
rescue StandardError
14+
nil
15+
end
16+
next unless pid&.positive?
17+
18+
begin
19+
# Doesn't actually kill, just detects if the process exists
20+
Process.kill(0, pid)
21+
return true if proc_comm(pid) == process_name || proc_cmdline(pid)&.match?(%r{(^|\s|/)#{process_name}(\s|$)})
22+
rescue Errno::ESRCH
23+
# If we can't confirm identity, still treat it as not running to be safe.
24+
next
25+
rescue Errno::EPERM
26+
# Exists but we can't inspect it; assume it's running.
27+
return true
28+
end
29+
end
30+
31+
# Fallback: Try to find it in /proc
32+
return false unless Dir.exist?('/proc')
33+
34+
Dir.glob('/proc/[0-9]*/comm').any? do |path|
35+
Facter::Util::FileHelper.safe_read(path, nil)&.strip == process_name
36+
end
37+
end
38+
39+
def self.proc_comm(pid)
40+
Facter::Util::FileHelper.safe_read("/proc/#{pid}/comm", nil)&.strip
41+
end
42+
43+
def self.proc_cmdline(pid)
44+
raw = Facter::Util::FileHelper.safe_read("/proc/#{pid}/cmdline", nil)
45+
raw&.tr("\0", ' ')
46+
end
47+
end
48+
end
49+
end

lib/facter/util/linux/dhcp.rb

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'facter/util/linux'
4+
35
module Facter
46
module Util
57
module Linux
@@ -78,7 +80,7 @@ def search_with_dhcpcd_command(interface_name)
7880
@dhcpcd_command ||= Facter::Core::Execution.which('dhcpcd')
7981
return unless @dhcpcd_command
8082

81-
unless dhcpcd_running?
83+
unless Facter::Util::Linux.process_running?('dhcpcd')
8284
@log.debug('Skipping dhcpcd -U because dhcpcd daemon is not running')
8385
return
8486
end
@@ -89,48 +91,6 @@ def search_with_dhcpcd_command(interface_name)
8991
dhcp = output.match(/dhcp_server_identifier='(.*)'/)
9092
dhcp[1] if dhcp
9193
end
92-
93-
def dhcpcd_running?
94-
pidfiles = Dir.glob('{/run,/var/run}/dhcpcd{,*,/}*.pid')
95-
pidfiles.each do |pf|
96-
next unless File.file?(pf)
97-
98-
pid = begin
99-
Integer(Facter::Util::FileHelper.safe_read(pf, '').strip, 10)
100-
rescue StandardError
101-
nil
102-
end
103-
next unless pid&.positive?
104-
105-
begin
106-
# Doesn't actually kill, just detects if the process exists
107-
Process.kill(0, pid)
108-
return true if proc_comm(pid) == 'dhcpcd' || proc_cmdline(pid)&.match?(%r{(^|\s|/)dhcpcd(\s|$)})
109-
rescue Errno::ESRCH
110-
# If we can't confirm identity, still treat it as not running to be safe.
111-
next
112-
rescue Errno::EPERM
113-
# Exists but we can't inspect it; assume it's running.
114-
return true
115-
end
116-
end
117-
118-
# Fallback: Try to find it in /proc
119-
return false unless Dir.exist?('/proc')
120-
121-
Dir.glob('/proc/[0-9]*/comm').any? do |path|
122-
Facter::Util::FileHelper.safe_read(path, nil)&.strip == 'dhcpcd'
123-
end
124-
end
125-
126-
def proc_comm(pid)
127-
Facter::Util::FileHelper.safe_read("/proc/#{pid}/comm", nil)&.strip
128-
end
129-
130-
def proc_cmdline(pid)
131-
raw = Facter::Util::FileHelper.safe_read("/proc/#{pid}/cmdline", nil)
132-
raw&.tr("\0", ' ')
133-
end
13494
end
13595
end
13696
end

spec/facter/util/linux/dhcp_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115
allow(File).to receive(:readable?).with('/var/lib/dhcp3/').and_return(false)
116116
allow(File).to receive(:readable?).with('/var/lib/NetworkManager/').and_return(false)
117117
allow(File).to receive(:readable?).with('/var/db/').and_return(false)
118+
allow(Dir).to receive(:glob).with('{/run,/var/run}/dhcpcd{,*,/}*.pid').and_return([''])
119+
118120

119121
allow(Facter::Core::Execution).to receive(:which)
120122
.with('dhcpcd').and_return('/usr/bin/dhcpcd')

0 commit comments

Comments
 (0)