author | Matthew Wild <mwild1@gmail.com> |
Fri, 16 Jan 2009 05:18:38 +0000 | |
changeset 728 | fa45dfb27ee5 |
parent 723 | c1e7d280c174 |
child 734 | cfb4ec5cba5e |
permissions | -rw-r--r-- |
728
fa45dfb27ee5
mod_posix: Check version of pposix
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
1 |
|
fa45dfb27ee5
mod_posix: Check version of pposix
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
2 |
local pposix_version = "0.3.0"; |
587 | 3 |
|
4 |
local pposix = assert(require "util.pposix"); |
|
728
fa45dfb27ee5
mod_posix: Check version of pposix
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
5 |
if pposix._VERSION ~= want_pposix_version then log("warn", "Unknown version (%s) of binary pposix module, expected %s", tostring(pposix._VERSION), pposix_version); end |
587 | 6 |
|
7 |
local config_get = require "core.configmanager".get; |
|
8 |
local logger_set = require "util.logger".setwriter; |
|
9 |
||
10 |
module.host = "*"; -- we're a global module |
|
11 |
||
12 |
if not config_get("*", "core", "no_daemonize") then |
|
13 |
local function daemonize_server() |
|
14 |
local logwriter; |
|
15 |
||
16 |
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
|
17 |
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
|
18 |
pposix.syslog_open("prosody"); |
728
fa45dfb27ee5
mod_posix: Check version of pposix
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
19 |
pposix.syslog_setminlevel(config.get("*", "core", "minimum_log_level") or "info"); |
722
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
20 |
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
|
21 |
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
|
22 |
if ... then |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
23 |
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
|
24 |
else |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
25 |
syslog(level, message); |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
26 |
end |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
27 |
end; |
63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents:
594
diff
changeset
|
28 |
elseif logfilename then |
587 | 29 |
local logfile = io.open(logfilename, "a+"); |
30 |
if logfile then |
|
31 |
local write, format, flush = logfile.write, string.format, logfile.flush; |
|
32 |
logwriter = function (name, level, message, ...) |
|
33 |
if ... then |
|
34 |
write(logfile, name, "\t", level, "\t", format(message, ...), "\n"); |
|
35 |
else |
|
36 |
write(logfile, name, "\t" , level, "\t", message, "\n"); |
|
37 |
end |
|
38 |
flush(logfile); |
|
39 |
end; |
|
40 |
end |
|
41 |
else |
|
42 |
log("debug", "No logging specified, will continue with default"); |
|
43 |
end |
|
44 |
||
45 |
local ok, ret = pposix.daemonize(); |
|
46 |
if not ok then |
|
47 |
log("error", "Failed to daemonize: %s", ret); |
|
48 |
elseif ret and ret > 0 then |
|
49 |
os.exit(0); |
|
50 |
else |
|
51 |
if logwriter then |
|
52 |
local ok, ret = logger_set(logwriter); |
|
53 |
if not ok then |
|
54 |
log("error", "Couldn't set new log output: %s", ret); |
|
55 |
end |
|
56 |
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
|
57 |
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
|
58 |
|
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 |
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
|
60 |
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
|
61 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
end |
587 | 69 |
end |
70 |
end |
|
71 |
module:add_event_hook("server-starting", daemonize_server); |
|
72 |
end |