Class: Arachni::Processes::Manager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/arachni/processes/manager.rb

Overview

Helper for managing processes.

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



34
35
36
37
# File 'lib/arachni/processes/manager.rb', line 34

def initialize
    @pids           = []
    @discard_output = true
end

Instance Attribute Details

#pidsArray<Integer> (readonly)

Returns PIDs of all running processes.

Returns:

  • (Array<Integer>)

    PIDs of all running processes.



32
33
34
# File 'lib/arachni/processes/manager.rb', line 32

def pids
  @pids
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/arachni/processes/manager.rb', line 114

def self.method_missing( sym, *args, &block )
    if instance.respond_to?( sym )
        instance.send( sym, *args, &block )
    elsif
    super( sym, *args, &block )
    end
end

.respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/arachni/processes/manager.rb', line 122

def self.respond_to?( m )
    super( m ) || instance.respond_to?( m )
end

Instance Method Details

#<<(pid) ⇒ Integer

Returns ‘pid`.

Parameters:

  • pid (Integer)

    Adds a PID to the #pids and detaches the process.

Returns:

  • (Integer)

    ‘pid`



45
46
47
48
49
# File 'lib/arachni/processes/manager.rb', line 45

def <<( pid )
    @pids << pid
    Process.detach pid
    pid
end

#discard_output(&block) ⇒ Object

Parameters:

  • block (Block)

    Block to run silently.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/arachni/processes/manager.rb', line 99

def discard_output( &block )
    if !block_given?
        @discard_output = true
        return
    end

    proc do
        if @discard_output
            $stdout.reopen( '/dev/null', 'w' )
            $stderr.reopen( '/dev/null', 'w' )
        end
        block.call
    end
end

#fork_em(*args, &block) ⇒ Object

Parameters:

  • block (Block)

    Block to fork and run inside EventMachine’s reactor thread – its output will be discarded..



89
90
91
# File 'lib/arachni/processes/manager.rb', line 89

def fork_em( *args, &block )
    self << ::EM.fork_reactor( *args, &discard_output( &block ) )
end

#kill(pid) ⇒ Object

Parameters:

  • pid (Integer)

    PID of the process to kill.



52
53
54
55
56
57
58
59
60
61
# File 'lib/arachni/processes/manager.rb', line 52

def kill( pid )
    loop do
        begin
            Process.kill( 'KILL', pid )
        rescue Errno::ESRCH
            @pids.delete pid
            return
        end
    end
end

#kill_emObject

Stops the EventMachine reactor.



75
76
77
78
79
# File 'lib/arachni/processes/manager.rb', line 75

def kill_em
    ::EM.stop while ::EM.reactor_running? && sleep( 0.1 )
rescue
    nil
end

#kill_many(pids) ⇒ Object

Parameters:

  • pids (Array<Integer>)

    PIDs of the process to #kill.



64
65
66
# File 'lib/arachni/processes/manager.rb', line 64

def kill_many( pids )
    pids.each { |pid| kill pid }
end

#killallObject

Kills all processes.



69
70
71
72
# File 'lib/arachni/processes/manager.rb', line 69

def killall
    kill_many @pids.dup
    @pids.clear
end

#preserve_outputObject

Overrides the default setting of discarding process outputs.



94
95
96
# File 'lib/arachni/processes/manager.rb', line 94

def preserve_output
    @discard_output = false
end

#quiet_fork(&block) ⇒ Object

Parameters:

  • block (Block)

    Block to fork and discard its output.



82
83
84
# File 'lib/arachni/processes/manager.rb', line 82

def quiet_fork( &block )
    self << fork( &discard_output( &block ) )
end