Skip to content
Open
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
74 changes: 74 additions & 0 deletions src/Renci.SshNet/CommandSignal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace Renci.SshNet
{
/// <summary>
/// The ssh compatible POSIX/ANSI signals with their libc compatible values.
/// </summary>
#pragma warning disable CA1720 // Identifier contains type name
public enum CommandSignal
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this list exhaustive? I am thinking not based on the penultimate paragraph of 4254 section 6.10:

Additional 'signal name' values MAY be sent in the format
"sig-name@xyz", where "sig-name" and "xyz" may be anything a
particular implementer wants (except the "@" sign).

I think it would be more future-proof to have SendSignal(string) rather than SendSignal(enum). This type could still exist as a static class for discoverability e.g.

public static class CommandSignals
{
    /// <summary>
    /// Hangup (POSIX).
    /// </summary>
    public const string HUP = nameof(HUP);
}

Similar to https://learn.microsoft.com/dotnet/api/system.net.mime.mediatypenames

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, that would be better, I will adjust them. I propose we prepend the SIG then so its:
public const string SIGHUP = "HUP";
Would that be ok?

{
/// <summary>
/// Hangup (POSIX).
/// </summary>
HUP = 1,

/// <summary>
/// Interrupt (ANSI).
/// </summary>
INT = 2,

/// <summary>
/// Quit (POSIX).
/// </summary>
QUIT = 3,

/// <summary>
/// Illegal instruction (ANSI).
/// </summary>
ILL = 4,

/// <summary>
/// Abort (ANSI).
/// </summary>
ABRT = 6,

/// <summary>
/// Floating-point exception (ANSI).
/// </summary>
FPE = 8,

/// <summary>
/// Kill, unblockable (POSIX).
/// </summary>
KILL = 9,

/// <summary>
/// User-defined signal 1 (POSIX).
/// </summary>
USR1 = 10,

/// <summary>
/// Segmentation violation (ANSI).
/// </summary>
SEGV = 11,

/// <summary>
/// User-defined signal 2 (POSIX).
/// </summary>
USR2 = 12,

/// <summary>
/// Broken pipe (POSIX).
/// </summary>
PIPE = 13,

/// <summary>
/// Alarm clock (POSIX).
/// </summary>
ALRM = 14,

/// <summary>
/// Termination (ANSI).
/// </summary>
TERM = 15,
}
}
67 changes: 67 additions & 0 deletions src/Renci.SshNet/SshCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,73 @@ public void CancelAsync(bool forceKill = false, int millisecondsTimeout = 500)
}
}

private static string? GetSignalName(CommandSignal signal)
{
#if NETCOREAPP
return Enum.GetName(signal);
#else

// Boxes signal, but Enum.GetName does not have a non-boxing overload prior to .NET Core.
return Enum.GetName(typeof(CommandSignal), signal);
#endif
}

/// <summary>
/// Tries to send a POSIX/ANSI signal to the remote process executing the command, such as SIGINT or SIGTERM.
/// </summary>
/// <param name="signal">The signal to send</param>
/// <returns>If the signal was sent.</returns>
public bool TrySendSignal(CommandSignal signal)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's drop this and stick with just SendSignal

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is some benefit of having a TrySendSignal though, it can be used as a last resort to send to every command started upon something happening like a server going down or similar without having to catch all the exceptions or doing all the checks yourself (some of which you don't even have access to).
channel.IsOpen for instance will cause an exception in SendSignal, but is impossible to check outside the class, leaving no option but to catch exceptions if that would be the case. Exceptions should be exceptional and methods that are prone to throwing should have Try variants according to ms documentation and best practices: https://learn.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions

{
var signalName = GetSignalName(signal);
if (signalName is null)
{
return false;
}

if (_tcs is null || _tcs.Task.IsCompleted || _channel?.IsOpen != true)
{
return false;
}

try
{
// Try to send the cancellation signal.
return _channel.SendSignalRequest(signalName);
}
catch (Exception)
{
// Exception can be ignored since we are in a Try method
// Possible exceptions here: InvalidOperationException, SshConnectionException, SshOperationTimeoutException
}

return false;
}

/// <summary>
/// Tries to send a POSIX/ANSI signal to the remote process executing the command, such as SIGINT or SIGTERM.
/// </summary>
/// <param name="signal">The signal to send</param>
/// <exception cref="ArgumentException">Signal was not a valid CommandSignal.</exception>
/// <exception cref="SshConnectionException">The client is not connected.</exception>
/// <exception cref="SshOperationTimeoutException">The operation timed out.</exception>
/// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>
Comment on lines +530 to +531
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <exception cref="SshOperationTimeoutException">The operation timed out.</exception>
/// <exception cref="InvalidOperationException">The size of the packet exceeds the maximum size defined by the protocol.</exception>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exactly would these be removed? They are exceptions from the underlying ssh calls.

/// <exception cref="InvalidOperationException">Command has not been started.</exception>
public void SendSignal(CommandSignal signal)
{
var signalName = GetSignalName(signal);
if (signalName is null)
{
throw new ArgumentException("Signal was not a valid CommandSignal.");
}
if (_tcs is null || _tcs.Task.IsCompleted || _channel?.IsOpen != true)
{
throw new InvalidOperationException("Command has not been started.");
}

_ = _channel.SendSignalRequest(signalName);
}

/// <summary>
/// Executes the command specified by <see cref="CommandText"/>.
/// </summary>
Expand Down