-
-
Notifications
You must be signed in to change notification settings - Fork 979
Add ability to send posix/ansi signals in SshCommand #1777
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| { | ||
| /// <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, | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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
|
||||||
| /// <returns>If the signal was sent.</returns> | ||||||
| public bool TrySendSignal(CommandSignal signal) | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's drop this and stick with just
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
| /// <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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just followed the call stack:
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
|
||||||
| 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> | ||||||
|
|
||||||
There was a problem hiding this comment.
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:
I think it would be more future-proof to have
SendSignal(string)rather thanSendSignal(enum). This type could still exist as a static class for discoverability e.g.Similar to https://learn.microsoft.com/dotnet/api/system.net.mime.mediatypenames
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good