Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,16 @@ internal static int NonWindowsGetProcessParentPid(int pid)
return IsMacOS ? Unix.NativeMethods.GetPPid(pid) : Unix.GetProcFSParentPid(pid);
}

internal static bool NonWindowsKillProcess(int pid)
{
return Unix.NativeMethods.KillProcess(pid);
}

internal static int NonWindowsWaitPid(int pid, bool nohang)
{
return Unix.NativeMethods.WaitPid(pid, nohang);
}

internal static class Windows
{
/// <summary>The native methods class.</summary>
Expand Down Expand Up @@ -1063,6 +1073,13 @@ internal static class NativeMethods
[DllImport(psLib, CharSet = CharSet.Ansi)]
internal static extern uint GetCurrentThreadId();

[DllImport(psLib)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool KillProcess(int pid);

[DllImport(psLib)]
internal static extern int WaitPid(int pid, bool nohang);

// This is a struct tm from <time.h>.
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct UnixTm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0-preview.3.21201.4" />
<!-- the following package(s) are from the powershell org -->
<PackageReference Include="Microsoft.Management.Infrastructure" Version="2.0.0" />
<PackageReference Include="Microsoft.PowerShell.Native" Version="7.2.0-preview.1" />
<PackageReference Include="Microsoft.PowerShell.Native" Version="7.2.0-preview.2" />
</ItemGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,11 @@ internal int StartSSHProcess(
return StartSSHProcessImpl(startInfo, out stdInWriterVar, out stdOutReaderVar, out stdErrReaderVar);
}

internal void KillSSHProcess(int pid)
{
KillSSHProcessImpl(pid);
}

#endregion

#region SSH Process Creation
Expand Down Expand Up @@ -2265,6 +2270,16 @@ private static int StartSSHProcessImpl(
return pid;
}

private static void KillSSHProcessImpl(int pid)
{
// killing a zombie might or might not return ESRCH, so we ignore kill's return value
Platform.NonWindowsKillProcess(pid);

// block while waiting for process to die
// shouldn't take long after SIGKILL
Platform.NonWindowsWaitPid(pid, false);
}

#region UNIX Create Process

//
Expand Down Expand Up @@ -2570,6 +2585,17 @@ private static int StartSSHProcessImpl(
return sshProcess.Id;
}

private static void KillSSHProcessImpl(int pid)
{
using (var sshProcess = System.Diagnostics.Process.GetProcessById(pid))
{
if ((sshProcess != null) && (sshProcess.Handle != IntPtr.Zero) && !sshProcess.HasExited)
{
sshProcess.Kill();
}
}
}

// Process creation flags
private const int CREATE_NEW_PROCESS_GROUP = 0x00000200;
private const int CREATE_SUSPENDED = 0x00000004;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1735,13 +1735,7 @@ private void CloseConnection()
{
try
{
using (var sshProcess = System.Diagnostics.Process.GetProcessById(sshProcessId))
{
if ((sshProcess != null) && (sshProcess.Handle != IntPtr.Zero) && !sshProcess.HasExited)
{
sshProcess.Kill();
}
}
_connectionInfo.KillSSHProcess(sshProcessId);
}
catch (ArgumentException) { }
catch (InvalidOperationException) { }
Expand Down