Class: Souffle::Daemon
- Inherits:
-
Object
- Object
- Souffle::Daemon
- Defined in:
- lib/souffle/daemon.rb
Overview
Daemon helper routines.
Class Attribute Summary collapse
-
.name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
._change_privilege(user, group = user) ⇒ Object
Change privileges of the process to be the specified user and group.
-
.change_privilege ⇒ Object
Change process user/group to those specified in Souffle::Config.
-
.daemonize(name) ⇒ Object
Daemonize the current process, managing pidfiles and process uid/gid.
-
.pid_file ⇒ String
Gets the pid file for @name.
-
.pid_from_file ⇒ Fixnum, NilClass
Sucks the pid out of pid_file.
-
.remove_pid_file ⇒ Object
Delete the PID from the filesystem.
-
.running? ⇒ Boolean
Checks if Souffle is running based on the pid_file.
-
.save_pid_file ⇒ Object
Store the PID on the filesystem.
Class Attribute Details
.name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/souffle/daemon.rb', line 6 def name @name end |
Class Method Details
._change_privilege(user, group = user) ⇒ Object
The group parameter defaults to user unless specified.
Change privileges of the process to be the specified user and group
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/souffle/daemon.rb', line 123 def _change_privilege(user, group=user) uid, gid = Process.euid, Process.egid begin target_uid = Etc.getpwnam(user).uid rescue ArgumentError => e msg = "Failed to get UID for user #{user}, does it exist? " msg << e. Souffle::Application.fatal!(msg) return false end begin target_gid = Etc.getgrnam(group).gid rescue ArgumentError => e msg = "Failed to get GID for group #{group}, does it exist? " msg << e. Souffle::Application.fatal!(msg) 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 msg = "Permission denied when trying to change #{uid}:#{gid} " msg << "to #{target_uid}:#{target_gid}. #{e.}" Souffle::Application.fatal!(msg) end |
.change_privilege ⇒ Object
Change process user/group to those specified in Souffle::Config
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/souffle/daemon.rb', line 101 def change_privilege Dir.chdir("/") msg = "About to change privilege to " if Souffle::Config[:user] and Souffle::Config[:group] msg << "#{Souffle::Config[:user]}:#{Souffle::Config[:group]}" Souffle::Log.info(msg) _change_privilege(Souffle::Config[:user], Souffle::Config[:group]) elsif Souffle::Config[:user] msg << "#{Souffle::Config[:user]}" Souffle::Log.info(msg) _change_privilege(Souffle::Config[:user]) end end |
.daemonize(name) ⇒ Object
Daemonize the current process, managing pidfiles and process uid/gid.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/souffle/daemon.rb', line 11 def daemonize(name) @name = name pid = pid_from_file unless running? remove_pid_file() Souffle::Log.info("Daemonizing...") begin exit if fork; Process.setsid; exit if fork msg = "Forked, in #{Process.pid}. " msg << "Privileges: #{Process.euid} #{Process.egid}" Souffle::Log.info(msg) File.umask Souffle::Config[:umask] $stdin.reopen("/dev/null") $stdout.reopen("/dev/null", "a") $stderr.reopen($stdout) save_pid_file; at_exit { remove_pid_file } rescue NotImplementedError => e Souffle::Application.fatal!("There is no fork: #{e.}") end else Souffle::Application.fatal!("Souffle is already running pid #{pid}") end end |
.pid_file ⇒ String
Gets the pid file for @name.
57 58 59 |
# File 'lib/souffle/daemon.rb', line 57 def pid_file Souffle::Config[:pid_file] or "/tmp/#{@name}.pid" end |
.pid_from_file ⇒ Fixnum, NilClass
Sucks the pid out of pid_file.
exist.
65 66 67 68 69 |
# File 'lib/souffle/daemon.rb', line 65 def pid_from_file File.read(pid_file).chomp.to_i rescue Errno::ENOENT, Errno::EACCES nil end |
.remove_pid_file ⇒ Object
Delete the PID from the filesystem
96 97 98 |
# File 'lib/souffle/daemon.rb', line 96 def remove_pid_file FileUtils.rm(pid_file) if File.exists?(pid_file) end |
.running? ⇒ Boolean
Checks if Souffle is running based on the pid_file.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/souffle/daemon.rb', line 39 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 msg = "You don't have access to the PID " msg << "file at #{pid_file}: #{e.}" Souffle::Application.fatal!(msg) end |
.save_pid_file ⇒ Object
This uses the Souffle::Config option or “/tmp/name.pid” by default.
Store the PID on the filesystem.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/souffle/daemon.rb', line 76 def save_pid_file file = pid_file begin FileUtils.mkdir_p(File.dirname(file)) rescue Errno::EACCES => e msg = "Failed store pid in #{File.dirname(file)}, " msg << "permission denied: #{e.}" Souffle::Application.fatal!(msg) end begin File.open(file, "w") { |f| f.write(Process.pid.to_s) } rescue Errno::EACCES => e msg = "Couldn't write to pidfile #{file}, " msg << "permission denied: #{e.}" Souffle::Application.fatal!(msg) end end |