8c2c7d7dd5ad44bfe29f6a2564271409584659c5
[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.2";
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 local umask = module:get_option("umask") or "027";
27 pposix.umask(umask);
28
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");
33                 if gid then
34                         local success, msg = pposix.setgid(gid);
35                         if success then
36                                 module:log("debug", "Changed group to "..gid.." successfully.");
37                         else
38                                 module:log("error", "Failed to change group to "..gid..". Error: "..msg);
39                                 prosody.shutdown("Failed to change group to "..gid);
40                         end
41                 end
42                 if uid then
43                         local success, msg = pposix.setuid(uid);
44                         if success then
45                                 module:log("debug", "Changed user to "..uid.." successfully.");
46                         else
47                                 module:log("error", "Failed to change user to "..uid..". Error: "..msg);
48                                 prosody.shutdown("Failed to change user to "..uid);
49                         end
50                 end
51         end);
52
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");
61                         end
62                 end
63         end);
64
65 local pidfile_written;
66
67 local function remove_pidfile()
68         if pidfile_written then
69                 os.remove(pidfile_written);
70                 pidfile_written = nil;
71         end
72 end
73
74 local function write_pidfile()
75         if pidfile_written then
76                 remove_pidfile();
77         end
78         local pidfile = module:get_option("pidfile");
79         if pidfile then
80                 local pf, err = io.open(pidfile, "w+");
81                 if not pf then
82                         module:log("error", "Couldn't write pidfile; %s", err);
83                 else
84                         pf:write(tostring(pposix.getpid()));
85                         pf:close();
86                         pidfile_written = pidfile;
87                 end
88         end
89 end
90
91 local syslog_opened 
92 function syslog_sink_maker(config)
93         if not syslog_opened then
94                 pposix.syslog_open("prosody");
95                 syslog_opened = true;
96         end
97         local syslog, format = pposix.syslog_log, string.format;
98         return function (name, level, message, ...)
99                         if ... then
100                                 syslog(level, format(message, ...));
101                         else
102                                 syslog(level, message);
103                         end
104                 end;
105 end
106 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
107
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));
115         end
116 end
117
118 if daemonize then
119         local function daemonize_server()
120                 local ok, ret = pposix.daemonize();
121                 if not ok then
122                         module:log("error", "Failed to daemonize: %s", ret);
123                 elseif ret and ret > 0 then
124                         os.exit(0);
125                 else
126                         module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
127                         write_pidfile();
128                 end
129         end
130         module:add_event_hook("server-starting", daemonize_server);
131 else
132         -- Not going to daemonize, so write the pid of this process
133         write_pidfile();
134 end
135
136 module:add_event_hook("server-stopped", remove_pidfile);
137
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();
145         end);
146
147         signal.signal("SIGHUP", function ()
148                 module:log("info", "Received SIGHUP");
149                 prosody.reload_config();
150                 prosody.reopen_logfiles();
151         end);
152         
153         signal.signal("SIGINT", function ()
154                 module:log("info", "Received SIGINT");
155                 prosody.unlock_globals();
156                 prosody.shutdown("Received SIGINT");
157                 prosody.lock_globals();
158         end);
159 end