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.3.6";
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
);
17 local signal
= select(2, pcall(require
, "util.signal"));
18 if type(signal
) == "string" then
19 module
:log("warn", "Couldn't load signal library, won't respond to SIGTERM");
22 local lfs
= require
"lfs";
23 local stat
= lfs
.attributes
;
25 local prosody
= _G
.prosody
;
27 module
:set_global(); -- we're a global module
29 local umask
= module
:get_option("umask") or "027";
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");
37 local success
, msg
= pposix
.setgid(gid
);
39 module
:log("debug", "Changed group to %s successfully.", gid
);
41 module
:log("error", "Failed to change group to %s. Error: %s", gid
, msg
);
42 prosody
.shutdown("Failed to change group to %s", gid
);
46 local success
, msg
= pposix
.setuid(uid
);
48 module
:log("debug", "Changed user to %s successfully.", uid
);
50 module
:log("error", "Failed to change user to %s. Error: %s", uid
, msg
);
51 prosody
.shutdown("Failed to change user to %s", uid
);
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");
71 local function remove_pidfile()
72 if pidfile_handle
then
73 pidfile_handle
:close();
75 pidfile
, pidfile_handle
= nil, nil;
79 local function write_pidfile()
80 if pidfile_handle
then
83 pidfile
= module
:get_option_string("pidfile");
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");
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
);
96 prosody
.shutdown("Prosody already running");
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");
104 if lfs
.lock(pidfile_handle
, "w") then
105 pidfile_handle
:write(tostring(pposix
.getpid()));
106 pidfile_handle
:flush();
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;
120 local syslog
, format = pposix
.syslog_log
, string.format;
121 return function (name
, level
, message
, ...)
123 syslog(level
, name
, format(message
, ...));
125 syslog(level
, name
, message
);
129 require
"core.loggingmanager".register_sink_type("syslog", syslog_sink_maker
);
131 local daemonize
= module
:get_option("daemonize");
132 if daemonize
== nil then
133 local no_daemonize
= module
:get_option("no_daemonize"); --COMPAT w/ 0.5
134 daemonize
= not no_daemonize
;
135 if no_daemonize
~= nil then
136 module
:log("warn", "The 'no_daemonize' option is now replaced by 'daemonize'");
137 module
:log("warn", "Update your config from 'no_daemonize = %s' to 'daemonize = %s'", tostring(no_daemonize
), tostring(daemonize
));
141 local function remove_log_sinks()
142 local lm
= require
"core.loggingmanager";
143 lm
.register_sink_type("console", nil);
144 lm
.register_sink_type("stdout", nil);
149 local function daemonize_server()
150 module
:log("info", "Prosody is about to detach from the console, disabling further console output");
152 local ok
, ret
= pposix
.daemonize();
154 module
:log("error", "Failed to daemonize: %s", ret
);
155 elseif ret
and ret
> 0 then
158 module
:log("info", "Successfully daemonized to PID %d", pposix
.getpid());
162 if not prosody
.start_time
then -- server-starting
166 -- Not going to daemonize, so write the pid of this process
170 module
:hook("server-stopped", remove_pidfile
);
172 -- Set signal handlers
173 if signal
.signal
then
174 signal
.signal("SIGTERM", function ()
175 module
:log("warn", "Received SIGTERM");
176 prosody
.unlock_globals();
177 prosody
.shutdown("Received SIGTERM");
178 prosody
.lock_globals();
181 signal
.signal("SIGHUP", function ()
182 module
:log("info", "Received SIGHUP");
183 prosody
.reload_config();
184 prosody
.reopen_logfiles();
187 signal
.signal("SIGINT", function ()
188 module
:log("info", "Received SIGINT");
189 prosody
.unlock_globals();
190 prosody
.shutdown("Received SIGINT");
191 prosody
.lock_globals();