mod_s2s: Handle authentication of s2sin and s2sout the same way
[prosody.git] / plugins / mod_posix.lua
bloba2a60dd0a8256556a0fa4ddb68436b215729a939
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 lfs = require "lfs";
24 local stat = lfs.attributes;
26 local prosody = _G.prosody;
28 module:set_global(); -- we're a global module
30 local umask = module:get_option_string("umask", "027");
31 pposix.umask(umask);
33 -- Allow switching away from root, some people like strange ports.
34 module:hook("server-started", function ()
35 local uid = module:get_option("setuid");
36 local gid = module:get_option("setgid");
37 if gid then
38 local success, msg = pposix.setgid(gid);
39 if success then
40 module:log("debug", "Changed group to %s successfully.", gid);
41 else
42 module:log("error", "Failed to change group to %s. Error: %s", gid, msg);
43 prosody.shutdown("Failed to change group to %s", gid);
44 end
45 end
46 if uid then
47 local success, msg = pposix.setuid(uid);
48 if success then
49 module:log("debug", "Changed user to %s successfully.", uid);
50 else
51 module:log("error", "Failed to change user to %s. Error: %s", uid, msg);
52 prosody.shutdown("Failed to change user to %s", uid);
53 end
54 end
55 end);
57 -- Don't even think about it!
58 if not prosody.start_time then -- server-starting
59 local suid = module:get_option("setuid");
60 if not suid or suid == 0 or suid == "root" then
61 if pposix.getuid() == 0 and not module:get_option_boolean("run_as_root") then
62 module:log("error", "Danger, Will Robinson! Prosody doesn't need to be run as root, so don't do it!");
63 module:log("error", "For more information on running Prosody as root, see https://prosody.im/doc/root");
64 prosody.shutdown("Refusing to run as root");
65 end
66 end
67 end
69 local pidfile;
70 local pidfile_handle;
72 local function remove_pidfile()
73 if pidfile_handle then
74 pidfile_handle:close();
75 os.remove(pidfile);
76 pidfile, pidfile_handle = nil, nil;
77 end
78 end
80 local function write_pidfile()
81 if pidfile_handle then
82 remove_pidfile();
83 end
84 pidfile = module:get_option_path("pidfile", nil, "data");
85 if pidfile then
86 local err;
87 local mode = stat(pidfile) and "r+" or "w+";
88 pidfile_handle, err = io.open(pidfile, mode);
89 if not pidfile_handle then
90 module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err);
91 prosody.shutdown("Couldn't write pidfile");
92 else
93 if not lfs.lock(pidfile_handle, "w") then -- Exclusive lock
94 local other_pid = pidfile_handle:read("*a");
95 module:log("error", "Another Prosody instance seems to be running with PID %s, quitting", other_pid);
96 pidfile_handle = nil;
97 prosody.shutdown("Prosody already running");
98 else
99 pidfile_handle:close();
100 pidfile_handle, err = io.open(pidfile, "w+");
101 if not pidfile_handle then
102 module:log("error", "Couldn't write pidfile at %s; %s", pidfile, err);
103 prosody.shutdown("Couldn't write pidfile");
104 else
105 if lfs.lock(pidfile_handle, "w") then
106 pidfile_handle:write(tostring(pposix.getpid()));
107 pidfile_handle:flush();
115 local daemonize = module:get_option("daemonize", prosody.installed);
117 local function remove_log_sinks()
118 local lm = require "core.loggingmanager";
119 lm.register_sink_type("console", nil);
120 lm.register_sink_type("stdout", nil);
121 lm.reload_logging();
124 if daemonize then
125 local function daemonize_server()
126 module:log("info", "Prosody is about to detach from the console, disabling further console output");
127 remove_log_sinks();
128 local ok, ret = pposix.daemonize();
129 if not ok then
130 module:log("error", "Failed to daemonize: %s", ret);
131 elseif ret and ret > 0 then
132 os.exit(0);
133 else
134 module:log("info", "Successfully daemonized to PID %d", pposix.getpid());
135 write_pidfile();
138 if not prosody.start_time then -- server-starting
139 daemonize_server();
141 else
142 -- Not going to daemonize, so write the pid of this process
143 write_pidfile();
146 module:hook("server-stopped", remove_pidfile);
148 -- Set signal handlers
149 if have_signal then
150 module:add_timer(0, function ()
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);
158 signal.signal("SIGHUP", function ()
159 module:log("info", "Received SIGHUP");
160 prosody.reload_config();
161 -- this also reloads logging
162 end);
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);