author | Matthew Wild <mwild1@gmail.com> |
Thu, 23 Apr 2009 21:35:24 +0100 | |
changeset 1062 | f9a1ac50782b |
parent 1061 | 8c5876378c6f |
child 1066 | 0cb325970a50 |
child 1092 | b547967d87fc |
permissions | -rw-r--r-- |
728
fa45dfb27ee5
mod_posix: Check version of pposix
Matthew Wild <mwild1@gmail.com>
parents:
723
diff
changeset
|
1 |
|
734
cfb4ec5cba5e
Fix for pposix version detection
Matthew Wild <mwild1@gmail.com>
parents:
728
diff
changeset
|
2 |
local want_pposix_version = "0.3.0"; |
587 | 3 |
|
4 |
local pposix = assert(require "util.pposix"); |
|
735 | 5 |
if pposix._VERSION ~= want_pposix_version then module:log("warn", "Unknown version (%s) of binary pposix module, expected %s", tostring(pposix._VERSION), want_pposix_version); end |
587 | 6 |
|
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
7 |
local signal = select(2, pcall(require, "util.signal")); |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
8 |
if type(signal) == "string" then |
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
9 |
module:log("warn", "Couldn't load signal library, won't respond to SIGTERM"); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
10 |
end |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
11 |
|
587 | 12 |
local config_get = require "core.configmanager".get; |
13 |
local logger_set = require "util.logger".setwriter; |
|
14 |
||
15 |
module.host = "*"; -- we're a global module |
|
16 |
||
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
17 |
local pidfile_written; |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
18 |
|
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
19 |
local function remove_pidfile() |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
20 |
if pidfile_written then |
1061
8c5876378c6f
mod_posix: Fix for removing the pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
1045
diff
changeset
|
21 |
os.remove(pidfile_written); |
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
22 |
pidfile_written = nil; |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
23 |
end |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
24 |
end |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
25 |
|
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
26 |
local function write_pidfile() |
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
27 |
if pidfile_written then |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
28 |
remove_pidfile(); |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
29 |
end |
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
30 |
local pidfile = config_get("*", "core", "pidfile"); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
31 |
if pidfile then |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
32 |
local pf, err = io.open(pidfile, "w+"); |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
33 |
if not pf then |
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
34 |
module:log("error", "Couldn't write pidfile; %s", err); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
35 |
else |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
36 |
pf:write(tostring(pposix.getpid())); |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
37 |
pf:close(); |
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
38 |
pidfile_written = pidfile; |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
39 |
end |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
40 |
end |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
41 |
end |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
42 |
|
1033
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
43 |
local syslog_opened |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
44 |
function syslog_sink_maker(config) |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
45 |
if not syslog_opened then |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
46 |
pposix.syslog_open("prosody"); |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
47 |
syslog_opened = true; |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
48 |
end |
1033
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
49 |
local syslog, format = pposix.syslog_log, string.format; |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
50 |
return function (name, level, message, ...) |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
51 |
if ... then |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
52 |
syslog(level, format(message, ...)); |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
53 |
else |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
54 |
syslog(level, message); |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
55 |
end |
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
56 |
end; |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
57 |
end |
1033
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
58 |
require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
59 |
|
587 | 60 |
if not config_get("*", "core", "no_daemonize") then |
61 |
local function daemonize_server() |
|
62 |
local ok, ret = pposix.daemonize(); |
|
63 |
if not ok then |
|
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
64 |
module:log("error", "Failed to daemonize: %s", ret); |
587 | 65 |
elseif ret and ret > 0 then |
66 |
os.exit(0); |
|
67 |
else |
|
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
68 |
module:log("info", "Successfully daemonized to PID %d", pposix.getpid()); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
69 |
write_pidfile(); |
587 | 70 |
end |
71 |
end |
|
72 |
module:add_event_hook("server-starting", daemonize_server); |
|
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
73 |
else |
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
74 |
-- Not going to daemonize, so write the pid of this process |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
75 |
write_pidfile(); |
587 | 76 |
end |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
77 |
|
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
78 |
module:add_event_hook("server-stopped", remove_pidfile); |
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
79 |
|
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
80 |
-- Set signal handler |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
81 |
if signal.signal then |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
82 |
signal.signal("SIGTERM", function () |
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
83 |
module:log("warn", "Received SIGTERM..."); |
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
84 |
_G.unlock_globals(); |
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
85 |
if _G.prosody_shutdown then |
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
86 |
_G.prosody_shutdown("Received SIGTERM"); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
87 |
else |
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
88 |
module:log("warn", "...no prosody_shutdown(), ignoring."); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
89 |
end |
1062
f9a1ac50782b
mod_posix: Fix calls to log() (replace with module:log) and make some global accesses explicit
Matthew Wild <mwild1@gmail.com>
parents:
1061
diff
changeset
|
90 |
_G.lock_globals(); |
991
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
91 |
end); |
cd0d75de8345
mod_posix: Allow logging and pidfile options to take effect without needing to daemonize. Add the ability to catch SIGTERM.
Matthew Wild <mwild1@gmail.com>
parents:
735
diff
changeset
|
92 |
end |