-
-
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 all commits
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 @@ 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) | ||||||
|
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). |
||||||
| { | ||||||
| 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
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. |
||||||
| /// <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> | ||||||
|
|
||||||
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?