Skip to content

Commit

Permalink
Pull in changes form mikehale.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Uehlinger committed May 15, 2010
1 parent a17e641 commit cf89686
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Releases
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
kill them automatically it if they do not stop within a given time
(force_kill_waittime). Use the option --no_wait to not wait for processes to
stop.
* Set log files mode to 0644 (mikehale).
* Set pid file permissions to 0644 (mikehale).
* Added ability to change process uid/gid (mikehale).

== Release 1.0.10: March 21, 2008

Expand Down
1 change: 1 addition & 0 deletions lib/daemons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'daemons/application_group'
require 'daemons/controller'

require 'timeout'

# All functions and classes that Daemons provides reside in this module.
#
Expand Down
20 changes: 17 additions & 3 deletions lib/daemons/application.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'daemons/pidfile'
require 'daemons/pidmem'
require 'daemons/change_privilege'

require 'timeout'

Expand Down Expand Up @@ -44,6 +45,12 @@ def initialize(group, add_options = {}, pid = nil)
end
end

def change_privilege
user = options[:user]
group = options[:group]
CurrentProcess.change_privilege(user, group) if user
end

def script
@script || @group.script
end
Expand All @@ -52,13 +59,19 @@ def pidfile_dir
Pid.dir(@dir_mode || @group.dir_mode, @dir || @group.dir, @script || @group.script)
end

def logdir
logdir = options[:log_dir]
unless logdir
logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
end
logdir
end

def output_logfile
logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
(options[:log_output] && logdir) ? File.join(logdir, @group.app_name + '.output') : nil
end

def logfile
logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
logdir ? File.join(logdir, @group.app_name + '.log') : nil
end

Expand Down Expand Up @@ -266,6 +279,7 @@ def start_proc


def start
change_privilege
@group.create_monitor(@group.applications[0] || self) unless options[:ontop] # we don't monitor applications in the foreground

case options[:mode]
Expand Down Expand Up @@ -364,7 +378,7 @@ def stop(no_wait = false)
begin
Process.kill(SIGNAL, pid)
rescue Errno::ESRCH => e
puts "#{e} #{@pid.pid}"
puts "#{e} #{pid}"
puts "deleting pid-file."
end

Expand Down
19 changes: 19 additions & 0 deletions lib/daemons/change_privilege.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'daemons/etc_extension'

class CurrentProcess
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
end
1 change: 1 addition & 0 deletions lib/daemons/daemonize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def redirect_io(logfile_name)
if logfile_name
begin
STDOUT.reopen logfile_name, "a"
File.chmod(0644, logfile_name)
STDOUT.sync = true
rescue ::Exception
begin; STDOUT.reopen "/dev/null"; rescue ::Exception; end
Expand Down
12 changes: 12 additions & 0 deletions lib/daemons/etc_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'etc'

Etc.instance_eval do
def groupname(gid)
Etc.group {|e| return e.name if gid == e.gid }
nil
end
def username(uid)
Etc.passwd {|e| return e.name if uid == e.uid }
nil
end
end
1 change: 1 addition & 0 deletions lib/daemons/pidfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def exist?

def pid=(p)
File.open(filename, 'w') {|f|
f.chmod(0644)
f.puts p #Process.pid
}
end
Expand Down

0 comments on commit cf89686

Please sign in to comment.