mod_windows is not a stub anymore. It starts providing very basic functionality
from mod_posix, like detection of running as Administrator, signal processing
and PID locking.
As a bonus, it will also change Command Prompt title to currently used Prosody
version using value from prosody.version.


-- 
You received this message because you are subscribed to the Google Groups 
"prosody-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/prosody-dev/97b895ef869de9570c87.1678302243%40EI08_MatisseX.
# HG changeset patch
# User Vitaly Orekhov <[email protected]>
# Date 1678300873 -10800
#      Wed Mar 08 21:41:13 2023 +0300
# Node ID 97b895ef869de9570c87fb22d31943d259443495
# Parent  aabb096c9346cb4eb2508aa17e86da1f41c3cf6a
mod_windows: Initial implementation

mod_windows is not a stub anymore. It starts providing very basic functionality
from mod_posix, like detection of running as Administrator, signal processing
and PID locking.
As a bonus, it will also change Command Prompt title to currently used Prosody
version using value from prosody.version.

diff -r aabb096c9346 -r 97b895ef869d plugins/mod_windows.lua
--- a/plugins/mod_windows.lua	Wed Mar 08 21:35:23 2023 +0300
+++ b/plugins/mod_windows.lua	Wed Mar 08 21:41:13 2023 +0300
@@ -1,4 +1,117 @@
--- Windows platform stub
+-- Prosody IM
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+-- Windows platform handlers
+
+local want_uwin_version = "0.1.0";
+local uwin = assert(require "util.windows");
+
+if uwin._VERSION ~= want_uwin_version then
+	module:log("warn",  "Unknown version (%s) of binary windows module, expected %s."
+		.. "Perhaps you need to recompile?", tostring(uwin._VERSION, want_uwin_version));
+end
+
+local async = require "util.async";
+local timer = require "util.timer";
+
+local have_signal, signal = pcall(require, "util.signal");
+if not have_signal then
+	module:log("warn", "Couldn't load signal library, won't respond to SIGTERM");
+end
+
+local prosody = _G.prosody;
+
 module:set_global();
 
--- TODO Add Windows-specific things here
+local function graceful_shutdown(reason, code, affect_globals)
+	module:add_timer(0, function ()
+		prosody.main_thread:run(function ()
+			if affect_globals then
+				prosody.unlock_globals();
+			end
+			prosody.shutdown(reason, code);
+			if affect_globals then
+				prosody.lock_globals();
+			end
+		end);
+	end);
+end
+
+-- Don't even think about it!
+if not prosody.start_time then -- server-starting
+	uwin.set_consoletitle("Prosody IM "..prosody.version.." [Starting]");
+	if uwin.get_processelevation() and not module:get_option_boolean("run_as_root") then
+		module:log("error", "Why is there a draught? Prosody doesn't need to be run as Administrator, so don't do it!");
+		module:log("error", "For more information on running Prosody as Administrator, see https://prosody.im/doc/root";);
+		graceful_shutdown("Refusing to run as Administrator", 1, false);
+	end
+end
+
+local pidfile;
+local pidfile_locked;
+
+local function remove_pidfile()
+	if pidfile_locked then
+		uwin.unset_pidfile(pidfile);
+		pidfile, pidfile_locked = nil, nil;
+	end
+end
+
+local function write_pidfile()
+	if pidfile_locked then
+		remove_pidfile();
+	end
+	pidfile = module:get_option_path("pidfile", nil, "data");
+	if pidfile then
+		if(uwin.set_pidfile(pidfile)) then
+			pidfile_locked = true;
+		else
+			pidfile_handle, err = io.open(pidfile, "r");
+			local other_pid = pidfile_handle:read("*a");
+			module:log("error", "Another Prosody instance seems to be running with PID %s, quitting", other_pid);
+			pidfile_handle = nil;
+			graceful_shutdown("Prosody already running", 1, false);
+		end
+	end
+end
+
+module:hook("server-started", function()
+	uwin.set_consoletitle("Prosody IM "..prosody.version);
+	write_pidfile();
+end);
+
+module:hook("server-stopping", function()
+	uwin.set_consoletitle("Prosody IM "..prosody.version.." [Stopping]");
+end);
+
+module:hook("server-stopped", remove_pidfile);
+
+-- Set signal handlers
+if have_signal then
+	module:add_timer(0, function ()
+		signal.signal("SIGTERM", function ()
+			module:log("warn", "Received SIGTERM");
+			graceful_shutdown("Received SIGTERM", 1, true);
+		end);
+
+		signal.signal("SIGINT", function ()
+			module:log("info", "Received SIGINT");
+			graceful_shutdown("Received SIGINT", 1, true);
+		end);
+
+		signal.signal("SIGBREAK", function ()
+			module:log("warn", "Received SIGBREAK");
+			graceful_shutdown("Received SIGBREAK", 1, true);
+		end);
+	end);
+end
+
+-- For other modules to reference
+features = {
+	signal_events = true;
+};

Reply via email to