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
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,26 @@ public void Refresh() { }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
public static bool TryGetProcessById(int processId, [System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out System.Diagnostics.Process? process) { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
public bool Signal(System.Runtime.InteropServices.PosixSignal signal) { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
public bool TryWaitForExitStatus(System.TimeSpan timeout, [System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] out System.Diagnostics.ProcessExitStatus? exitStatus) { throw null; }
public void WaitForExit() { }
public bool WaitForExit(int milliseconds) { throw null; }
public bool WaitForExit(System.TimeSpan timeout) { throw null; }
public System.Threading.Tasks.Task WaitForExitAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
public System.Diagnostics.ProcessExitStatus WaitForExitStatus() { throw null; }
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
public System.Threading.Tasks.Task<System.Diagnostics.ProcessExitStatus> WaitForExitStatusAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public bool WaitForInputIdle() { throw null; }
public bool WaitForInputIdle(int milliseconds) { throw null; }
public bool WaitForInputIdle(System.TimeSpan timeout) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Pipes;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -61,27 +62,40 @@ public void Kill()

EnsureState(State.HaveId);

SignalCore(PosixSignal.SIGKILL);
}

private bool SignalCore(PosixSignal signal)
{
// Check if we know the process has exited. This avoids us targeting another
// process that has a recycled PID. This only checks our internal state, the Kill call below
// actively checks if the process is still alive.
if (GetHasExited(refresh: false))
{
return;
return false;
}

int signalNumber = Interop.Sys.GetPlatformSignalNumber(signal);
if (signalNumber == 0)
{
throw new PlatformNotSupportedException();
}

int killResult = Interop.Sys.Kill(_processId, Interop.Sys.GetPlatformSignalNumber(PosixSignal.SIGKILL));
int killResult = Interop.Sys.Kill(_processId, signalNumber);
if (killResult != 0)
{
Interop.Error error = Interop.Sys.GetLastError();

// Don't throw if the process has exited.
if (error == Interop.Error.ESRCH)
{
return;
return false;
}

throw new Win32Exception(); // same exception as on Windows
}

return true;
}

private bool GetHasExited(bool refresh)
Expand Down Expand Up @@ -273,6 +287,17 @@ private void UpdateHasExited()
}
}

private bool TryGetExitStatus([NotNullWhen(true)] out ProcessExitStatus? exitStatus)
{
if (GetWaitState().GetExited(out exitStatus, refresh: false) && exitStatus is not null)
{
return true;
}

exitStatus = null;
return false;
}

/// <summary>Gets the time that the associated process exited.</summary>
private DateTime ExitTimeCore
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
Expand Down Expand Up @@ -81,6 +82,14 @@ public void Kill()
}
}

private bool SignalCore(PosixSignal signal)
{
using SafeProcessHandle handle = GetProcessHandle(Interop.Advapi32.ProcessOptions.PROCESS_TERMINATE | Interop.Advapi32.ProcessOptions.PROCESS_QUERY_LIMITED_INFORMATION, throwIfExited: false);
return handle.IsInvalid
? false // If the process has exited, the returned handle is invalid.
: handle.Signal(signal);
}

/// <summary>Discards any information about the associated process.</summary>
private void RefreshCore()
{
Expand Down Expand Up @@ -186,6 +195,19 @@ private void UpdateHasExited()
}
}

private bool TryGetExitStatus([NotNullWhen(true)] out ProcessExitStatus? exitStatus)
{
if (_exited)
{
// We pass canceled: false here because Process does not expose WaitForExitOrKill methods.
exitStatus = new ProcessExitStatus(_exitCode, canceled: false);
return true;
}

exitStatus = null;
return false;
}

/// <summary>Gets the time that the associated process exited.</summary>
private DateTime ExitTimeCore
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
Expand Down Expand Up @@ -1758,6 +1757,128 @@ async Task WaitUntilOutputEOF(CancellationToken cancellationToken)
}
}

