Class: HrrRbLxns::Files

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hrr_rb_lxns/files.rb,
lib/hrr_rb_lxns/files/file.rb

Overview

Represents namespace files information in /proc/PID/ns/ directory of a process.

Examples:

# Collects the caller process's or a specific process's namespace files information
files = HrrRbLxns::Files.new
files = HrrRbLxns::Files.new 12345

# Each namespace file is accessible as a method or a Hash-like key
files.uts.path    # => "/proc/12345/ns/uts"
files[:uts].path  # => "/proc/12345/ns/uts"
files["uts"].path # => "/proc/12345/ns/uts"

Defined Under Namespace

Classes: File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid = "self") ⇒ Files

Returns a new instance of Files.

Parameters:

  • pid (Integer, String) (defaults to: "self") —

    The pid of a process to collect namespace files information. If nil, assumes that it is the caller process.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hrr_rb_lxns/files.rb', line 79

def initialize pid="self"
  @mnt               = File.new "/proc/#{pid}/ns/mnt"
  @uts               = File.new "/proc/#{pid}/ns/uts"
  @ipc               = File.new "/proc/#{pid}/ns/ipc"
  @net               = File.new "/proc/#{pid}/ns/net"
  @pid               = File.new "/proc/#{pid}/ns/pid"
  @pid_for_children  = File.new "/proc/#{pid}/ns/pid_for_children"
  @user              = File.new "/proc/#{pid}/ns/user"
  @cgroup            = File.new "/proc/#{pid}/ns/cgroup"
  @time              = File.new "/proc/#{pid}/ns/time"
  @time_for_children = File.new "/proc/#{pid}/ns/time_for_children"
end

Instance Attribute Details

#cgroup ⇒ HrrRbLxns::Files::File (readonly)

Returns the file information of attribute :cgroup.



63
64
65
# File 'lib/hrr_rb_lxns/files.rb', line 63

def cgroup
  @cgroup
end

#ipc ⇒ HrrRbLxns::Files::File (readonly)

Returns the file information of attribute :ipc.



33
34
35
# File 'lib/hrr_rb_lxns/files.rb', line 33

def ipc
  @ipc
end

#mnt ⇒ HrrRbLxns::Files::File (readonly)

Returns the file information of attribute :mnt.



21
22
23
# File 'lib/hrr_rb_lxns/files.rb', line 21

def mnt
  @mnt
end

#net ⇒ HrrRbLxns::Files::File (readonly)

Returns the file information of attribute :net.



39
40
41
# File 'lib/hrr_rb_lxns/files.rb', line 39

def net
  @net
end

#pid ⇒ HrrRbLxns::Files::File (readonly)

Returns the file information of attribute :pid.



45
46
47
# File 'lib/hrr_rb_lxns/files.rb', line 45

def pid
  @pid
end

#pid_for_children ⇒ HrrRbLxns::Files::File (readonly)

Returns the file information of attribute :pid_for_children.



51
52
53
# File 'lib/hrr_rb_lxns/files.rb', line 51

def pid_for_children
  @pid_for_children
end

#time ⇒ HrrRbLxns::Files::File (readonly)

Returns the file information of attribute :time.



69
70
71
# File 'lib/hrr_rb_lxns/files.rb', line 69

def time
  @time
end

#time_for_children ⇒ HrrRbLxns::Files::File (readonly)

Returns the file information of attribute :time_for_children.



75
76
77
# File 'lib/hrr_rb_lxns/files.rb', line 75

def time_for_children
  @time_for_children
end

#user ⇒ HrrRbLxns::Files::File (readonly)

Returns the file information of attribute :user.



57
58
59
# File 'lib/hrr_rb_lxns/files.rb', line 57

def user
  @user
end

#uts ⇒ HrrRbLxns::Files::File (readonly)

Returns the file information of attribute :uts.



27
28
29
# File 'lib/hrr_rb_lxns/files.rb', line 27

def uts
  @uts
end

Instance Method Details

#[](key) ⇒ HrrRbLxns::Files::File

Returns the file information of the specified key.

Examples:

files = HrrRbLxns::Files.new 12345
files[:uts].path  # => "/proc/12345/ns/uts"
files["uts"].path # => "/proc/12345/ns/uts"

Parameters:

  • key (Symbol, String) —

    The namespace file name, which is :mnt, :uts, pid, :pid_for_children, ....

Returns:



102
103
104
# File 'lib/hrr_rb_lxns/files.rb', line 102

def [] key
  __send__ key
end

#each ⇒ Object

Calls the given block once for each key with associated file information. If no block is given, an Enumerator is returned.

Examples:

files = HrrRbLxns::Files.new 12345
files.each do |type, namespace_file|
  # at first iteration
  type                # => :cgroup
  namespace_file.path # => "/proc/12345/ns/cgroup"
end


117
118
119
120
121
122
123
124
125
126
# File 'lib/hrr_rb_lxns/files.rb', line 117

def each
  keys_with_files = [:mnt, :uts, :ipc, :net, :pid, :pid_for_children, :user, :cgroup, :time, :time_for_children].map{ |key| [key, __send__(key)] }
  if block_given?
    keys_with_files.each do |kv|
      yield kv
    end
  else
    keys_with_files.each
  end
end