Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove process suspend #4586

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 struct ProcessBasicInformation
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);
}