summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@opscode.com>2013-04-11 13:59:33 -0700
committerdanielsdeleo <dan@opscode.com>2013-04-22 11:02:35 -0700
commita46abea00cb98218c1ef8ed2d711adf8d9a553c0 (patch)
tree6c899dcf25b27f1fb82e91c2991e8689abcd06a8
parent4f80778152dc6e70b07a87d30dcf6a30c32904d8 (diff)
downloadchef-a46abea00cb98218c1ef8ed2d711adf8d9a553c0.tar.gz
Merge branch 'CHEF-3367'11.4.2.rc.0
-rw-r--r--lib/chef/daemon.rb28
-rw-r--r--spec/unit/daemon_spec.rb26
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