forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutOfProcTransportManager.cs
More file actions
2365 lines (1986 loc) · 90.8 KB
/
Copy pathOutOfProcTransportManager.cs
File metadata and controls
2365 lines (1986 loc) · 90.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************++
* Copyright (c) Microsoft Corporation. All rights reserved.
* --********************************************************************/
/*
* Common file that contains implementation for both server and client transport
* managers for Out-Of-Process and Named Pipe (on the local machine) remoting implementation.
* These interfaces are used by *-Job cmdlets to support background jobs and
* attach-to-process feature without depending on WinRM (WinRM has complex requirements like
* elevation to support local machine remoting).
*/
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Management.Automation.Internal;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Tracing;
using System.Net;
using System.Threading;
using System.Xml;
using PSRemotingCryptoHelper = System.Management.Automation.Internal.PSRemotingCryptoHelper;
using PSRemotingCryptoHelperServer = System.Management.Automation.Internal.PSRemotingCryptoHelperServer;
using RunspaceConnectionInfo = System.Management.Automation.Runspaces.RunspaceConnectionInfo;
using ClientRemotePowerShell = System.Management.Automation.Runspaces.Internal.ClientRemotePowerShell;
using NewProcessConnectionInfo = System.Management.Automation.Runspaces.NewProcessConnectionInfo;
using PSTask = System.Management.Automation.Internal.PSTask;
using PSOpcode = System.Management.Automation.Internal.PSOpcode;
using PSEventId = System.Management.Automation.Internal.PSEventId;
using TypeTable = System.Management.Automation.Runspaces.TypeTable;
using Dbg = System.Management.Automation.Diagnostics;
namespace System.Management.Automation.Remoting
{
internal static class OutOfProcessUtils
{
#region Helper Fields
internal const string PS_OUT_OF_PROC_DATA_TAG = "Data";
internal const string PS_OUT_OF_PROC_DATA_ACK_TAG = "DataAck";
internal const string PS_OUT_OF_PROC_STREAM_ATTRIBUTE = "Stream";
internal const string PS_OUT_OF_PROC_PSGUID_ATTRIBUTE = "PSGuid";
internal const string PS_OUT_OF_PROC_CLOSE_TAG = "Close";
internal const string PS_OUT_OF_PROC_CLOSE_ACK_TAG = "CloseAck";
internal const string PS_OUT_OF_PROC_COMMAND_TAG = "Command";
internal const string PS_OUT_OF_PROC_COMMAND_ACK_TAG = "CommandAck";
internal const string PS_OUT_OF_PROC_SIGNAL_TAG = "Signal";
internal const string PS_OUT_OF_PROC_SIGNAL_ACK_TAG = "SignalAck";
internal const int EXITCODE_UNHANDLED_EXCEPTION = 0x0FA0;
internal static XmlReaderSettings XmlReaderSettings;
#endregion
#region Static Constructor
static OutOfProcessUtils()
{
// data coming from inputs stream is in Xml format. create appropriate
// xml reader settings only once and reuse the same settings for all
// the reads from StdIn stream.
XmlReaderSettings = new XmlReaderSettings();
XmlReaderSettings.CheckCharacters = false;
XmlReaderSettings.IgnoreComments = true;
XmlReaderSettings.IgnoreProcessingInstructions = true;
#if !CORECLR // No XmlReaderSettings.XmlResolver in CoreCLR
XmlReaderSettings.XmlResolver = null;
#endif
XmlReaderSettings.ConformanceLevel = ConformanceLevel.Fragment;
}
#endregion
#region Packet Creation Helper Methods
internal static string CreateDataPacket(byte[] data, DataPriorityType streamType, Guid psGuid)
{
string result = string.Format(CultureInfo.InvariantCulture,
"<{0} {1}='{2}' {3}='{4}'>{5}</{0}>",
PS_OUT_OF_PROC_DATA_TAG,
PS_OUT_OF_PROC_STREAM_ATTRIBUTE,
streamType.ToString(),
PS_OUT_OF_PROC_PSGUID_ATTRIBUTE,
psGuid.ToString(),
Convert.ToBase64String(data));
return result;
}
internal static string CreateDataAckPacket(Guid psGuid)
{
return CreatePSGuidPacket(PS_OUT_OF_PROC_DATA_ACK_TAG, psGuid);
}
internal static string CreateCommandPacket(Guid psGuid)
{
return CreatePSGuidPacket(PS_OUT_OF_PROC_COMMAND_TAG, psGuid);
}
internal static string CreateCommandAckPacket(Guid psGuid)
{
return CreatePSGuidPacket(PS_OUT_OF_PROC_COMMAND_ACK_TAG, psGuid);
}
internal static string CreateClosePacket(Guid psGuid)
{
return CreatePSGuidPacket(PS_OUT_OF_PROC_CLOSE_TAG, psGuid);
}
internal static string CreateCloseAckPacket(Guid psGuid)
{
return CreatePSGuidPacket(PS_OUT_OF_PROC_CLOSE_ACK_TAG, psGuid);
}
internal static string CreateSignalPacket(Guid psGuid)
{
return CreatePSGuidPacket(PS_OUT_OF_PROC_SIGNAL_TAG, psGuid);
}
internal static string CreateSignalAckPacket(Guid psGuid)
{
return CreatePSGuidPacket(PS_OUT_OF_PROC_SIGNAL_ACK_TAG, psGuid);
}
/// <summary>
/// Common method to create a packet that contains only a PS Guid
/// with element name changing
/// </summary>
/// <param name="element"></param>
/// <param name="psGuid"></param>
/// <returns></returns>
private static string CreatePSGuidPacket(string element, Guid psGuid)
{
string result = string.Format(CultureInfo.InvariantCulture,
"<{0} {1}='{2}' />",
element,
PS_OUT_OF_PROC_PSGUID_ATTRIBUTE,
psGuid.ToString());
return result;
}
#endregion
#region Packet Processing Helper Methods / Delegates
internal delegate void DataPacketReceived(byte[] rawData, string stream, Guid psGuid);
internal delegate void DataAckPacketReceived(Guid psGuid);
internal delegate void CommandCreationPacketReceived(Guid psGuid);
internal delegate void CommandCreationAckReceived(Guid psGuid);
internal delegate void ClosePacketReceived(Guid psGuid);
internal delegate void CloseAckPacketReceived(Guid psGuid);
internal delegate void SignalPacketReceived(Guid psGuid);
internal delegate void SignalAckPacketReceived(Guid psGuid);
internal struct DataProcessingDelegates
{
internal DataPacketReceived DataPacketReceived;
internal DataAckPacketReceived DataAckPacketReceived;
internal CommandCreationPacketReceived CommandCreationPacketReceived;
internal CommandCreationAckReceived CommandCreationAckReceived;
internal SignalPacketReceived SignalPacketReceived;
internal SignalAckPacketReceived SignalAckPacketReceived;
internal ClosePacketReceived ClosePacketReceived;
internal CloseAckPacketReceived CloseAckPacketReceived;
}
/// <summary>
/// Process's data. Data should be a valid XML.
/// </summary>
/// <param name="data"></param>
/// <param name="callbacks"></param>
/// <exception cref="PSRemotingTransportException">
/// 1. Expected only two attributes with names "{0}" and "{1}" in "{2}" element.
/// 2. Not enough data available to process "{0}" element.
/// 3. Unknown node "{0}" in "{1}" element. Only "{2}" is expected in "{1}" element.
/// </exception>
internal static void ProcessData(string data, DataProcessingDelegates callbacks)
{
if (string.IsNullOrEmpty(data))
{
return;
}
XmlReader reader = XmlReader.Create(new StringReader(data), XmlReaderSettings);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
ProcessElement(reader, callbacks);
break;
case XmlNodeType.EndElement:
break;
default:
throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownNodeType, RemotingErrorIdStrings.IPCUnknownNodeType,
reader.NodeType.ToString(),
XmlNodeType.Element.ToString(),
XmlNodeType.EndElement.ToString());
}
}
}
/// <summary>
/// Process an XmlElement. The element name must be one of the following:
/// "Data"
/// </summary>
/// <param name="xmlReader"></param>
/// <param name="callbacks"></param>
/// <exception cref="PSRemotingTransportException">
/// 1. Expected only two attributes with names "{0}" and "{1}" in "{2}" element.
/// 2. Not enough data available to process "{0}" element.
/// 3. Unknown node "{0}" in "{1}" element. Only "{2}" is expected in "{1}" element.
/// </exception>
private static void ProcessElement(XmlReader xmlReader, DataProcessingDelegates callbacks)
{
Dbg.Assert(null != xmlReader, "xmlReader cannot be null.");
Dbg.Assert(xmlReader.NodeType == XmlNodeType.Element, "xmlReader's NodeType should be of type Element");
PowerShellTraceSource tracer = PowerShellTraceSourceFactory.GetTraceSource();
switch (xmlReader.LocalName)
{
case OutOfProcessUtils.PS_OUT_OF_PROC_DATA_TAG:
{
// A <Data> should have 1 attribute identifying the stream
if (xmlReader.AttributeCount != 2)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForDataElement,
RemotingErrorIdStrings.IPCWrongAttributeCountForDataElement,
OutOfProcessUtils.PS_OUT_OF_PROC_STREAM_ATTRIBUTE,
OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE,
OutOfProcessUtils.PS_OUT_OF_PROC_DATA_TAG);
}
string stream = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_STREAM_ATTRIBUTE);
string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE);
Guid psGuid = new Guid(psGuidString);
// Now move the reader to the data portion
if (!xmlReader.Read())
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCInsufficientDataforElement,
RemotingErrorIdStrings.IPCInsufficientDataforElement,
OutOfProcessUtils.PS_OUT_OF_PROC_DATA_TAG);
}
if (xmlReader.NodeType != XmlNodeType.Text)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCOnlyTextExpectedInDataElement,
RemotingErrorIdStrings.IPCOnlyTextExpectedInDataElement,
xmlReader.NodeType, OutOfProcessUtils.PS_OUT_OF_PROC_DATA_TAG, XmlNodeType.Text);
}
string data = xmlReader.Value;
tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_DATA received, psGuid : " + psGuid.ToString());
byte[] rawData = Convert.FromBase64String(data);
callbacks.DataPacketReceived(rawData, stream, psGuid);
}
break;
case OutOfProcessUtils.PS_OUT_OF_PROC_DATA_ACK_TAG:
{
if (xmlReader.AttributeCount != 1)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement,
RemotingErrorIdStrings.IPCWrongAttributeCountForElement,
OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE,
OutOfProcessUtils.PS_OUT_OF_PROC_DATA_ACK_TAG);
}
string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE);
Guid psGuid = new Guid(psGuidString);
tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_DATA_ACK received, psGuid : " + psGuid.ToString());
callbacks.DataAckPacketReceived(psGuid);
}
break;
case OutOfProcessUtils.PS_OUT_OF_PROC_COMMAND_TAG:
{
if (xmlReader.AttributeCount != 1)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement,
RemotingErrorIdStrings.IPCWrongAttributeCountForElement,
OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE,
OutOfProcessUtils.PS_OUT_OF_PROC_COMMAND_TAG);
}
string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE);
Guid psGuid = new Guid(psGuidString);
tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_COMMAND received, psGuid : " + psGuid.ToString());
callbacks.CommandCreationPacketReceived(psGuid);
}
break;
case OutOfProcessUtils.PS_OUT_OF_PROC_COMMAND_ACK_TAG:
{
if (xmlReader.AttributeCount != 1)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement,
RemotingErrorIdStrings.IPCWrongAttributeCountForElement,
OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE,
OutOfProcessUtils.PS_OUT_OF_PROC_COMMAND_ACK_TAG);
}
string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE);
Guid psGuid = new Guid(psGuidString);
tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_COMMAND_ACK received, psGuid : " + psGuid.ToString());
callbacks.CommandCreationAckReceived(psGuid);
}
break;
case OutOfProcessUtils.PS_OUT_OF_PROC_CLOSE_TAG:
{
if (xmlReader.AttributeCount != 1)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement,
RemotingErrorIdStrings.IPCWrongAttributeCountForElement,
OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE,
OutOfProcessUtils.PS_OUT_OF_PROC_CLOSE_TAG);
}
string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE);
Guid psGuid = new Guid(psGuidString);
tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_CLOSE received, psGuid : " + psGuid.ToString());
callbacks.ClosePacketReceived(psGuid);
}
break;
case OutOfProcessUtils.PS_OUT_OF_PROC_CLOSE_ACK_TAG:
{
if (xmlReader.AttributeCount != 1)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement,
RemotingErrorIdStrings.IPCWrongAttributeCountForElement,
OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE,
OutOfProcessUtils.PS_OUT_OF_PROC_CLOSE_ACK_TAG);
}
string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE);
Guid psGuid = new Guid(psGuidString);
tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_CLOSE_ACK received, psGuid : " + psGuid.ToString());
callbacks.CloseAckPacketReceived(psGuid);
}
break;
case OutOfProcessUtils.PS_OUT_OF_PROC_SIGNAL_TAG:
{
if (xmlReader.AttributeCount != 1)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement,
RemotingErrorIdStrings.IPCWrongAttributeCountForElement,
OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE,
OutOfProcessUtils.PS_OUT_OF_PROC_SIGNAL_TAG);
}
string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE);
Guid psGuid = new Guid(psGuidString);
tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_SIGNAL received, psGuid : " + psGuid.ToString());
callbacks.SignalPacketReceived(psGuid);
}
break;
case OutOfProcessUtils.PS_OUT_OF_PROC_SIGNAL_ACK_TAG:
{
if (xmlReader.AttributeCount != 1)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCWrongAttributeCountForElement,
RemotingErrorIdStrings.IPCWrongAttributeCountForElement,
OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE,
OutOfProcessUtils.PS_OUT_OF_PROC_SIGNAL_ACK_TAG);
}
string psGuidString = xmlReader.GetAttribute(OutOfProcessUtils.PS_OUT_OF_PROC_PSGUID_ATTRIBUTE);
Guid psGuid = new Guid(psGuidString);
tracer.WriteMessage("OutOfProcessUtils.ProcessElement : PS_OUT_OF_PROC_SIGNAL_ACK received, psGuid : " + psGuid.ToString());
callbacks.SignalAckPacketReceived(psGuid);
}
break;
default:
throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownElementReceived,
RemotingErrorIdStrings.IPCUnknownElementReceived,
xmlReader.LocalName);
}
}
#endregion
}
/// <summary>
/// A wrapper around TextWriter to allow for synchronized writing to a stream.
/// Synchronization is required to avoid collision when multiple TransportManager's
/// write data at the same time to the same writer
/// </summary>
internal class OutOfProcessTextWriter
{
#region Private Data
private TextWriter _writer;
private bool _isStopped;
private object _syncObject = new object();
#endregion
#region Constructors
/// <summary>
/// Constructs the wrapper
/// </summary>
/// <param name="writerToWrap"></param>
internal OutOfProcessTextWriter(TextWriter writerToWrap)
{
Dbg.Assert(null != writerToWrap, "Cannot wrap a null writer.");
_writer = writerToWrap;
}
#endregion
#region Methods
/// <summary>
/// Calls writer.WriteLine() with data.
/// </summary>
/// <param name="data"></param>
internal virtual void WriteLine(string data)
{
if (_isStopped)
{
return;
}
lock (_syncObject)
{
_writer.WriteLine(data);
_writer.Flush();
}
}
/// <summary>
/// Stops the writer from writing anything to the stream.
/// This is used by session transport manager when the server
/// process is exited but the session data structure handlers
/// are not notified yet. So while the data structure handler
/// is disposing we should not let anyone use the stream.
/// </summary>
internal void StopWriting()
{
_isStopped = true;
}
#endregion
}
}
namespace System.Management.Automation.Remoting.Client
{
internal abstract class OutOfProcessClientSessionTransportManagerBase : BaseClientSessionTransportManager
{
#region Data
private PrioritySendDataCollection.OnDataAvailableCallback _onDataAvailableToSendCallback;
private OutOfProcessUtils.DataProcessingDelegates _dataProcessingCallbacks;
private Dictionary<Guid, OutOfProcessClientCommandTransportManager> _cmdTransportManagers;
private Timer _closeTimeOutTimer;
protected OutOfProcessTextWriter stdInWriter;
protected PowerShellTraceSource _tracer;
#endregion
#region Constructor
internal OutOfProcessClientSessionTransportManagerBase(
Guid runspaceId,
PSRemotingCryptoHelper cryptoHelper)
: base(runspaceId, cryptoHelper)
{
_onDataAvailableToSendCallback =
new PrioritySendDataCollection.OnDataAvailableCallback(OnDataAvailableCallback);
_cmdTransportManagers = new Dictionary<Guid, OutOfProcessClientCommandTransportManager>();
_dataProcessingCallbacks = new OutOfProcessUtils.DataProcessingDelegates();
_dataProcessingCallbacks.DataPacketReceived += new OutOfProcessUtils.DataPacketReceived(OnDataPacketReceived);
_dataProcessingCallbacks.DataAckPacketReceived += new OutOfProcessUtils.DataAckPacketReceived(OnDataAckPacketReceived);
_dataProcessingCallbacks.CommandCreationPacketReceived += new OutOfProcessUtils.CommandCreationPacketReceived(OnCommandCreationPacketReceived);
_dataProcessingCallbacks.CommandCreationAckReceived += new OutOfProcessUtils.CommandCreationAckReceived(OnCommandCreationAckReceived);
_dataProcessingCallbacks.SignalPacketReceived += new OutOfProcessUtils.SignalPacketReceived(OnSignalPacketReceived);
_dataProcessingCallbacks.SignalAckPacketReceived += new OutOfProcessUtils.SignalAckPacketReceived(OnSiganlAckPacketReceived);
_dataProcessingCallbacks.ClosePacketReceived += new OutOfProcessUtils.ClosePacketReceived(OnClosePacketReceived);
_dataProcessingCallbacks.CloseAckPacketReceived += new OutOfProcessUtils.CloseAckPacketReceived(OnCloseAckReceived);
dataToBeSent.Fragmentor = base.Fragmentor;
// session transport manager can recieve unlimited data..however each object is limited
// by maxRecvdObjectSize. this is to allow clients to use a session for an unlimited time..
// also the messages that can be sent to a session are limited and very controlled.
// However a command transport manager can be restricted to receive only a fixed amount of data
// controlled by maxRecvdDataSizeCommand..This is because commands can accept any number of input
// objects.
ReceivedDataCollection.MaximumReceivedDataSize = null;
ReceivedDataCollection.MaximumReceivedObjectSize = BaseTransportManager.MaximumReceivedObjectSize;
// timers initialization
_closeTimeOutTimer = new Timer(OnCloseTimeOutTimerElapsed, null, Timeout.Infinite, Timeout.Infinite);
_tracer = PowerShellTraceSourceFactory.GetTraceSource();
}
#endregion
#region Overrides
internal override void ConnectAsync()
{
throw new NotImplementedException(RemotingErrorIdStrings.IPCTransportConnectError);
}
/// <summary>
/// Closes the server process.
/// </summary>
internal override void CloseAsync()
{
bool shouldRaiseCloseCompleted = false;
lock (syncObject)
{
if (isClosed == true)
{
return;
}
// first change the state..so other threads
// will know that we are closing.
isClosed = true;
if (null == stdInWriter)
{
// this will happen if CloseAsync() is called
// before ConnectAsync()..in which case we
// just need to raise close completed event.
shouldRaiseCloseCompleted = true;
}
}
base.CloseAsync();
if (shouldRaiseCloseCompleted)
{
RaiseCloseCompleted();
return;
}
PSEtwLog.LogAnalyticInformational(PSEventId.WSManCloseShell,
PSOpcode.Disconnect, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic,
RunspacePoolInstanceId.ToString());
_tracer.WriteMessage("OutOfProcessClientSessionTransportManager.CloseAsync, when sending close session packet, progress command count should be zero, current cmd count: " + _cmdTransportManagers.Count + ", RunSpacePool Id : " + this.RunspacePoolInstanceId);
try
{
// send Close signal to the server and let it die gracefully.
stdInWriter.WriteLine(OutOfProcessUtils.CreateClosePacket(Guid.Empty));
// start the timer..so client can fail deterministically
_closeTimeOutTimer.Change(60 * 1000, Timeout.Infinite);
}
catch (IOException)
{
// Cannot communicate with server. Allow client to complete close operation.
shouldRaiseCloseCompleted = true;
}
if (shouldRaiseCloseCompleted)
{
RaiseCloseCompleted();
}
}
/// <summary>
/// Create a transport manager for command
/// </summary>
/// <param name="connectionInfo"></param>
/// <param name="cmd"></param>
/// <param name="noInput"></param>
/// <returns></returns>
internal override BaseClientCommandTransportManager CreateClientCommandTransportManager(
RunspaceConnectionInfo connectionInfo,
ClientRemotePowerShell cmd,
bool noInput)
{
Dbg.Assert(null != cmd, "Cmd cannot be null");
OutOfProcessClientCommandTransportManager result = new
OutOfProcessClientCommandTransportManager(cmd, noInput, this, stdInWriter);
AddCommandTransportManager(cmd.InstanceId, result);
return result;
}
/// <summary>
/// Kills the server process and disposes other resources
/// </summary>
/// <param name="isDisposing"></param>
internal override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (isDisposing)
{
_cmdTransportManagers.Clear();
_closeTimeOutTimer.Dispose();
}
}
#endregion
#region Helper Methods
private void AddCommandTransportManager(Guid key, OutOfProcessClientCommandTransportManager cmdTM)
{
lock (syncObject)
{
if (isClosed)
{
// It is possible for this add command to occur after/during session close via
// asynchronous stop pipeline or Stop-Job. In this case ignore the command.
_tracer.WriteMessage("OutOfProcessClientSessionTransportManager.AddCommandTransportManager, Adding command transport on closed session, RunSpacePool Id : " + this.RunspacePoolInstanceId);
return;
}
Dbg.Assert(!_cmdTransportManagers.ContainsKey(key), "key already exists");
_cmdTransportManagers.Add(key, cmdTM);
}
}
internal override void RemoveCommandTransportManager(Guid key)
{
lock (syncObject)
{
// We always need to remove commands from collection, even if isClosed is true.
// If we don't then we hang because CloseAsync() will not complete until all
// commands are closed.
if (!_cmdTransportManagers.Remove(key))
{
_tracer.WriteMessage("key does not exist to remove from cmdTransportManagers");
}
}
}
private OutOfProcessClientCommandTransportManager GetCommandTransportManager(Guid key)
{
lock (syncObject)
{
OutOfProcessClientCommandTransportManager result = null;
_cmdTransportManagers.TryGetValue(key, out result);
return result;
}
}
private void OnCloseSessionCompleted()
{
//stop timer
_closeTimeOutTimer.Change(Timeout.Infinite, Timeout.Infinite);
RaiseCloseCompleted();
CleanupConnection();
}
protected abstract void CleanupConnection();
#endregion
#region Event Handlers
protected void HandleOutputDataReceived(string data)
{
try
{
OutOfProcessUtils.ProcessData(data, _dataProcessingCallbacks);
}
catch (Exception exception)
{
CommandProcessorBase.CheckForSevereException(exception);
PSRemotingTransportException psrte =
new PSRemotingTransportException(PSRemotingErrorId.IPCErrorProcessingServerData,
RemotingErrorIdStrings.IPCErrorProcessingServerData,
exception.Message);
RaiseErrorHandler(new TransportErrorOccuredEventArgs(psrte, TransportMethodEnum.ReceiveShellOutputEx));
}
}
protected void HandleErrorDataReceived(string data)
{
lock (syncObject)
{
if (isClosed)
{
return;
}
}
PSRemotingTransportException psrte = new PSRemotingTransportException(PSRemotingErrorId.IPCServerProcessReportedError,
RemotingErrorIdStrings.IPCServerProcessReportedError,
data);
RaiseErrorHandler(new TransportErrorOccuredEventArgs(psrte, TransportMethodEnum.Unknown));
}
protected void OnExited(object sender, EventArgs e)
{
TransportMethodEnum transportMethod = TransportMethodEnum.Unknown;
lock (syncObject)
{
// There is no need to return when IsClosed==true here as in a legitimate case process exits
// after Close is called..In that legitimate case, Exit handler is removed before
// calling Exit..So, this Exit must have been called abnormally.
if (isClosed)
{
transportMethod = TransportMethodEnum.CloseShellOperationEx;
}
// dont let the writer write new data as the process is exited.
// Not asssigning null to stdInWriter to fix the race condition between OnExited() and CloseAsync() methods.
//
stdInWriter.StopWriting();
}
PSRemotingTransportException psrte = new PSRemotingTransportException(PSRemotingErrorId.IPCServerProcessExited,
RemotingErrorIdStrings.IPCServerProcessExited);
RaiseErrorHandler(new TransportErrorOccuredEventArgs(psrte, transportMethod));
}
#endregion
#region Sending Data related Methods
protected void SendOneItem()
{
DataPriorityType priorityType;
// This will either return data or register callback but doesn't do both.
byte[] data = dataToBeSent.ReadOrRegisterCallback(_onDataAvailableToSendCallback,
out priorityType);
if (null != data)
{
SendData(data, priorityType);
}
}
private void OnDataAvailableCallback(byte[] data, DataPriorityType priorityType)
{
Dbg.Assert(null != data, "data cannot be null in the data available callback");
tracer.WriteLine("Received data to be sent from the callback.");
SendData(data, priorityType);
}
private void SendData(byte[] data, DataPriorityType priorityType)
{
PSEtwLog.LogAnalyticInformational(
PSEventId.WSManSendShellInputEx, PSOpcode.Send, PSTask.None,
PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic,
RunspacePoolInstanceId.ToString(),
Guid.Empty.ToString(),
data.Length.ToString(CultureInfo.InvariantCulture));
lock (syncObject)
{
if (isClosed)
{
return;
}
stdInWriter.WriteLine(OutOfProcessUtils.CreateDataPacket(data,
priorityType,
Guid.Empty));
}
}
private void OnRemoteSessionSendCompleted()
{
PSEtwLog.LogAnalyticInformational(PSEventId.WSManSendShellInputExCallbackReceived,
PSOpcode.Connect, PSTask.None, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic,
RunspacePoolInstanceId.ToString(), Guid.Empty.ToString());
SendOneItem();
}
#endregion
#region Data Processing handlers
private void OnDataPacketReceived(byte[] rawData, string stream, Guid psGuid)
{
string streamTemp = System.Management.Automation.Remoting.Client.WSManNativeApi.WSMAN_STREAM_ID_STDOUT;
if (stream.Equals(DataPriorityType.PromptResponse.ToString(), StringComparison.OrdinalIgnoreCase))
{
streamTemp = System.Management.Automation.Remoting.Client.WSManNativeApi.WSMAN_STREAM_ID_PROMPTRESPONSE;
}
if (psGuid == Guid.Empty)
{
PSEtwLog.LogAnalyticInformational(
PSEventId.WSManReceiveShellOutputExCallbackReceived, PSOpcode.Receive, PSTask.None,
PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic,
RunspacePoolInstanceId.ToString(),
Guid.Empty.ToString(),
rawData.Length.ToString(CultureInfo.InvariantCulture));
// this data is meant for session.
base.ProcessRawData(rawData, streamTemp);
}
else
{
// this is for a command
OutOfProcessClientCommandTransportManager cmdTM = GetCommandTransportManager(psGuid);
if (null != cmdTM)
{
// not throwing the exception in null case as the command might have already
// closed. The RS data structure handler does not wait for the close ack before
// it clears the command transport manager..so this might happen.
cmdTM.OnRemoteCmdDataReceived(rawData, streamTemp);
}
}
}
private void OnDataAckPacketReceived(Guid psGuid)
{
if (psGuid == Guid.Empty)
{
// this data is meant for session.
OnRemoteSessionSendCompleted();
}
else
{
// this is for a command
OutOfProcessClientCommandTransportManager cmdTM = GetCommandTransportManager(psGuid);
if (null != cmdTM)
{
// not throwing the exception in null case as the command might have already
// closed. The RS data structure handler does not wait for the close ack before
// it clears the command transport manager..so this might happen.
cmdTM.OnRemoteCmdSendCompleted();
}
}
}
private void OnCommandCreationPacketReceived(Guid psGuid)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownElementReceived,
RemotingErrorIdStrings.IPCUnknownElementReceived,
OutOfProcessUtils.PS_OUT_OF_PROC_COMMAND_TAG);
}
private void OnCommandCreationAckReceived(Guid psGuid)
{
OutOfProcessClientCommandTransportManager cmdTM = GetCommandTransportManager(psGuid);
if (null == cmdTM)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownCommandGuid,
RemotingErrorIdStrings.IPCUnknownCommandGuid,
psGuid.ToString(), OutOfProcessUtils.PS_OUT_OF_PROC_COMMAND_ACK_TAG);
}
cmdTM.OnCreateCmdCompleted();
_tracer.WriteMessage("OutOfProcessClientSessionTransportManager.OnCommandCreationAckReceived, in progress command count after cmd creation ACK : " + _cmdTransportManagers.Count + ", psGuid : " + psGuid.ToString());
}
private void OnSignalPacketReceived(Guid psGuid)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownElementReceived,
RemotingErrorIdStrings.IPCUnknownElementReceived,
OutOfProcessUtils.PS_OUT_OF_PROC_SIGNAL_TAG);
}
private void OnSiganlAckPacketReceived(Guid psGuid)
{
if (psGuid == Guid.Empty)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCNoSignalForSession,
RemotingErrorIdStrings.IPCNoSignalForSession,
OutOfProcessUtils.PS_OUT_OF_PROC_SIGNAL_ACK_TAG);
}
else
{
OutOfProcessClientCommandTransportManager cmdTM = GetCommandTransportManager(psGuid);
if (null != cmdTM)
{
cmdTM.OnRemoteCmdSignalCompleted();
}
}
}
private void OnClosePacketReceived(Guid psGuid)
{
throw new PSRemotingTransportException(PSRemotingErrorId.IPCUnknownElementReceived,
RemotingErrorIdStrings.IPCUnknownElementReceived,
OutOfProcessUtils.PS_OUT_OF_PROC_CLOSE_TAG);
}
private void OnCloseAckReceived(Guid psGuid)
{
int commandCount;
lock (syncObject)
{
commandCount = _cmdTransportManagers.Count;
}
if (psGuid == Guid.Empty)
{
_tracer.WriteMessage("OutOfProcessClientSessionTransportManager.OnCloseAckReceived, progress command count after CLOSE ACK should be zero = " + commandCount + " psGuid : " + psGuid.ToString());
this.OnCloseSessionCompleted();
}
else
{
_tracer.WriteMessage("OutOfProcessClientSessionTransportManager.OnCloseAckReceived, in progress command count should be greater than zero: " + commandCount + ", RunSpacePool Id : " + this.RunspacePoolInstanceId + ", psGuid : " + psGuid.ToString());
OutOfProcessClientCommandTransportManager cmdTM = GetCommandTransportManager(psGuid);
if (null != cmdTM)
{
// this might legitimately happen if cmd is already closed before we get an
// ACK back from server.
cmdTM.OnCloseCmdCompleted();
}
}
}
#endregion
#region Private Timeout handlers
internal void OnCloseTimeOutTimerElapsed(object source)
{
PSRemotingTransportException psrte = new PSRemotingTransportException(PSRemotingErrorId.IPCCloseTimedOut, RemotingErrorIdStrings.IPCCloseTimedOut);
RaiseErrorHandler(new TransportErrorOccuredEventArgs(psrte, TransportMethodEnum.CloseShellOperationEx));
}
#endregion
}
internal class OutOfProcessClientSessionTransportManager : OutOfProcessClientSessionTransportManagerBase
{
#region Private Data
private Process _serverProcess;
private NewProcessConnectionInfo _connectionInfo;
private bool _processCreated = true;
private PowerShellProcessInstance _processInstance;
#endregion
#region Constructor
internal OutOfProcessClientSessionTransportManager(Guid runspaceId,
NewProcessConnectionInfo connectionInfo,
PSRemotingCryptoHelper cryptoHelper)
: base(runspaceId, cryptoHelper)
{
_connectionInfo = connectionInfo;
}
#endregion
#region Overrides
/// <summary>
/// Launch a new Process (PowerShell.exe -s) to perform remoting. This is used by *-Job cmdlets
/// to support background jobs without depending on WinRM (WinRM has complex requirements like
/// elevation to support local machine remoting)
/// </summary>
/// <exception cref="System.InvalidOperationException">
/// </exception>
/// <exception cref="System.ComponentModel.Win32Exception">
/// 1. There was an error in opening the associated file.
/// </exception>
internal override void CreateAsync()
{
if (null != _connectionInfo)
{
_processInstance = _connectionInfo.Process ?? new PowerShellProcessInstance(_connectionInfo.PSVersion,
_connectionInfo.Credential,
_connectionInfo.InitializationScript,
_connectionInfo.RunAs32);
if (_connectionInfo.Process != null)
{
_processCreated = false;
}
// _processInstance.Start();
}
PSEtwLog.LogAnalyticInformational(PSEventId.WSManCreateShell, PSOpcode.Connect,
PSTask.CreateRunspace, PSKeyword.Transport | PSKeyword.UseAlwaysAnalytic,
RunspacePoolInstanceId.ToString());
try
{
lock (syncObject)
{
if (isClosed)
{
return;
}
// Attach handlers and start the process
_serverProcess = _processInstance.Process;
if (_processInstance.RunspacePool != null)
{
_processInstance.RunspacePool.Close();
_processInstance.RunspacePool.Dispose();
}