2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
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
);
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");
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");
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");
38 local success
, msg
= pposix
.setgid(gid
);
40 module
:log("debug", "Changed group to %s successfully.", gid
);
42 module
:log("error", "Failed to change group to %s. Error: %s", gid
, msg
);
43 prosody
.shutdown("Failed to change group to %s", gid
);
47 local success
, msg
= pposix
.setuid(uid
);
49 module
:log("debug", "Changed user to %s successfully.", uid
);
51 module
:log("error", "Failed to change user to %s. Error: %s", uid
, msg
);
52 prosody
.shutdown("Failed to change user to %s", uid
);
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");
72 local function remove_pidfile()
73 if pidfile_handle
then
74 pidfile_handle
:close();
76 pidfile
, pidfile_handle
= nil, nil;
80 local function write_pidfile()
81 if pidfile_handle
then
84 pidfile
= module
:get_option_path("pidfile", nil, "data");
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");
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
);
97 prosody
.shutdown("Prosody already running");
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");
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);
125 local function daemonize_server()
126 module
:log("info", "Prosody is about to detach from the console, disabling further console output");
128 local ok
, ret
= pposix
.daemonize();
130 module
:log("error", "Failed to daemonize: %s", ret
);
131 elseif ret
and ret
> 0 then
134 module
:log("info", "Successfully daemonized to PID %d", pposix
.getpid());
138 if not prosody
.start_time
then -- server-starting
142 -- Not going to daemonize, so write the pid of this process
146 module
:hook("server-stopped", remove_pidfile
);
148 -- Set signal handlers
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();
158 signal
.signal("SIGHUP", function ()
159 module
:log("info", "Received SIGHUP");
160 prosody
.reload_config();
161 -- this also reloads logging
164 signal
.signal("SIGINT", function ()
165 module
:log("info", "Received SIGINT");
166 prosody
.unlock_globals();
167 prosody
.shutdown("Received SIGINT");
168 prosody
.lock_globals();