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.2";
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 logger_set = require "util.logger".setwriter;
22 local prosody = _G.prosody;
24 module.host = "*"; -- we're a global module
26 local umask = module:get_option("umask") or "027";
29 -- Allow switching away from root, some people like strange ports.
30 module:add_event_hook("server-started", function ()
31 local uid = module:get_option("setuid");
32 local gid = module:get_option("setgid");
34 local success, msg = pposix.setgid(gid);
36 module:log("debug", "Changed group to "..gid.." successfully.");
38 module:log("error", "Failed to change group to "..gid..". Error: "..msg);
39 prosody.shutdown("Failed to change group to "..gid);
43 local success, msg = pposix.setuid(uid);
45 module:log("debug", "Changed user to "..uid.." successfully.");
47 module:log("error", "Failed to change user to "..uid..". Error: "..msg);
48 prosody.shutdown("Failed to change user to "..uid);
53 -- Don't even think about it!
54 module:add_event_hook("server-starting", function ()
55 local suid = module:get_option("setuid");
56 if not suid or suid == 0 or suid == "root" then
57 if pposix.getuid() == 0 and not module:get_option("run_as_root") then
58 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
59 module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root");
60 prosody.shutdown("Refusing to run as root");
65 local pidfile_written;
67 local function remove_pidfile()
68 if pidfile_written then
69 os.remove(pidfile_written);
70 pidfile_written = nil;
74 local function write_pidfile()
75 if pidfile_written then
78 local pidfile = module:get_option("pidfile");
80 local pf, err = io.open(pidfile, "w+");
82 module:log("error", "Couldn't write pidfile; %s", err);
84 pf:write(tostring(pposix.getpid()));
86 pidfile_written = pidfile;
92 function syslog_sink_maker(config)
93 if not syslog_opened then
94 pposix.syslog_open("prosody");
97 local syslog, format = pposix.syslog_log, string.format;
98 return function (name, level, message, ...)
100 syslog(level, format(message, ...));
102 syslog(level, message);
106 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
108 local daemonize = module:get_option("daemonize");
109 if daemonize == nil then
110 local no_daemonize = module:get_option("no_daemonize"); --COMPAT w/ 0.5
111 daemonize = not no_daemonize;
112 if no_daemonize ~= nil then
113 module:log("warn", "The 'no_daemonize' option is now replaced by 'daemonize'");
114 module:log("warn", "Update your config from 'no_daemonize = %s' to 'daemonize = %s'", tostring(no_daemonize), tostring(daemonize));
119 local function daemonize_server()
120 local ok, ret = pposix.daemonize();
122 module:log("error", "Failed to daemonize: %s", ret);
123 elseif ret and ret > 0 then
126 module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
130 module:add_event_hook("server-starting", daemonize_server);
132 -- Not going to daemonize, so write the pid of this process
136 module:add_event_hook("server-stopped", remove_pidfile);
138 -- Set signal handlers
139 if signal.signal then
140 signal.signal("SIGTERM", function ()
141 module:log("warn", "Received SIGTERM");
142 prosody.unlock_globals();
143 prosody.shutdown("Received SIGTERM");
144 prosody.lock_globals();
147 signal.signal("SIGHUP", function ()
148 module:log("info", "Received SIGHUP");
149 prosody.reload_config();
150 prosody.reopen_logfiles();
153 signal.signal("SIGINT", function ()
154 module:log("info", "Received SIGINT");
155 prosody.unlock_globals();
156 prosody.shutdown("Received SIGINT");
157 prosody.lock_globals();