mod_posix: Fix to not loop in SIGTERM either, but the same happens with SIGHUP (where...
[prosody.git] / plugins / mod_posix.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local want_pposix_version = "0.3.1";
11
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
14
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");
18 end
19
20 local logger_set = require "util.logger".setwriter;
21
22 local prosody = _G.prosody;
23
24 module.host = "*"; -- we're a global module
25
26 -- Allow switching away from root, some people like strange ports.
27 module:add_event_hook("server-started", function ()
28                 local uid = module:get_option("setuid");
29                 local gid = module:get_option("setgid");
30                 if gid then
31                         local success, msg = pposix.setgid(gid);
32                         if success then
33                                 module:log("debug", "Changed group to "..gid.." successfully.");
34                         else
35                                 module:log("error", "Failed to change group to "..gid..". Error: "..msg);
36                                 prosody.shutdown("Failed to change group to "..gid);
37                         end
38                 end
39                 if uid then
40                         local success, msg = pposix.setuid(uid);
41                         if success then
42                                 module:log("debug", "Changed user to "..uid.." successfully.");
43                         else
44                                 module:log("error", "Failed to change user to "..uid..". Error: "..msg);
45                                 prosody.shutdown("Failed to change user to "..uid);
46                         end
47                 end
48         end);
49
50 -- Don't even think about it!
51 module:add_event_hook("server-starting", function ()
52                 local suid = module:get_option("setuid");
53                 if not suid or suid == 0 or suid == "root" then
54                         if pposix.getuid() == 0 and not module:get_option("run_as_root") then
55                                 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
56                                 module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root");
57                                 prosody.shutdown("Refusing to run as root");
58                         end
59                 end
60         end);
61
62 local pidfile_written;
63
64 local function remove_pidfile()
65         if pidfile_written then
66                 os.remove(pidfile_written);
67                 pidfile_written = nil;
68         end
69 end
70
71 local function write_pidfile()
72         if pidfile_written then
73                 remove_pidfile();
74         end
75         local pidfile = module:get_option("pidfile");
76         if pidfile then
77                 local pf, err = io.open(pidfile, "w+");
78                 if not pf then
79                         module:log("error", "Couldn't write pidfile; %s", err);
80                 else
81                         pf:write(tostring(pposix.getpid()));
82                         pf:close();
83                         pidfile_written = pidfile;
84                 end
85         end
86 end
87
88 local syslog_opened 
89 function syslog_sink_maker(config)
90         if not syslog_opened then
91                 pposix.syslog_open("prosody");
92                 syslog_opened = true;
93         end
94         local syslog, format = pposix.syslog_log, string.format;
95         return function (name, level, message, ...)
96                         if ... then
97                                 syslog(level, format(message, ...));
98                         else
99                                 syslog(level, message);
100                         end
101                 end;
102 end
103 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
104
105 local daemonize = module:get_option("daemonize");
106 if daemonize == nil then
107         local no_daemonize = module:get_option("no_daemonize"); --COMPAT w/ 0.5
108         daemonize = not no_daemonize;
109         if no_daemonize ~= nil then
110                 module:log("warn", "The 'no_daemonize' option is now replaced by 'daemonize'");
111                 module:log("warn", "Update your config from 'no_daemonize = %s' to 'daemonize = %s'", tostring(no_daemonize), tostring(daemonize));
112         end
113 end
114
115 if daemonize then
116         local function daemonize_server()
117                 local ok, ret = pposix.daemonize();
118                 if not ok then
119                         module:log("error", "Failed to daemonize: %s", ret);
120                 elseif ret and ret > 0 then
121                         os.exit(0);
122                 else
123                         module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
124                         write_pidfile();
125                 end
126         end
127         module:add_event_hook("server-starting", daemonize_server);
128 else
129         -- Not going to daemonize, so write the pid of this process
130         write_pidfile();
131 end
132
133 module:add_event_hook("server-stopped", remove_pidfile);
134
135 -- Set signal handlers
136 if signal.signal then
137         signal.signal("SIGTERM", function ()
138                 module:log("warn", "Received SIGTERM");
139                 signal.signal("SIGTERM", function () end); -- Fixes us getting into some kind of loop
140                 prosody.unlock_globals();
141                 prosody.shutdown("Received SIGTERM");
142                 prosody.lock_globals();
143         end);
144
145         signal.signal("SIGHUP", function ()
146                 module:log("info", "Received SIGHUP");
147                 prosody.reload_config();
148                 prosody.reopen_logfiles();
149         end);
150         
151         signal.signal("SIGINT", function ()
152                 module:log("info", "Received SIGINT");
153                 signal.signal("SIGINT", function () end); -- Fix to not loop
154                 prosody.unlock_globals();
155                 prosody.shutdown("Received SIGINT");
156                 prosody.lock_globals();
157         end);
158 end