Class: RealUser::ProcFs

Inherits:
Object
  • Object
show all
Defined in:
lib/realuser.rb

Overview

The `ProcFs` class interacts with the `/proc` filesystem to fetch information about the real user ID (RUID) and parent process ID (PPID) of a given process.

Class Method Summary collapse

Class Method Details

.ppid(pid = Process.pid) ⇒ Integer?

Retrieves the parent process ID (PPID) of the process specified by `pid`. Defaults to the current process ID if none is provided.

Parameters:

  • pid (Integer) (defaults to: Process.pid)

    the process ID for which to retrieve the PPID. Defaults to `Process.pid`.

Returns:

  • (Integer, nil)

    the parent process ID of the process, or `nil` if it cannot be determined.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/realuser.rb', line 56

def self.ppid(pid = Process.pid)
  @ppid_cache ||= {}
  key = pid.to_s.to_sym

  result = @ppid_cache[key]
  return result if result

  result = File.read("/proc/#{pid}/status").match(/^PPid:\s+(\d+)/)[1].to_i
  @ppid_cache[key] = result
  result
rescue
  @ppid_cache[key] = nil
  nil
end

.ruid(pid = Process.pid) ⇒ Integer?

Retrieves the real user ID (RUID) of the process specified by `pid`. Defaults to the current process ID if none is provided.

Parameters:

  • pid (Integer) (defaults to: Process.pid)

    the process ID for which to retrieve the RUID. Defaults to `Process.pid`.

Returns:

  • (Integer, nil)

    the real user ID of the process, or `nil` if it cannot be determined.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/realuser.rb', line 36

def self.ruid(pid = Process.pid)
  @ruid_cache ||= {}
  key = pid.to_s.to_sym

  result = @ruid_cache[key]
  return result if result

  result = File.stat("/proc/#{pid}").uid
  @ruid_cache[key] = result
  result
rescue
  @ruid_cache[key] = nil
  nil
end