2 local want_pposix_version = "0.3.0";
4 local pposix = assert(require "util.pposix");
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
7 local signal = select(2, pcall(require, "util.signal"));
8 if type(signal) == "string" then
9 module:log("warn", "Couldn't load signal library, won't respond to SIGTERM");
12 local config_get = require "core.configmanager".get;
13 local logger_set = require "util.logger".setwriter;
15 local prosody = _G.prosody;
17 module.host = "*"; -- we're a global module
19 -- Don't even think about it!
20 module:add_event_hook("server-starting", function ()
21 if pposix.getuid() == 0 and not config_get("*", "core", "run_as_root") then
22 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
23 module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root");
24 prosody.shutdown("Refusing to run as root");
28 local pidfile_written;
30 local function remove_pidfile()
31 if pidfile_written then
32 os.remove(pidfile_written);
33 pidfile_written = nil;
37 local function write_pidfile()
38 if pidfile_written then
41 local pidfile = config_get("*", "core", "pidfile");
43 local pf, err = io.open(pidfile, "w+");
45 module:log("error", "Couldn't write pidfile; %s", err);
47 pf:write(tostring(pposix.getpid()));
49 pidfile_written = pidfile;
55 function syslog_sink_maker(config)
56 if not syslog_opened then
57 pposix.syslog_open("prosody");
60 local syslog, format = pposix.syslog_log, string.format;
61 return function (name, level, message, ...)
63 syslog(level, format(message, ...));
65 syslog(level, message);
69 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
71 if not config_get("*", "core", "no_daemonize") then
72 local function daemonize_server()
73 local ok, ret = pposix.daemonize();
75 module:log("error", "Failed to daemonize: %s", ret);
76 elseif ret and ret > 0 then
79 module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
83 module:add_event_hook("server-starting", daemonize_server);
85 -- Not going to daemonize, so write the pid of this process
89 module:add_event_hook("server-stopped", remove_pidfile);
91 -- Set signal handlers
93 signal.signal("SIGTERM", function ()
94 module:log("warn", "Received SIGTERM");
95 prosody.unlock_globals();
96 prosody.shutdown("Received SIGTERM");
97 prosody.lock_globals();
100 signal.signal("SIGHUP", function ()
101 module:log("info", "Received SIGHUP");
102 prosody.reload_config();
103 prosody.reopen_logfiles();