mod_presence: return true from the presence handler
[prosody.git] / plugins / mod_posix.lua
1
2 local want_pposix_version = "0.3.0";
3
4 local pposix = assert(require "util.pposix");
5 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
6
7 local signal = select(2, pcall(require, "util.signal"));
8 if type(signal) == "string" then
9         module:log("warn", "Couldn't load signal library, won't respond to SIGTERM");
10 end
11
12 local config_get = require "core.configmanager".get;
13 local logger_set = require "util.logger".setwriter;
14
15 module.host = "*"; -- we're a global module
16
17 -- Don't even think about it!
18 module:add_event_hook("server-starting", function ()
19                 if pposix.getuid() == 0 and not config_get("*", "core", "run_as_root") then
20                         module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
21                         module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root");
22                         _G.prosody_shutdown("Refusing to run as root");
23                 end
24         end);
25
26 local pidfile_written;
27
28 local function remove_pidfile()
29         if pidfile_written then
30                 os.remove(pidfile_written);
31                 pidfile_written = nil;
32         end
33 end
34
35 local function write_pidfile()
36         if pidfile_written then
37                 remove_pidfile();
38         end
39         local pidfile = config_get("*", "core", "pidfile");
40         if pidfile then
41                 local pf, err = io.open(pidfile, "w+");
42                 if not pf then
43                         module:log("error", "Couldn't write pidfile; %s", err);
44                 else
45                         pf:write(tostring(pposix.getpid()));
46                         pf:close();
47                         pidfile_written = pidfile;
48                 end
49         end
50 end
51
52 local syslog_opened 
53 function syslog_sink_maker(config)
54         if not syslog_opened then
55                 pposix.syslog_open("prosody");
56                 syslog_opened = true;
57         end
58         local syslog, format = pposix.syslog_log, string.format;
59         return function (name, level, message, ...)
60                         if ... then
61                                 syslog(level, format(message, ...));
62                         else
63                                 syslog(level, message);
64                         end
65                 end;
66 end
67 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
68
69 if not config_get("*", "core", "no_daemonize") then
70         local function daemonize_server()
71                 local ok, ret = pposix.daemonize();
72                 if not ok then
73                         module:log("error", "Failed to daemonize: %s", ret);
74                 elseif ret and ret > 0 then
75                         os.exit(0);
76                 else
77                         module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
78                         write_pidfile();
79                 end
80         end
81         module:add_event_hook("server-starting", daemonize_server);
82 else
83         -- Not going to daemonize, so write the pid of this process
84         write_pidfile();
85 end
86
87 module:add_event_hook("server-stopped", remove_pidfile);
88
89 -- Set signal handlers
90 if signal.signal then
91         signal.signal("SIGTERM", function ()
92                 module:log("warn", "Received SIGTERM");
93                 _G.unlock_globals();
94                 _G.prosody_shutdown("Received SIGTERM");
95                 _G.lock_globals();
96         end);
97
98         signal.signal("SIGHUP", function ()
99                 module:log("info", "Received SIGHUP");
100                 _G.prosody_reload_config();
101                 _G.prosody_reopen_logfiles();
102         end);
103 end