def change_privilege(user, group)
begin
uid, gid = Process.euid, Process.egid
target_uid = Etc.getpwnam(user).uid if user
target_gid = Etc.getgrnam(group).gid if group
if uid != target_uid or gid != target_gid
log "Initiating groups for #{user.inspect}:#{group.inspect}."
Process.initgroups(user, target_gid)
log "Changing group to #{group.inspect}."
Process::GID.change_privilege(target_gid)
log "Changing user to #{user.inspect}."
Process::UID.change_privilege(target_uid)
end
rescue Errno::EPERM => e
log "Couldn't change user and group to #{user.inspect}:#{group.inspect}: #{e.to_s}."
log "Mongrel failed to start."
exit 1
end
end