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

Constant Summary collapse

WNOHANG =

Option passed to LibC::wait4.

1

Class Method Summary collapse

Class Method Details

._wait4(pid, options) ⇒ Object

Raises:

  • (SystemCallError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/exec_sandbox/wait4.rb', line 27

def _wait4(pid, options)
  status_ptr = FFI::MemoryPointer.new :int
  rusage = ExecSandbox::Wait4::Rusage.new
  returned_pid = LibC.wait4(pid, status_ptr, options, rusage.pointer)
  raise SystemCallError, FFI.errno if returned_pid < 0
  return nil 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

.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



11
12
13
# File 'lib/exec_sandbox/wait4.rb', line 11

def self.wait4(pid)
  _wait4 pid, 0
end

.wait4_nonblock(pid) ⇒ Hash

Collects a child process’ 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; nil if the process hasn’t terminated



21
22
23
# File 'lib/exec_sandbox/wait4.rb', line 21

def self.wait4_nonblock(pid)
  _wait4 pid, WNOHANG
end