Change privileges of the process to be the specified user and group
user<String> |
The user to change the process to. |
group<String> |
The group to change the process to. |
If group is left out, the user will be used (changing to user:user)
# File lib/chef/daemon.rb, line 159 def _change_privilege(user, group=user) uid, gid = Process.euid, Process.egid begin target_uid = Etc.getpwnam(user).uid rescue ArgumentError => e Chef::Application.fatal!("Failed to get UID for user #{user}, does it exist? #{e.message}") return false end begin target_gid = Etc.getgrnam(group).gid rescue ArgumentError => e Chef::Application.fatal!("Failed to get GID for group #{group}, does it exist? #{e.message}") return false end if (uid != target_uid) or (gid != target_gid) Process.initgroups(user, target_gid) Process::GID.change_privilege(target_gid) Process::UID.change_privilege(target_uid) end true rescue Errno::EPERM => e Chef::Application.fatal!("Permission denied when trying to change #{uid}:#{gid} to #{target_uid}:#{target_gid}. #{e.message}") end
Change process user/group to those specified in Chef::Config
# File lib/chef/daemon.rb, line 138 def change_privilege Dir.chdir("/") if Chef::Config[:user] and Chef::Config[:group] Chef::Log.info("About to change privilege to #{Chef::Config[:user]}:#{Chef::Config[:group]}") _change_privilege(Chef::Config[:user], Chef::Config[:group]) elsif Chef::Config[:user] Chef::Log.info("About to change privilege to #{Chef::Config[:user]}") _change_privilege(Chef::Config[:user]) end end
Daemonize the current process, managing pidfiles and process uid/gid
name<String> |
The name to be used for the pid file |
# File lib/chef/daemon.rb, line 33 def daemonize(name) @name = name pid = pid_from_file unless running? remove_pid_file() Chef::Log.info("Daemonizing..") begin exit if fork Process.setsid exit if fork Chef::Log.info("Forked, in #{Process.pid}. Privileges: #{Process.euid} #{Process.egid}") File.umask Chef::Config[:umask] $stdin.reopen("/dev/null") $stdout.reopen("/dev/null", "a") $stderr.reopen($stdout) save_pid_file at_exit { Chef::Log.debug("daemon pid #{Process.pid} in at_exit handler") remove_pid_file } rescue NotImplementedError => e Chef::Application.fatal!("There is no fork: #{e.message}") end else Chef::Application.fatal!("Chef is already running pid #{pid}") end end
Gets the pid file for @name
Location of the pid file for @name |
# File lib/chef/daemon.rb, line 84 def pid_file Chef::Config[:pid_file] or "/tmp/#{@name}.pid" end
Delete the PID from the filesystem
# File lib/chef/daemon.rb, line 122 def remove_pid_file return unless File.exists?(pid_file) daemon_pid = pid_from_file my_pid = Process.pid if daemon_pid != my_pid Chef::Log.debug("My pid is #{my_pid}; not removing #{pid_file} which contains #{daemon_pid}") return end FileUtils.rm(pid_file) Chef::Log.debug("Removed #{pid_file}") end
Check if Chef is running based on the pid_file
Boolean |
# File lib/chef/daemon.rb, line 67 def running? if pid_from_file.nil? false else Process.kill(0, pid_from_file) true end rescue Errno::ESRCH, Errno::ENOENT false rescue Errno::EACCES => e Chef::Application.fatal!("You don't have access to the PID file at #{pid_file}: #{e.message}") end
Store the PID on the filesystem This uses the Chef::Config option, or "/tmp/name.pid" otherwise
# File lib/chef/daemon.rb, line 104 def save_pid_file file = pid_file begin FileUtils.mkdir_p(File.dirname(file)) rescue Errno::EACCES => e Chef::Application.fatal!("Failed store pid in #{File.dirname(file)}, permission denied: #{e.message}") end begin pid = Process.pid.to_s File.open(file, "w") { |f| f.write(pid) } Chef::Log.debug("Wrote #{pid} to #{file}") rescue Errno::EACCES => e Chef::Application.fatal!("Couldn't write to pidfile #{file}, permission denied: #{e.message}") end end
Generated with the Darkfish Rdoc Generator 2.