Class: RealUser::Resolver

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

Overview

The ‘Resolver` class provides methods to resolve the real user ID (RUID) of a process and its parent processes, either through a deep search (resolving the root RUID) or a shallow search.

Class Method Summary collapse

Class Method Details

.deep(pid) ⇒ Integer?

Resolves the real user ID (RUID) of the root process ancestor of the given ‘pid`. It recursively traverses the parent processes until it finds the root process.

Parameters:

  • pid (Integer)

    the process ID for which to resolve the root RUID.

Returns:

  • (Integer, nil)

    the root RUID of the process, or ‘nil` if it cannot be determined.



81
82
83
84
85
86
87
88
89
90
# File 'lib/realuser.rb', line 81

def self.deep(pid)
  ppid = ProcFs.ppid(pid)

  # has parent and parent is not init
  if ppid && ppid > 1
    deep(ppid)
  else
    ProcFs.ruid(pid)
  end
end

.shallow(pid, pruid = nil) ⇒ Integer?

Resolves the real user ID (RUID) of the given ‘pid` or its nearest ancestor where the RUID differs from the process’s parent RUID.

Parameters:

  • pid (Integer)

    the process ID for which to resolve the shallow RUID.

  • pruid (Integer, nil) (defaults to: nil)

    the parent process’s RUID. Used to compare and stop if a difference is found.

Returns:

  • (Integer, nil)

    the RUID of the process or the nearest ancestor with a different RUID.



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/realuser.rb', line 98

def self.shallow(pid, pruid = nil)
  ruid = ProcFs.ruid(pid)
  return ruid if pruid && (ruid != pruid || ruid.nil?)

  ppid = ProcFs.ppid(pid)

  # has parent and parent is not init
  if ppid && ppid > 1
    shallow(ppid, ruid)
  else
    ruid
  end
end