Class: Chef::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/daemon.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.nameObject

Returns the value of attribute name


27
28
29
# File 'lib/chef/daemon.rb', line 27

def name
  @name
end

.runlockObject

Returns the value of attribute runlock


28
29
30
# File 'lib/chef/daemon.rb', line 28

def runlock
  @runlock
end

Class Method Details

._change_privilege(user, group = user) ⇒ Object

Change privileges of the process to be the specified user and group

Parameters

user<String>

The user to change the process to.

group<String>

The group to change the process to.

Alternatives

If group is left out, the user will be used (changing to user:user)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/chef/daemon.rb', line 103

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) || (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_privilegeObject

Change process user/group to those specified in Chef::Config


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef/daemon.rb', line 82

def change_privilege
  Dir.chdir("/")

  if Chef::Config[:user] && 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(name) ⇒ Object

Daemonize the current process, managing pidfiles and process uid/gid

Parameters

name<String>

The name to be used for the pid file


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef/daemon.rb', line 35

def daemonize(name)
  @name = name
  @runlock = RunLock.new(pid_file)
  if runlock.test
    # We've acquired the daemon lock. Now daemonize.
    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)
      runlock.save_pid
    rescue NotImplementedError => e
      Chef::Application.fatal!("There is no fork: #{e.message}")
    end
  else
    Chef::Application.fatal!("Chef is already running pid #{pid_from_file}")
  end
end

.pid_fileObject

Gets the pid file for @name

Returns

String

Location of the pid file for @name


63
64
65
# File 'lib/chef/daemon.rb', line 63

def pid_file
  Chef::Config[:pid_file] || "/tmp/#{@name}.pid"
end

.pid_from_fileObject

Suck the pid out of pid_file

Returns

Integer

The PID from pid_file

nil

Returned if the pid_file does not exist.


74
75
76
77
78
# File 'lib/chef/daemon.rb', line 74

def pid_from_file
  File.read(pid_file).chomp.to_i
rescue Errno::ENOENT, Errno::EACCES
  nil
end