Skip to content

Commit 9581db1

Browse files
committed
Use Etc.nprocessors and handle sandboxed environments
Replace OS-specific processor detection (sysctl, /proc/cpuinfo, etc.) with Ruby's built-in Etc.nprocessors. The previous implementation was copied from the parallel gem in 2015 to support older Rubies, but Etc.nprocessors has been available since Ruby 2.2 and overcommit requires Ruby 2.6+. Also rescue Errno::EPERM/ENOENT in parent_command so that sandboxed environments where ps is unavailable don't crash the hook run.
1 parent 11838c6 commit 9581db1

File tree

1 file changed

+6
-34
lines changed

1 file changed

+6
-34
lines changed

lib/overcommit/utils.rb

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

3+
require 'etc'
34
require 'pathname'
45
require 'overcommit/os'
56
require 'overcommit/subprocess'
@@ -150,6 +151,9 @@ def parent_command
150151
else
151152
`ps -ocommand= -p #{Process.ppid}`.chomp
152153
end
154+
rescue Errno::EPERM, Errno::ENOENT
155+
# Process information may not be available, such as inside sandboxed environments
156+
nil
153157
end
154158

155159
# Execute a command in a subprocess, capturing exit status and output from
@@ -212,40 +216,8 @@ def execute_in_background(args)
212216
end
213217

214218
# Return the number of processors used by the OS for process scheduling.
215-
#
216-
# @see https://github.com/grosser/parallel/blob/v1.6.1/lib/parallel/processor_count.rb#L17-L51
217-
def processor_count # rubocop:disable all
218-
@processor_count ||=
219-
begin
220-
if Overcommit::OS.windows?
221-
require 'win32ole'
222-
result = WIN32OLE.connect('winmgmts://').ExecQuery(
223-
'select NumberOfLogicalProcessors from Win32_Processor'
224-
)
225-
result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+)
226-
elsif File.readable?('/proc/cpuinfo')
227-
IO.read('/proc/cpuinfo').scan(/^processor/).size
228-
elsif File.executable?('/usr/bin/hwprefs')
229-
IO.popen('/usr/bin/hwprefs thread_count').read.to_i
230-
elsif File.executable?('/usr/sbin/psrinfo')
231-
IO.popen('/usr/sbin/psrinfo').read.scan(/^.*on-*line/).size
232-
elsif File.executable?('/usr/sbin/ioscan')
233-
IO.popen('/usr/sbin/ioscan -kC processor') do |out|
234-
out.read.scan(/^.*processor/).size
235-
end
236-
elsif File.executable?('/usr/sbin/pmcycles')
237-
IO.popen('/usr/sbin/pmcycles -m').read.count("\n")
238-
elsif File.executable?('/usr/sbin/lsdev')
239-
IO.popen('/usr/sbin/lsdev -Cc processor -S 1').read.count("\n")
240-
elsif File.executable?('/usr/sbin/sysctl')
241-
IO.popen('/usr/sbin/sysctl -n hw.ncpu').read.to_i
242-
elsif File.executable?('/sbin/sysctl')
243-
IO.popen('/sbin/sysctl -n hw.ncpu').read.to_i
244-
else
245-
# Unknown platform; assume 1 processor
246-
1
247-
end
248-
end
219+
def processor_count
220+
@processor_count ||= Etc.nprocessors
249221
end
250222

251223
# Calls a block of code with a modified set of environment variables,

0 commit comments

Comments
 (0)