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 module.host = "*"; -- we're a global module
17 -- Don't even think about it!
18 module:add_event_hook("server-starting", function ()
19 if pposix.getuid() == 0 and not config_get("*", "core", "run_as_root") then
20 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
21 module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root");
22 _G.prosody_shutdown("Refusing to run as root");
26 local pidfile_written;
28 local function remove_pidfile()
29 if pidfile_written then
30 os.remove(pidfile_written);
31 pidfile_written = nil;
35 local function write_pidfile()
36 if pidfile_written then
39 local pidfile = config_get("*", "core", "pidfile");
41 local pf, err = io.open(pidfile, "w+");
43 module:log("error", "Couldn't write pidfile; %s", err);
45 pf:write(tostring(pposix.getpid()));
47 pidfile_written = pidfile;
53 function syslog_sink_maker(config)
54 if not syslog_opened then
55 pposix.syslog_open("prosody");
58 local syslog, format = pposix.syslog_log, string.format;
59 return function (name, level, message, ...)
61 syslog(level, format(message, ...));
63 syslog(level, message);
67 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
69 if not config_get("*", "core", "no_daemonize") then
70 local function daemonize_server()
71 local ok, ret = pposix.daemonize();
73 module:log("error", "Failed to daemonize: %s", ret);
74 elseif ret and ret > 0 then
77 module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
81 module:add_event_hook("server-starting", daemonize_server);
83 -- Not going to daemonize, so write the pid of this process
87 module:add_event_hook("server-stopped", remove_pidfile);
89 -- Set signal handlers
91 signal.signal("SIGTERM", function ()
92 module:log("warn", "Received SIGTERM");
94 _G.prosody_shutdown("Received SIGTERM");
98 signal.signal("SIGHUP", function ()
99 module:log("info", "Received SIGHUP");
100 _G.prosody_reload_config();
101 _G.prosody_reopen_logfiles();