Sha256: 1b1432344681122cb1428e4f897c960cb68c4c3e91b2cf47c65bb1a4f5f81262

Contents?: true

Size: 781 Bytes

Versions: 44

Compression:

Stored size: 781 Bytes

Contents

module Open3
  extend self

  def popen3(*cmd)
    pw = IO::pipe   # pipe[0] for read, pipe[1] for write
    pr = IO::pipe
    pe = IO::pipe

    pid = fork{
      # child
      fork{
        # grandchild
        pw[1].close
        STDIN.reopen(pw[0])
        pw[0].close

        pr[0].close
        STDOUT.reopen(pr[1])
        pr[1].close

        pe[0].close
        STDERR.reopen(pe[1])
        pe[1].close

        exec(*cmd)
      }
      exit!(0)
    }

    pw[0].close
    pr[1].close
    pe[1].close
    Process.waitpid(pid)
    pi = [pw[1], pr[0], pe[0]]
    pw[1].sync = true
    if defined? yield
      begin
        return yield(*pi)
      ensure
        Process.detach(pid) if pid
        pi.each { |p| p.close unless p.closed? }
      end
    end
    pi
  end
end

Version data entries

44 entries across 39 versions & 15 rubygems

Version Path
boof-grit-1.1.2 lib/open3_detach.rb
davetron5000-grit-1.1.2 lib/open3_detach.rb
davetron5000-grit-1.1.3 lib/open3_detach.rb
joelmoss-grit-1.1.4 lib/open3_detach.rb
joelmoss-grit-1.1.5 lib/open3_detach.rb
joelmoss-grit-1.1.6 lib/open3_detach.rb
kubicek-grit-1.0.3 lib/open3_detach.rb
kubicek-grit-1.1.1.1 lib/open3_detach.rb
kubicek-grit-1.1.1 lib/open3_detach.rb
mojombo-grit-0.9.4 lib/open3_detach.rb
mojombo-grit-1.1.1 lib/open3_detach.rb
robinluckey-grit-1.1.1 lib/open3_detach.rb
schacon-grit-0.9.4 lib/open3_detach.rb
schacon-grit-1.1.1 lib/open3_detach.rb
square-circle-triangle-grit-1.1.2 lib/open3_detach.rb
square-circle-triangle-grit-1.1.3 lib/open3_detach.rb
square-circle-triangle-grit-1.1.4 lib/open3_detach.rb
steveh-grit-1.1.1 lib/open3_detach.rb
tekkub-fugit-0.0.6 lib/grit/lib/open3_detach.rb
tekkub-fugit-0.0.7 lib/grit/lib/open3_detach.rb