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