Class: CurrentProcess

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

Class Method Summary collapse

Class Method Details

.change_privilege(user, group = user) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/daemons/change_privilege.rb', line 4

def self.change_privilege(user, group = user)
  puts "Changing process privilege to #{user}:#{group}"

  uid, gid = Process.euid, Process.egid
  target_uid = Etc.getpwnam(user).uid
  target_gid = Etc.getgrnam(group).gid

  if uid != target_uid || gid != target_gid
    Process.initgroups(user, target_gid)
    Process::GID.change_privilege(target_gid)
    Process::UID.change_privilege(target_uid)
  end
rescue Errno::EPERM => e
  raise "Couldn't change user and group to #{user}:#{group}: #{e}"
end