2026-01-02 20:00:18 +01:00
|
|
|
|
#include "stdafx.h"
|
2025-11-23 18:13:39 +01:00
|
|
|
|
#include "ServerSessionMonitor.h"
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <tlhelp32.h>
|
|
|
|
|
|
#include <userenv.h>
|
|
|
|
|
|
|
|
|
|
|
|
#pragma comment(lib, "userenv.lib")
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鍔ㄦ€佹暟缁勫垵濮嬪閲?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
#define INITIAL_CAPACITY 4
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鍓嶅悜澹版槑
|
2025-11-23 18:13:39 +01:00
|
|
|
|
static DWORD WINAPI MonitorThreadProc(LPVOID param);
|
|
|
|
|
|
static void MonitorLoop(ServerSessionMonitor* self);
|
|
|
|
|
|
static BOOL LaunchGuiInSession(ServerSessionMonitor* self, DWORD sessionId);
|
|
|
|
|
|
static BOOL IsGuiRunningInSession(ServerSessionMonitor* self, DWORD sessionId);
|
|
|
|
|
|
static void TerminateAllGui(ServerSessionMonitor* self);
|
|
|
|
|
|
static void CleanupDeadProcesses(ServerSessionMonitor* self);
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鍔ㄦ€佹暟缁勮緟鍔╁嚱鏁?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
static void AgentArray_Init(ServerAgentProcessArray* arr);
|
|
|
|
|
|
static void AgentArray_Free(ServerAgentProcessArray* arr);
|
|
|
|
|
|
static BOOL AgentArray_Add(ServerAgentProcessArray* arr, const ServerAgentProcessInfo* info);
|
|
|
|
|
|
static void AgentArray_RemoveAt(ServerAgentProcessArray* arr, size_t index);
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鍔ㄦ€佹暟缁勫疄鐜?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
// ============================================
|
|
|
|
|
|
|
|
|
|
|
|
static void AgentArray_Init(ServerAgentProcessArray* arr)
|
|
|
|
|
|
{
|
|
|
|
|
|
arr->items = NULL;
|
|
|
|
|
|
arr->count = 0;
|
|
|
|
|
|
arr->capacity = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void AgentArray_Free(ServerAgentProcessArray* arr)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (arr->items) {
|
|
|
|
|
|
free(arr->items);
|
|
|
|
|
|
arr->items = NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
arr->count = 0;
|
|
|
|
|
|
arr->capacity = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static BOOL AgentArray_Add(ServerAgentProcessArray* arr, const ServerAgentProcessInfo* info)
|
|
|
|
|
|
{
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 闇€瑕佹墿瀹?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (arr->count >= arr->capacity) {
|
|
|
|
|
|
size_t newCapacity = arr->capacity == 0 ? INITIAL_CAPACITY : arr->capacity * 2;
|
|
|
|
|
|
ServerAgentProcessInfo* newItems = (ServerAgentProcessInfo*)realloc(
|
2025-11-29 23:22:55 +01:00
|
|
|
|
arr->items, newCapacity * sizeof(ServerAgentProcessInfo));
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (!newItems) {
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
arr->items = newItems;
|
|
|
|
|
|
arr->capacity = newCapacity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
arr->items[arr->count] = *info;
|
|
|
|
|
|
arr->count++;
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void AgentArray_RemoveAt(ServerAgentProcessArray* arr, size_t index)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (index >= arr->count) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鍚庨潰鐨勫厓绱犲墠绉?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
for (size_t i = index; i < arr->count - 1; i++) {
|
|
|
|
|
|
arr->items[i] = arr->items[i + 1];
|
|
|
|
|
|
}
|
|
|
|
|
|
arr->count--;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鍏叡鎺ュ彛瀹炵幇
|
2025-11-23 18:13:39 +01:00
|
|
|
|
// ============================================
|
|
|
|
|
|
|
|
|
|
|
|
void ServerSessionMonitor_Init(ServerSessionMonitor* self)
|
|
|
|
|
|
{
|
|
|
|
|
|
self->monitorThread = NULL;
|
|
|
|
|
|
self->running = FALSE;
|
|
|
|
|
|
InitializeCriticalSection(&self->csProcessList);
|
|
|
|
|
|
AgentArray_Init(&self->agentProcesses);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ServerSessionMonitor_Cleanup(ServerSessionMonitor* self)
|
|
|
|
|
|
{
|
|
|
|
|
|
ServerSessionMonitor_Stop(self);
|
|
|
|
|
|
DeleteCriticalSection(&self->csProcessList);
|
|
|
|
|
|
AgentArray_Free(&self->agentProcesses);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOL ServerSessionMonitor_Start(ServerSessionMonitor* self)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (self->running) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("Monitor already running");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
return TRUE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("========================================");
|
|
|
|
|
|
Mprintf("Starting server session monitor...");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
self->running = TRUE;
|
|
|
|
|
|
self->monitorThread = CreateThread(NULL, 0, MonitorThreadProc, self, 0, NULL);
|
|
|
|
|
|
|
|
|
|
|
|
if (!self->monitorThread) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("ERROR: Failed to create monitor thread");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
self->running = FALSE;
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("Server session monitor thread created");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
return TRUE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ServerSessionMonitor_Stop(ServerSessionMonitor* self)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!self->running) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("Stopping server session monitor...");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
self->running = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
if (self->monitorThread) {
|
|
|
|
|
|
DWORD waitResult = WaitForSingleObject(self->monitorThread, 10000);
|
|
|
|
|
|
if (waitResult == WAIT_TIMEOUT) {
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 绾跨▼鏈湪瑙勫畾鏃堕棿鍐呴€€鍑猴紝寮哄埗缁堟
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("WARNING: Monitor thread did not exit in time, terminating...");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
TerminateThread(self->monitorThread, 1);
|
|
|
|
|
|
}
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(self->monitorThread);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
self->monitorThread = NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 缁堟鎵€鏈塆UI杩涚▼
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("Terminating all GUI processes...");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
// TerminateAllGui(self);
|
|
|
|
|
|
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("Server session monitor stopped");
|
|
|
|
|
|
Mprintf("========================================");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鍐呴儴鍑芥暟瀹炵幇
|
2025-11-23 18:13:39 +01:00
|
|
|
|
// ============================================
|
|
|
|
|
|
|
|
|
|
|
|
static DWORD WINAPI MonitorThreadProc(LPVOID param)
|
|
|
|
|
|
{
|
|
|
|
|
|
ServerSessionMonitor* monitor = (ServerSessionMonitor*)param;
|
|
|
|
|
|
MonitorLoop(monitor);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void MonitorLoop(ServerSessionMonitor* self)
|
|
|
|
|
|
{
|
|
|
|
|
|
int loopCount = 0;
|
|
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("Monitor loop started");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
while (self->running) {
|
|
|
|
|
|
loopCount++;
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 娓呯悊宸茬粓姝㈢殑杩涚▼
|
2025-11-23 18:13:39 +01:00
|
|
|
|
CleanupDeadProcesses(self);
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鏋氫妇鎵€鏈変細璇?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
PWTS_SESSION_INFO pSessionInfo = NULL;
|
|
|
|
|
|
DWORD dwCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1,
|
2025-11-29 23:22:55 +01:00
|
|
|
|
&pSessionInfo, &dwCount)) {
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
BOOL foundActiveSession = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
for (DWORD i = 0; i < dwCount; i++) {
|
|
|
|
|
|
if (pSessionInfo[i].State == WTSActive) {
|
|
|
|
|
|
DWORD sessionId = pSessionInfo[i].SessionId;
|
|
|
|
|
|
foundActiveSession = TRUE;
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 璁板綍浼氳瘽锛堟瘡5娆″惊鐜褰曚竴娆★紝閬垮厤鏃ュ織杩囧锛?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (loopCount % 5 == 1) {
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "Active session found: ID=%d, Name=%s",
|
2025-11-29 23:22:55 +01:00
|
|
|
|
(int)sessionId,
|
|
|
|
|
|
pSessionInfo[i].pWinStationName);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 妫€鏌UI鏄惁鍦ㄨ浼氳瘽涓繍琛?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (!IsGuiRunningInSession(self, sessionId)) {
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "GUI not running in session %d, launching...", (int)sessionId);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
if (LaunchGuiInSession(self, sessionId)) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("GUI launched successfully");
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 缁欑▼搴忎竴浜涙椂闂村惎鍔?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
Sleep(2000);
|
2025-11-29 23:22:55 +01:00
|
|
|
|
} else {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("Failed to launch GUI");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鍙鐞嗙涓€涓椿鍔ㄤ細璇?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!foundActiveSession && loopCount % 5 == 1) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("No active sessions found");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WTSFreeMemory(pSessionInfo);
|
2025-11-29 23:22:55 +01:00
|
|
|
|
} else {
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (loopCount % 5 == 1) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("WTSEnumerateSessions failed");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 姣?0绉掓鏌ヤ竴娆?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
for (int j = 0; j < 100 && self->running; j++) {
|
|
|
|
|
|
Sleep(100);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("Monitor loop exited");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static BOOL IsGuiRunningInSession(ServerSessionMonitor* self, DWORD sessionId)
|
|
|
|
|
|
{
|
2026-01-02 20:00:18 +01:00
|
|
|
|
(void)self; // 鏈娇鐢?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鑾峰彇褰撳墠杩涚▼鐨?exe 鍚嶇О
|
2025-11-23 18:13:39 +01:00
|
|
|
|
char currentExeName[MAX_PATH];
|
|
|
|
|
|
if (!GetModuleFileNameA(NULL, currentExeName, MAX_PATH)) {
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鑾峰彇鏂囦欢鍚嶏紙涓嶅惈璺緞锛?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
char* pFileName = strrchr(currentExeName, '\\');
|
|
|
|
|
|
if (pFileName) {
|
|
|
|
|
|
pFileName++;
|
2025-11-29 23:22:55 +01:00
|
|
|
|
} else {
|
2025-11-23 18:13:39 +01:00
|
|
|
|
pFileName = currentExeName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鑾峰彇褰撳墠鏈嶅姟杩涚▼鐨?PID
|
2025-11-23 18:13:39 +01:00
|
|
|
|
DWORD currentPID = GetCurrentProcessId();
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鍒涘缓杩涚▼蹇収
|
2025-11-23 18:13:39 +01:00
|
|
|
|
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
|
|
if (hSnapshot == INVALID_HANDLE_VALUE) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("CreateToolhelp32Snapshot failed");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PROCESSENTRY32 pe32;
|
|
|
|
|
|
pe32.dwSize = sizeof(PROCESSENTRY32);
|
|
|
|
|
|
BOOL found = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
if (Process32First(hSnapshot, &pe32)) {
|
|
|
|
|
|
do {
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鏌ユ壘鍚屽悕鐨?exe
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (_stricmp(pe32.szExeFile, pFileName) == 0) {
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鎺掗櫎鏈嶅姟杩涚▼鑷繁
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (pe32.th32ProcessID == currentPID) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鑾峰彇杩涚▼鐨勪細璇滻D
|
2025-11-23 18:13:39 +01:00
|
|
|
|
DWORD procSessionId;
|
|
|
|
|
|
if (ProcessIdToSessionId(pe32.th32ProcessID, &procSessionId)) {
|
|
|
|
|
|
if (procSessionId == sessionId) {
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鎵惧埌浜嗭細鍚屽悕 exe锛屼笉鍚?PID锛屽湪鐩爣浼氳瘽涓?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
found = TRUE;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} while (Process32Next(hSnapshot, &pe32));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(hSnapshot);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
return found;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 缁堟鎵€鏈塆UI杩涚▼
|
2025-11-23 18:13:39 +01:00
|
|
|
|
static void TerminateAllGui(ServerSessionMonitor* self)
|
|
|
|
|
|
{
|
|
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
|
|
|
|
EnterCriticalSection(&self->csProcessList);
|
|
|
|
|
|
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "Terminating %d GUI process(es)", (int)self->agentProcesses.count);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < self->agentProcesses.count; i++) {
|
|
|
|
|
|
ServerAgentProcessInfo* info = &self->agentProcesses.items[i];
|
|
|
|
|
|
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "Terminating GUI PID=%d (Session %d)",
|
2025-11-29 23:22:55 +01:00
|
|
|
|
(int)info->processId, (int)info->sessionId);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 妫€鏌ヨ繘绋嬫槸鍚﹁繕娲荤潃
|
2025-11-23 18:13:39 +01:00
|
|
|
|
DWORD exitCode;
|
|
|
|
|
|
if (GetExitCodeProcess(info->hProcess, &exitCode)) {
|
|
|
|
|
|
if (exitCode == STILL_ACTIVE) {
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 杩涚▼杩樺湪杩愯锛岀粓姝㈠畠
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (!TerminateProcess(info->hProcess, 0)) {
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "WARNING: Failed to terminate PID=%d, error=%d",
|
2025-11-29 23:22:55 +01:00
|
|
|
|
(int)info->processId, (int)GetLastError());
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-29 23:22:55 +01:00
|
|
|
|
} else {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("GUI terminated successfully");
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 绛夊緟杩涚▼瀹屽叏閫€鍑?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
WaitForSingleObject(info->hProcess, 5000);
|
|
|
|
|
|
}
|
2025-11-29 23:22:55 +01:00
|
|
|
|
} else {
|
2025-11-23 18:13:39 +01:00
|
|
|
|
sprintf_s(buf, sizeof(buf), "GUI PID=%d already exited with code %d",
|
2025-11-29 23:22:55 +01:00
|
|
|
|
(int)info->processId, (int)exitCode);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(info->hProcess);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
self->agentProcesses.count = 0; // 娓呯┖鍒楄〃
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
LeaveCriticalSection(&self->csProcessList);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("All GUI processes terminated");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 娓呯悊宸茬粡缁堟鐨勮繘绋?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
static void CleanupDeadProcesses(ServerSessionMonitor* self)
|
|
|
|
|
|
{
|
|
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
|
|
|
|
EnterCriticalSection(&self->csProcessList);
|
|
|
|
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
while (i < self->agentProcesses.count) {
|
|
|
|
|
|
ServerAgentProcessInfo* info = &self->agentProcesses.items[i];
|
|
|
|
|
|
|
|
|
|
|
|
DWORD exitCode;
|
|
|
|
|
|
if (GetExitCodeProcess(info->hProcess, &exitCode)) {
|
|
|
|
|
|
if (exitCode != STILL_ACTIVE) {
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 杩涚▼宸查€€鍑?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
sprintf_s(buf, sizeof(buf), "GUI PID=%d exited with code %d, cleaning up",
|
2025-11-29 23:22:55 +01:00
|
|
|
|
(int)info->processId, (int)exitCode);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(info->hProcess);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
AgentArray_RemoveAt(&self->agentProcesses, i);
|
2026-01-02 20:00:18 +01:00
|
|
|
|
continue; // 涓嶅鍔?i锛屽洜涓哄垹闄や簡鍏冪礌
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
2025-11-29 23:22:55 +01:00
|
|
|
|
} else {
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鏃犳硶鑾峰彇閫€鍑轰唬鐮侊紝鍙兘杩涚▼宸蹭笉瀛樺湪
|
2025-11-23 18:13:39 +01:00
|
|
|
|
sprintf_s(buf, sizeof(buf), "Cannot query GUI PID=%d, removing from list",
|
2025-11-29 23:22:55 +01:00
|
|
|
|
(int)info->processId);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(info->hProcess);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
AgentArray_RemoveAt(&self->agentProcesses, i);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LeaveCriticalSection(&self->csProcessList);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static BOOL LaunchGuiInSession(ServerSessionMonitor* self, DWORD sessionId)
|
|
|
|
|
|
{
|
|
|
|
|
|
char buf[512];
|
|
|
|
|
|
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "Attempting to launch GUI in session %d", (int)sessionId);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
STARTUPINFO si;
|
|
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
memset(&si, 0, sizeof(si));
|
|
|
|
|
|
memset(&pi, 0, sizeof(pi));
|
|
|
|
|
|
|
|
|
|
|
|
si.cb = sizeof(STARTUPINFO);
|
2026-01-02 20:00:18 +01:00
|
|
|
|
si.lpDesktop = (LPSTR)"winsta0\\default"; // 鍏抽敭锛氭寚瀹氭闈?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鑾峰彇褰撳墠鏈嶅姟杩涚▼鐨?SYSTEM 浠ょ墝
|
2025-11-23 18:13:39 +01:00
|
|
|
|
HANDLE hToken = NULL;
|
|
|
|
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY, &hToken)) {
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "OpenProcessToken failed: %d", (int)GetLastError());
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 澶嶅埗涓哄彲鐢ㄤ簬鍒涘缓杩涚▼鐨勪富浠ょ墝
|
2025-11-23 18:13:39 +01:00
|
|
|
|
HANDLE hDupToken = NULL;
|
|
|
|
|
|
if (!DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL,
|
2025-11-29 23:22:55 +01:00
|
|
|
|
SecurityImpersonation, TokenPrimary, &hDupToken)) {
|
2025-11-23 18:13:39 +01:00
|
|
|
|
sprintf_s(buf, sizeof(buf), "DuplicateTokenEx failed: %d", (int)GetLastError());
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(hToken);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 淇敼浠ょ墝鐨勪細璇?ID 涓虹洰鏍囩敤鎴蜂細璇?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (!SetTokenInformation(hDupToken, TokenSessionId, &sessionId, sizeof(sessionId))) {
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "SetTokenInformation failed: %d", (int)GetLastError());
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(hDupToken);
|
|
|
|
|
|
SAFE_CLOSE_HANDLE(hToken);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("Token duplicated");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鑾峰彇褰撳墠绋嬪簭璺緞锛堝氨鏄嚜宸憋級
|
2025-11-23 18:13:39 +01:00
|
|
|
|
char exePath[MAX_PATH];
|
|
|
|
|
|
if (!GetModuleFileNameA(NULL, exePath, MAX_PATH)) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("GetModuleFileName failed");
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(hDupToken);
|
|
|
|
|
|
SAFE_CLOSE_HANDLE(hToken);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "Service path: %s", exePath);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 妫€鏌ユ枃浠舵槸鍚﹀瓨鍦?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
DWORD fileAttr = GetFileAttributesA(exePath);
|
|
|
|
|
|
if (fileAttr == INVALID_FILE_ATTRIBUTES) {
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "ERROR: Executable not found at: %s", exePath);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(hDupToken);
|
|
|
|
|
|
SAFE_CLOSE_HANDLE(hToken);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鏋勫缓鍛戒护琛岋細鍚屼竴涓?exe锛?浣嗘坊鍔?-agent 鍙傛暟
|
2025-11-23 18:13:39 +01:00
|
|
|
|
char cmdLine[MAX_PATH + 20];
|
|
|
|
|
|
sprintf_s(cmdLine, sizeof(cmdLine), "\"%s\" -agent", exePath);
|
|
|
|
|
|
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "Command line: %s", cmdLine);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鑾峰彇鐢ㄦ埛浠ょ墝锛堢敤浜庤幏鍙栫幆澧冨潡锛?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
LPVOID lpEnvironment = NULL;
|
|
|
|
|
|
HANDLE hUserToken = NULL;
|
|
|
|
|
|
if (!WTSQueryUserToken(sessionId, &hUserToken)) {
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "WTSQueryUserToken failed: %d", (int)GetLastError());
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 浣跨敤鐢ㄦ埛浠ょ墝鍒涘缓鐜鍧?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (hUserToken) {
|
|
|
|
|
|
if (!CreateEnvironmentBlock(&lpEnvironment, hUserToken, FALSE)) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("CreateEnvironmentBlock failed");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(hUserToken);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鍦ㄧ敤鎴蜂細璇濅腑鍒涘缓杩涚▼锛圙UI绋嬪簭锛屼笉闅愯棌绐楀彛锛?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
BOOL result = CreateProcessAsUserA(
|
2025-11-29 23:22:55 +01:00
|
|
|
|
hDupToken,
|
2026-01-02 20:00:18 +01:00
|
|
|
|
NULL, // 搴旂敤绋嬪簭鍚嶏紙鍦ㄥ懡浠よ涓В鏋愶級
|
|
|
|
|
|
cmdLine, // 鍛戒护琛屽弬鏁帮細Yama.exe -agent
|
|
|
|
|
|
NULL, // 杩涚▼瀹夊叏灞炴€?
|
|
|
|
|
|
NULL, // 绾跨▼瀹夊叏灞炴€?
|
|
|
|
|
|
FALSE, // 涓嶇户鎵垮彞鏌?
|
|
|
|
|
|
NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT, // GUI绋嬪簭涓嶉渶瑕?CREATE_NO_WINDOW
|
|
|
|
|
|
lpEnvironment, // 鐜鍙橀噺
|
|
|
|
|
|
NULL, // 褰撳墠鐩綍
|
2025-11-29 23:22:55 +01:00
|
|
|
|
&si,
|
|
|
|
|
|
&pi
|
|
|
|
|
|
);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
if (lpEnvironment) {
|
|
|
|
|
|
DestroyEnvironmentBlock(lpEnvironment);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "SUCCESS: GUI process created (PID=%d)", (int)pi.dwProcessId);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 淇濆瓨杩涚▼淇℃伅锛屼互渚垮仠姝㈡椂鍙互缁堟瀹?
|
2025-11-23 18:13:39 +01:00
|
|
|
|
EnterCriticalSection(&self->csProcessList);
|
|
|
|
|
|
ServerAgentProcessInfo info;
|
|
|
|
|
|
info.processId = pi.dwProcessId;
|
|
|
|
|
|
info.sessionId = sessionId;
|
2026-01-02 20:00:18 +01:00
|
|
|
|
info.hProcess = pi.hProcess; // 涓嶅叧闂彞鏌勶紝鐣欑潃鍚庨潰缁堟
|
2025-11-23 18:13:39 +01:00
|
|
|
|
AgentArray_Add(&self->agentProcesses, &info);
|
|
|
|
|
|
LeaveCriticalSection(&self->csProcessList);
|
|
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(pi.hThread); // 绾跨▼鍙ユ焺鍙互鍏抽棴
|
2025-11-29 23:22:55 +01:00
|
|
|
|
} else {
|
2025-11-23 18:13:39 +01:00
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
|
|
sprintf_s(buf, sizeof(buf), "CreateProcessAsUser failed: %d", (int)err);
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf(buf);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
2026-01-02 20:00:18 +01:00
|
|
|
|
// 鎻愪緵鏇磋缁嗙殑閿欒淇℃伅
|
2025-11-23 18:13:39 +01:00
|
|
|
|
if (err == ERROR_FILE_NOT_FOUND) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("ERROR: Executable not found");
|
2025-11-29 23:22:55 +01:00
|
|
|
|
} else if (err == ERROR_ACCESS_DENIED) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("ERROR: Access denied - service may not have sufficient privileges");
|
2025-11-29 23:22:55 +01:00
|
|
|
|
} else if (err == 1314) {
|
2025-12-11 11:00:52 +01:00
|
|
|
|
Mprintf("ERROR: Service does not have SE_INCREASE_QUOTA privilege");
|
2025-11-23 18:13:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 15:57:27 +01:00
|
|
|
|
SAFE_CLOSE_HANDLE(hDupToken);
|
|
|
|
|
|
SAFE_CLOSE_HANDLE(hToken);
|
2025-11-23 18:13:39 +01:00
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|