Module: Brush::Pipeline::POSIX

Included in:
Brush::Pipeline
Defined in:
lib/brush/pipeline.rb

Defined Under Namespace

Classes: ProcessInfo

Instance Method Summary collapse

Instance Method Details

#create_process(argv, options, close_pipes) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/brush/pipeline.rb', line 350

def create_process (argv, options, close_pipes)

  # The following is used for manual specification testing to verify
  # that pipes are correctly closed after fork.  This is extremely
  # difficult to write an RSpec test for, and it is only possible on
  # platforms that have a /proc filesystem anyway.  Regardless, this
  # will be moved into an RSpec test at some point.
  #
  #$stderr.puts "===== P #{$$}: #{ %x"ls -l /proc/#{$$}/fd" }"

  pid = fork do               # child process
    [:stdin, :stdout, :stderr].each do |io_sym|
      io = options[io_sym]
      if io != Kernel.const_get(io_sym.to_s.upcase)
        Kernel.const_get(io_sym.to_s.upcase).reopen(io)
        io.close
      end
    end

    close_pipes.each { |io| io.close }
    Brush::Pipeline::PARENT_PIPES.each_key { |io| io.close }

    # This is the second half of the manual specification testing
    # started above.  See comment above for more information.
    #
    #$stderr.puts "===== C #{$$}: #{ %x"ls -l /proc/#{$$}/fd" }"

    Dir.chdir(options[:cd]) do
      exec [options[:executable], argv[0]], *argv[1..-1]
    end

    raise Error, "failed to exec"
  end

  ProcessInfo.new(pid)
end

#find_in_path(name) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/brush/pipeline.rb', line 387

def find_in_path (name)
  if name.index(File::SEPARATOR)      # Path is absolute or relative.
    return File.expand_path(name)
  else
    each_path_element do |dir|
      chkname = nil
      return chkname if File.exists?(chkname = File.join(dir, name))
    end
  end

  nil                                 # Didn't find a match. :(
end

#sys_wait(process_info) ⇒ Object



345
346
347
348
# File 'lib/brush/pipeline.rb', line 345

def sys_wait (process_info)
  #system("ls -l /proc/#{$$}/fd /proc/#{process_info.process_id}/fd")
  Process.waitpid2(process_info.process_id)[1]
end