Class: PosixPsutil::PsutilHelper::Processes

Inherits:
Object
  • Object
show all
Defined in:
lib/posixpsutil/linux/helper.rb

Overview

There is a module called Process in ruby. To distinguish from it, I name this one with ‘Processes`.

Direct Known Subclasses

PosixPsutil::PlatformSpecificProcess

Class Method Summary collapse

Class Method Details

.get_all_inodesObject

format: inodes= {

inode1 => [[pid, fd], [pid, fd], ...]
inode2...
}


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/posixpsutil/linux/helper.rb', line 51

def self.get_all_inodes()
  inodes = {}
  self.pids.each do |pid|
    begin
      inodes.merge!(self.get_proc_inodes(pid)){ |k, v1, v2| v1 + v2}
    rescue SystemCallError => e
      # Not Permission denied?
      raise unless [Errno::ENOENT::Errno, Errno::ESRCH::Errno, 
                    Errno::EPERM::Errno, Errno::EACCES::Errno].include?(e.errno)
    end
  end
  inodes
end

.get_proc_inodes(pid) ⇒ Object

be aware of Permission denied



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/posixpsutil/linux/helper.rb', line 26

def self.get_proc_inodes(pid)
  inodes = {}
  Dir.entries("/proc/#{pid}/fd").each do |fd|
    next if fd == '.' || fd == '..'
    # get actual path
    begin
      inode = File.readlink("/proc/#{pid}/fd/#{fd}")
      # check if it is a socket
      if inode.start_with? 'socket:['
        inode = inode[8...-1].to_i
        inodes[inode] ||= []
        inodes[inode].push([pid, fd.to_i]) 
      end
    rescue SystemCallError
      next
    end
  end
  inodes
end

.pidsObject

Returns a list of PIDs currently running on the system.



21
22
23
# File 'lib/posixpsutil/linux/helper.rb', line 21

def self.pids()
  Dir.entries('/proc').map(&:to_i).delete_if {|x| x == 0}
end