Module: Pazuzu::Utility::ProcessSpawning

Defined in:
lib/pazuzu/utility/process_spawning.rb

Class Method Summary collapse

Class Method Details

.prepare_child_process!Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pazuzu/utility/process_spawning.rb', line 6

def self.prepare_child_process!
  # Close file handles
  3.upto(255) do |n|
    File.for_fd(n, 'r').close rescue nil
  end

  # Reopen standard file handles
  $stdin.reopen("/dev/null", 'r')
  $stdout.reopen("/dev/null", 'w')
  $stderr.reopen("/dev/null", 'w')

  # Ignore SIGHUP
  Signal.trap("HUP") { }
end

.set_user_and_group!(user, group) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pazuzu/utility/process_spawning.rb', line 21

def self.set_user_and_group!(user, group)
  current_uid, current_gid = Process.euid, Process.egid

  new_uid = Etc.getpwnam(user).uid if user
  new_uid ||= current_uid
  new_gid = Etc.getgrnam(group).gid if group
  new_gid ||= current_gid
  if new_uid != current_uid or new_gid != current_gid
    user_name = user
    user_name ||= Etc.getpwnam(new_uid).name
    Process.initgroups(user_name, new_gid)
    Process::GID.change_privilege(new_gid)
    Process::UID.change_privilege(new_uid)
  end
end