mod_csi_simple: Consider messages encrypted payload as important (fixes part of ...
[prosody.git] / plugins / mod_posix.lua
blob23df4d23ace67fa9e407a99a3e8a98e7730fd6c0
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 --
10 local want_pposix_version = "0.4.0";
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."
15 .. "Perhaps you need to recompile?", tostring(pposix._VERSION), want_pposix_version);
16 end
18 local have_signal, signal = pcall(require, "util.signal");
19 if not have_signal then
20 module:log("warn", "Couldn't load signal library, won't respond to SIGTERM");
21 end
23 local format = require "util.format".format;
24 local lfs = require "lfs";
25 local stat = lfs.attributes;
27 local prosody = _G.prosody;
29 module:set_global(); -- we're a global module
31 local umask = module:get_option_string("umask", "027");
32 pposix.umask(umask);
34 -- Allow switching away from root, some people like strange ports.
35 module:hook("server-started", function ()
36 local uid = module:get_option("setuid");
37 local gid = module:get_option("setgid");
38 if gid then
39 local success, msg = pposix.setgid(gid);
40 if success then
41 module:log("debug", "Changed group to %s successfully.", gid);
42 else
43 module:log("error", "Failed to change group to %s. Error: %s", gid, msg);
44 prosody.shutdown("Failed to change group to %s", gid);
45 end
46 end
47 if uid then
48 local success, msg = pposix.setuid(uid);
49 if success then
50 module:log("debug", "Changed user to %s successfully.", uid);
51 else
52 module:log("error", "Failed to change user to %s. Error: %s", uid, msg);
53 prosody.shutdown("Failed to change user to %s", uid);
54 end
55 end
56 end);
58 -- Don't even think about it!
59 if not prosody.start_time then -- server-starting
60 local suid = module:get_option("setuid");
61 if not suid or suid == 0 or suid == "root" then
62 if pposix.getuid() == 0 and not module:get_option_boolean("run_as_root") then
63 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
64 module:log("error", "For more information on running Prosody as root, see https://prosody.im/doc/root");
65 prosody.shutdown("Refusing to run as root");
66 end
67 end
68 end
70 local pidfile;
71 local pidfile_handle;
73 local function remove_pidfile()
74 if pidfile_handle then
75 pidfile_handle:close();
76 os.remove(pidfile);
77 pidfile, pidfile_handle = nil, nil;
78 end
79 end
81 local function write_pidfile()
82 if pidfile_handle then
83 remove_pidfile();
84 end
85 pidfile = module:get_option_path("pidfile", nil, "data");
86 if pidfile then
87 local err;
88 local mode = stat(pidfile) and "r+" or "w+";
89 pidfile_handle, err = io.open(pidfile, mode);
90 if not pidfile_handle then
91 module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err);
92 prosody.shutdown("Couldn't write pidfile");
93 else
94 if not lfs.lock(pidfile_handle, "w") then -- Exclusive lock
95 local other_pid = pidfile_handle:read("*a");
96 module:log("error", "Another Prosody instance seems to be running with PID %s, quitting", other_pid);
97 pidfile_handle = nil;
98 prosody.shutdown("Prosody already running");
99 else
100 pidfile_handle:close();
101 pidfile_handle, err = io.open(pidfile, "w+");
102 if not pidfile_handle then
103 module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err);
104 prosody.shutdown("Couldn't write pidfile");
105 else
106 if lfs.lock(pidfile_handle, "w") then
107 pidfile_handle:write(tostring(pposix.getpid()));
108 pidfile_handle:flush();
116 local syslog_opened;
117 function syslog_sink_maker(config) -- luacheck: ignore 212/config
118 if not syslog_opened then
119 pposix.syslog_open("prosody", module:get_option_string("syslog_facility"));
120 syslog_opened = true;
122 local syslog = pposix.syslog_log;
123 return function (name, level, message, ...)
124 syslog(level, name, format(message, ...));
125 end;
127 require "core.loggingmanager".register_sink_type("syslog", syslog_sink_maker);
129 local daemonize = module:get_option("daemonize", prosody.installed);
131 local function remove_log_sinks()
132 local lm = require "core.loggingmanager";
133 lm.register_sink_type("console", nil);
134 lm.register_sink_type("stdout", nil);
135 lm.reload_logging();
138 if daemonize then
139 local function daemonize_server()
140 module:log("info", "Prosody is about to detach from the console, disabling further console output");
141 remove_log_sinks();
142 local ok, ret = pposix.daemonize();
143 if not ok then
144 module:log("error", "Failed to daemonize: %s", ret);
145 elseif ret and ret > 0 then
146 os.exit(0);
147 else
148 module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
149 write_pidfile();
152 if not prosody.start_time then -- server-starting
153 daemonize_server();
155 else
156 -- Not going to daemonize, so write the pid of this process
157 write_pidfile();
160 module:hook("server-stopped", remove_pidfile);
162 -- Set signal handlers
163 if have_signal then
164 module:add_timer(0, function ()
165 signal.signal("SIGTERM", function ()
166 module:log("warn", "Received SIGTERM");
167 prosody.unlock_globals();
168 prosody.shutdown("Received SIGTERM");
169 prosody.lock_globals();
170 end);
172 signal.signal("SIGHUP", function ()
173 module:log("info", "Received SIGHUP");
174 prosody.reload_config();
175 -- this also reloads logging
176 end);
178 signal.signal("SIGINT", function ()
179 module:log("info", "Received SIGINT");
180 prosody.unlock_globals();
181 prosody.shutdown("Received SIGINT");
182 prosody.lock_globals();
183 end);
184 end);