Module: LinuxStat::User
- Defined in:
- lib/linux_stat/user.rb
Overview
Shows various User related information of the current system.
Class Method Summary collapse
-
.get_euid ⇒ Object
Returns the effective user ID as integer.
-
.get_gid ⇒ Object
Returns the group ID as integer.
-
.get_login ⇒ Object
Calls LinuxStat::Sysconf.get_login().
-
.get_uid ⇒ Object
Returns the user ID as integer.
-
.get_user ⇒ Object
(also: get_current_user)
Returns the user ID as integer.
-
.gid_by_username(username = get_user) ⇒ Object
gid_by_username(username = get_user).
-
.gids ⇒ Object
Returns all the Group identifiers as Hash.
-
.home_by_gid(id = get_gid) ⇒ Object
home_by_gid(id = get_uid).
-
.home_by_username(user = get_user) ⇒ Object
home_by_username(user = get_user).
-
.home_directories ⇒ Object
Returns all the home directories as Hash.
-
.homes_by_uid(id = get_uid) ⇒ Object
home_by_uid(id = get_uid).
-
.ids ⇒ Object
Returns all the Group ids directories as Hash.
-
.list ⇒ Object
Returns an array of users as string.
-
.uid_by_username(username = get_user) ⇒ Object
uid_by_username(username = get_user).
-
.uids ⇒ Object
Returns all the user IDs as Hash.
-
.username_by_gid(gid = get_gid) ⇒ Object
username_by_gid(gid = get_gid).
-
.usernames_by_uid(uid = get_uid) ⇒ Object
def usernames_by_uid(gid = get_uid).
Class Method Details
.get_euid ⇒ Object
Returns the effective user ID as integer
It directly calls LinuxStat::Sysconf.get_euid
146 147 148 |
# File 'lib/linux_stat/user.rb', line 146 def get_euid LinuxStat::Sysconf.get_euid end |
.get_gid ⇒ Object
Returns the group ID as integer
It directly calls LinuxStat::Sysconf.get_uid
138 139 140 |
# File 'lib/linux_stat/user.rb', line 138 def get_gid LinuxStat::Sysconf.get_gid end |
.get_login ⇒ Object
Calls LinuxStat::Sysconf.get_login()
The username is returned as a String.
It doesn’t get affected by ENV
But if the name isn’t available (say inside a container), it will return an empty String.
This is meant for speed but not for reliability. To get more reliable output, you might try LinuxStat::User.get_user()
161 162 163 |
# File 'lib/linux_stat/user.rb', line 161 def get_login LinuxStat::Sysconf.get_login end |
.get_uid ⇒ Object
Returns the user ID as integer
It directly calls LinuxStat::Sysconf.get_uid
130 131 132 |
# File 'lib/linux_stat/user.rb', line 130 def get_uid LinuxStat::Sysconf.get_uid end |
.get_user ⇒ Object Also known as: get_current_user
Returns the user ID as integer
It directly calls LinuxStat::Sysconf.get_uid and LinuxStat::Sysconf.get_gid
and then reads /etc/passwd and matches the values with UID and GID.
It doesn’t get affected with the assignment of USER environment variable
If either /etc/passwd is readable or LinuxStat::Sysconf.get_login() is not empty.
But if /etc/passwd isn’t readable (which is weird), it will fall back to sysconf.h’s get_login() If that’s not available, like in docker, falls back to ENV.to_s
It should return the username under most robust circumstances. But if nothing is available for some reason, it will return an empty String.
Note that this is not cached or memoized so use this at your own processing expense.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/linux_stat/user.rb', line 107 def get_user unless passwd_readable? _l = LinuxStat::Sysconf.get_login().freeze return _l.empty? ? ENV['USER'.freeze].to_s : _l end uid, gid = LinuxStat::Sysconf.get_uid, LinuxStat::Sysconf.get_gid username = ''.freeze passwd.each { |x| splitted = x.split(?:).freeze if splitted[2].to_i == uid && splitted[3].to_i == gid username = splitted[0] break end } username end |
.gid_by_username(username = get_user) ⇒ Object
gid_by_username(username = get_user)
Where username is the username to look for, by default it is the current user.
It returns the gid by the username. For example:
LinuxStat::User.gid_by_username('root')
=> 0
The return type is Integer.
But if user passed doesn’t exist or if the info isn’t available, it will return nil.
224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/linux_stat/user.rb', line 224 def gid_by_username(username = get_user) return nil unless passwd_readable? gid = nil passwd.each do |x| splitted = x.split(?:.freeze) if splitted[0] == username gid = splitted[3].to_i break end end gid end |
.gids ⇒ Object
Returns all the Group identifiers as Hash.
For example:
LinuxStat::User.gids
=> {:root=>0, :bin=>1, :daemon=>2, :mail=>12, :ftp=>11}
But if the status isn’t available it will return an empty Hash.
62 63 64 65 66 67 68 69 |
# File 'lib/linux_stat/user.rb', line 62 def gids return {} unless passwd_readable? passwd_splitted.reduce({}) { |h, x| h.store(x[0].to_sym, x[3].to_i) h } end |
.home_by_gid(id = get_gid) ⇒ Object
home_by_gid(id = get_uid)
Gets the home of the user corresponding to the GID.
It returns a String in this format:
Assuming both the users share same UID.
If the info isn’t available, it will return an empty frozen String.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/linux_stat/user.rb', line 323 def home_by_gid(id = get_gid) return ''.freeze unless passwd_readable? home = ''.freeze passwd.each do |x| splitted = x.split(?:.freeze) if splitted[3].to_i == id home = splitted[5] break end end home end |
.home_by_username(user = get_user) ⇒ Object
home_by_username(user = get_user)
Where user is the name of the user.
Returns the user’s home. By default it returns the home of the current user.
If the info isn’t available, it will return ENV.to_s.freeze
275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/linux_stat/user.rb', line 275 def home_by_username(user = get_user) return ENV['HOME'].to_s.freeze unless passwd_readable? home = '' passwd.each { |x| splitted = x.split(?:) if splitted[0] == user home = splitted[5] break end } home end |
.home_directories ⇒ Object
Returns all the home directories as Hash.
For example:
LinuxStat::User.home_directories
=> {:root=>"/root", :bin=>"/", :daemon=>"/", :mail=>"/var/spool/mail", :ftp=>"/srv/ftp", :http=>"/srv/http", :nobody=>"/"}
But if the status isn’t available it will return an empty Hash.
80 81 82 83 84 85 86 87 |
# File 'lib/linux_stat/user.rb', line 80 def home_directories return {} unless passwd_readable? passwd.reduce({}) { |h, x| splitted = x.split(?:) h.store(splitted[0].to_sym, splitted[5]) h } end |
.homes_by_uid(id = get_uid) ⇒ Object
home_by_uid(id = get_uid)
Gets all the users home directory with user id.
It returns an Array in this format:
LinuxStat::User.homes_by_uid(1001)
=> ["/home/userx", "/home/usery"]
Assuming both the users share same UID.
If the info isn’t available, it will return an empty Array.
302 303 304 305 306 307 308 309 310 311 |
# File 'lib/linux_stat/user.rb', line 302 def homes_by_uid(id = get_uid) return [] unless passwd_readable? home = [] passwd.each do |x| splitted = x.split(?:.freeze) home << splitted[5] if splitted[2].to_i == id end home end |
.ids ⇒ Object
Returns all the Group ids directories as Hash.
For example:
{:root=>{:uid=>0, :gid=>0}, :bin=>{:uid=>1, :gid=>1}, :daemon=>{:uid=>2, :gid=>2}, :mail=>{:uid=>8, :gid=>12}, :ftp=>{:uid=>14, :gid=>11}}
But if the status isn’t available it will return an empty Hash.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/linux_stat/user.rb', line 25 def ids return {} unless passwd_readable? passwd_splitted.reduce({}) { |h, x| h.store(x[0].to_sym, { uid: x[2].to_i, gid: x[3].to_i }) h } end |
.list ⇒ Object
Returns an array of users as string
For example:
["root", "bin", "daemon", "mail", "ftp", "http", "nobody"]
But if the status isn’t available it will return an empty Array.
13 14 15 16 |
# File 'lib/linux_stat/user.rb', line 13 def list return [] unless passwd_readable? passwd.map { |x| x[/.+?:/][0..-2].freeze } end |
.uid_by_username(username = get_user) ⇒ Object
uid_by_username(username = get_user)
Where username is the username to look for, by default it is the current user.
It returns the uid by the username.
For example:
LinuxStat::User.uid_by_username('root')
=> 0
The return type is Integer.
But if user passed doesn’t exist or if the info isn’t available, it will return nil.
253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/linux_stat/user.rb', line 253 def uid_by_username(username = get_user) return nil unless passwd_readable? uid = nil passwd.each do |x| splitted = x.split(?:.freeze) if splitted[0] == username uid = splitted[2].to_i break end end uid end |
.uids ⇒ Object
Returns all the user IDs as Hash.
For example:
LinuxStat::User.uids
=> {:root=>0, :bin=>1, :daemon=>2, :mail=>8, :ftp=>14}
But if the status isn’t available it will return an empty Hash.
44 45 46 47 48 49 50 51 |
# File 'lib/linux_stat/user.rb', line 44 def uids return {} unless passwd_readable? passwd_splitted.reduce({}) { |h, x| h.store(x[0].to_sym, x[2].to_i) h } end |
.username_by_gid(gid = get_gid) ⇒ Object
username_by_gid(gid = get_gid)
Where gid is the group id of the user. By default it’s the gid of the current user.
It returns a String cotaining the username corresponding to the gid
But if the info isn’t available it will return an empty frozen String.
196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/linux_stat/user.rb', line 196 def username_by_gid(gid = get_gid) return ''.freeze unless passwd_readable? username = ''.freeze passwd.each do |x| splitted = x.split(?:.freeze) if splitted[2].to_i == gid username = splitted[0] break end end username end |
.usernames_by_uid(uid = get_uid) ⇒ Object
def usernames_by_uid(gid = get_uid)
Where uid is the group id of the user. By default it’s the uid of the current user.
It returns an Array containing the username corresponding to the uid.
For example:
LinuxStat::User.usernames_by_uid(1001)
=> ["userx", "usery"]
But if the info isn’t available it will return an empty Array.
178 179 180 181 182 183 184 185 186 |
# File 'lib/linux_stat/user.rb', line 178 def usernames_by_uid(uid = get_uid) return [] unless passwd_readable? usernames = [] passwd_splitted.each { |x| usernames << x[0] if x[2].to_i == uid } usernames end |