ab3a8a2659254a652a80b8f14584891ff60118cb
[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.3";
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 lfs = require "lfs";
23
24 local prosody = _G.prosody;
25
26 module.host = "*"; -- we're a global module
27
28 local umask = module:get_option("umask") or "027";
29 pposix.umask(umask);
30
31 -- Allow switching away from root, some people like strange ports.
32 module:add_event_hook("server-started", function ()
33                 local uid = module:get_option("setuid");
34                 local gid = module:get_option("setgid");
35                 if gid then
36                         local success, msg = pposix.setgid(gid);
37                         if success then
38                                 module:log("debug", "Changed group to "..gid.." successfully.");
39                         else
40                                 module:log("error", "Failed to change group to "..gid..". Error: "..msg);
41                                 prosody.shutdown("Failed to change group to "..gid);
42                         end
43                 end
44                 if uid then
45                         local success, msg = pposix.setuid(uid);
46                         if success then
47                                 module:log("debug", "Changed user to "..uid.." successfully.");
48                         else
49                                 module:log("error", "Failed to change user to "..uid..". Error: "..msg);
50                                 prosody.shutdown("Failed to change user to "..uid);
51                         end
52                 end
53         end);
54
55 -- Don't even think about it!
56 module:add_event_hook("server-starting", function ()
57                 local suid = module:get_option("setuid");
58                 if not suid or suid == 0 or suid == "root" then
59                         if pposix.getuid() == 0 and not module:get_option("run_as_root") then
60                                 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
61                                 module:log("error", "For more information on running Prosody as root, see http://prosody.im/doc/root");
62                                 prosody.shutdown("Refusing to run as root");
63                         end
64                 end
65         end);
66
67 local pidfile;
68 local pidfile_handle;
69
70 local function remove_pidfile()
71         if pidfile_handle then
72                 pidfile_handle:close();
73                 os.remove(pidfile);
74                 pidfile, pidfile_handle = nil, nil;
75         end
76 end
77
78 local function write_pidfile()
79         if pidfile_handle then
80                 remove_pidfile();
81         end
82         pidfile = module:get_option("pidfile");
83         if pidfile then
84                 pidfile_handle, err = io.open(pidfile, "a+");
85                 if not pidfile_handle then
86                         module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err);
87                         prosody.shutdown("Couldn't write pidfile");
88                 else
89                         if not lfs.lock(pidfile_handle, "w") then -- Exclusive lock
90                                 local other_pid = pidfile_handle:read("*a");
91                                 module:log("error", "Another Prosody instance seems to be running with PID %s, quitting", other_pid);
92                                 pidfile_handle = nil;
93                                 prosody.shutdown("Prosody already running");
94                         else
95                                 pidfile_handle:write(tostring(pposix.getpid()));
96                                 pidfile_handle:flush();
97                         end
98                 end
99         end
100 end
101
102 local syslog_opened 
103 function syslog_sink_maker(config)
104         if not syslog_opened then
105                 pposix.syslog_open("prosody");
106                 syslog_opened = true;
107         end
108         local syslog, format = pposix.syslog_log, string.format;
109         return function (name, level, message, ...)
110                         if ... then
111                                 syslog(level, format(message, ...));
112                         else
113                                 syslog(level, message);
114                         end
115                 end;
116 end
117 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
118
119 local daemonize = module:get_option("daemonize");
120 if daemonize == nil then
121         local no_daemonize = module:get_option("no_daemonize"); --COMPAT w/ 0.5
122         daemonize = not no_daemonize;
123         if no_daemonize ~= nil then
124                 module:log("warn", "The 'no_daemonize' option is now replaced by 'daemonize'");
125                 module:log("warn", "Update your config from 'no_daemonize = %s' to 'daemonize = %s'", tostring(no_daemonize), tostring(daemonize));
126         end
127 end
128
129 if daemonize then
130         local function daemonize_server()
131                 local ok, ret = pposix.daemonize();
132                 if not ok then
133                         module:log("error", "Failed to daemonize: %s", ret);
134                 elseif ret and ret > 0 then
135                         os.exit(0);
136                 else
137                         module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
138                         write_pidfile();
139                 end
140         end
141         module:add_event_hook("server-starting", daemonize_server);
142 else
143         -- Not going to daemonize, so write the pid of this process
144         write_pidfile();
145 end
146
147 module:add_event_hook("server-stopped", remove_pidfile);
148
149 -- Set signal handlers
150 if signal.signal then
151         signal.signal("SIGTERM", function ()
152                 module:log("warn", "Received SIGTERM");
153                 prosody.unlock_globals();
154                 prosody.shutdown("Received SIGTERM");
155                 prosody.lock_globals();
156         end);
157
158         signal.signal("SIGHUP", function ()
159                 module:log("info", "Received SIGHUP");
160                 prosody.reload_config();
161                 prosody.reopen_logfiles();
162         end);
163         
164         signal.signal("SIGINT", function ()
165                 module:log("info", "Received SIGINT");
166                 prosody.unlock_globals();
167                 prosody.shutdown("Received SIGINT");
168                 prosody.lock_globals();
169         end);
170 end