Remove duplicate remote server mediator code (#16027)

This commit is contained in:
Paul Higinbotham 2021-09-01 09:31:19 -07:00 committed by Aditya Patwardhan
parent 7e65ade6bd
commit 5f2e397154
2 changed files with 52 additions and 119 deletions

View file

@ -193,7 +193,20 @@ namespace Microsoft.PowerShell
{ {
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("ServerMode"); ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("ServerMode");
ProfileOptimization.StartProfile("StartupProfileData-ServerMode"); ProfileOptimization.StartProfile("StartupProfileData-ServerMode");
System.Management.Automation.Remoting.Server.OutOfProcessMediator.Run(s_cpp.InitialCommand, s_cpp.WorkingDirectory); System.Management.Automation.Remoting.Server.StdIOProcessMediator.Run(
initialCommand: s_cpp.InitialCommand,
workingDirectory: s_cpp.WorkingDirectory,
configurationName: null);
exitCode = 0;
}
else if (s_cpp.SSHServerMode)
{
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SSHServer");
ProfileOptimization.StartProfile("StartupProfileData-SSHServerMode");
System.Management.Automation.Remoting.Server.StdIOProcessMediator.Run(
initialCommand: s_cpp.InitialCommand,
workingDirectory: null,
configurationName: null);
exitCode = 0; exitCode = 0;
} }
else if (s_cpp.NamedPipeServerMode) else if (s_cpp.NamedPipeServerMode)
@ -201,22 +214,16 @@ namespace Microsoft.PowerShell
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("NamedPipe"); ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("NamedPipe");
ProfileOptimization.StartProfile("StartupProfileData-NamedPipeServerMode"); ProfileOptimization.StartProfile("StartupProfileData-NamedPipeServerMode");
System.Management.Automation.Remoting.RemoteSessionNamedPipeServer.RunServerMode( System.Management.Automation.Remoting.RemoteSessionNamedPipeServer.RunServerMode(
s_cpp.ConfigurationName); configurationName: s_cpp.ConfigurationName);
exitCode = 0;
}
else if (s_cpp.SSHServerMode)
{
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SSHServer");
ProfileOptimization.StartProfile("StartupProfileData-SSHServerMode");
System.Management.Automation.Remoting.Server.SSHProcessMediator.Run(s_cpp.InitialCommand);
exitCode = 0; exitCode = 0;
} }
else if (s_cpp.SocketServerMode) else if (s_cpp.SocketServerMode)
{ {
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SocketServerMode"); ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SocketServerMode");
ProfileOptimization.StartProfile("StartupProfileData-SocketServerMode"); ProfileOptimization.StartProfile("StartupProfileData-SocketServerMode");
System.Management.Automation.Remoting.Server.HyperVSocketMediator.Run(s_cpp.InitialCommand, System.Management.Automation.Remoting.Server.HyperVSocketMediator.Run(
s_cpp.ConfigurationName); initialCommand: s_cpp.InitialCommand,
configurationName: s_cpp.ConfigurationName);
exitCode = 0; exitCode = 0;
} }
else else

View file

