summaryrefslogtreecommitdiff
path: root/lib/chef/daemon.rb
diff options
context:
space:
mode:
authorBrandon Adams <brandon.adams@me.com>2013-04-09 10:44:21 -0700
committerBryan McLellan <btm@opscode.com>2013-04-11 13:48:24 -0700
commitd637073f6c53ff83c098a4f22598b77a7b975962 (patch)
tree1ba50b069f4f803bd0af61f3e237d27aa92a313c /lib/chef/daemon.rb
parentb76c62e98c1b22c3dd0050a9d6b86a9c93e970fa (diff)
downloadchef-d637073f6c53ff83c098a4f22598b77a7b975962.tar.gz
Handle pid file appropriately in forked process
Code and tests to keep a daemonized chef-client's forked process from reaping the parent pid file. Without this fix a forked process will reap its parent's pid file, breaking init script functionality. A start call to the init script will then launch a new process, since the init script is ignorant of the already running process. A process is considered a fork in case the chef-client daemon is running and the process pid is not equal to the pid defined in the pidfile, or if the chef-client daemon is not running and the parent pid of the process is 1, implying that the process was a fork at some point and has become orphaned.
Diffstat (limited to 'lib/chef/daemon.rb')
-rw-r--r--lib/chef/daemon.rb28
1 files changed, 24 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