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