Class: SafeExec::PipeExecutor

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

Direct Known Subclasses

Executor

Defined Under Namespace

Classes: TimeoutDelegate

Constant Summary collapse

PAGE_SIZE =
4096

Instance Method Summary collapse

Constructor Details

#initialize(stdin, stdout, stderr) ⇒ PipeExecutor

Returns a new instance of PipeExecutor.



11
12
13
14
# File 'lib/safe_exec/pipe_executor.rb', line 11

def initialize(stdin, stdout, stderr)
  @stdin, @stdout, @stderr = stdin, stdout, stderr
  @mutex = Mutex.new
end

Instance Method Details

#run(cmd, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/safe_exec/pipe_executor.rb', line 16

def run(cmd, *args)
  assert_untainted_command(cmd, *args)
  @mutex.synchronize do
    Open3.popen3([cmd, cmd], *args) do |stdin, stdout, stderr, wait_thr|
      @threads = [
        pusher(@stdin, stdin),
        drainer(stdout, @stdout),
        drainer(stderr, @stderr)
      ]
      @threads.each { |t| t.abort_on_exception = true }

      yield wait_thr if block_given?

      wait_thr.value.tap { @threads.each { |t| t.join } }
    end
  end
end

#timeout(seconds, exception = Timeout::Error) ⇒ Object



34
35
36
# File 'lib/safe_exec/pipe_executor.rb', line 34

def timeout(seconds, exception = Timeout::Error)
  TimeoutDelegate.new(self, seconds, exception)
end