Class: Assassin

Inherits:
Object
  • Object
show all
Defined in:
lib/assassin.rb

Constant Summary collapse

Version =
'1.4.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(child_pid, options = {}) ⇒ Assassin

Returns a new instance of Assassin.



28
29
30
31
32
33
# File 'lib/assassin.rb', line 28

def initialize(child_pid, options = {})
  @child_pid = child_pid.to_s.to_i
  @parent_pid = Process.pid
  @options = Assassin.options_for(options)
  @pid, @path = Assassin.generate(@child_pid, @options)
end

Instance Attribute Details

#child_pidObject

Returns the value of attribute child_pid.



24
25
26
# File 'lib/assassin.rb', line 24

def child_pid
  @child_pid
end

#parent_pidObject

Returns the value of attribute parent_pid.



23
24
25
# File 'lib/assassin.rb', line 23

def parent_pid
  @parent_pid
end

#pathObject

Returns the value of attribute path.



26
27
28
# File 'lib/assassin.rb', line 26

def path
  @path
end

#pidObject

Returns the value of attribute pid.



25
26
27
# File 'lib/assassin.rb', line 25

def pid
  @pid
end

Class Method Details

.at_exit_kill(*args, &block) ⇒ Object



15
16
17
# File 'lib/assassin.rb', line 15

def Assassin.at_exit_kill(*args, &block)
  new(*args, &block)
end

.ate(*args, &block) ⇒ Object



19
20
21
# File 'lib/assassin.rb', line 19

def Assassin.ate(*args, &block)
  new(*args, &block)
end

.descriptionObject



11
12
13
# File 'lib/assassin.rb', line 11

def Assassin.description
  'no zombies ever, not even on `exit!` or `kill -9`'
end

.generate(child_pid, options = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/assassin.rb', line 39

def Assassin.generate(child_pid, options = {})
  path = File.join(Dir.tmpdir, "assassin-#{ child_pid }-#{ SecureRandom.uuid }.rb")
  script = Assassin.script_for(child_pid, options)
  IO.binwrite(path, script)
  pid = Process.spawn "ruby #{ path }"
  [pid, path]
end

.options_for(options) ⇒ Object



35
36
37
# File 'lib/assassin.rb', line 35

def Assassin.options_for(options)
  options.inject({}){|h, kv| k,v = kv; h.update(k.to_s.to_sym => v)}
end

.script_for(child_pid, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/assassin.rb', line 47

def Assassin.script_for(child_pid, options = {})
  parent_pid = Process.pid
  delay = (options[:delay] || 0.42).to_f

  script = <<-__
    Process.daemon

    require 'fileutils'
    at_exit{ FileUtils.rm_f(__FILE__) }

    parent_pid = #{ parent_pid }
    child_pid = #{ child_pid }
    delay = #{ delay }

    m = 24*60*60
    n = 42
    
    m.times do
      begin
        Process.kill(0, parent_pid)
      rescue Object => e
        sleep(delay)

        if e.is_a?(Errno::ESRCH)
          n.times do
            begin
              Process.kill(15, child_pid) rescue nil
              sleep(rand + rand)
              Process.kill(9, child_pid) rescue nil
              sleep(rand + rand)
              Process.kill(0, child_pid)
            rescue Errno::ESRCH
              break
            end
          end
        end

        exit
      end

      sleep(1)
    end
  __

  return script
end

.versionObject



7
8
9
# File 'lib/assassin.rb', line 7

def Assassin.version
  Version
end