Module: ExecSandbox::Wait4

Defined in:
lib/exec_sandbox/wait4.rb

Overview

Interface to the wait4 system call using the ffi library.

Defined Under Namespace

Modules: LibC Classes: Rusage

Class Method Summary collapse

Class Method Details

.wait4(pid) ⇒ Hash

Waits for a process to end, and collects its exit status and resource usage.

Parameters:

  • pid (Fixnum)

    the PID of the process to wait for; should be a child of this process

Returns:

  • (Hash)

    exit code and resource usage information

Raises:

  • (SystemCallError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/exec_sandbox/wait4.rb', line 11

def self.wait4(pid)
  status_ptr = FFI::MemoryPointer.new :int
  rusage = ExecSandbox::Wait4::Rusage.new
  returned_pid = LibC.wait4(pid, status_ptr, 0, rusage.pointer)
  raise SystemCallError, FFI.errno if returned_pid < 0
  status = { bits: status_ptr.read_int }
  status_ptr.free

  signal_code = status[:bits] & 0x7f
  status[:exit_code] = (signal_code != 0) ? -signal_code : status[:bits] >> 8
  status[:user_time] = rusage[:ru_utime_sec] +
                       rusage[:ru_utime_usec] * 0.000_001
  status[:system_time] = rusage[:ru_stime_sec] +
                         rusage[:ru_stime_usec] * 0.000_001
  status[:rss] = rusage[:ru_maxrss] / 1024.0
  return status
end