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