author | Matthew Wild <mwild1@gmail.com> |
Thu, 15 Jan 2009 20:59:36 +0000 | |
changeset 723 | c1e7d280c174 |
parent 722 | 63456c9d0522 |
child 728 | fa45dfb27ee5 |
permissions | -rw-r--r-- |
587 | 1 |
|
2 |
local pposix = assert(require "util.pposix"); |
|
3 |
||
4 |
local config_get = require "core.configmanager".get; |
|
5 |
local logger_set = require "util.logger".setwriter; |
|
6 |
||
7 |
module.host = "*"; -- we're a global module |
|
8 |
||
9 |
if not config_get("*", "core", "no_daemonize") then |
|
10 |
local function daemonize_server() |
|
11 |
local logwriter; |
|
12 |
||
13 |
local logfilename = config_get("*", "core", "log"); |
|
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
14 |
if logfilename == "syslog" then |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
15 |
pposix.syslog_open("prosody"); |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
16 |
local syslog, format = pposix.syslog_log, string.format; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
17 |
logwriter = function (name, level, message, ...) |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
18 |
if ... then |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
19 |
syslog(level, format(message, ...)); |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
20 |
else |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
21 |
syslog(level, message); |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
22 |
end |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
23 |
end; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
24 |
elseif logfilename then |
587 | 25 |
local logfile = io.open(logfilename, "a+"); |
26 |
if logfile then |
|
27 |
local write, format, flush = logfile.write, string.format, logfile.flush; |
|
28 |
logwriter = function (name, level, message, ...) |
|
29 |
if ... then |
|
30 |
write(logfile, name, "\t", level, "\t", format(message, ...), "\n"); |
|
31 |
else |
|
32 |
write(logfile, name, "\t" , level, "\t", message, "\n"); |
|
33 |
end |
|
34 |
flush(logfile); |
|
35 |
end; |
|
36 |
end |
|
37 |
else |
|
38 |
log("debug", "No logging specified, will continue with default"); |
|
39 |
end |
|
40 |
||
41 |
local ok, ret = pposix.daemonize(); |
|
42 |
if not ok then |
|
43 |
log("error", "Failed to daemonize: %s", ret); |
|
44 |
elseif ret and ret > 0 then |
|
45 |
os.exit(0); |
|
46 |
else |
|
47 |
if logwriter then |
|
48 |
local ok, ret = logger_set(logwriter); |
|
49 |
if not ok then |
|
50 |
log("error", "Couldn't set new log output: %s", ret); |
|
51 |
end |
|
52 |
end |
|
723
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
53 |
log("info", "Successfully daemonized to PID %d", pposix.getpid()); |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
54 |
|
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
55 |
local pidfile = config.get("*", "core", "pidfile"); |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
56 |
if pidfile then |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
57 |
local pf, err = io.open(pidfile, "w+"); |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
58 |
if not pf then |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
59 |
log("error", "Couldn't write pidfile; %s", err); |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
60 |
else |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
61 |
pf:write(tostring(pposix.getpid())); |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
62 |
pf:close(); |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
63 |
end |
c1e7d280c174
mod_posix/pposix: Fix reporting of incorrect PID on daemonization. Log correct PID, and support writing a pidfile (pidfile = '/path/to/prosody.pid' in config). Added getpid() to pposix and improved function names.
Matthew Wild <mwild1@gmail.com>
parents:
722
diff
changeset
|
64 |
end |
587 | 65 |
end |
66 |
end |
|
67 |
module:add_event_hook("server-starting", daemonize_server); |
|
68 |
end |