class Inspec::ShellDetector
ShellDetector
attempts to detect the shell the invoking user is running by checking:
- The command of our parent - The SHELL environment variable - The shell returned by getpwuid for our process UID
Since none of these methods is fullproof, the detected shell is verified against a list of known shells before being returned to the caller.
Constants
- KNOWN_SHELLS
- NOT_DETECTED
Public Class Methods
new()
click to toggle source
# File lib/inspec/shell_detector.rb, line 21 def initialize @shell = NOT_DETECTED end
Public Instance Methods
shell()
click to toggle source
# File lib/inspec/shell_detector.rb, line 25 def shell @shell = detect unless detected?(@shell) @shell end
shell!()
click to toggle source
# File lib/inspec/shell_detector.rb, line 30 def shell! @shell = detect end
Private Instance Methods
detect()
click to toggle source
# File lib/inspec/shell_detector.rb, line 36 def detect # Most of our detection code assumes a unix-like environment return nil if RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/ shellpath = detect_by_ppid if shellpath.nil? || shellpath.empty? || !known_shell?(shellpath) shellpath = detect_by_env end if shellpath.nil? || shellpath.empty? || !known_shell?(shellpath) shellpath = detect_by_getpwuid end shellname(shellpath) if known_shell?(shellpath) end
detect_by_env()
click to toggle source
# File lib/inspec/shell_detector.rb, line 66 def detect_by_env ENV["SHELL"] end
detect_by_getpwuid()
click to toggle source
# File lib/inspec/shell_detector.rb, line 70 def detect_by_getpwuid Etc.getpwuid(Process.uid).shell end
detect_by_ppid()
click to toggle source
# File lib/inspec/shell_detector.rb, line 57 def detect_by_ppid ppid = Process.ppid if Dir.exist?("/proc") File.readlink("/proc/#{ppid}/exe") else `ps -cp #{ppid} -o command=`.chomp end end
detected?(arg)
click to toggle source
# File lib/inspec/shell_detector.rb, line 53 def detected?(arg) arg != NOT_DETECTED end
known_shell?(shell)
click to toggle source
Only return shells that we know about, just to be sure we never do anything very silly.
# File lib/inspec/shell_detector.rb, line 85 def known_shell?(shell) KNOWN_SHELLS.include?(shellname(shell)) end
shellname(shellpath)
click to toggle source
Strip any leading path elements
# File lib/inspec/shell_detector.rb, line 77 def shellname(shellpath) shellpath.split("/").last end