Changeset

1119:61a011ebe243

Merge with 0.4
author Matthew Wild <mwild1@gmail.com>
date Tue, 05 May 2009 14:20:26 +0100
parents 1112:e8a00a2ea910 (current diff) 1118:239d4362a040 (diff)
children 1121:063036ac82b7
files net/httpserver.lua plugins/mod_posix.lua
diffstat 5 files changed, 75 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/core/loggingmanager.lua	Mon May 04 19:57:05 2009 +0100
+++ b/core/loggingmanager.lua	Tue May 05 14:20:26 2009 +0100
@@ -10,7 +10,7 @@
 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
 
 local config = require "core.configmanager";
-
+local eventmanager = require "core.eventmanager";
 local logger = require "util.logger";
 
 _G.log = logger.init("general");
@@ -190,12 +190,26 @@
 	end
 end
 
+local empty_function = function () end;
 function log_sink_types.file(config)
 	local log = config.filename;
 	local logfile = io_open(log, "a+");
 	if not logfile then
-		return function () end
+		return empty_function;
 	end
+	local write, flush = logfile.write, logfile.flush;
+
+	eventmanager.add_event_hook("reopen-log-files", function ()
+			if logfile then
+				logfile:close();
+			end
+			logfile = io_open(log, "a+");
+			if not logfile then
+				write, flush = empty_function, empty_function;
+			else
+				write, flush = logfile.write, logfile.flush;
+			end
+		end);
 
 	local timestamps = config.timestamps;
 
@@ -203,7 +217,6 @@
 		timestamps = default_timestamp; -- Default format
 	end
 
-	local write, format, flush = logfile.write, format, logfile.flush;
 	return function (name, level, message, ...)
 		if timestamps then
 			write(logfile, os_date(timestamps), " ");
--- a/net/httpserver.lua	Mon May 04 19:57:05 2009 +0100
+++ b/net/httpserver.lua	Tue May 05 14:20:26 2009 +0100
@@ -11,7 +11,7 @@
 local s_match, s_gmatch = string.match, string.gmatch;
 local tonumber, tostring, pairs = tonumber, tostring, pairs;
 
-local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
+local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
 
 local log = require "util.logger".init("httpserver");
 
--- a/plugins/mod_posix.lua	Mon May 04 19:57:05 2009 +0100
+++ b/plugins/mod_posix.lua	Tue May 05 14:20:26 2009 +0100
@@ -86,16 +86,18 @@
 
 module:add_event_hook("server-stopped", remove_pidfile);
 
--- Set signal handler
+-- Set signal handlers
 if signal.signal then
 	signal.signal("SIGTERM", function ()
-		module:log("warn", "Received SIGTERM...");
+		module:log("warn", "Received SIGTERM");
 		_G.unlock_globals();
-		if _G.prosody_shutdown then
-			_G.prosody_shutdown("Received SIGTERM");
-		else
-			module:log("warn", "...no prosody_shutdown(), ignoring.");
-		end
+		_G.prosody_shutdown("Received SIGTERM");
 		_G.lock_globals();
 	end);
+
+	signal.signal("SIGHUP", function ()
+		module:log("info", "Received SIGHUP");
+		_G.prosody_reload_config();
+		_G.prosody_reopen_logfiles();
+	end);
 end
--- a/prosody	Mon May 04 19:57:05 2009 +0100
+++ b/prosody	Tue May 05 14:20:26 2009 +0100
@@ -114,7 +114,27 @@
 
 ----------- End of out-of-place code --------------
 
--- Global function to initiate prosody shutdown
+-- Function to reload the config file
+function prosody_reload_config()
+	log("info", "Reloading configuration file");
+	eventmanager.fire_event("reloading-config");
+	local ok, level, err = config.load((rawget(_G, "CFG_CONFIGDIR") or ".").."/prosody.cfg.lua");
+	if not ok then
+		if level == "parser" then
+			log("error", "There was an error parsing the configuration file: %s", tostring(err));
+		elseif level == "file" then
+			log("error", "Couldn't read the config file when trying to reload: %s", tostring(err));
+		end
+	end
+end
+
+-- Function to reopen logfiles
+function prosody_reopen_logfiles()
+	log("info", "Re-opening log files");
+	eventmanager.fire_event("reopen-log-files"); -- Handled by appropriate log sinks
+end
+
+-- Function to initiate prosody shutdown
 function prosody_shutdown(reason)
 	log("info", "Shutting down: %s", reason or "unknown reason");
 	eventmanager.fire_event("server-stopping", { reason = reason });
--- a/prosodyctl	Mon May 04 19:57:05 2009 +0100
+++ b/prosodyctl	Tue May 05 14:20:26 2009 +0100
@@ -61,6 +61,29 @@
 	end
 end
 
+-- Switch away from root and into the prosody user --
+local switched_user, current_uid;
+local ok, pposix = pcall(require, "util.pposix");
+if ok and pposix then
+	current_uid = pposix.getuid();
+	if current_uid == 0 then
+		-- We haz root!
+		local desired_user = config.get("*", "core", "prosody_user") or "prosody";
+		local ok, err = pposix.setuid(desired_user);
+		if ok then
+			-- Yay!
+			switched_user = true;
+		else
+			-- Boo!
+			print("Warning: Couldn't switch to Prosody user '"..tostring(desired_user).."': "..tostring(err));
+		end
+	end
+else
+	print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
+	print("For more help send the below error to us through http://prosody.im/discuss");
+	print(tostring(pposix))
+end
+
 local error_messages = setmetatable({ 
 		["invalid-username"] = "The given username is invalid in a Jabber ID";
 		["invalid-hostname"] = "The given hostname is invalid";
@@ -291,6 +314,11 @@
 		return 0;
 	else
 		show_message("Prosody is not running");
+		if not switched_user and current_uid ~= 0 then
+			print("\nNote: You will also see this if prosodyctl is not running under the same");
+			print("      user account as Prosody. Try running as root (e.g. with 'sudo' in front) to");
+			print("      gain access to Prosody's real status.");
+		end
 		return 2
 	end
 	return 1;