Method: Open4.open4

Defined in:
lib/crazy_ivan/vendor/open4-1.0.1/lib/open4.rb

.open4Object

–}}}



79
80
81
82
83
84
85
86
87
88
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
136
137
138
139
140
141
142
143
144
# File 'lib/crazy_ivan/vendor/open4-1.0.1/lib/open4.rb', line 79

def popen4(*cmd, &b)
#--{{{
  pw, pr, pe, ps = IO.pipe, IO.pipe, IO.pipe, IO.pipe

  verbose = $VERBOSE
  begin
    $VERBOSE = nil
    ps.first.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
    ps.last.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)

    cid = fork {
      pw.last.close
      STDIN.reopen pw.first
      pw.first.close

      pr.first.close
      STDOUT.reopen pr.last
      pr.last.close

      pe.first.close
      STDERR.reopen pe.last
      pe.last.close

      STDOUT.sync = STDERR.sync = true

      begin
        exec(*cmd)
        raise 'forty-two' 
      rescue Exception => e
        Marshal.dump(e, ps.last)
        ps.last.flush
      end
      ps.last.close unless (ps.last.closed?)
      exit!
    }
  ensure
    $VERBOSE = verbose
  end

  [pw.first, pr.last, pe.last, ps.last].each{|fd| fd.close}

  begin
    e = Marshal.load ps.first
    raise(Exception === e ? e : "unknown failure!")
  rescue EOFError # If we get an EOF error, then the exec was successful
    42
  ensure
    ps.first.close
  end

  pw.last.sync = true

  pi = [pw.last, pr.first, pe.first]

  if b 
    begin
      b[cid, *pi]
      Process.waitpid2(cid).last
    ensure
      pi.each{|fd| fd.close unless fd.closed?}
    end
  else
    [cid, pw.last, pr.first, pe.first]
  end
#--}}}
end