From a46abea00cb98218c1ef8ed2d711adf8d9a553c0 Mon Sep 17 00:00:00 2001 From: Bryan McLellan Date: Thu, 11 Apr 2013 13:59:33 -0700 Subject: Merge branch 'CHEF-3367' --- lib/chef/daemon.rb | 28 ++++++++++++++++++++++++---- spec/unit/daemon_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/chef/daemon.rb b/lib/chef/daemon.rb index bb5ccf753a..9a3d5a884a 100644 --- a/lib/chef/daemon.rb +++ b/lib/chef/daemon.rb @@ -74,6 +74,24 @@ class Chef Chef::Application.fatal!("You don't have access to the PID file at #{pid_file}: #{e.message}") end + # Check if this process if forked from a Chef daemon + # ==== Returns + # Boolean:: + # True if this process is forked + # False if this process is not forked + # + def forked? + if running? and Process.ppid == pid_from_file.to_i + # chef daemon is running and this process is a child of it + true + elsif not running? and Process.ppid == 1 + # an orphaned fork, its parent becomes init, launchd, etc. after chef daemon dies + true + else + false + end + end + # Gets the pid file for @name # ==== Returns # String:: @@ -112,12 +130,14 @@ class Chef Chef::Application.fatal!("Couldn't write to pidfile #{file}, permission denied: #{e.message}") end end - + # Delete the PID from the filesystem def remove_pid_file - FileUtils.rm(pid_file) if File.exists?(pid_file) - end - + if not forked? then + FileUtils.rm(pid_file) if File.exists?(pid_file) + end + end + # Change process user/group to those specified in Chef::Config # def change_privilege diff --git a/spec/unit/daemon_spec.rb b/spec/unit/daemon_spec.rb index 1efdf2a2ad..5a0bbd6a47 100644 --- a/spec/unit/daemon_spec.rb +++ b/spec/unit/daemon_spec.rb @@ -154,8 +154,34 @@ describe Chef::Daemon do FileUtils.should_receive(:rm).with("/var/run/chef/chef-client.pid") Chef::Daemon.remove_pid_file end + + end + describe "when the pid file exists and the process is forked" do + + before do + File.stub!(:exists?).with("/var/run/chef/chef-client.pid").and_return(true) + Chef::Daemon.stub!(:forked?) { true } + end + + it "should not remove the file" do + FileUtils.should_not_receive(:rm) + Chef::Daemon.remove_pid_file + end + + end + + describe "when the pid file exists and the process is not forked" do + before do + File.stub!(:exists?).with("/var/run/chef/chef-client.pid").and_return(true) + Chef::Daemon.stub!(:forked?) { false } + end + + it "should remove the file" do + FileUtils.should_receive(:rm) + Chef::Daemon.remove_pid_file + end end describe "when the pid file does not exist" do -- cgit v1.2.1