/// <inheritdoc cref="SafeProcessHandle.Signal(PosixSignal)"/>
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
public bool Signal(PosixSignal signal)
{
if (!ProcessUtils.PlatformSupportsProcessStartAndKill)
{
throw new PlatformNotSupportedException();
}

CheckDisposed();
EnsureState(State.HaveId);

return _processHandle?.Signal(signal) ?? SignalCore(signal);
}

/// <inheritdoc cref="SafeProcessHandle.WaitForExit()"/>
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
public ProcessExitStatus WaitForExitStatus()
{
if (!ProcessUtils.PlatformSupportsProcessStartAndKill)
{
throw new PlatformNotSupportedException();
}

CheckDisposed();
EnsureState(State.HaveId | State.IsLocal);

if (TryGetExitStatus(out ProcessExitStatus? exitStatus))
{
return exitStatus;
}
else if (_processHandle is not null)
Comment thread
adamsitnik marked this conversation as resolved.
Resolved
{
return _processHandle.WaitForExit();
}

if (SafeProcessHandle.TryOpen(_processId, out SafeProcessHandle? processHandle))
{
using (processHandle)
Comment thread
adamsitnik marked this conversation as resolved.
Resolved
{
return processHandle.WaitForExit();
}
}

// Check the cached exit status one last time, in case the process exited between the previous check and the handle open attempt.
return TryGetExitStatus(out exitStatus) ? exitStatus : throw new InvalidOperationException(SR.Format(SR.ProcessHasExited, _processId.ToString()));
}

/// <inheritdoc cref="SafeProcessHandle.TryWaitForExit(TimeSpan, out ProcessExitStatus?)"/>
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
public bool TryWaitForExitStatus(TimeSpan timeout, [NotNullWhen(true)] out ProcessExitStatus? exitStatus)
{
if (!ProcessUtils.PlatformSupportsProcessStartAndKill)
{
throw new PlatformNotSupportedException();
}

CheckDisposed();
EnsureState(State.HaveId | State.IsLocal);
_ = ProcessUtils.ToTimeoutMilliseconds(timeout);

if (TryGetExitStatus(out exitStatus))
{
return true;
}
Comment thread
adamsitnik marked this conversation as resolved.
Resolved
else if (_processHandle is not null)
{
return _processHandle.TryWaitForExit(timeout, out exitStatus);
}

if (SafeProcessHandle.TryOpen(_processId, out SafeProcessHandle? processHandle))
{
using (processHandle)
{
return processHandle.TryWaitForExit(timeout, out exitStatus);
}
}

return TryGetExitStatus(out exitStatus) ? true : throw new InvalidOperationException(SR.Format(SR.ProcessHasExited, _processId.ToString()));
}

/// <inheritdoc cref="SafeProcessHandle.WaitForExitAsync(CancellationToken)"/>
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
public async Task<ProcessExitStatus> WaitForExitStatusAsync(CancellationToken cancellationToken = default)
{
if (!ProcessUtils.PlatformSupportsProcessStartAndKill)
{
throw new PlatformNotSupportedException();
}

CheckDisposed();
EnsureState(State.HaveId | State.IsLocal);

if (TryGetExitStatus(out ProcessExitStatus? exitStatus))
{
return exitStatus;
}
else if (_processHandle is not null)
{
return await _processHandle.WaitForExitAsync(cancellationToken).ConfigureAwait(false);
}

if (SafeProcessHandle.TryOpen(_processId, out SafeProcessHandle? processHandle))
{
using (processHandle)
{
return await processHandle.WaitForExitAsync(cancellationToken).ConfigureAwait(false);
}
}

// Check the cached exit status one last time, in case the process exited between the previous check and the handle open attempt.
return TryGetExitStatus(out exitStatus) ? exitStatus : throw new InvalidOperationException(SR.Format(SR.ProcessHasExited, _processId.ToString()));
}

/// <devdoc>
/// <para>
/// Instructs the <see cref='System.Diagnostics.Process'/> component to start
Expand Down
Loading
Loading