2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local want_pposix_version = "0.3.1";
12 local pposix = assert(require "util.pposix");
13 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
15 local signal = select(2, pcall(require, "util.signal"));
16 if type(signal) == "string" then
17 module:log("warn", "Couldn't load signal library, won't respond to SIGTERM");
20 local config_get = require "core.configmanager".get;
21 local logger_set = require "util.logger".setwriter;
23 local prosody = _G.prosody;
25 module.host = "*"; -- we're a global module
27 -- Allow switching away from root, some people like strange ports.
28 module:add_event_hook("server-started", function ()
29 local uid = config_get("*", "core", "setuid");
30 local gid = config_get("*", "core", "setgid");
32 local success, msg = pposix.setgid(gid);
34 module:log("debug", "Changed group to "..gid.." successfully.");
36 module:log("error", "Failed to change group to "..gid..". Error: "..msg);
37 prosody.shutdown("Failed to change group to "..gid);
41 local success, msg = pposix.setuid(uid);
43 module:log("debug", "Changed user to "..uid.." successfully.");
45 module:log("error", "Failed to change user to "..uid..". Error: "..msg);
46 prosody.shutdown("Failed to change user to "..uid);
51 -- Don't even think about it!
52 module:add_event_hook("server-starting", function ()
53 local suid = config_get("*", "core", "setuid");
54 if not suid or suid == 0 or suid == "root" then
55 if pposix.getuid() == 0 and not config_get("*", "core", "run_as_root") then
56 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
57 module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root");
58 prosody.shutdown("Refusing to run as root");
63 local pidfile_written;
65 local function remove_pidfile()
66 if pidfile_written then
67 os.remove(pidfile_written);
68 pidfile_written = nil;
72 local function write_pidfile()
73 if pidfile_written then
76 local pidfile = config_get("*", "core", "pidfile");
78 local pf, err = io.open(pidfile, "w+");
80 module:log("error", "Couldn't write pidfile; %s", err);
82 pf:write(tostring(pposix.getpid()));
84 pidfile_written = pidfile;
90 function syslog_sink_maker(config)
91 if not syslog_opened then
92 pposix.syslog_open("prosody");
95 local syslog, format = pposix.syslog_log, string.format;
96 return function (name, level, message, ...)
98 syslog(level, format(message, ...));
100 syslog(level, message);
104 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
106 if not config_get("*", "core", "no_daemonize") then
107 local function daemonize_server()
108 local ok, ret = pposix.daemonize();
110 module:log("error", "Failed to daemonize: %s", ret);
111 elseif ret and ret > 0 then
114 module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
118 module:add_event_hook("server-starting", daemonize_server);
120 -- Not going to daemonize, so write the pid of this process
124 module:add_event_hook("server-stopped", remove_pidfile);
126 -- Set signal handlers
127 if signal.signal then
128 signal.signal("SIGTERM", function ()
129 module:log("warn", "Received SIGTERM");
130 prosody.unlock_globals();
131 prosody.shutdown("Received SIGTERM");
132 prosody.lock_globals();
135 signal.signal("SIGHUP", function ()
136 module:log("info", "Received SIGHUP");
137 prosody.reload_config();
138 prosody.reopen_logfiles();