Skip to content

Commit

Permalink
Remove process suspend (#4586)
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Aug 4, 2023
1 parent 09027e2 commit 14a2995
Showing 1 changed file with 5 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public static void Suspend(this Process process)

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
SuspendWindows(process);
// There is no supported or documented API to suspend a process. If there was you should call it here.
// SuspendWindows(process);
}
else
{
Expand Down Expand Up @@ -70,40 +71,6 @@ public static List<ProcessTreeNode> GetProcessTree(this Process process)
return new List<ProcessTreeNode> { new ProcessTreeNode { Process = process, Level = 0 } }.Concat(acc.Where(a => a.Level > 0)).ToList();
}

internal static void SuspendWindows(Process process)
{
EqtTrace.Verbose($"ProcessCodeMethods.Suspend: Suspending process {process.Id} - {process.ProcessName}.");
NtSuspendProcess(process.Handle);
}

internal static void SuspendLinuxMacOs(Process process)
{
try
{
var output = new StringBuilder();
var err = new StringBuilder();
Process ps = new();
ps.StartInfo.FileName = "kill";
ps.StartInfo.Arguments = $"-STOP {process.Id}";
ps.StartInfo.UseShellExecute = false;
ps.StartInfo.RedirectStandardOutput = true;
ps.OutputDataReceived += (_, e) => output.Append(e.Data);
ps.ErrorDataReceived += (_, e) => err.Append(e.Data);
ps.Start();
ps.BeginOutputReadLine();
ps.WaitForExit(5_000);

if (!err.ToString().IsNullOrWhiteSpace())
{
EqtTrace.Verbose($"ProcessCodeMethods.SuspendLinuxMacOs: Error suspending process {process.Id} - {process.ProcessName}, {err}.");
}
}
catch (Exception ex)
{
EqtTrace.Verbose($"ProcessCodeMethods.GetParentPidMacOs: Error getting parent of process {process.Id} - {process.ProcessName}, {ex}.");
}
}

/// <summary>
/// Returns the parent id of a process or -1 if it fails.
/// </summary>
Expand Down Expand Up @@ -248,6 +215,7 @@ private static extern int NtQueryInformationProcess(
int processInformationLength,
out int returnLength);

[DllImport("ntdll.dll", SetLastError = true)]
private static extern IntPtr NtSuspendProcess(IntPtr processHandle);
// This call is undocumented api, and should not be used.
//[DllImport("ntdll.dll", SetLastError = true)]
//private static extern IntPtr NtSuspendProcess(IntPtr processHandle);
}

0 comments on commit 14a2995

Please sign in to comment.