Module: Isono::Runner::Daemonize

Defined in:
lib/isono/runner/base.rb

Class Method Summary collapse

Class Method Details

.change_privilege(user, group = user) ⇒ Object

Change privileges of the process to the specified user and group.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/isono/runner/base.rb', line 16

def self.change_privilege(user, group=user)
  logger.info("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
    # Change process ownership
    Process.initgroups(user, target_gid)
    Process::GID.change_privilege(target_gid)
    Process::UID.change_privilege(target_uid)
  end
rescue Errno::EPERM => e
  logger.error("Couldn't change user and group to #{user}:#{group}: #{e}")
end

.daemonize(log_io = STDOUT) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/isono/runner/base.rb', line 33

def self.daemonize(log_io=STDOUT)
  exit if fork
  srand
  trap 'SIGHUP', 'DEFAULT'
  
  STDIN.reopen('/dev/null')
  
  STDOUT.reopen(log_io)
  STDERR.reopen(log_io)
end