class Inspec::ShellDetector
the caller.
verified against a list of known shells before being returned to
Since none of these methods is fullproof, the detected shell is
- The shell returned by getpwuid for our process UID
- The SHELL environment variable
- The command of our parent
running by checking:
ShellDetector attempts to detect the shell the invoking user is
def detect
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
def detect_by_env
def detect_by_env ENV['SHELL'] end
def detect_by_getpwuid
def detect_by_getpwuid Etc.getpwuid(Process.uid).shell end
def detect_by_ppid
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
def detected?(arg)
def detected?(arg) arg != NOT_DETECTED end
def initialize
def initialize @shell = NOT_DETECTED end
def known_shell?(shell)
do anything very silly.
Only return shells that we know about, just to be sure we never
def known_shell?(shell) KNOWN_SHELLS.include?(shellname(shell)) end
def shell
def shell @shell = detect if !detected?(@shell) @shell end
def shell!
def shell! @shell = detect end
def shellname(shellpath)
Strip any leading path elements
def shellname(shellpath) shellpath.split('/').last end