Software / code / prosody
Comparison
plugins/mod_posix.lua @ 11200:bf8f2da84007
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 05 Nov 2020 22:31:25 +0100 |
| parent | 11179:96da09c771a1 |
| child | 11830:7fe2fbfbdb1c |
comparison
equal
deleted
inserted
replaced
| 11199:6c7c50a4de32 | 11200:bf8f2da84007 |
|---|---|
| 18 local have_signal, signal = pcall(require, "util.signal"); | 18 local have_signal, signal = pcall(require, "util.signal"); |
| 19 if not have_signal then | 19 if not have_signal then |
| 20 module:log("warn", "Couldn't load signal library, won't respond to SIGTERM"); | 20 module:log("warn", "Couldn't load signal library, won't respond to SIGTERM"); |
| 21 end | 21 end |
| 22 | 22 |
| 23 local format = require "util.format".format; | |
| 24 local lfs = require "lfs"; | 23 local lfs = require "lfs"; |
| 25 local stat = lfs.attributes; | 24 local stat = lfs.attributes; |
| 26 | 25 |
| 27 local prosody = _G.prosody; | 26 local prosody = _G.prosody; |
| 28 | 27 |
| 29 module:set_global(); -- we're a global module | 28 module:set_global(); -- we're a global module |
| 30 | 29 |
| 31 local umask = module:get_option_string("umask", "027"); | 30 local umask = module:get_option_string("umask", "027"); |
| 32 pposix.umask(umask); | 31 pposix.umask(umask); |
| 33 | 32 |
| 34 -- Allow switching away from root, some people like strange ports. | |
| 35 module:hook("server-started", function () | |
| 36 local uid = module:get_option("setuid"); | |
| 37 local gid = module:get_option("setgid"); | |
| 38 if gid then | |
| 39 local success, msg = pposix.setgid(gid); | |
| 40 if success then | |
| 41 module:log("debug", "Changed group to %s successfully.", gid); | |
| 42 else | |
| 43 module:log("error", "Failed to change group to %s. Error: %s", gid, msg); | |
| 44 prosody.shutdown("Failed to change group to %s", gid); | |
| 45 end | |
| 46 end | |
| 47 if uid then | |
| 48 local success, msg = pposix.setuid(uid); | |
| 49 if success then | |
| 50 module:log("debug", "Changed user to %s successfully.", uid); | |
| 51 else | |
| 52 module:log("error", "Failed to change user to %s. Error: %s", uid, msg); | |
| 53 prosody.shutdown("Failed to change user to %s", uid); | |
| 54 end | |
| 55 end | |
| 56 end); | |
| 57 | |
| 58 -- Don't even think about it! | 33 -- Don't even think about it! |
| 59 if not prosody.start_time then -- server-starting | 34 if not prosody.start_time then -- server-starting |
| 60 local suid = module:get_option("setuid"); | 35 if pposix.getuid() == 0 and not module:get_option_boolean("run_as_root") then |
| 61 if not suid or suid == 0 or suid == "root" then | 36 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!"); |
| 62 if pposix.getuid() == 0 and not module:get_option_boolean("run_as_root") then | 37 module:log("error", "For more information on running Prosody as root, see https://prosody.im/doc/root"); |
| 63 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!"); | 38 prosody.shutdown("Refusing to run as root"); |
| 64 module:log("error", "For more information on running Prosody as root, see https://prosody.im/doc/root"); | |
| 65 prosody.shutdown("Refusing to run as root"); | |
| 66 end | |
| 67 end | 39 end |
| 68 end | 40 end |
| 69 | 41 |
| 70 local pidfile; | 42 local pidfile; |
| 71 local pidfile_handle; | 43 local pidfile_handle; |
| 111 end | 83 end |
| 112 end | 84 end |
| 113 end | 85 end |
| 114 end | 86 end |
| 115 | 87 |
| 116 local syslog_opened; | |
| 117 function syslog_sink_maker(config) -- luacheck: ignore 212/config | |
| 118 if not syslog_opened then | |
| 119 pposix.syslog_open("prosody", module:get_option_string("syslog_facility")); | |
| 120 syslog_opened = true; | |
| 121 end | |
| 122 local syslog = pposix.syslog_log; | |
| 123 return function (name, level, message, ...) | |
| 124 syslog(level, name, format(message, ...)); | |
| 125 end; | |
| 126 end | |
| 127 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker); | |
| 128 | |
| 129 local daemonize = prosody.opts.daemonize; | 88 local daemonize = prosody.opts.daemonize; |
| 130 | 89 |
| 131 if daemonize == nil then | 90 if daemonize == nil then |
| 132 -- Fall back to config file if not specified on command-line | 91 -- Fall back to config file if not specified on command-line |
| 133 daemonize = module:get_option("daemonize", prosody.installed); | 92 daemonize = module:get_option_boolean("daemonize", nil); |
| 93 if daemonize ~= nil then | |
| 94 module:log("warn", "The 'daemonize' option has been deprecated, specify -D or -F on the command line instead."); | |
| 95 -- TODO: Write some docs and include a link in the warning. | |
| 96 end | |
| 134 end | 97 end |
| 135 | 98 |
| 136 local function remove_log_sinks() | 99 local function remove_log_sinks() |
| 137 local lm = require "core.loggingmanager"; | 100 local lm = require "core.loggingmanager"; |
| 138 lm.register_sink_type("console", nil); | 101 lm.register_sink_type("console", nil); |
| 152 else | 115 else |
| 153 module:log("info", "Successfully daemonized to PID %d", pposix.getpid()); | 116 module:log("info", "Successfully daemonized to PID %d", pposix.getpid()); |
| 154 write_pidfile(); | 117 write_pidfile(); |
| 155 end | 118 end |
| 156 end | 119 end |
| 157 if not prosody.start_time then -- server-starting | 120 module:hook("server-started", daemonize_server) |
| 158 daemonize_server(); | |
| 159 end | |
| 160 else | 121 else |
| 161 -- Not going to daemonize, so write the pid of this process | 122 -- Not going to daemonize, so write the pid of this process |
| 162 write_pidfile(); | 123 write_pidfile(); |
| 163 end | 124 end |
| 164 | 125 |
| 184 module:log("info", "Received SIGINT"); | 145 module:log("info", "Received SIGINT"); |
| 185 prosody.unlock_globals(); | 146 prosody.unlock_globals(); |
| 186 prosody.shutdown("Received SIGINT"); | 147 prosody.shutdown("Received SIGINT"); |
| 187 prosody.lock_globals(); | 148 prosody.lock_globals(); |
| 188 end); | 149 end); |
| 150 | |
| 151 signal.signal("SIGUSR1", function () | |
| 152 module:log("info", "Received SIGUSR1"); | |
| 153 module:fire_event("signal/SIGUSR1"); | |
| 154 end); | |
| 155 | |
| 156 signal.signal("SIGUSR2", function () | |
| 157 module:log("info", "Received SIGUSR2"); | |
| 158 module:fire_event("signal/SIGUSR2"); | |
| 159 end); | |
| 189 end); | 160 end); |
| 190 end | 161 end |
| 162 | |
| 163 -- For other modules to reference | |
| 164 features = { | |
| 165 signal_events = true; | |
| 166 }; |