Software / code / prosody
Annotate
plugins/mod_posix.lua @ 1317:f6e56a555c37
mod_console: Allow running code in the global environment by prefixing with '>'
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 05 Jun 2009 20:18:55 +0100 |
| parent | 1238:f4c08caca3e7 |
| child | 1522:569d58d21612 |
| rev | line source |
|---|---|
|
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 | |
|
1238
f4c08caca3e7
mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
1119
diff
changeset
|
15 local prosody = _G.prosody; |
|
f4c08caca3e7
mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
1119
diff
changeset
|
16 |
| 587 | 17 module.host = "*"; -- we're a global module |
| 18 | |
|
1092
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
19 -- Don't even think about it! |
|
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
20 module:add_event_hook("server-starting", function () |
|
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
21 if pposix.getuid() == 0 and not config_get("*", "core", "run_as_root") then |
|
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
22 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!"); |
|
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
23 module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root"); |
|
1238
f4c08caca3e7
mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
1119
diff
changeset
|
24 prosody.shutdown("Refusing to run as root"); |
|
1092
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
25 end |
|
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
26 end); |
|
b547967d87fc
mod_posix: Don't let the server run as root without the magic run_as_root in config
Matthew Wild <mwild1@gmail.com>
parents:
1062
diff
changeset
|
27 |
|
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
28 local pidfile_written; |
|
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
29 |
|
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
30 local function remove_pidfile() |
|
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
31 if pidfile_written then |
|
1061
8c5876378c6f
mod_posix: Fix for removing the pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
1045
diff
changeset
|
32 os.remove(pidfile_written); |
|
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
33 pidfile_written = nil; |
|
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
34 end |
|
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
35 end |
|
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
36 |
|
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
|
37 local function write_pidfile() |
|
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
38 if pidfile_written then |
|
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
39 remove_pidfile(); |
|
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 pf:close(); |
|
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 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
|
53 |
|
1033
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 end |
|
1033
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
60 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
|
61 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
|
62 if ... then |
|
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
63 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
|
64 else |
|
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
65 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
|
66 end |
|
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
67 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
|
68 end |
|
1033
4a9f0d482028
mod_posix: Integrate with loggingmanager, register syslog sink, remove redundant logging code
Matthew Wild <mwild1@gmail.com>
parents:
1032
diff
changeset
|
69 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
|
70 |
| 587 | 71 if not config_get("*", "core", "no_daemonize") then |
| 72 local function daemonize_server() | |
| 73 local ok, ret = pposix.daemonize(); | |
| 74 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
|
75 module:log("error", "Failed to daemonize: %s", ret); |
| 587 | 76 elseif ret and ret > 0 then |
| 77 os.exit(0); | |
| 78 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
|
79 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
|
80 write_pidfile(); |
| 587 | 81 end |
| 82 end | |
| 83 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
|
84 else |
|
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
85 -- 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
|
86 write_pidfile(); |
| 587 | 87 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
|
88 |
|
1032
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
89 module:add_event_hook("server-stopped", remove_pidfile); |
|
409f22d0430f
mod_posix: Remove pidfile on exit
Matthew Wild <mwild1@gmail.com>
parents:
991
diff
changeset
|
90 |
|
1118
239d4362a040
mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents:
1092
diff
changeset
|
91 -- Set signal handlers |
|
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
|
92 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
|
93 signal.signal("SIGTERM", function () |
|
1118
239d4362a040
mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents:
1092
diff
changeset
|
94 module:log("warn", "Received SIGTERM"); |
|
1238
f4c08caca3e7
mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
1119
diff
changeset
|
95 prosody.unlock_globals(); |
|
f4c08caca3e7
mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
1119
diff
changeset
|
96 prosody.shutdown("Received SIGTERM"); |
|
f4c08caca3e7
mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
1119
diff
changeset
|
97 prosody.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
|
98 end); |
|
1118
239d4362a040
mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents:
1092
diff
changeset
|
99 |
|
239d4362a040
mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents:
1092
diff
changeset
|
100 signal.signal("SIGHUP", function () |
|
239d4362a040
mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents:
1092
diff
changeset
|
101 module:log("info", "Received SIGHUP"); |
|
1238
f4c08caca3e7
mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
1119
diff
changeset
|
102 prosody.reload_config(); |
|
f4c08caca3e7
mod_posix: Use global prosody object
Matthew Wild <mwild1@gmail.com>
parents:
1119
diff
changeset
|
103 prosody.reopen_logfiles(); |
|
1118
239d4362a040
mod_posix: Reload the config and reopen log files on SIGHUP
Matthew Wild <mwild1@gmail.com>
parents:
1092
diff
changeset
|
104 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
|
105 end |