Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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?

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.

Sounds good

{
/// <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 @@
}
}

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>

Check failure on line 495 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows Integration Tests .NET Framework

Check failure on line 495 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows

Check failure on line 495 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows

Check failure on line 495 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows

Check failure on line 495 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Linux

Check failure on line 495 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Linux

Check failure on line 495 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows Integration Tests .NET

/// <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

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.

I get it but if the best we can do is a catch-all then I would prefer to leave that up to the caller until there is a better solution (which I don't think would happen soon since the library throws a lot of exceptions around, for better or worse)

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.

Its not just a catch all, it completely avoids the exceptions for the channel not being open or command having completed, which is the biggest issue atm since its not possible to check for. The internal catch would otherwise need to have been done on the caller site either way so i don't really see the issue with that? It is an option and not mandatory, so people who want exceptions can still get them, but having a method that doesn't throw when executing in a dispose scenario is really a must. Exceptions take time to catch and unwind, which is not feasible at all in high usage scenarios. Especially for a call that is likely to be used in cleanup scenarios.

{
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>

Check failure on line 527 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows Integration Tests .NET Framework

Check failure on line 527 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows

Check failure on line 527 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows

Check failure on line 527 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows

Check failure on line 527 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Linux

Check failure on line 527 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Linux

Check failure on line 527 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows Integration Tests .NET

/// <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 +518 to +519
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.

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.

  1. I am not seeing where SshOperationTimeoutException would be thrown in this path
  2. You can leave it I guess, and maybe the library is inconsistent in its documentation, but I don't think this is documented (publicly) on any other path where it could happen, and it doesn't seem to have been a problem

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 just followed the call stack:

  • SShCommand.SendSignal()
  • ChannelSession.SendSignalRequest()
  • ChannelSession.SendMessage() => missing documentation of exceptions
  • Session.SendMessage() => documented as throwing them
  • Session.WaitOnHandle()
  • Session.WaitOnHandle() => throws SshOperationTimeoutException

I think its better to have a few calls that have all of them rather than all of them are missing ;)

/// <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.");
}

Check failure on line 539 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows Integration Tests .NET Framework

Check failure on line 539 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows

Check failure on line 539 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows

Check failure on line 539 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows

Check failure on line 539 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows

Check failure on line 539 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Linux

Check failure on line 539 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Linux

Check failure on line 539 in src/Renci.SshNet/SshCommand.cs

View workflow job for this annotation

GitHub Actions / Windows Integration Tests .NET

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
Loading