[PATCH] util.windows: Add new functions to prepare for Windows support comeback

33 views
Skip to first unread message

vkvo...@vivaldi.net

unread,
Feb 24, 2023, 8:22:42 AM2/24/23
to proso...@googlegroups.com
# HG changeset patch
# User Vitaly Orekhov
# Date 1677191666 -10800
# Fri Feb 24 01:34:26 2023 +0300
# Node ID b5a8b462184b898d7a216639dac32fa76813468a
# Parent 5c90862e39aa0fdff0df02a22297fb47c2ab77a8
util.windows: Add new functions to prepare for Windows support comeback

Windows support had been deprecated, but not ended yet. Maintaining Windows
version of Prosody will be done by me for now.

diff -r 5c90862e39aa -r b5a8b462184b util-src/windows.c
--- a/util-src/windows.c Thu Feb 23 18:39:02 2023 +0100
+++ b/util-src/windows.c Fri Feb 24 01:34:26 2023 +0300
@@ -31,11 +31,11 @@

status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);

- if(status == 0) {
+ if (status == 0) {
DWORD i;
lua_createtable(L, ips->AddrCount, 0);

- for(i = 0; i < ips->AddrCount; i++) {
+ for (i = 0; i < ips->AddrCount; i++) {
DWORD ip = ips->AddrArray[i];
char ip_str[16] = "";
sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
@@ -64,30 +64,31 @@

CONSOLE_SCREEN_BUFFER_INFO info;

- if(console == INVALID_HANDLE_VALUE) {
+ if (console == INVALID_HANDLE_VALUE) {
return lerror(L, "GetStdHandle");
}

- if(!GetConsoleScreenBufferInfo(console, &info)) {
+ if (!GetConsoleScreenBufferInfo(console, &info)) {
return lerror(L, "GetConsoleScreenBufferInfo");
}

- if(!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) {
+ if (!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) {
return lerror(L, "ReadConsoleOutputAttribute");
}

lua_pushnumber(L, color);
return 1;
}
-static int Lset_consolecolor(lua_State *L) {
- int color = luaL_checkint(L, 1);
+
+static int Lset_consolecolor(lua_State* L) {
+ int color = luaL_checkinteger(L, 1);
HWND console = GetStdHandle(STD_OUTPUT_HANDLE);

- if(console == INVALID_HANDLE_VALUE) {
+ if (console == INVALID_HANDLE_VALUE) {
return lerror(L, "GetStdHandle");
}

- if(!SetConsoleTextAttribute(console, color)) {
+ if (!SetConsoleTextAttribute(console, color)) {
return lerror(L, "SetConsoleTextAttribute");
}

@@ -95,10 +96,72 @@
return 1;
}

+static int Lset_consoletitle(lua_State* L) {
+ const char* szConsoleTitle;
+ szConsoleTitle = lua_tostring(L, 1);
+
+ if (!SetConsoleTitle(szConsoleTitle)) {
+ return lerror(L, "SetConsoleTitle");
+ }
+
+ lua_pushboolean(L, 1);
+ return 1;
+}
+
+static int Lget_windowsbuild(lua_State* L) {
+ char buildLab[255];
+ DWORD dBufferSize = 1024;
+
+ RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "BuildLab", RRF_RT_REG_SZ, NULL, (PVOID)&buildLab, &dBufferSize);
+ lua_pushstring(L, buildLab);
+
+ return 1;
+}
+
+static int lget_processelevation(lua_State* L) {
+ BOOL fRet = FALSE;
+ HANDLE hToken = NULL;
+ if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
+ TOKEN_ELEVATION Elevation;
+ DWORD cbSize = sizeof(TOKEN_ELEVATION);
+ if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize)) {
+ fRet = Elevation.TokenIsElevated;
+ }
+ }
+ if (hToken) {
+ CloseHandle(hToken);
+ }
+ lua_pushboolean(L, fRet);
+ return 1;
+}
+
+static int Levtp_start(lua_State* L) {
+ HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
+ if (hOut == INVALID_HANDLE_VALUE) {
+ return lerror(L, "GetStdHandle");
+ }
+
+ DWORD dwMode = 0;
+ if (!GetConsoleMode(hOut, &dwMode)) {
+ return lerror(L, "GetConsoleMode");
+ }
+
+ dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_INPUT;
+ if (!SetConsoleMode(hOut, dwMode)) {
+ return lerror(L, "SetConsoleMode");
+ }
+
+ return 1;
+}
+
static const luaL_Reg Reg[] = {
- { "get_nameservers", Lget_nameservers },
- { "get_consolecolor", Lget_consolecolor },
- { "set_consolecolor", Lset_consolecolor },
+ { "get_nameservers", Lget_nameservers },
+ { "get_consolecolor", Lget_consolecolor },
+ { "set_consolecolor", Lset_consolecolor },
+ { "set_consoletitle", Lset_consoletitle },
+ { "get_processelevation", lget_processelevation },
+ { "get_windowsbuild", Lget_windowsbuild },
+ { "evtp_start", Levtp_start },
{ NULL, NULL }
};

prosody-trunk.patch

Vitaly Orekhov

unread,
Mar 8, 2023, 2:16:41 PM3/8/23
to proso...@googlegroups.com
(As I might have failed writing the subject probably, attempt №2 here)

[PATCH] util.windows: Port mod_posix and util.pposix specific functions
for PID

Future design of util.windows should be matching util.pposix in terms of
available functions and returned structures or data. This patch is
required for mod_windows to enable Prosody preventing from launching
additional instance with the same configuration file if pidfile if
defined in it.

This patch depends on one from the initial post. Apply them in sequence.
12932.patch

Vitaly Orekhov

unread,
Mar 13, 2023, 7:53:24 PM3/13/23
to proso...@googlegroups.com
[PATCH] util.windows: Add get_consoletype() function

This function determines whether the user is using Modern Console Host
or Legacy
one. Windows versions preceding 10.0.10586 don't have Modern one, so
that will
function will always return false on them.
windows.c_12942.patch
Reply all
Reply to author
Forward
0 new messages