forked from ripienaar/angelia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angelia-spoold.rb
65 lines (53 loc) · 1.23 KB
/
angelia-spoold.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/ruby
require 'angelia'
require 'getoptlong'
require 'etc'
opts = GetoptLong.new(
[ '--config', '-c', GetoptLong::REQUIRED_ARGUMENT ],
[ '--foreground', '-f', GetoptLong::NO_ARGUMENT ]
)
want_daemon = true
conffile = "/etc/angelia/angelia.cfg"
opts.each do |opt, arg|
case opt
when '--config'
conffile = arg
when '--foreground'
want_daemon = false
end
end
# Goes into the background, chdir's to /tmp, and redirect all input/output to null
# Beginning Ruby p. 489-490
def daemonize
fork do
Process.setsid
exit if fork
#Dir.chdir('/tmp')
STDIN.reopen('/dev/null')
STDOUT.reopen('/dev/null', 'a')
STDERR.reopen('/dev/null', 'a')
trap("TERM") {
exit
}
yield
end
end
# Do this outside of daemonize, in case there are errors
Angelia::Config.new(conffile)
s = Angelia::Spool.new
if Angelia::Util.config.group
Process::GID.change_privilege(Etc.getgrnam(Angelia::Util.config.group)["gid"])
end
if Angelia::Util.config.user
Process::UID.change_privilege(Etc.getpwnam(Angelia::Util.config.user)["uid"])
end
if want_daemon
daemonize do
File.open(Angelia::Util.config.pidfile, 'w') do |f|
f.write(Process.pid.to_s)
end
s.run
end
else
s.run
end