Be more selective about what python processes we are killing

This commit is contained in:
Twangboy 2023-02-24 10:05:54 -07:00 committed by Pedro Algarvio
parent 8b0284ab41
commit 50c8639f01
3 changed files with 36 additions and 25 deletions

View file

@ -464,20 +464,21 @@ namespace MinionConfigurationExtension {
// because a running process can prevent removal of files
// Get full path and command line from running process
// see https://github.com/saltstack/salt/issues/42862
session.Log("...BEGIN kill_python_exe");
using (var wmi_searcher = new ManagementObjectSearcher
("SELECT ProcessID, ExecutablePath, CommandLine FROM Win32_Process WHERE Name = 'python.exe'")) {
session.Log("...BEGIN kill_python_exe (CustomAction01.cs)");
using (
var wmi_searcher = new ManagementObjectSearcher(
"SELECT ProcessID, ExecutablePath, CommandLine FROM Win32_Process WHERE CommandLine LIKE '%salt-minion%'"
)
) {
foreach (ManagementObject wmi_obj in wmi_searcher.Get()) {
try {
String ProcessID = wmi_obj["ProcessID"].ToString();
Int32 pid = Int32.Parse(ProcessID);
String ExecutablePath = wmi_obj["ExecutablePath"].ToString();
String CommandLine = wmi_obj["CommandLine"].ToString();
if (CommandLine.ToLower().Contains("salt-minion") || ExecutablePath.ToLower().Contains("salt\\scripts\\python.exe")) {
session.Log("...kill_python_exe " + ExecutablePath + " " + CommandLine);
Process proc11 = Process.GetProcessById(pid);
proc11.Kill();
}
session.Log("...kill_python_exe " + ExecutablePath + " " + CommandLine);
Process proc11 = Process.GetProcessById(pid);
proc11.Kill();
} catch (Exception) {
// ignore wmiresults without these properties
}

View file

@ -219,20 +219,17 @@ namespace MinionConfigurationExtension {
public static ActionResult kill_python_exe(Session session) {
// because a running process can prevent removal of files
// Get full path and command line from running process
session.Log("...BEGIN kill_python_exe");
using (var wmi_searcher = new ManagementObjectSearcher
("SELECT ProcessID, ExecutablePath, CommandLine FROM Win32_Process WHERE Name = 'python.exe'")) {
session.Log("...BEGIN kill_python_exe (Program.cs)");
using (var wmi_searcher = new ManagementObjectSearcher ("SELECT ProcessID, ExecutablePath, CommandLine FROM Win32_Process WHERE CommandLine LIKE '%salt-minion%'")) {
foreach (ManagementObject wmi_obj in wmi_searcher.Get()) {
try {
String ProcessID = wmi_obj["ProcessID"].ToString();
Int32 pid = Int32.Parse(ProcessID);
String ExecutablePath = wmi_obj["ExecutablePath"].ToString();
String CommandLine = wmi_obj["CommandLine"].ToString();
if (CommandLine.ToLower().Contains("salt") || ExecutablePath.ToLower().Contains("salt")) {
session.Log("...kill_python_exe " + ExecutablePath + " " + CommandLine);
Process proc11 = Process.GetProcessById(pid);
proc11.Kill();
}
session.Log("...kill_python_exe " + ExecutablePath + " " + CommandLine);
Process proc11 = Process.GetProcessById(pid);
proc11.Kill();
} catch (Exception) {
// ignore wmiresults without these properties
}

View file

@ -29,6 +29,9 @@ IMCAC - Immediate Custom Action - It's immediate
InstallPrivileges = "elevated"
/>
<!-- Prevent downgrade -->
<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
<!-- Do not create cab files -->
<MediaTemplate EmbedCab="yes" CompressionLevel="high" />
@ -60,7 +63,7 @@ IMCAC - Immediate Custom Action - It's immediate
<!-- Properties default values. For Properties unset by default see README.md -->
<Property Id="CONFIG_TYPE" Value="Default" />
<Property Id="START_MINION" Value="1" />
<!-- <Property Id="START_MINION" Value="1" /> -->
<Property Id="ROOTDRIVE" Value="C:\" /> <!-- Prevent msi to choose the drive with most free space -->
<Property Id="ARPPRODUCTICON" Value="icon.ico" />
<Property Id="MSIRESTARTMANAGERCONTROL" Value="Disable" />
@ -68,9 +71,17 @@ IMCAC - Immediate Custom Action - It's immediate
<Property Id="WIXUI_INSTALLDIR" Value="INSTALLDIR" />
<!-- Allow command line alias for Property -->
<SetProperty Id="INSTALLDIR" Value="[INSTALLFOLDER]" Before="LaunchConditions">INSTALLFOLDER</SetProperty>
<SetProperty Id="INSTALLDIR" Value="[INSTALLFOLDER]" Before="LaunchConditions">INSTALLFOLDER</SetProperty>
<!--
This sets the checkbox in the UI:
- If START_MINION is not passed, default to 1.
- If START_MINION is passed a value greater than 0, set to 1
-->
<SetProperty Id="START_MINION" Value="1" Before="LaunchConditions">(NOT START_MINION) or (START_MINION != "0")</SetProperty>
<!-- MINION_CONFIG implies REMOVE_CONFIG -->
<SetProperty Id="REMOVE_CONFIG" Value="1" Before="LaunchConditions">MINION_CONFIG</SetProperty>
<SetProperty Id="REMOVE_CONFIG" Value="1" Before="LaunchConditions">MINION_CONFIG</SetProperty>
<!-- Search for old config minion file -->
<Property Id="OLD_CONF_EXISTS">
@ -141,6 +152,7 @@ IMCAC - Immediate Custom Action - It's immediate
Set objShell = CreateObject("WScript.Shell")
objShell.Run "net stop salt-minion", 0, true
</CustomAction>
<!-- This is the import statement for the Custom Actions:
IMCAC, DECAC, etc...
-->
@ -181,6 +193,13 @@ IMCAC - Immediate Custom Action - It's immediate
-->
<Custom Action="stopSalt" Before="InstallValidate" >1</Custom>
<!--
On uninstall or upgrade: stop salt python.exe processes that would lock dll's
- This will only run on silent installs. /quiet or /qn
- All other installs will display a dialog box of process that need to be closed
-->
<Custom Action="kill_python_exe" After="StopServices" >(REMOVE ~= "ALL") or WIX_UPGRADE_DETECTED</Custom>
<!-- ReadConfig_IMCAC must be called before CostInitialize so features can depend on properties set-->
<Custom Action="ReadConfig_IMCAC" Before="CostInitialize" >NOT Installed</Custom>
<Custom Action="del_NSIS_DECAC" After="InstallInitialize" >nsis_install_found</Custom>
@ -200,9 +219,6 @@ IMCAC - Immediate Custom Action - It's immediate
<!-- Optionally start the service -->
<StartServices Sequence="5900">START_MINION</StartServices>
<!-- On uninstall or upgrade: stop salt python.exe processes that would lock dll's -->
<Custom Action="kill_python_exe" After="StopServices" >(REMOVE ~= "ALL") or WIX_UPGRADE_DETECTED</Custom>
<!-- On uninstall (not upgrade): delete config and cache -->
<Custom Action="DeleteConfig_CADH" Before="DeleteConfig_DECAC" >REMOVE ~= "ALL"</Custom>
<Custom Action="DeleteConfig_DECAC" After="RemoveFolders" >REMOVE ~= "ALL"</Custom>
@ -228,9 +244,6 @@ IMCAC - Immediate Custom Action - It's immediate
<CustomAction Id="DeleteConfig2_CADH" Property="DeleteConfig2_DECAC" Value="CLEAN_INSTALL=[CLEAN_INSTALL];REMOVE_CONFIG=[REMOVE_CONFIG];INSTALLDIR=[INSTALLDIR];ROOTDIR=[ROOTDIR];" />
<CustomAction Id="MoveInsecureConfig_CADH" Property="MoveInsecureConfig_DECAC" Value="INSECURE_CONFIG_FOUND=[INSECURE_CONFIG_FOUND];" />
<!-- Prevent downgrade -->
<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
<!-- Install VC++ runtime -->
<DirectoryRef Id="TARGETDIR">
<!-- Visual C++ runtimes depend on the target platform -->