From eff8555f9bd6d0c982d3cad3e6e4d1ca9e176ac8 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 13 Sep 2025 21:18:57 -0400 Subject: [PATCH] [idd] helper: wait on parent process directly --- idd/LGIddHelper/main.cpp | 100 ++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 60 deletions(-) diff --git a/idd/LGIddHelper/main.cpp b/idd/LGIddHelper/main.cpp index 0ef50d5f..91140a3e 100644 --- a/idd/LGIddHelper/main.cpp +++ b/idd/LGIddHelper/main.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -27,8 +28,6 @@ static void ReportSvcStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD d static std::wstring l_executable; static HandleT l_process; -static HandleT l_exitEvent; -static std::wstring l_exitEventName; static void Launch(); @@ -67,10 +66,10 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ g_debug.Init("looking-glass-idd-helper"); DEBUG_INFO("Looking Glass IDD Helper Process (" LG_VERSION_STR ")"); - l_exitEvent.Attach(OpenEvent(SYNCHRONIZE, FALSE, args[1].c_str())); - if (!l_exitEvent.IsValid()) + HandleT hParent(OpenProcess(SYNCHRONIZE, FALSE, std::stoul(args[1]))); + if (!hParent.IsValid()) { - DEBUG_ERROR_HR(GetLastError(), "Failed to open the exit event: %s", args[1].c_str()); + DEBUG_ERROR_HR(GetLastError(), "Failed to open parent process"); return EXIT_FAILURE; } @@ -85,44 +84,43 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ if (!g_pipe.Init()) { window.destroy(); - l_exitEvent.Close(); + hParent.Close(); } while (true) { - switch (MsgWaitForMultipleObjects( - l_exitEvent.IsValid() ? 1 : 0, - l_exitEvent.GetAddressOf(), - FALSE, INFINITE, QS_ALLINPUT)) + DWORD dwHandles = hParent.IsValid() ? 1 : 0; + LPHANDLE lpHandles = hParent.GetAddressOf(); + + DWORD dwResult = MsgWaitForMultipleObjects(dwHandles, lpHandles, FALSE, INFINITE, QS_ALLINPUT); + if (dwResult == WAIT_FAILED) { - case WAIT_OBJECT_0: - window.destroy(); - l_exitEvent.Close(); - break; - - case WAIT_OBJECT_0 + 1: + DEBUG_ERROR_HR(GetLastError(), "MsgWaitForMultipleObjects Failed"); + g_pipe.DeInit(); + return EXIT_FAILURE; + } + else if (dwResult == WAIT_OBJECT_0 + dwHandles) + { + MSG msg; + while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { - MSG msg; - while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) - { - if (msg.message == WM_QUIT) - goto exit; - TranslateMessage(&msg); - DispatchMessage(&msg); - } - break; + if (msg.message == WM_QUIT) + goto exit; + TranslateMessage(&msg); + DispatchMessage(&msg); } - - case WAIT_FAILED: - DEBUG_ERROR_HR(GetLastError(), "MsgWaitForMultipleObjects Failed"); - g_pipe.DeInit(); - return EXIT_FAILURE; + } + else + { + DEBUG_INFO("Parent process exited, exiting..."); + hParent.Close(); + window.destroy(); } } exit: + DEBUG_INFO("Helper window destroyed."); g_pipe.DeInit(); - return EXIT_SUCCESS; } @@ -187,31 +185,6 @@ static void WINAPI SvcMain(DWORD dwArgc, LPTSTR* lpszArgv) return; } - UUID uuid; - RPC_WSTR uuidStr; - RPC_STATUS status = UuidCreate(&uuid); - if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY && status != RPC_S_UUID_NO_ADDRESS) - { - DEBUG_ERROR("UuidCreate Failed: 0x%x", status); - ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0); - return; - } - - if (UuidToString(&uuid, &uuidStr) == RPC_S_OK) - { - l_exitEventName = L"Global\\"; - l_exitEventName += (wchar_t*) uuidStr; - RpcStringFree(&uuidStr); - - l_exitEvent.Attach(CreateEvent(NULL, FALSE, FALSE, l_exitEventName.c_str())); - if (!l_exitEvent.IsValid()) - { - DEBUG_ERROR_HR(GetLastError(), "CreateEvent Failed"); - ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0); - return; - } - } - ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0); bool running = true; while (running) @@ -276,7 +249,6 @@ static void WINAPI SvcMain(DWORD dwArgc, LPTSTR* lpszArgv) Sleep(1000); } - SetEvent(l_exitEvent.Get()); ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0); } @@ -417,9 +389,17 @@ static void Launch() si.wShowWindow = SW_SHOW; si.lpDesktop = (LPWSTR) L"WinSta0\\Default"; - wchar_t cmdBuf[128] = { 0 }; - if (l_exitEvent.IsValid()) - _snwprintf_s(cmdBuf, ARRAY_SIZE(cmdBuf), L"LGIddHelper.exe %s", l_exitEventName.c_str()); + HandleT hProcSync; + if (!DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(), + hProcSync.GetAddressOf(), SYNCHRONIZE, TRUE, 0)) + { + DEBUG_ERROR("Failed to duplicate own handle for synchronization"); + return; + } + + wchar_t cmdBuf[128]; + _snwprintf_s(cmdBuf, ARRAY_SIZE(cmdBuf), L"LGIddHelper.exe %" PRId32, + GetCurrentProcessId()); if (!CreateProcessAsUser( token.Get(),