# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/components/scope' require 'cs__os_information/cs__os_information' module Contrast module Utils # Simple utility used to make OS calls and determine state. For that state # which will not change at runtime, such as the operating system, the # Utility memozies to avoid multiple lookups. module OS extend Contrast::Components::Scope::InstanceMethods class << self def running? result = false with_contrast_scope do process = `ps aux | grep contrast-servic[e]` processes = process.split("\n") result = !processes.empty? && processes.any? { |process_descriptor| !process_descriptor.include?('grep') } end result end # check if service was killed and is a zombie process # returns an array of zombie process PIDs as strings; empty array if there are none def zombie_pids with_contrast_scope do # retrieve pid of service processes zombie_pid_list = `ps aux | grep contrast-servic[e] | grep Z | awk '{print $2}'` zombie_pid_list.split("\n") end end # Check current OS type # returns true if check is correct or false if not def windows? return @_windows unless @_windows.nil? @_windows = !(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM).nil? end def mac? @_mac = RUBY_PLATFORM.include? 'darwin' if @_mac.nil? @_mac end def unix? !windows? end def linux? (unix? and !mac?) end end end end end