diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-08-12 09:55:48 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-08-14 10:03:00 +0200 |
commit | db4cecf21cc27eeded7c1cb8524ca0b2e6dbcdaa (patch) | |
tree | 9bd5d987194d33e51cabcc4f158d11c76dce6028 | |
parent | installer: clean up logic (diff) | |
download | wireguard-windows-jd/moderatekillproc.tar.xz wireguard-windows-jd/moderatekillproc.zip |
installer: kill leftover wireguard.exe processes forciblyjd/moderatekillproc
-rw-r--r-- | installer/customactions.c | 27 | ||||
-rw-r--r-- | installer/wireguard.wxs | 8 |
2 files changed, 35 insertions, 0 deletions
diff --git a/installer/customactions.c b/installer/customactions.c index ae9e7d7f..6fa7742c 100644 --- a/installer/customactions.c +++ b/installer/customactions.c @@ -4,6 +4,7 @@ */ #include <windows.h> +#include <tlhelp32.h> #include <msi.h> #include <msidefs.h> #include <msiquery.h> @@ -274,3 +275,29 @@ out: CoUninitialize(); return ERROR_SUCCESS; } + +__declspec(dllexport) UINT __stdcall KillWireGuardProcesses(MSIHANDLE installer) +{ + HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0), process; + PROCESSENTRY32 entry = { .dwSize = sizeof(PROCESSENTRY32) }; + bool is_com_initialized = SUCCEEDED(CoInitialize(NULL)); + + for (bool ret = Process32First(snapshot, &entry); ret; ret = Process32Next(snapshot, &entry)) { + if (_tcsicmp(entry.szExeFile, TEXT("wireguard.exe")) && _tcsicmp(entry.szExeFile, TEXT("wg.exe"))) + continue; + //TODO: check that wireguard.exe and wg.exe are actually ours somehow. Metadata? + + process = OpenProcess(PROCESS_TERMINATE, false, entry.th32ProcessID); + if (!process) + continue; + log_messagef(installer, LOG_LEVEL_INFO, TEXT("Killing %1 (pid %2!d!)"), entry.szExeFile, entry.th32ProcessID); + if (TerminateProcess(process, 0)) + WaitForSingleObject(process, INFINITE); + CloseHandle(process); + } + CloseHandle(snapshot); + + if (is_com_initialized) + CoUninitialize(); + return ERROR_SUCCESS; +} diff --git a/installer/wireguard.wxs b/installer/wireguard.wxs index 492422dd..133e37c6 100644 --- a/installer/wireguard.wxs +++ b/installer/wireguard.wxs @@ -136,6 +136,14 @@ </InstallExecuteSequence> <!-- + Kill lingering processes + --> + <CustomAction Id="KillWireGuardProcesses" BinaryKey="customactions.dll" DllEntry="KillWireGuardProcesses" Execute="deferred" Impersonate="no" /> + <InstallExecuteSequence> + <Custom Action="KillWireGuardProcesses" After="StopServices" /> + </InstallExecuteSequence> + + <!-- Launch wireguard.exe after setup complete --> <CustomAction Id="LaunchApplication" HideTarget="yes" Impersonate="no" Execute="deferred" FileKey="wireguard.exe" ExeCommand="" Return="asyncNoWait" /> |