Class: Msf::ThreadManager

Inherits:
Array
  • Object
show all
Includes:
Framework::Offspring
Defined in:
lib/msf/core/thread_manager.rb

Overview

This class manages the threads spawned by the framework object, this provides some additional features over standard ruby threads.

Instance Attribute Summary collapse

Attributes included from Framework::Offspring

#framework

Instance Method Summary collapse

Constructor Details

#initialize(framework) ⇒ ThreadManager

Initializes the thread manager.


56
57
58
59
# File 'lib/msf/core/thread_manager.rb', line 56

def initialize(framework)
  self.framework = framework
  self.monitor   = spawn_monitor
end

Instance Attribute Details

#monitorObject

Returns the value of attribute monitor


51
52
53
# File 'lib/msf/core/thread_manager.rb', line 51

def monitor
  @monitor
end

Instance Method Details

#kill(idx) ⇒ Object

Kills a thread by index


176
177
178
# File 'lib/msf/core/thread_manager.rb', line 176

def kill(idx)
  self[idx].kill rescue false
end

#register(t, name, crit) ⇒ Object

Registers an existing thread


140
141
142
143
144
145
146
147
# File 'lib/msf/core/thread_manager.rb', line 140

def register(t, name, crit)
  t[:tm_name] = name
  t[:tm_crit] = crit
  t[:tm_call] = caller
  t[:tm_time] = Time.now
  self << t
  t
end

#spawn(name, crit, *args, &block) ⇒ Object

Spawns a new thread


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/msf/core/thread_manager.rb', line 89

def spawn(name, crit, *args, &block)
  t = nil

  if block
    t = ::Thread.new(name, crit, caller, block, *args) do |*argv|
      ::Thread.current[:tm_name] = argv.shift.to_s
      ::Thread.current[:tm_crit] = argv.shift
      ::Thread.current[:tm_call] = argv.shift
      ::Thread.current[:tm_time] = Time.now

      begin
        argv.shift.call(*argv)
      rescue ::Exception => e
        elog("thread exception: #{::Thread.current[:tm_name]}  critical=#{::Thread.current[:tm_crit]}  error:#{e.class} #{e} source:#{::Thread.current[:tm_call].inspect}")
        elog("Call Stack\n#{e.backtrace.join("\n")}")
        raise e
      ensure
        if framework.db and framework.db.active
          # NOTE: despite the Deprecation Warning's advice, this should *NOT*
          # be ActiveRecord::Base.connection.close which causes unrelated
          # threads to raise ActiveRecord::StatementInvalid exceptions at
          # some point in the future, presumably due to the pool manager
          # believing that the connection is still usable and handing it out
          # to another thread.
          ::ActiveRecord::Base.connection_pool.release_connection
        end
      end
    end
  else
    t = ::Thread.new(name, crit, caller, *args) do |*argv|
      ::Thread.current[:tm_name] = argv.shift
      ::Thread.current[:tm_crit] = argv.shift
      ::Thread.current[:tm_call] = argv.shift
      ::Thread.current[:tm_time] = Time.now
      # Calling spawn without a block means we cannot force a database
      # connection release when the thread completes, so doing so can
      # potentially use up all database resources and starve all subsequent
      # threads that make use of the database. Log a warning so we can track
      # down this kind of usage.
      dlog("Thread spawned without a block!")
      dlog("Call stack: \n#{::Thread.current[:tm_call].join("\n")}")
    end
  end

  self << t
  t
end

#spawn_monitorObject

Spawns a monitor thread for removing dead threads


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/msf/core/thread_manager.rb', line 64

def spawn_monitor
  ::Thread.new do
    begin

    ::Thread.current[:tm_name] = "Thread Monitor"
    ::Thread.current[:tm_crit] = true

    while true
      ::IO.select(nil, nil, nil, 1.0)
      self.each_index do |i|
        state = self[i].alive? rescue false
        self[i] = nil if not state
      end
      self.delete(nil)
    end

    rescue ::Exception => e
      elog("thread monitor: #{e} #{e.backtrace} source:#{self[:tm_call].inspect}")
    end
  end
end

#update(ut, name, crit) ⇒ Object

Updates an existing thread


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/msf/core/thread_manager.rb', line 152

def update(ut, name, crit)
  ti = nil
  self.each_index do |i|
    tt = self[i]
    next if not tt
    if ut.__id__ == tt.__id__
      ti = i
      break
    end
  end

  t = self[ti]
  if not t
    raise RuntimeError, "Thread not found"
  end

  t[:tm_name] = name
  t[:tm_crit] = crit
  t
end