From a7ea9737411e3b6298b664daa6a60c80fb7abb53 Mon Sep 17 00:00:00 2001 From: Stefan Gehn Date: Sat, 26 Dec 2009 15:11:13 +0000 Subject: [PATCH] - Win32: enumerate users using WTS-API. NOTE: This makes GKrellM require Win XP or newer, code for Win 2k is still present though (depends on value of _WIN32_WINNT) --- server/Makefile | 4 ++-- src/Makefile | 4 ++-- src/sysdeps/win32.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/server/Makefile b/server/Makefile index 92fa1e5..dbb55df 100644 --- a/server/Makefile +++ b/server/Makefile @@ -169,10 +169,10 @@ endif windows: libgkrellmd.a $(MAKE) \ - CFLAGS="${CFLAGS} -D_WIN32_WINNT=0x0500 -DWINVER=0x0500" \ + CFLAGS="${CFLAGS} -D_WIN32_WINNT=0x0501 -DWINVER=0x0501" \ LINK_FLAGS="${LINK_FLAGS} -mconsole" \ EXTRAOBJS="win32-resource.o win32-plugin.o" \ - SYS_LIBS=" -llargeint -lws2_32 -lpdh -lnetapi32 -liphlpapi -lntdll -lintl" \ + SYS_LIBS=" -llargeint -lws2_32 -lwtsapi32 -lpdh -lnetapi32 -liphlpapi -lntdll -lintl" \ gkrellmd install: install_bin install_inc install_man diff --git a/src/Makefile b/src/Makefile index 1fb1e8f..26ec6d3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -196,10 +196,10 @@ solaris: windows: libgkrellm.a $(MAKE) \ - CFLAGS="${CFLAGS} -D_WIN32_WINNT=0x0500 -DWINVER=0x0500" \ + CFLAGS="${CFLAGS} -D_WIN32_WINNT=0x0501 -DWINVER=0x0501" \ LINK_FLAGS="${LINK_FLAGS} -mwindows" \ EXTRAOBJS="${EXTRAOBJS} winops-win32.o win32-plugin.o win32-resource.o" \ - SYS_LIBS=" -llargeint -lws2_32 -lpdh -lnetapi32 -liphlpapi -lntdll -lintl" \ + SYS_LIBS=" -llargeint -lws2_32 -lwtsapi32 -lpdh -lnetapi32 -liphlpapi -lntdll -lintl" \ SMC_LIBS="" \ UNIXOBJS="" \ gkrellm diff --git a/src/sysdeps/win32.c b/src/sysdeps/win32.c index 48a1e9d..a19adc9 100644 --- a/src/sysdeps/win32.c +++ b/src/sysdeps/win32.c @@ -122,6 +122,7 @@ static PDH_HQUERY pdhQueryHandle = NULL; static const wchar_t* PDHDLL = L"PDH.DLL"; static const wchar_t* NTDLL = L"NTDLL.DLL"; +static const wchar_t* WTSAPI32 = L"WTSAPI32.DLL"; // ---------------------------------------------------------------------------- @@ -1490,12 +1491,61 @@ gkrellm_sys_proc_read_data(void) num_forks /*n_forks*/, fload); } + +#if _WIN32_WINNT >= 0x501 // Windows XP or newer +#include + +typedef struct _WTS_SESSION_INFO { + DWORD SessionId; + LPTSTR pWinStationName; + WTS_CONNECTSTATE_CLASS State; +} WTS_SESSION_INFO, *PWTS_SESSION_INFO; + +BOOL WINAPI WTSEnumerateSessionsW( + HANDLE hServer, + DWORD Reserved, + DWORD Version, + PWTS_SESSION_INFO *ppSessionInfo, + DWORD *pCount); +#endif // _WIN32_WINNT >= 0x501 + void gkrellm_sys_proc_read_users(void) { gint i; // Number of interactive users gint n_users = 0; + +#if _WIN32_WINNT >= 0x501 // Windows XP or newer + BOOL ret; + WTS_SESSION_INFO *pSessionList = NULL; + DWORD sessionListCount = 0; + WTS_SESSION_INFO *pSession = NULL; + + gkrellm_debug(DEBUG_SYSDEP, "Enumerating terminal sessions...\n"); + // Returns list of terminal sessions in pSessionInfo[] + ret = WTSEnumerateSessionsW(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionList, + &sessionListCount); + if (!ret) + { + win32_warning(WTSAPI32, + GetLastError(), + "Enumerating terminal sessions failed"); + } + else if (pSessionList != NULL) + { + gkrellm_debug(DEBUG_SYSDEP, "Found %d terminal sessions\n", sessionListCount); + for (i = 0; i < sessionListCount; i++) + { + pSession = &pSessionList[i]; + gkrellm_debug(DEBUG_SYSDEP, "Session %d (%ls) has state %d\n", + pSession->SessionId, pSession->pWinStationName, pSession->State); + if (pSession->State == WTSActive) + n_users++; + } + WTSFreeMemory(pSessionList); + } +#else // TODO: Remove this code-branch if nobody wants win2k-support anymore // Return value for Lsa functions NTSTATUS ntstatus; // Arguments for LsaEnumerateLogonSessions() @@ -1575,6 +1625,7 @@ gkrellm_sys_proc_read_users(void) // Free LUID list provided by OS, even if function returned an error before pfLFRB(pSessions); +#endif gkrellm_proc_assign_users(n_users); }