diff --git a/lib/daemon_controller.rb b/lib/daemon_controller.rb index b62b1af..7510372 100644 --- a/lib/daemon_controller.rb +++ b/lib/daemon_controller.rb @@ -177,6 +177,10 @@ class DaemonizationTimeout < TimeoutError # descriptors except stdin, stdout and stderr. However if there are any file # descriptors you want to keep open, specify the IO objects here. This must be # an array of IO objects. + # + # [:env] + # This must be a Hash. The hash will contain the environment variables available + # to be made available to the daemon. Hash keys must be strings, not symbols. def initialize(options) [:identifier, :start_command, :ping_command, :pid_file, :log_file].each do |option| if !options.has_key?(option) @@ -198,6 +202,7 @@ def initialize(options) @daemonize_for_me = options[:daemonize_for_me] @keep_ios = options[:keep_ios] || [] @lock_file = determine_lock_file(options, @identifier, @pid_file) + @env = options[:env] || {} end # Start the daemon and wait until it can be pinged. @@ -598,10 +603,10 @@ def run_command(command) Config::CONFIG['bindir'], Config::CONFIG['RUBY_INSTALL_NAME'] ) + Config::CONFIG['EXEEXT'] - pid = Process.spawn(ruby_interpreter, SPAWNER_FILE, + pid = Process.spawn(@env, ruby_interpreter, SPAWNER_FILE, command, options) else - pid = Process.spawn(command, options) + pid = Process.spawn(@env, command, options) end else pid = safe_fork(@daemonize_for_me) do @@ -613,6 +618,7 @@ def run_command(command) STDIN.reopen("/dev/null", "r") STDOUT.reopen(tempfile_path, "w") STDERR.reopen(tempfile_path, "w") + ENV.update(@env) exec(command) end end diff --git a/spec/daemon_controller_spec.rb b/spec/daemon_controller_spec.rb index 50b9f0a..07a5312 100644 --- a/spec/daemon_controller_spec.rb +++ b/spec/daemon_controller_spec.rb @@ -234,6 +234,14 @@ @controller.stop end end + + it "receives environment variables" do + new_controller(:env => {'ENV_FILE' => 'spec/env_file.tmp'}) + File.unlink('spec/env_file.tmp') if File.exist?('spec/env_file.tmp') + @controller.start + File.exist?('spec/env_file.tmp').should be_true + @controller.stop + end end describe DaemonController, "#stop" do diff --git a/spec/echo_server.rb b/spec/echo_server.rb index d7338a6..0b68604 100755 --- a/spec/echo_server.rb +++ b/spec/echo_server.rb @@ -61,6 +61,10 @@ end end +if ENV['ENV_FILE'] + options[:env_file] = File.expand_path(ENV['ENV_FILE']) +end + def main(options) STDIN.reopen("/dev/null", 'r') STDOUT.reopen(options[:log_file], 'a') @@ -69,6 +73,15 @@ def main(options) STDERR.sync = true Dir.chdir(options[:chdir]) File.umask(0) + + if options[:env_file] + File.open(options[:env_file], 'w') do |f| + f.write("\0") + end + at_exit do + File.unlink(options[:env_file]) rescue nil + end + end if options[:pid_file] sleep(options[:wait1]) @@ -122,4 +135,4 @@ def main(options) end else main(options) -end \ No newline at end of file +end