Skip to content

Commit 7ccdf0f

Browse files
author
Michael Hale
committed
only change privilege when starting process
1 parent 3cc1572 commit 7ccdf0f

File tree

4 files changed

+47
-37
lines changed

4 files changed

+47
-37
lines changed

lib/change_privilege.rb

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
base_dir = File.dirname(__FILE__)
2-
require File.join(base_dir, 'etc_patch')
1+
require File.join(File.dirname(__FILE__), 'etc_patch')
32

4-
def change_privilege(user, group=user)
5-
puts ">> Changing process privilege to #{user}:#{group}"
3+
class CurrentProcess
4+
def self.change_privilege(user, group=user)
5+
puts ">> Changing process privilege to #{user}:#{group}"
66

7-
uid, gid = Process.euid, Process.egid
8-
target_uid = Etc.getpwnam(user).uid
9-
target_gid = Etc.getgrnam(group).gid
7+
uid, gid = Process.euid, Process.egid
8+
target_uid = Etc.getpwnam(user).uid
9+
target_gid = Etc.getgrnam(group).gid
1010

11-
if uid != target_uid || gid != target_gid
12-
# Change process ownership
13-
Process.initgroups(user, target_gid)
14-
Process::GID.change_privilege(target_gid)
15-
Process::UID.change_privilege(target_uid)
11+
if uid != target_uid || gid != target_gid
12+
# Change process ownership
13+
Process.initgroups(user, target_gid)
14+
Process::GID.change_privilege(target_gid)
15+
Process::UID.change_privilege(target_uid)
16+
end
17+
rescue Errno::EPERM => e
18+
raise "Couldn't change user and group to #{user}:#{group}: #{e}"
1619
end
17-
rescue Errno::EPERM => e
18-
raise "Couldn't change user and group to #{user}:#{group}: #{e}"
19-
end
20-
21-
environment_stat = File.stat(File.join(base_dir, '..', 'config', 'environment.rb'))
22-
user = Etc.username(environment_stat.uid)
23-
group = Etc.groupname(environment_stat.gid)
24-
change_privilege(user, group)
20+
end

lib/daemons_patch.rb

+31-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
1-
#Tweak the daemons code so that we can specify the logdir seperately from the piddir
2-
module Daemons
3-
class Application
4-
def logdir
5-
logdir = options[:log_dir]
6-
unless logdir
7-
logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
8-
end
9-
logdir
1+
# Optionally specify the logdir seperately from the piddir
2+
class Daemons::Application
3+
def logdir
4+
logdir = options[:log_dir]
5+
unless logdir
6+
logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
107
end
8+
logdir
9+
end
1110

12-
def output_logfile
13-
(options[:log_output] && logdir) ? File.join(logdir, @group.app_name + '.output') : nil
14-
end
11+
def output_logfile
12+
(options[:log_output] && logdir) ? File.join(logdir, @group.app_name + '.output') : nil
13+
end
1514

16-
def logfile
17-
logdir ? File.join(logdir, @group.app_name + '.log') : nil
15+
def logfile
16+
logdir ? File.join(logdir, @group.app_name + '.log') : nil
17+
end
18+
end
19+
20+
require File.join(File.dirname(__FILE__), 'change_privilege')
21+
class Daemons::Application
22+
alias :old_initialize :initialize
23+
class_eval %{
24+
def initialize(*args, &block)
25+
old_initialize(*args, &block)
26+
change_privilege
1827
end
28+
}
29+
30+
def change_privilege
31+
environment_stat = File.stat(File.join(File.dirname(__FILE__), '..', 'config', 'environment.rb'))
32+
user = Etc.username(environment_stat.uid)
33+
group = Etc.groupname(environment_stat.gid)
34+
CurrentProcess.change_privilege(user, group)
1935
end
20-
end
36+
end

script/email_notifier

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ root = File.expand_path(File.dirname(__FILE__) + '/../')
44
require 'rubygems'
55
require 'daemons'
66
require root + '/lib/daemons_patch'
7-
require root + '/lib/change_privilege'
87

98
Daemons.run_proc(
109
"email_notifier",

script/rss_feed_fetcher_daemon

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ root = File.expand_path(File.dirname(__FILE__) + '/../')
44
require 'rubygems'
55
require 'daemons'
66
require root + '/lib/daemons_patch'
7-
require root + '/lib/change_privilege'
87

98
Daemons.run(
109
File.join(root, 'script', 'rss_feed_fetcher.rb'),

0 commit comments

Comments
 (0)