lib/dante/runner.rb in dante-0.1.3 vs lib/dante/runner.rb in dante-0.1.4
- old
+ new
@@ -1,9 +1,10 @@
require 'fileutils'
require 'optparse'
require 'yaml'
require 'erb'
+require 'etc'
=begin
This is a utility for setting up a binary executable for a service.
@@ -51,12 +52,24 @@
if options.include?(:kill)
self.stop
else # create process
self.stop if options.include?(:restart)
- Process.euid = options[:user] if options[:user]
- Process.egid = options[:group] if options[:group]
+
+ # If a username, uid, groupname, or gid is passed,
+ # drop privileges accordingly.
+
+ if options[:group]
+ gid = options[:group].is_a?(Integer) ? options[:group] : Etc.getgrnam(options[:group]).gid
+ Process::GID.change_privilege(gid)
+ end
+
+ if options[:user]
+ uid = options[:user].is_a?(Integer) ? options[:user] : Etc.getpwnam(options[:user]).uid
+ Process::UID.change_privilege(uid)
+ end
+
@startup_command = block if block_given?
options[:daemonize] ? daemonize : start
end
end
@@ -113,12 +126,16 @@
self.stop
self.start
end
def interrupt
- raise Interrupt
- sleep(1)
+ # begin
+ raise Interrupt
+ sleep(1)
+ # rescue Interrupt
+ # log "Interrupt received; stopping #{@name}"
+ # end
end
# Returns true if process is not running
def daemon_stopped?
! self.daemon_running?
@@ -205,14 +222,14 @@
# Redirect output based on log settings (reopens stdout/stderr to specified logfile)
# If log_path is nil, redirect to /dev/null to quiet output
def redirect_output!
if log_path = options[:log_path]
FileUtils.touch log_path
- File.open(log_path, 'a') do |f|
- $stdout.reopen(f)
- $stderr.reopen(f)
- end
+ STDOUT.reopen(log_path, 'a')
+ STDERR.reopen STDOUT
+ File.chmod(0644, log_path)
+ STDOUT.sync = true
else # redirect to /dev/null
STDIN.reopen "/dev/null"
STDOUT.reopen "/dev/null", "a"
STDERR.reopen STDOUT
end
@@ -233,6 +250,6 @@
def log(message)
puts message if options[:debug]
end
end
-end
\ No newline at end of file
+end