core.s2smanager, xmppserver_listener: Move the responsibility of setting session...
[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 config_get = require "core.configmanager".get;
21 local logger_set = require "util.logger".setwriter;
22
23 local prosody = _G.prosody;
24
25 module.host = "*"; -- we're a global module
26
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");
31                 if gid then
32                         local success, msg = pposix.setgid(gid);
33                         if success then
34                                 module:log("debug", "Changed group to "..gid.." successfully.");
35                         else
36                                 module:log("error", "Failed to change group to "..gid..". Error: "..msg);
37                                 prosody.shutdown("Failed to change group to "..gid);
38                         end
39                 end
40                 if uid then
41                         local success, msg = pposix.setuid(uid);
42                         if success then
43                                 module:log("debug", "Changed user to "..uid.." successfully.");
44                         else
45                                 module:log("error", "Failed to change user to "..uid..". Error: "..msg);
46                                 prosody.shutdown("Failed to change user to "..uid);
47                         end
48                 end
49         end);
50
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");
59                         end
60                 end
61         end);
62
63 local pidfile_written;
64
65 local function remove_pidfile()
66         if pidfile_written then
67                 os.remove(pidfile_written);
68                 pidfile_written = nil;
69         end
70 end
71
72 local function write_pidfile()
73         if pidfile_written then
74                 remove_pidfile();
75         end
76         local pidfile = config_get("*", "core", "pidfile");
77         if pidfile then
78                 local pf, err = io.open(pidfile, "w+");
79                 if not pf then
80                         module:log("error", "Couldn't write pidfile; %s", err);
81                 else
82                         pf:write(tostring(pposix.getpid()));
83                         pf:close();
84                         pidfile_written = pidfile;
85                 end
86         end
87 end
88
89 local syslog_opened 
90 function syslog_sink_maker(config)
91         if not syslog_opened then
92                 pposix.syslog_open("prosody");
93                 syslog_opened = true;
94         end
95         local syslog, format = pposix.syslog_log, string.format;
96         return function (name, level, message, ...)
97                         if ... then
98                                 syslog(level, format(message, ...));
99                         else
100                                 syslog(level, message);
101                         end
102                 end;
103 end
104 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
105
106 if not config_get("*", "core", "no_daemonize") then
107         local function daemonize_server()
108                 local ok, ret = pposix.daemonize();
109                 if not ok then
110                         module:log("error", "Failed to daemonize: %s", ret);
111                 elseif ret and ret > 0 then
112                         os.exit(0);
113                 else
114                         module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
115                         write_pidfile();
116                 end
117         end
118         module:add_event_hook("server-starting", daemonize_server);
119 else
120         -- Not going to daemonize, so write the pid of this process
121         write_pidfile();
122 end
123
124 module:add_event_hook("server-stopped", remove_pidfile);
125
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();
133         end);
134
135         signal.signal("SIGHUP", function ()
136                 module:log("info", "Received SIGHUP");
137                 prosody.reload_config();
138                 prosody.reopen_logfiles();
139         end);
140 end