Skip to content
Merged
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
29 changes: 18 additions & 11 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,30 +193,37 @@ internal static int Start(string bannerText, string helpText)
{
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("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;
}
else if (s_cpp.NamedPipeServerMode)
{
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("NamedPipe");
ProfileOptimization.StartProfile("StartupProfileData-NamedPipeServerMode");
System.Management.Automation.Remoting.RemoteSessionNamedPipeServer.RunServerMode(
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);
configurationName: s_cpp.ConfigurationName);
exitCode = 0;
}
else if (s_cpp.SocketServerMode)
{
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SocketServerMode");
ProfileOptimization.StartProfile("StartupProfileData-SocketServerMode");
System.Management.Automation.Remoting.Server.HyperVSocketMediator.Run(s_cpp.InitialCommand,
s_cpp.ConfigurationName);
System.Management.Automation.Remoting.Server.HyperVSocketMediator.Run(
initialCommand: s_cpp.InitialCommand,
configurationName: s_cpp.ConfigurationName);
exitCode = 0;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#if !UNIX
using System.Security.Principal;
#endif
using Microsoft.Win32.SafeHandles;

using Dbg = System.Management.Automation.Diagnostics;

Expand Down Expand Up @@ -73,14 +72,6 @@ protected void ProcessingThreadStart(object state)
{
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;
OutOfProcessUtils.ProcessData(data, callbacks);
}
Expand Down Expand Up @@ -307,7 +298,10 @@ protected void OnCloseAckPacketReceived(Guid psGuid)

#region Methods

protected OutOfProcessServerSessionTransportManager CreateSessionTransportManager(string configurationName, PSRemotingCryptoHelperServer cryptoHelper, string workingDirectory)
protected OutOfProcessServerSessionTransportManager CreateSessionTransportManager(
string configurationName,
PSRemotingCryptoHelperServer cryptoHelper,
string workingDirectory)
{
PSSenderInfo senderInfo;
#if !UNIX
Expand Down Expand Up @@ -335,7 +329,11 @@ protected OutOfProcessServerSessionTransportManager CreateSessionTransportManage
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;

Expand Down Expand Up @@ -417,46 +415,24 @@ protected void Start(string initialCommand, PSRemotingCryptoHelperServer cryptoH
}

#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 OutOfProcessMediator : OutOfProcessMediatorBase
internal sealed class StdIOProcessMediator : OutOfProcessMediatorBase
{
#region Private Data

private static OutOfProcessMediator s_singletonInstance;
private static StdIOProcessMediator s_singletonInstance;

#endregion

#region Constructors

/// <summary>
/// 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.
/// </summary>
private OutOfProcessMediator() : base(true)
private StdIOProcessMediator() : base(true)
{
// Create input stream reader from Console standard input stream.
// We don't use the provided Console.In TextReader because it can have
Expand All @@ -465,9 +441,6 @@ private OutOfProcessMediator() : base(true)
// stream encoding. This way the stream encoding is determined by the
// stream BOM as needed.
originalStdIn = new StreamReader(Console.OpenStandardInput(), true);

// replacing StdIn with Null so that no other app messes with the
// original stream.
Console.SetIn(TextReader.Null);

// replacing StdOut with Null so that no other app messes with the
Expand All @@ -490,61 +463,11 @@ private OutOfProcessMediator() : base(true)
/// </summary>
/// <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>
internal static void Run(string initialCommand, string workingDirectory)
{
lock (SyncObject)
{
if (s_singletonInstance != null)
{
Dbg.Assert(false, "Run should not be called multiple times");
return;
}

s_singletonInstance = new OutOfProcessMediator();
}

#if !CORECLR // AppDomain is not available in CoreCLR
// Setup unhandled exception to log events
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException);
#endif
s_singletonInstance.Start(initialCommand, new PSRemotingCryptoHelperServer(), workingDirectory);
}

#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)
/// <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)
{
Expand All @@ -554,10 +477,14 @@ internal static void Run(string initialCommand)
return;
}

s_singletonInstance = new SSHProcessMediator();
s_singletonInstance = new StdIOProcessMediator();
}

s_singletonInstance.Start(initialCommand, new PSRemotingCryptoHelperServer());
s_singletonInstance.Start(
initialCommand: initialCommand,
cryptoHelper: new PSRemotingCryptoHelperServer(),
workingDirectory: workingDirectory,
configurationName: configurationName);
}

#endregion
Expand Down Expand Up @@ -626,11 +553,11 @@ internal static void Run(
s_singletonInstance = new NamedPipeProcessMediator(namedPipeServer);
}

#if !CORECLR
// AppDomain is not available in CoreCLR
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException);
#endif
s_singletonInstance.Start(initialCommand, new PSRemotingCryptoHelperServer(), namedPipeServer.ConfigurationName);
s_singletonInstance.Start(
initialCommand: initialCommand,
cryptoHelper: new PSRemotingCryptoHelperServer(),
workingDirectory: null,
configurationName: namedPipeServer.ConfigurationName);
}

#endregion
Expand Down Expand Up @@ -716,12 +643,11 @@ internal static void Run(
s_instance = new HyperVSocketMediator();
}

#if !CORECLR
// AppDomain is not available in CoreCLR
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException);
#endif

s_instance.Start(initialCommand, new PSRemotingCryptoHelperServer(), configurationName);
s_instance.Start(
initialCommand: initialCommand,
cryptoHelper: new PSRemotingCryptoHelperServer(),
workingDirectory: null,
configurationName: configurationName);
}

#endregion
Expand Down