Module: Open4

Defined in:
lib/alib-0.5.1/open4.rb,
lib/alib-0.5.1/open4-0.9.1.rb

Defined Under Namespace

Classes: Error, SpawnError, ThreadEnsemble

Constant Summary collapse

VERSION =

–{{{

'0.9.1'

Class Method Summary collapse

Class Method Details

.alive(pid) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
# File 'lib/alib-0.5.1/open4.rb', line 362

def alive pid
#--{{{
  pid = Integer pid
  begin
    Process.kill 0, pid
    true
  rescue Errno::ESRCH
    false
  end
#--}}}
end

.alive?Object

–}}}



373
374
375
376
377
378
379
380
381
382
383
# File 'lib/alib-0.5.1/open4.rb', line 373

def alive pid
#--{{{
  pid = Integer pid
  begin
    Process.kill 0, pid
    true
  rescue Errno::ESRCH
    false
  end
#--}}}
end

.background(arg, *argv) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/alib-0.5.1/open4.rb', line 319

def background arg, *argv 
#--{{{
  require 'thread'
  q = Queue.new
  opts['pid'] = opts[:pid] = q
  thread = Thread.new(arg, argv){|arg, argv| spawn arg, *argv}
  pid = q.pop
  sc = class << thread; self; end
  sc.module_eval {
    define_method(:pid){ pid }
    define_method(:spawn_status){ @spawn_status ||= value }
    define_method(:exitstatus){ spawn_status.exitstatus }
  }
  thread
#--}}}
end

.bgObject

–}}}



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/alib-0.5.1/open4.rb', line 335

def background arg, *argv 
#--{{{
  require 'thread'
  q = Queue.new
  opts['pid'] = opts[:pid] = q
  thread = Thread.new(arg, argv){|arg, argv| spawn arg, *argv}
  pid = q.pop
  sc = class << thread; self; end
  sc.module_eval {
    define_method(:pid){ pid }
    define_method(:spawn_status){ @spawn_status ||= value }
    define_method(:exitstatus){ spawn_status.exitstatus }
  }
  thread
#--}}}
end

.getopts(opts = {}) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/alib-0.5.1/open4.rb', line 177

def getopts opts = {}
#--{{{
  lambda do |*args|
    keys, default, ignored = args
    catch('opt') do
      [keys].flatten.each do |key|
        [key, key.to_s, key.to_s.intern].each do |key|
          throw 'opt', opts[key] if opts.has_key?(key)
        end
      end
      default
    end
  end
#--}}}
end

.maim(pid, opts = {}) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/alib-0.5.1/open4.rb', line 339

def maim pid, opts = {}
#--{{{
  getopt = getopts opts
  sigs = getopt[ 'signals', %w(SIGTERM SIGQUIT SIGKILL) ]
  suspend = getopt[ 'suspend', 4 ]
  pid = Integer pid
  existed = false
  sigs.each do |sig|
    begin
      Process.kill sig, pid
      existed = true 
    rescue Errno::ESRCH
      return(existed ? nil : true)
    end
    return true unless alive? pid
    sleep suspend
    return true unless alive? pid
  end
  return(not alive?(pid)) 
#--}}}
end

.new_thread(*a, &b) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/alib-0.5.1/open4.rb', line 163

def new_thread *a, &b
#--{{{
  cur = Thread.current
  Thread.new(*a) do |*a|
    begin
      b[*a]
    rescue Exception => e
      cur.raise e
    end
  end
#--}}}
end

.open4Object

–}}}



75
76
77
78
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
# File 'lib/alib-0.5.1/open4.rb', line 75

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

  verbose = $VERBOSE
  begin
    $VERBOSE = nil
    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
  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

.popen4(*cmd, &b) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/alib-0.5.1/open4.rb', line 12

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

  verbose = $VERBOSE
  begin
    $VERBOSE = nil
    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
  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

.relay(src, dst = nil, t = nil) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/alib-0.5.1/open4.rb', line 194

