Skip to content

Commit

Permalink
Force the monitor thread to close the rsock
Browse files Browse the repository at this point in the history
This means that if the rsock is closed from another thread like a DIO
handler that the monitor thread can ensure that it has processed all
data before closing it.
  • Loading branch information
zeroSteiner committed Jan 25, 2022
1 parent e6d9c93 commit ecb278f
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions lib/rex/io/socket_abstraction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,36 @@ def localinfo
#
attr_reader :rsock

module MonitoredRSock
def close
@close_requested = true
@monitor_thread.join
nil
end

def sysclose
self.class.instance_method(:close).bind(self).call
end

attr_reader :close_requested
attr_writer :monitor_thread
end

protected

def monitor_rsock(threadname = 'SocketMonitorRemote')
self.monitor_thread = Rex::ThreadFactory.spawn(threadname, false) do
rsock.extend(MonitoredRSock)
rsock.monitor_thread = self.monitor_thread = Rex::ThreadFactory.spawn(threadname, false) do
loop do
closed = false
buf = nil
closed = rsock.nil? || rsock.close_requested

unless rsock
wlog('monitor_rsock: the remote socket is nil, exiting loop')
if closed
wlog('monitor_rsock: the remote socket has been closed, exiting loop')
break
end

buf = nil

begin
s = Rex::ThreadSafe.select([rsock], nil, nil, 0.2)
next if s.nil? || s[0].nil?
Expand Down Expand Up @@ -187,12 +204,16 @@ def monitor_rsock(threadname = 'SocketMonitorRemote')
close_write if respond_to?('close_write')
rescue StandardError
end

break
end

rsock.sysclose
end
end

attr_accessor :monitor_thread
attr_writer :lsock, :rsock
end
end; end
end
end

0 comments on commit ecb278f

Please sign in to comment.