Sha256: e43dc3719af6b7bc5a338abe34ddafc1f5b6d06f4e2bbf751db3372c0feba14f

Contents?: true

Size: 1.94 KB

Versions: 48

Compression:

Stored size: 1.94 KB

Contents

require "etc" unless defined?(Etc)
require "rbconfig" unless defined?(RbConfig)

module Inspec
  #
  # 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.
  #
  class ShellDetector
    NOT_DETECTED = Object.new.freeze
    KNOWN_SHELLS = %w{bash zsh ksh csh sh fish}.freeze

    def initialize
      @shell = NOT_DETECTED
    end

    def shell
      @shell = detect unless detected?(@shell)
      @shell
    end

    def shell!
      @shell = detect
    end

    private

    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 detected?(arg)
      arg != NOT_DETECTED
    end

    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 detect_by_env
      ENV["SHELL"]
    end

    def detect_by_getpwuid
      Etc.getpwuid(Process.uid).shell
    end

    #
    # Strip any leading path elements
    #
    def shellname(shellpath)
      shellpath.split("/").last
    end

    #
    # Only return shells that we know about, just to be sure we never
    # do anything very silly.
    #
    def known_shell?(shell)
      KNOWN_SHELLS.include?(shellname(shell))
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
inspec-core-6.8.11 lib/inspec/shell_detector.rb
inspec-core-5.22.58 lib/inspec/shell_detector.rb
inspec-core-5.22.55 lib/inspec/shell_detector.rb
inspec-core-6.8.1 lib/inspec/shell_detector.rb
inspec-core-5.22.40 lib/inspec/shell_detector.rb
inspec-core-6.6.0 lib/inspec/shell_detector.rb
inspec-core-5.22.36 lib/inspec/shell_detector.rb
inspec-core-5.22.29 lib/inspec/shell_detector.rb
inspec-core-4.56.58 lib/inspec/shell_detector.rb
inspec-core-5.22.3 lib/inspec/shell_detector.rb
inspec-core-5.21.29 lib/inspec/shell_detector.rb
inspec-core-5.18.14 lib/inspec/shell_detector.rb
inspec-core-5.17.4 lib/inspec/shell_detector.rb
inspec-core-5.14.0 lib/inspec/shell_detector.rb
inspec-core-4.56.20 lib/inspec/shell_detector.rb
inspec-core-5.12.2 lib/inspec/shell_detector.rb
inspec-core-5.10.5 lib/inspec/shell_detector.rb
inspec-core-4.56.19 lib/inspec/shell_detector.rb
inspec-core-5.7.9 lib/inspec/shell_detector.rb
inspec-core-4.56.17 lib/inspec/shell_detector.rb