@ -8,7 +8,6 @@ using System.Threading;
#if !UNIX #if !UNIX
using System.Security.Principal; using System.Security.Principal;
#endif #endif
using Microsoft.Win32.SafeHandles;
using Dbg = System.Management.Automation.Diagnostics; using Dbg = System.Management.Automation.Diagnostics;
@ -73,14 +72,6 @@ namespace System.Management.Automation.Remoting.Server
{ {
try try
{ {
#if !CORECLR
// CurrentUICulture is not available in Thread Class in CSS
// WinBlue: 621775. Thread culture is not properly set
// for local background jobs causing experience differences
// between local console and local background jobs.
Thread.CurrentThread.CurrentUICulture = Microsoft.PowerShell.NativeCultureResolver.UICulture;
Thread.CurrentThread.CurrentCulture = Microsoft.PowerShell.NativeCultureResolver.Culture;
#endif
string data = state as string; string data = state as string;
OutOfProcessUtils.ProcessData(data, callbacks); OutOfProcessUtils.ProcessData(data, callbacks);
} }
@ -307,7 +298,10 @@ namespace System.Management.Automation.Remoting.Server
#region Methods #region Methods
protected OutOfProcessServerSessionTransportManager CreateSessionTransportManager(string configurationName, PSRemotingCryptoHelperServer cryptoHelper, string workingDirectory) protected OutOfProcessServerSessionTransportManager CreateSessionTransportManager(
string configurationName,
PSRemotingCryptoHelperServer cryptoHelper,
string workingDirectory)
{ {
PSSenderInfo senderInfo; PSSenderInfo senderInfo;
#if !UNIX #if !UNIX
@ -335,7 +329,11 @@ namespace System.Management.Automation.Remoting.Server
return tm; return tm;
} }
protected void Start(string initialCommand, PSRemotingCryptoHelperServer cryptoHelper, string workingDirectory = null, string configurationName = null) protected void Start(
string initialCommand,
PSRemotingCryptoHelperServer cryptoHelper,
string workingDirectory,
string configurationName)
{ {
_initialCommand = initialCommand; _initialCommand = initialCommand;
@ -417,35 +415,13 @@ namespace System.Management.Automation.Remoting.Server
} }
#endregion #endregion
#region Static Methods
internal static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs args)
{
// args can never be null.
Exception exception = (Exception)args.ExceptionObject;
// log the exception to crimson event logs
PSEtwLog.LogOperationalError(PSEventId.AppDomainUnhandledException,
PSOpcode.Close, PSTask.None,
PSKeyword.UseAlwaysOperational,
exception.GetType().ToString(), exception.Message,
exception.StackTrace);
PSEtwLog.LogAnalyticError(PSEventId.AppDomainUnhandledException_Analytic,
PSOpcode.Close, PSTask.None,
PSKeyword.ManagedPlugin | PSKeyword.UseAlwaysAnalytic,
exception.GetType().ToString(), exception.Message,
exception.StackTrace);
} }
#endregion internal sealed class StdIOProcessMediator : OutOfProcessMediatorBase
}
internal sealed class OutOfProcessMediator : OutOfProcessMediatorBase
{ {
#region Private Data #region Private Data
private static OutOfProcessMediator s_singletonInstance; private static StdIOProcessMediator s_singletonInstance;
#endregion #endregion
@ -453,10 +429,10 @@ namespace System.Management.Automation.Remoting.Server
/// <summary> /// <summary>
/// The mediator will take actions from the StdIn stream and responds to them. /// The mediator will take actions from the StdIn stream and responds to them.
/// It will replace StdIn,StdOut and StdErr stream with TextWriter.Null's. This is /// It will replace StdIn,StdOut and StdErr stream with TextWriter.Null. This is
/// to make sure these streams are totally used by our Mediator. /// to make sure these streams are totally used by our Mediator.
/// </summary> /// </summary>
private OutOfProcessMediator() : base(true) private StdIOProcessMediator() : base(true)
{ {
// Create input stream reader from Console standard input stream. // Create input stream reader from Console standard input stream.
// We don't use the provided Console.In TextReader because it can have // We don't use the provided Console.In TextReader because it can have
@ -465,9 +441,6 @@ namespace System.Management.Automation.Remoting.Server
// stream encoding. This way the stream encoding is determined by the // stream encoding. This way the stream encoding is determined by the
// stream BOM as needed. // stream BOM as needed.
originalStdIn = new StreamReader(Console.OpenStandardInput(), true); originalStdIn = new StreamReader(Console.OpenStandardInput(), true);
// replacing StdIn with Null so that no other app messes with the
// original stream.
Console.SetIn(TextReader.Null); Console.SetIn(TextReader.Null);
// replacing StdOut with Null so that no other app messes with the // replacing StdOut with Null so that no other app messes with the
@ -490,7 +463,11 @@ namespace System.Management.Automation.Remoting.Server
/// </summary> /// </summary>
/// <param name="initialCommand">Specifies the initialization script.</param> /// <param name="initialCommand">Specifies the initialization script.</param>
/// <param name="workingDirectory">Specifies the initial working directory. The working directory is set before the initial command.</param> /// <param name="workingDirectory">Specifies the initial working directory. The working directory is set before the initial command.</param>
internal static void Run(string initialCommand, string workingDirectory) /// <param name="configurationName">Specifies an optional configuration name that configures the endpoint session.</param>
internal static void Run(
string initialCommand,
string workingDirectory,
string configurationName)
{ {
lock (SyncObject) lock (SyncObject)
{ {
@ -500,64 +477,14 @@ namespace System.Management.Automation.Remoting.Server
return; return;
} }
s_singletonInstance = new OutOfProcessMediator(); s_singletonInstance = new StdIOProcessMediator();
} }
#if !CORECLR // AppDomain is not available in CoreCLR s_singletonInstance.Start(
// Setup unhandled exception to log events initialCommand: initialCommand,
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException); cryptoHelper: new PSRemotingCryptoHelperServer(),
#endif workingDirectory: workingDirectory,
s_singletonInstance.Start(initialCommand, new PSRemotingCryptoHelperServer(), workingDirectory); configurationName: configurationName);
}
#endregion
}
internal sealed class SSHProcessMediator : OutOfProcessMediatorBase
{
#region Private Data
private static SSHProcessMediator s_singletonInstance;
#endregion
#region Constructors
private SSHProcessMediator() : base(true)
{
originalStdIn = new StreamReader(Console.OpenStandardInput(), true);
originalStdOut = new OutOfProcessTextWriter(
new StreamWriter(Console.OpenStandardOutput()));
originalStdErr = new OutOfProcessTextWriter(
new StreamWriter(Console.OpenStandardError()));
// Disable console from writing to the PSRP streams.
Console.SetIn(TextReader.Null);
Console.SetOut(TextWriter.Null);
Console.SetError(TextWriter.Null);
}
#endregion
#region Static Methods
/// <summary>
/// </summary>
/// <param name="initialCommand"></param>
internal static void Run(string initialCommand)
{
lock (SyncObject)
{
if (s_singletonInstance != null)
{
Dbg.Assert(false, "Run should not be called multiple times");
return;
}
s_singletonInstance = new SSHProcessMediator();
}
s_singletonInstance.Start(initialCommand, new PSRemotingCryptoHelperServer());
} }
#endregion #endregion
@ -626,11 +553,11 @@ namespace System.Management.Automation.Remoting.Server
s_singletonInstance = new NamedPipeProcessMediator(namedPipeServer); s_singletonInstance = new NamedPipeProcessMediator(namedPipeServer);
} }
#if !CORECLR s_singletonInstance.Start(
// AppDomain is not available in CoreCLR initialCommand: initialCommand,
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException); cryptoHelper: new PSRemotingCryptoHelperServer(),
#endif workingDirectory: null,
s_singletonInstance.Start(initialCommand, new PSRemotingCryptoHelperServer(), namedPipeServer.ConfigurationName); configurationName: namedPipeServer.ConfigurationName);
} }
#endregion #endregion
@ -716,12 +643,11 @@ namespace System.Management.Automation.Remoting.Server
s_instance = new HyperVSocketMediator(); s_instance = new HyperVSocketMediator();
} }
#if !CORECLR s_instance.Start(
// AppDomain is not available in CoreCLR initialCommand: initialCommand,
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException); cryptoHelper: new PSRemotingCryptoHelperServer(),
#endif workingDirectory: null,
configurationName: configurationName);
s_instance.Start(initialCommand, new PSRemotingCryptoHelperServer(), configurationName);
} }
#endregion #endregion