Merge 0.10->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.6";
11
12 local pposix = assert(require "util.pposix");
13 if pposix._VERSION ~= want_pposix_version then
14         module:log("warn", "Unknown version (%s) of binary pposix module, expected %s. Perhaps you need to recompile?", tostring(pposix._VERSION), want_pposix_version);
15 end
16
17 local have_signal, signal = pcall(require, "util.signal");
18 if not have_signal then
19         module:log("warn", "Couldn't load signal library, won't respond to SIGTERM");
20 end
21
22 local lfs = require "lfs";
23 local stat = lfs.attributes;
24
25 local prosody = _G.prosody;
26
27 module:set_global(); -- 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: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 %s successfully.", gid);
40                 else
41                         module:log("error", "Failed to change group to %s. Error: %s", gid, msg);
42                         prosody.shutdown("Failed to change group to %s", 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 %s successfully.", uid);
49                 else
50                         module:log("error", "Failed to change user to %s. Error: %s", uid, msg);
51                         prosody.shutdown("Failed to change user to %s", 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 https://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_string("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:close();
99                                 pidfile_handle, err = io.open(pidfile, "w+");
100                                 if not pidfile_handle then
101                                         module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err);
102                                         prosody.shutdown("Couldn't write pidfile");
103                                 else
104                                         if lfs.lock(pidfile_handle, "w") then
105                                                 pidfile_handle:write(tostring(pposix.getpid()));
106                                                 pidfile_handle:flush();
107                                         end
108                                 end
109                         end
110                 end
111         end
112 end
113
114 local syslog_opened;
115 function syslog_sink_maker(config)
116         if not syslog_opened then
117                 pposix.syslog_open("prosody", module:get_option_string("syslog_facility"));
118                 syslog_opened = true;
119         end
120         local syslog, format = pposix.syslog_log, string.format;
121         return function (name, level, message, ...)
122                 if ... then
123                         syslog(level, name, format(message, ...));
124                 else
125                         syslog(level, name, message);
126                 end
127         end;
128 end
129 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
130
131 local daemonize = module:get_option("daemonize", prosody.installed);
132
133 local function remove_log_sinks()
134         local lm = require "core.loggingmanager";
135         lm.register_sink_type("console", nil);
136         lm.register_sink_type("stdout", nil);
137         lm.reload_logging();
138 end
139
140 if daemonize then
141         local function daemonize_server()
142                 module:log("info", "Prosody is about to detach from the console, disabling further console output");
143                 remove_log_sinks();
144                 local ok, ret = pposix.daemonize();
145                 if not ok then
146                         module:log("error", "Failed to daemonize: %s", ret);
147                 elseif ret and ret > 0 then
148                         os.exit(0);
149                 else
150                         module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
151                         write_pidfile();
152                 end
153         end
154         if not prosody.start_time then -- server-starting
155                 daemonize_server();
156         end
157 else
158         -- Not going to daemonize, so write the pid of this process
159         write_pidfile();
160 end
161
162 module:hook("server-stopped", remove_pidfile);
163
164 -- Set signal handlers
165 if have_signal then
166         signal.signal("SIGTERM", function ()
167                 module:log("warn", "Received SIGTERM");
168                 prosody.unlock_globals();
169                 prosody.shutdown("Received SIGTERM");
170                 prosody.lock_globals();
171         end);
172
173         signal.signal("SIGHUP", function ()
174                 module:log("info", "Received SIGHUP");
175                 prosody.reload_config();
176                 prosody.reopen_logfiles();
177         end);
178
179         signal.signal("SIGINT", function ()
180                 module:log("info", "Received SIGINT");
181                 prosody.unlock_globals();
182                 prosody.shutdown("Received SIGINT");
183                 prosody.lock_globals();
184         end);
185 end