def relay src, dst = nil, t = nil
#--{{{
  unless src.nil?
    if src.respond_to? :gets
      while buf = to(t){ src.gets }
        dst << buf if dst
      end

    elsif src.respond_to? :each
      q = Queue.new
      th = nil

      timer_set = lambda do |t|
        th = new_thread{ to(t){ q.pop } }
      end

      timer_cancel = lambda do |t|
        th.kill if th rescue nil
      end

      timer_set[t]
      begin
        src.each do |buf|
          timer_cancel[t]
          dst << buf if dst
          timer_set[t]
        end
      ensure
        timer_cancel[t]
      end

    elsif src.respond_to? :read
      buf = to(t){ src.read }
      dst << buf if dst 

    else
      buf = to(t){ src.to_s }
      dst << buf if dst 
    end
  end
#--}}}
end

.spawn(arg, *argv) ⇒ Object

Raises:



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/alib-0.5.1/open4.rb', line 238

def spawn arg, *argv 
#--{{{
  argv.unshift(arg)
  opts = ((argv.size > 1 and Hash === argv.last) ? argv.pop : {})
  argv.flatten!
  cmd = argv.join(' ')


  getopt = getopts opts

  ignore_exit_failure = getopt[ 'ignore_exit_failure', getopt['quiet', false] ]
  ignore_exec_failure = getopt[ 'ignore_exec_failure', !getopt['raise', true] ]
  exitstatus = getopt[ %w( exitstatus exit_status status ) ]
  stdin = getopt[ %w( stdin in i 0 ) << 0 ]
  stdout = getopt[ %w( stdout out o 1 ) << 1 ]
  stderr = getopt[ %w( stderr err e 2 ) << 2 ]
  pid = getopt[ 'pid' ]
  timeout = getopt[ %w( timeout spawn_timeout ) ]
  stdin_timeout = getopt[ %w( stdin_timeout ) ]
  stdout_timeout = getopt[ %w( stdout_timeout io_timeout ) ]
  stderr_timeout = getopt[ %w( stderr_timeout ) ]
  status = getopt[ %w( status ) ]
  cwd = getopt[ %w( cwd dir ), Dir.pwd ]

  exitstatus =
    case exitstatus
      when TrueClass, FalseClass
        ignore_exit_failure = true if exitstatus
        [0]
      else
        [*(exitstatus || 0)].map{|i| Integer i}
    end

  stdin ||= '' if stdin_timeout
  stdout ||= '' if stdout_timeout
  stderr ||= '' if stderr_timeout

  started = false

  status =
    begin
      Dir.chdir(cwd) do
        Timeout::timeout(timeout) do
          popen4(*argv) do |c, i, o, e|
            started = true

            %w( replace pid= << push update ).each do |msg|
              break(pid.send(msg, c)) if pid.respond_to? msg 
            end

            te = ThreadEnsemble.new c

            te.add_thread(i, stdin) do |i, stdin|
              relay stdin, i, stdin_timeout
              i.close rescue nil
            end

            te.add_thread(o, stdout) do |o, stdout|
              relay o, stdout, stdout_timeout
            end

            te.add_thread(e, stderr) do |o, stderr|
              relay e, stderr, stderr_timeout
            end

            te.run
          end
        end
      end
    rescue
      raise unless(not started and ignore_exec_failure)
    end

  raise SpawnError.new(cmd, status) unless
    (ignore_exit_failure or (status.nil? and ignore_exec_failure) or exitstatus.include?(status.exitstatus))

  status
#--}}}
end

.to(timeout = nil) ⇒ Object



156
157
158
159
160
# File 'lib/alib-0.5.1/open4.rb', line 156

def to timeout = nil
#--{{{
  Timeout.timeout(timeout){ yield }
#--}}}
end

.versionObject



8
# File 'lib/alib-0.5.1/open4.rb', line 8

def self.version() VERSION end