Class: OsCtl::Lib::KernelKeyring
- Inherits:
-
Object
- Object
- OsCtl::Lib::KernelKeyring
- Defined in:
- lib/libosctl/kernel_keyring.rb
Overview
Provides access to usage info of the kernel keyring
Defined Under Namespace
Classes: KeyUser
Instance Method Summary collapse
-
#each {|| ... } ⇒ Object
Iterate over keyring users.
-
#for_id_map(id_map) ⇒ Array<KeyUser>
Get key users whose IDs are included in a map.
-
#for_uid(uid) ⇒ KeyUser?
Get key user by user ID.
-
#initialize(key_users: '/proc/key-users') ⇒ KernelKeyring
constructor
A new instance of KernelKeyring.
-
#maxbytes ⇒ Integer
System-wide maximum number of bytes.
-
#maxkeys ⇒ Integer
System-wide maximum number of keys.
- #parse_key_users(path) ⇒ Object protected
-
#sort ⇒ Array<KeyUser>
Get key users sorted by the number of owned keys.
Constructor Details
#initialize(key_users: '/proc/key-users') ⇒ KernelKeyring
Returns a new instance of KernelKeyring.
31 32 33 |
# File 'lib/libosctl/kernel_keyring.rb', line 31 def initialize(key_users: '/proc/key-users') @key_users = parse_key_users(key_users) end |
Instance Method Details
#each {|| ... } ⇒ Object
Iterate over keyring users
69 70 71 |
# File 'lib/libosctl/kernel_keyring.rb', line 69 def each(&) @key_users.each_value(&) end |
#for_id_map(id_map) ⇒ Array<KeyUser>
Get key users whose IDs are included in a map
45 46 47 |
# File 'lib/libosctl/kernel_keyring.rb', line 45 def for_id_map(id_map) @key_users.each_value.select { |ku| id_map.include_host_id?(ku.uid) } end |
#for_uid(uid) ⇒ KeyUser?
Get key user by user ID
38 39 40 |
# File 'lib/libosctl/kernel_keyring.rb', line 38 def for_uid(uid) @key_users[uid] end |
#maxbytes ⇒ Integer
System-wide maximum number of bytes
63 64 65 |
# File 'lib/libosctl/kernel_keyring.rb', line 63 def maxbytes @maxbytes ||= File.read('/proc/sys/kernel/keys/maxbytes').strip.to_i end |
#maxkeys ⇒ Integer
System-wide maximum number of keys
57 58 59 |
# File 'lib/libosctl/kernel_keyring.rb', line 57 def maxkeys @maxkeys ||= File.read('/proc/sys/kernel/keys/maxkeys').strip.to_i end |
#parse_key_users(path) ⇒ Object (protected)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/libosctl/kernel_keyring.rb', line 75 def parse_key_users(path) ret = {} # See man keyrings(7) for the file format File.open(path) do |f| f.each_line do |line| uid, usage, total_keys, owned_keys, owned_bytes = line.split uid_int = uid[0..-2].to_i nkeys, nikeys = total_keys.split('/') qnkeys, maxkeys = owned_keys.split('/') qnbytes, maxbytes = owned_bytes.split('/') ret[uid_int] = KeyUser.new( uid_int, usage.to_i, nkeys.to_i, nikeys.to_i, qnkeys.to_i, maxkeys.to_i, qnbytes.to_i, maxbytes.to_i ) end end ret end |
#sort ⇒ Array<KeyUser>
Get key users sorted by the number of owned keys
51 52 53 |
# File 'lib/libosctl/kernel_keyring.rb', line 51 def sort @key_users.values.sort { |a, b| a.qnkeys <=> b.qnkeys } end |