Module: RailsPwnerer::Util

Extended by:
Base
Defined in:
lib/rails_pwnerer/util/main.rb,
lib/rails_pwnerer/util/kill_process_set.rb

Class Method Summary collapse

Methods included from Base

_setup_unix, _setup_windows, all_packages, all_packages_without_caching, atomic_erase, atomic_read, atomic_write, best_package_matching, check_rails_root, control_boot_script, cpu_cores, current_user, gem_exists?, gid_for_username, group_for_username, hook_boot_script, install_gem, install_gems, install_package, install_package_impl, install_package_matching, install_packages, kill_tree, os_distro, package_info_hash, path_to_boot_script, path_to_boot_script_defaults, path_to_gemdir, process_info, prompt_user_for_password, remove_package, remove_packages, search_packages, uid_for_username, unroll_collection, update_all_packages, update_all_packages_impl, update_gems, update_package_metadata, upgrade_gem, upgrade_gems, upgrade_package, upgrade_package_impl, upgrade_packages, with_package_source, with_temp_dir

Class Method Details

.kill_process_set(kill_script, pid_patterns, process_patterns, options = {}) ⇒ Object

Complex procedure for killing a process or a bunch of process replicas kill_command is the script that’s supposed to kill the process / processes (tried first) pid_patters are globs identifying PID files (a file can match any of the patterns) process_patterns are strings that should show on a command line (a process must match all)

options:

:verbose - display what gets killed
:script_delay - the amount of seconds to sleep after launching the kill script
:force_script - the kill script is executed even if there are no PID files


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rails_pwnerer/util/kill_process_set.rb', line 12

def self.kill_process_set(kill_script, pid_patterns, process_patterns, options = {})
  # Phase 1: kill order (only if there's a PID file)
  pid_patterns = [pid_patterns] unless pid_patterns.kind_of? Enumerable
  unless options[:force_script]
    pid_files = pid_patterns.map { |pattern| Dir.glob(pattern) }.flatten
  end
  if options[:force_script] or !(pid_files.empty? or kill_script.nil?)
    print "Issuing kill order: #{kill_script}\n" if options[:verbose]
    success = Kernel.system kill_script unless kill_script.nil?
    if !success and options[:verbose]
      print "Kill order failed with exit code #{$CHILD_STATUS.exitstatus}\n"
    end
    
    deadline_time = Time.now + (options[:script_delay] || 0.5)
    while Time.now < deadline_time
      pid_files = pid_patterns.map { |pattern| Dir.glob(pattern) }.flatten
      break if pid_files.empty?
      sleep 0.05
    end
  end
  
  # Phase 2: look through PID files and issue kill orders
  pinfo = process_info()
  pid_files = pid_patterns.map { |pattern| Dir.glob(pattern) }.flatten 
  pid_files.each do |fname|
    begin
      pid = File.open(fname, 'r') { |f| f.read.strip! }
      process_cmdline = pinfo[pid][:cmdline]
      # avoid killing innocent victims
      if pinfo[pid].nil? or process_patterns.all? { |pattern| process_cmdline.index pattern } 
        print "Killing #{pid}: #{process_cmdline}\n" if options[:verbose]
        kill_tree pid.to_i
      end
    rescue
      # just in case the file gets wiped before we see it
    end
    begin
      print "Deleting #{fname}\n" if options[:verbose]
      File.delete fname if File.exists? fname
    rescue
      # prevents crashing if the file is wiped after we call exists?
    end
  end
  
  # Phase 3: look through the process table and kill anything that looks good
  pinfo = process_info()
  pinfo.each do |pid, info|
    next unless process_patterns.all? { |pattern| info[:cmdline].index pattern }
    print "Killing #{pid}: #{pinfo[pid][:cmdline]}\n" if options[:verbose]
    kill_tree pid.to_i
  end
end