/
srvinit.cpp
865 lines (721 loc) · 34.5 KB
/
srvinit.cpp
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "srvinit.h"
#include "dbcs.h"
#include "handle.h"
#include "registry.hpp"
#include "renderFontDefaults.hpp"
#include "..\propslib\DelegationConfig.hpp"
#include "ApiRoutines.h"
#include "../types/inc/GlyphWidth.hpp"
#include "..\server\Entrypoints.h"
#include "..\server\IoSorter.h"
#include "..\interactivity\inc\ServiceLocator.hpp"
#include "..\interactivity\base\ApiDetector.hpp"
#include "renderData.hpp"
#include "../renderer/base/renderer.hpp"
#include "..\host\dll\ITerminalHandoff_h.h"
#include <wil/resource.h>
#include <wil/winrt.h>
#include <windows.foundation.collections.h>
#include <Windows.ApplicationModel.h>
#include <Windows.ApplicationModel.AppExtensions.h>
#pragma hdrstop
using namespace Microsoft::Console::Interactivity;
using namespace Microsoft::Console::Render;
const UINT CONSOLE_EVENT_FAILURE_ID = 21790;
const UINT CONSOLE_LPC_PORT_FAILURE_ID = 21791;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Foundation::Collections;
using namespace ABI::Windows::ApplicationModel;
using namespace ABI::Windows::ApplicationModel::AppExtensions;
HRESULT LookupCatalog()
{
auto coinit = wil::CoInitializeEx(COINIT_MULTITHREADED);
ComPtr<IAppExtensionCatalogStatics> catalogStatics;
RETURN_IF_FAILED(Windows::Foundation::GetActivationFactory(HStringReference(RuntimeClass_Windows_ApplicationModel_AppExtensions_AppExtensionCatalog).Get(), &catalogStatics));
ComPtr<IAppExtensionCatalog> catalog;
RETURN_IF_FAILED(catalogStatics->Open(HStringReference(L"com.microsoft.windows.console.host").Get(), &catalog));
ComPtr<IAsyncOperation<IVectorView<AppExtension*>*>> findOperation;
RETURN_IF_FAILED(catalog->FindAllAsync(&findOperation));
ComPtr<IVectorView<AppExtension*>> extensionList;
RETURN_IF_FAILED(wil::wait_for_completion_nothrow(findOperation.Get(), &extensionList));
UINT extensionCount;
RETURN_IF_FAILED(extensionList->get_Size(&extensionCount));
for (UINT index = 0; index < extensionCount; index++)
{
ComPtr<IAppExtension> extension;
RETURN_IF_FAILED(extensionList->GetAt(index, &extension));
ComPtr<IPackage> extensionPackage;
RETURN_IF_FAILED(extension->get_Package(&extensionPackage));
ComPtr<IPackageId> extensionPackageId;
RETURN_IF_FAILED(extensionPackage->get_Id(&extensionPackageId));
HString publisherId;
RETURN_IF_FAILED(extensionPackageId->get_PublisherId(publisherId.GetAddressOf()));
// PackageId.Name
HString name;
RETURN_IF_FAILED(extensionPackageId->get_Name(name.GetAddressOf()));
// PackageId.Version
PackageVersion version;
RETURN_IF_FAILED(extensionPackageId->get_Version(&version));
}
return S_OK;
}
[[nodiscard]] HRESULT ConsoleServerInitialization(_In_ HANDLE Server, const ConsoleArguments* const args)
try
{
Globals& Globals = ServiceLocator::LocateGlobals();
Globals.pDeviceComm = new DeviceComm(Server);
Globals.launchArgs = *args;
Globals.uiOEMCP = GetOEMCP();
Globals.uiWindowsCP = GetACP();
Globals.pFontDefaultList = new RenderFontDefaults();
FontInfoBase::s_SetFontDefaultList(Globals.pFontDefaultList);
//LookupCatalog();
// TODO: This is looked up twice in the middle hop console.
IID delegationClsid;
if (SUCCEEDED(DelegationConfig::s_GetConsole(delegationClsid)))
{
Globals.handoffConsoleClsid = delegationClsid;
}
if (SUCCEEDED(DelegationConfig::s_GetTerminal(delegationClsid)))
{
Globals.handoffTerminalClsid = delegationClsid;
}
// Removed allocation of scroll buffer here.
return S_OK;
}
CATCH_RETURN();
static bool s_IsOnDesktop()
{
// Persist this across calls so we don't dig it out a whole bunch of times. Once is good enough for the system.
static bool fAlreadyQueried = false;
static bool fIsDesktop = false;
if (!fAlreadyQueried)
{
Microsoft::Console::Interactivity::ApiLevel level;
const NTSTATUS status = Microsoft::Console::Interactivity::ApiDetector::DetectNtUserWindow(&level);
LOG_IF_NTSTATUS_FAILED(status);
if (NT_SUCCESS(status))
{
switch (level)
{
case Microsoft::Console::Interactivity::ApiLevel::OneCore:
fIsDesktop = false;
break;
case Microsoft::Console::Interactivity::ApiLevel::Win32:
fIsDesktop = true;
break;
}
}
fAlreadyQueried = true;
}
return fIsDesktop;
}
[[nodiscard]] NTSTATUS SetUpConsole(_Inout_ Settings* pStartupSettings,
_In_ DWORD TitleLength,
_In_reads_bytes_(TitleLength) LPWSTR Title,
_In_ LPCWSTR CurDir,
_In_ LPCWSTR AppName)
{
// We will find and locate all relevant preference settings and then create the console here.
// The precedence order for settings is:
// 0. Launch arguments passed on the commandline.
// 1. STARTUPINFO settings
// 2a. Shortcut/Link settings
// 2b. Registry specific settings
// 3. Registry default settings
// 4. Hardcoded default settings
// To establish this hierarchy, we will need to load the settings and apply them in reverse order.
// 4. Initializing Settings will establish hardcoded defaults.
// Set to reference of global console information since that's the only place we need to hold the settings.
CONSOLE_INFORMATION& settings = ServiceLocator::LocateGlobals().getConsoleInformation();
const auto& launchArgs = ServiceLocator::LocateGlobals().launchArgs;
// 4b. On Desktop editions, we need to apply a series of Desktop-specific defaults that are better than the
// ones from the constructor (which are great for OneCore systems.)
if (s_IsOnDesktop())
{
settings.ApplyDesktopSpecificDefaults();
}
// Use the launch arguments to check if we're going to be started in pseudoconsole mode.
// If we are, we don't want to load any user settings, because that could
// result in some strange rendering results in the end terminal.
// Use the launch args because the VtIo hasn't been initialized yet.
if (!launchArgs.InConptyMode())
{
// 3. Read the default registry values.
Registry reg(&settings);
reg.LoadGlobalsFromRegistry();
reg.LoadDefaultFromRegistry();
// 2. Read specific settings
// Link is expecting the flags from the process to be in already, so apply that first
settings.SetStartupFlags(pStartupSettings->GetStartupFlags());
// We need to see if we were spawned from a link. If we were, we need to
// call back into the shell to try to get all the console information from the link.
ServiceLocator::LocateSystemConfigurationProvider()->GetSettingsFromLink(&settings, Title, &TitleLength, CurDir, AppName);
// If we weren't started from a link, this will already be set.
// If LoadLinkInfo couldn't find anything, it will remove the flag so we can dig in the registry.
if (!(settings.IsStartupTitleIsLinkNameSet()))
{
reg.LoadFromRegistry(Title);
}
}
else
{
// microsoft/terminal#1965 - Let's just always enable VT processing by
// default for conpty clients. This prevents peculiar differences in
// behavior between conhost and terminal applications when the user has
// VirtualTerminalLevel=1 in their registry.
// We want everyone to be using VT by default anyways, so this is a
// strong nudge in that direction. If an application _doesn't_ want VT
// processing, it's free to disable this setting, even in conpty mode.
settings.SetVirtTermLevel(1);
}
// 1. The settings we were passed contains STARTUPINFO structure settings to be applied last.
settings.ApplyStartupInfo(pStartupSettings);
// 0. The settings passed in via commandline arguments. These should override anything else.
settings.ApplyCommandlineArguments(launchArgs);
// Validate all applied settings for correctness against final rules.
settings.Validate();
// As of the graphics refactoring to library based, all fonts are now DPI aware. Scaling is
// performed at the Blt time for raster fonts.
// Note that we can only declare our DPI awareness once per process launch.
// Set the process's default dpi awareness context to PMv2 so that new top level windows
// inherit their WM_DPICHANGED* broadcast mode (and more, like dialog scaling) from the thread.
IHighDpiApi* pHighDpiApi = ServiceLocator::LocateHighDpiApi();
if (pHighDpiApi)
{
// N.B.: There is no high DPI support on OneCore (non-UAP) systems.
// Instead of implementing a no-op interface, just skip all high
// DPI configuration if it is not supported. All callers into the
// high DPI API are in the Win32-specific interactivity DLL.
if (!pHighDpiApi->SetProcessDpiAwarenessContext())
{
// Fallback to per-monitor aware V1 if the API isn't available.
LOG_IF_FAILED(pHighDpiApi->SetProcessPerMonitorDpiAwareness());
// Allow child dialogs (i.e. Properties and Find) to scale automatically based on DPI if we're currently DPI aware.
// Note that we don't need to do this if we're PMv2.
pHighDpiApi->EnablePerMonitorDialogScaling();
}
}
//Save initial font name for comparison on exit. We want telemetry when the font has changed
if (settings.IsFaceNameSet())
{
settings.SetLaunchFaceName(settings.GetFaceName());
}
// Allocate console will read the global ServiceLocator::LocateGlobals().getConsoleInformation
// for the settings we just set.
NTSTATUS Status = CONSOLE_INFORMATION::AllocateConsole({ Title, TitleLength / sizeof(wchar_t) });
if (!NT_SUCCESS(Status))
{
return Status;
}
return STATUS_SUCCESS;
}
[[nodiscard]] NTSTATUS RemoveConsole(_In_ ConsoleProcessHandle* ProcessData)
{
CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
LockConsole();
NTSTATUS Status = STATUS_SUCCESS;
CommandHistory::s_Free((HANDLE)ProcessData);
bool const fRecomputeOwner = ProcessData->fRootProcess;
gci.ProcessHandleList.FreeProcessData(ProcessData);
if (fRecomputeOwner)
{
Microsoft::Console::Types::IConsoleWindow* pWindow = ServiceLocator::LocateConsoleWindow();
if (pWindow != nullptr)
{
pWindow->SetOwner();
}
}
UnlockConsole();
return Status;
}
DWORD WINAPI ConsoleIoThread(LPVOID lpParameter);
void ConsoleCheckDebug()
{
#ifdef DBG
wil::unique_hkey hCurrentUser;
wil::unique_hkey hConsole;
NTSTATUS status = RegistrySerialization::s_OpenConsoleKey(&hCurrentUser, &hConsole);
if (NT_SUCCESS(status))
{
DWORD dwData = 0;
status = RegistrySerialization::s_QueryValue(hConsole.get(),
L"DebugLaunch",
sizeof(dwData),
REG_DWORD,
(BYTE*)&dwData,
nullptr);
if (NT_SUCCESS(status))
{
if (dwData != 0)
{
DebugBreak();
}
}
}
#endif
}
HRESULT ConsoleCreateIoThread(_In_ HANDLE Server,
const ConsoleArguments* const args, // this can't stay like this because ConsoleArguments could change...
HANDLE driverInputEvent,
PCONSOLE_API_MSG connectMessage)
{
auto& g = ServiceLocator::LocateGlobals();
RETURN_IF_FAILED(ConsoleServerInitialization(Server, args));
RETURN_IF_FAILED(g.hConsoleInputInitEvent.create(wil::EventOptions::None));
if (driverInputEvent != INVALID_HANDLE_VALUE)
{
// Store the driver input event. It's already been told that it exists by whomever started us.
g.hInputEvent.reset(driverInputEvent);
}
else
{
// Set up and tell the driver about the input available event.
RETURN_IF_FAILED(g.hInputEvent.create(wil::EventOptions::ManualReset));
CD_IO_SERVER_INFORMATION ServerInformation;
ServerInformation.InputAvailableEvent = ServiceLocator::LocateGlobals().hInputEvent.get();
RETURN_IF_FAILED(g.pDeviceComm->SetServerInformation(&ServerInformation));
}
HANDLE const hThread = CreateThread(nullptr, 0, ConsoleIoThread, connectMessage, 0, nullptr);
RETURN_HR_IF(E_HANDLE, hThread == nullptr);
LOG_IF_WIN32_BOOL_FALSE(CloseHandle(hThread)); // The thread will run on its own and close itself. Free the associated handle.
// See MSFT:19918626
// Make sure to always set up the signal thread if we need to.
// Do this first, because breaking the signal pipe is used by the conpty API
// to indicate that we should close.
// The conpty i/o threads need an actual client to be connected before they
// can start, so they're started below, in ConsoleAllocateConsole
auto& gci = g.getConsoleInformation();
RETURN_IF_FAILED(gci.GetVtIo()->Initialize(args));
RETURN_IF_FAILED(gci.GetVtIo()->CreateAndStartSignalThread());
return S_OK;
}
HRESULT ConsoleEstablishHandoff(_In_ HANDLE Server,
const ConsoleArguments* const args, // this can't stay like this because ConsoleArguments could change...
HANDLE driverInputEvent,
PCONSOLE_API_MSG connectMessage)
try
{
auto& g = ServiceLocator::LocateGlobals();
g.handoffTarget = true;
IID delegationClsid;
if (SUCCEEDED(DelegationConfig::s_GetConsole(delegationClsid)))
{
g.handoffConsoleClsid = delegationClsid;
}
if (SUCCEEDED(DelegationConfig::s_GetTerminal(delegationClsid)))
{
g.handoffTerminalClsid = delegationClsid;
}
if (!g.handoffTerminalClsid)
{
return E_NOT_SET;
}
wil::unique_handle signalPipeTheirSide;
wil::unique_handle signalPipeOurSide;
wil::unique_handle inPipeTheirSide;
wil::unique_handle inPipeOurSide;
wil::unique_handle outPipeTheirSide;
wil::unique_handle outPipeOurSide;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
// Mark inheritable for signal handle when creating. It'll have the same value on the other side.
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = nullptr;
RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(signalPipeOurSide.addressof(), signalPipeTheirSide.addressof(), nullptr, 0));
RETURN_IF_WIN32_BOOL_FALSE(SetHandleInformation(signalPipeTheirSide.get(), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT));
RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(inPipeOurSide.addressof(), inPipeTheirSide.addressof(), nullptr, 0));
RETURN_IF_WIN32_BOOL_FALSE(SetHandleInformation(inPipeTheirSide.get(), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT));
RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(outPipeTheirSide.addressof(), outPipeOurSide.addressof(), nullptr, 0));
RETURN_IF_WIN32_BOOL_FALSE(SetHandleInformation(outPipeTheirSide.get(), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT));
::Microsoft::WRL::ComPtr<ITerminalHandoff> handoff;
RETURN_IF_FAILED(CoCreateInstance(g.handoffTerminalClsid.value(), nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&handoff)));
RETURN_IF_FAILED(handoff->EstablishHandoff(inPipeTheirSide.get(),
outPipeTheirSide.get(),
signalPipeTheirSide.get()));
inPipeTheirSide.release();
outPipeTheirSide.release();
signalPipeTheirSide.release();
auto originalCommandLine = args->GetOriginalCommandLine();
auto str2 = fmt::format(L" --headless --signal {:#x}", (int64_t)signalPipeOurSide.release());
originalCommandLine = originalCommandLine.append(str2);
ConsoleArguments args2(originalCommandLine, inPipeOurSide.release(), outPipeOurSide.release());
RETURN_IF_FAILED(args2.ParseCommandline());
return ConsoleCreateIoThread(Server, &args2, driverInputEvent, connectMessage);
}
CATCH_RETURN()
[[nodiscard]] HRESULT ConsoleCreateIoThreadLegacy(_In_ HANDLE Server, const ConsoleArguments* const args)
{
return ConsoleCreateIoThread(Server, args, INVALID_HANDLE_VALUE, nullptr);
}
#define SYSTEM_ROOT (L"%SystemRoot%")
#define SYSTEM_ROOT_LENGTH (sizeof(SYSTEM_ROOT) - sizeof(WCHAR))
// Routine Description:
// - This routine translates path characters into '_' characters because the NT registry apis do not allow the creation of keys with
// names that contain path characters. It also converts absolute paths into %SystemRoot% relative ones. As an example, if both behaviors were
// specified it would convert a title like C:\WINNT\System32\cmd.exe to %SystemRoot%_System32_cmd.exe.
// Arguments:
// - ConsoleTitle - Pointer to string to translate.
// - Unexpand - Convert absolute path to %SystemRoot% relative one.
// - Substitute - Whether string-substitution ('_' for '\') should occur.
// Return Value:
// - Pointer to translated title or nullptr.
// Note:
// - This routine allocates a buffer that must be freed.
PWSTR TranslateConsoleTitle(_In_ PCWSTR pwszConsoleTitle, const BOOL fUnexpand, const BOOL fSubstitute)
{
LPWSTR Tmp = nullptr;
size_t cbConsoleTitle;
size_t cbSystemRoot;
LPWSTR pwszSysRoot = new (std::nothrow) wchar_t[MAX_PATH];
if (nullptr != pwszSysRoot)
{
if (0 != GetWindowsDirectoryW(pwszSysRoot, MAX_PATH))
{
if (SUCCEEDED(StringCbLengthW(pwszConsoleTitle, STRSAFE_MAX_CCH, &cbConsoleTitle)) &&
SUCCEEDED(StringCbLengthW(pwszSysRoot, MAX_PATH, &cbSystemRoot)))
{
int const cchSystemRoot = (int)(cbSystemRoot / sizeof(WCHAR));
int const cchConsoleTitle = (int)(cbConsoleTitle / sizeof(WCHAR));
cbConsoleTitle += sizeof(WCHAR); // account for nullptr terminator
if (fUnexpand &&
cchConsoleTitle >= cchSystemRoot &&
#pragma prefast(suppress : 26018, "We've guaranteed that cchSystemRoot is equal to or smaller than cchConsoleTitle in size.")
(CSTR_EQUAL == CompareStringOrdinal(pwszConsoleTitle, cchSystemRoot, pwszSysRoot, cchSystemRoot, TRUE)))
{
cbConsoleTitle -= cbSystemRoot;
pwszConsoleTitle += cchSystemRoot;
cbSystemRoot = SYSTEM_ROOT_LENGTH;
}
else
{
cbSystemRoot = 0;
}
LPWSTR pszTranslatedConsoleTitle;
const size_t cbTranslatedConsoleTitle = cbSystemRoot + cbConsoleTitle;
Tmp = pszTranslatedConsoleTitle = (PWSTR) new BYTE[cbTranslatedConsoleTitle];
if (pszTranslatedConsoleTitle == nullptr)
{
return nullptr;
}
// No need to check return here -- pszTranslatedConsoleTitle is guaranteed large enough for SYSTEM_ROOT
(void)StringCbCopy(pszTranslatedConsoleTitle, cbTranslatedConsoleTitle, SYSTEM_ROOT);
pszTranslatedConsoleTitle += (cbSystemRoot / sizeof(WCHAR)); // skip by characters -- not bytes
for (UINT i = 0; i < cbConsoleTitle; i += sizeof(WCHAR))
{
#pragma prefast(suppress : 26018, "We are reading the null portion of the buffer on purpose and will escape on reaching it below.")
if (fSubstitute && *pwszConsoleTitle == '\\')
{
#pragma prefast(suppress : 26019, "Console title must contain system root if this path was followed.")
*pszTranslatedConsoleTitle++ = (WCHAR)'_';
}
else
{
*pszTranslatedConsoleTitle++ = *pwszConsoleTitle;
if (*pwszConsoleTitle == L'\0')
{
break;
}
}
pwszConsoleTitle++;
}
}
}
delete[] pwszSysRoot;
}
return Tmp;
}
[[nodiscard]] NTSTATUS GetConsoleLangId(const UINT uiOutputCP, _Out_ LANGID* const pLangId)
{
NTSTATUS Status = STATUS_NOT_SUPPORTED;
// -- WARNING -- LOAD BEARING CODE --
// Only attempt to return the Lang ID if the Windows ACP on console launch was an East Asian Code Page.
// -
// As of right now, this is a load bearing check and causes a domino effect of errors during OEM preinstallation if removed
// resulting in a crash on launch of CMD.exe
// (and consequently any scripts OEMs use to customize an image during the auditUser preinstall step inside their unattend.xml files.)
// I have no reason to believe that removing this check causes any problems on any other SKU or scenario types.
// -
// Returning STATUS_NOT_SUPPORTED will skip a call to SetThreadLocale inside the Windows loader. This has the effect of not
// setting the appropriate locale on the client end of the pipe, but also avoids the error.
// Returning STATUS_SUCCESS will trigger the call to SetThreadLocale inside the loader.
// This method is called on process launch by the loader and on every SetConsoleOutputCP call made from the client application to
// maintain the synchrony of the client's Thread Locale state.
// -
// It is important to note that a comment exists inside the loader stating that DBCS code pages (CJK languages)
// must have the SetThreadLocale synchronized with the console in order for FormatMessage to output correctly.
// I'm not sure of the full validity of that comment at this point in time (Nov 2016), but the least risky thing is to trust it and revert
// the behavior to this function until it can be otherwise proven.
// -
// See MSFT: 9808579 for the complete story on what happened here and why this must stay until the other dominos are resolved.
// -
// I would also highly advise against expanding the LANGIDs returned here or modifying them in any way until the cascading impacts
// discovered in MSFT: 9808579 are vetted against any changes.
// -- END WARNING --
if (IsAvailableEastAsianCodePage(ServiceLocator::LocateGlobals().uiWindowsCP))
{
if (pLangId != nullptr)
{
switch (uiOutputCP)
{
case CP_JAPANESE:
*pLangId = MAKELANGID(LANG_JAPANESE, SUBLANG_DEFAULT);
break;
case CP_KOREAN:
*pLangId = MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN);
break;
case CP_CHINESE_SIMPLIFIED:
*pLangId = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);
break;
case CP_CHINESE_TRADITIONAL:
*pLangId = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL);
break;
default:
*pLangId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
break;
}
}
Status = STATUS_SUCCESS;
}
return Status;
}
[[nodiscard]] HRESULT ApiRoutines::GetConsoleLangIdImpl(LANGID& langId) noexcept
{
try
{
const CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
LockConsole();
auto Unlock = wil::scope_exit([&] { UnlockConsole(); });
// This fails a lot and it's totally expected. It only works for a few East Asian code pages.
// As such, just return it. Do NOT use a wil macro here. It is very noisy.
return HRESULT_FROM_NT(GetConsoleLangId(gci.OutputCP, &langId));
}
CATCH_RETURN();
}
// Routine Description:
// - This routine reads the connection information from a 'connect' IO, validates it and stores them in an internal format.
// - N.B. The internal connection contains information not sent by clients in their connect IOs and initialized by other routines.
// Arguments:
// - Server - Supplies a handle to the console server.
// - Message - Supplies the message representing the connect IO.
// - Cac - Receives the connection information.
// Return Value:
// - NTSTATUS indicating if the connection information was successfully initialized.
[[nodiscard]] NTSTATUS ConsoleInitializeConnectInfo(_In_ PCONSOLE_API_MSG Message, _Out_ PCONSOLE_API_CONNECTINFO Cac)
{
CONSOLE_SERVER_MSG Data = { 0 };
// Try to receive the data sent by the client.
NTSTATUS Status = NTSTATUS_FROM_HRESULT(Message->ReadMessageInput(0, &Data, sizeof(Data)));
if (!NT_SUCCESS(Status))
{
return Status;
}
// Validate that strings are within the buffers and null-terminated.
if ((Data.ApplicationNameLength > (sizeof(Data.ApplicationName) - sizeof(WCHAR))) ||
(Data.TitleLength > (sizeof(Data.Title) - sizeof(WCHAR))) ||
(Data.CurrentDirectoryLength > (sizeof(Data.CurrentDirectory) - sizeof(WCHAR))) ||
(Data.ApplicationName[Data.ApplicationNameLength / sizeof(WCHAR)] != UNICODE_NULL) ||
(Data.Title[Data.TitleLength / sizeof(WCHAR)] != UNICODE_NULL) || (Data.CurrentDirectory[Data.CurrentDirectoryLength / sizeof(WCHAR)] != UNICODE_NULL))
{
return STATUS_INVALID_BUFFER_SIZE;
}
// Initialize (partially) the connect info with the received data.
FAIL_FAST_IF(!(sizeof(Cac->AppName) == sizeof(Data.ApplicationName)));
FAIL_FAST_IF(!(sizeof(Cac->Title) == sizeof(Data.Title)));
FAIL_FAST_IF(!(sizeof(Cac->CurDir) == sizeof(Data.CurrentDirectory)));
// unused(Data.IconId)
Cac->ConsoleInfo.SetHotKey(Data.HotKey);
Cac->ConsoleInfo.SetStartupFlags(Data.StartupFlags);
Cac->ConsoleInfo.SetFillAttribute(Data.FillAttribute);
Cac->ConsoleInfo.SetShowWindow(Data.ShowWindow);
Cac->ConsoleInfo.SetScreenBufferSize(Data.ScreenBufferSize);
Cac->ConsoleInfo.SetWindowSize(Data.WindowSize);
Cac->ConsoleInfo.SetWindowOrigin(Data.WindowOrigin);
Cac->ProcessGroupId = Data.ProcessGroupId;
Cac->ConsoleApp = Data.ConsoleApp;
Cac->WindowVisible = Data.WindowVisible;
Cac->TitleLength = Data.TitleLength;
Cac->AppNameLength = Data.ApplicationNameLength;
Cac->CurDirLength = Data.CurrentDirectoryLength;
memmove(Cac->AppName, Data.ApplicationName, sizeof(Cac->AppName));
memmove(Cac->Title, Data.Title, sizeof(Cac->Title));
memmove(Cac->CurDir, Data.CurrentDirectory, sizeof(Cac->CurDir));
return STATUS_SUCCESS;
}
[[nodiscard]] bool ConsoleConnectionDeservesVisibleWindow(PCONSOLE_API_CONNECTINFO p)
{
Globals& g = ServiceLocator::LocateGlobals();
// processes that are created ...
// ... with CREATE_NO_WINDOW never get a window.
// ... on Desktop, with a visible window always get one (even a fake one)
// ... not on Desktop, with a visible window only get one if we are headful (not ConPTY).
// This prevents pseudoconsole-hosted applications from taking over the screen,
// even if they really beg us for a window.
return p->WindowVisible && (s_IsOnDesktop() || !g.IsHeadless());
}
[[nodiscard]] NTSTATUS ConsoleAllocateConsole(PCONSOLE_API_CONNECTINFO p)
{
// AllocConsole is outside our codebase, but we should be able to mostly track the call here.
Telemetry::Instance().LogApiCall(Telemetry::ApiCall::AllocConsole);
Globals& g = ServiceLocator::LocateGlobals();
CONSOLE_INFORMATION& gci = g.getConsoleInformation();
NTSTATUS Status = SetUpConsole(&p->ConsoleInfo, p->TitleLength, p->Title, p->CurDir, p->AppName);
if (!NT_SUCCESS(Status))
{
return Status;
}
// No matter what, create a renderer.
try
{
g.pRender = nullptr;
auto renderThread = std::make_unique<RenderThread>();
// stash a local pointer to the thread here -
// We're going to give ownership of the thread to the Renderer,
// but the thread also need to be told who its renderer is,
// and we can't do that until the renderer is constructed.
auto* const localPointerToThread = renderThread.get();
g.pRender = new Renderer(&gci.renderData, nullptr, 0, std::move(renderThread));
THROW_IF_FAILED(localPointerToThread->Initialize(g.pRender));
// Allow the renderer to paint.
g.pRender->EnablePainting();
// Set up the renderer to be used to calculate the width of a glyph,
// should we be unable to figure out its width another way.
auto pfn = std::bind(&Renderer::IsGlyphWideByFont, static_cast<Renderer*>(g.pRender), std::placeholders::_1);
SetGlyphWidthFallback(pfn);
}
catch (...)
{
Status = NTSTATUS_FROM_HRESULT(wil::ResultFromCaughtException());
}
if (NT_SUCCESS(Status) && ConsoleConnectionDeservesVisibleWindow(p))
{
HANDLE Thread = nullptr;
IConsoleInputThread* pNewThread = nullptr;
LOG_IF_FAILED(ServiceLocator::CreateConsoleInputThread(&pNewThread));
FAIL_FAST_IF_NULL(pNewThread);
Thread = pNewThread->Start();
if (Thread == nullptr)
{
Status = STATUS_NO_MEMORY;
}
else
{
ServiceLocator::LocateGlobals().dwInputThreadId = pNewThread->GetThreadId();
// The ConsoleInputThread needs to lock the console so we must first unlock it ourselves.
UnlockConsole();
g.hConsoleInputInitEvent.wait();
LockConsole();
// OK, we've been told that the input thread is done initializing under lock.
// Cleanup the handles and events we used to maintain our virtual lock passing dance.
CloseHandle(Thread); // This doesn't stop the thread from running.
if (!NT_SUCCESS(g.ntstatusConsoleInputInitStatus))
{
Status = g.ntstatusConsoleInputInitStatus;
}
else
{
Status = STATUS_SUCCESS;
}
// If we're not headless, we'll make a real window.
// Allow UI Access to the real window but not the little
// fake window we would make in headless mode.
if (!g.launchArgs.IsHeadless())
{
/*
* Tell driver to allow clients with UIAccess to connect
* to this server even if the security descriptor doesn't
* allow it.
*
* N.B. This allows applications like narrator.exe to have
* access to the console. This is ok because they already
* have access to the console window anyway - this function
* is only called when a window is created.
*/
LOG_IF_FAILED(g.pDeviceComm->AllowUIAccess());
}
}
}
// Potentially start the VT IO (if needed)
// Make sure to do this after the i/o buffers have been created.
// We'll need the size of the screen buffer in the vt i/o initialization
if (NT_SUCCESS(Status))
{
HRESULT hr = gci.GetVtIo()->CreateIoHandlers();
if (hr == S_FALSE)
{
// We're not in VT I/O mode, this is fine.
}
else if (SUCCEEDED(hr))
{
// Actually start the VT I/O threads
hr = gci.GetVtIo()->StartIfNeeded();
// Don't convert S_FALSE to an NTSTATUS - the equivalent NTSTATUS
// is treated as an error
if (hr != S_FALSE)
{
Status = NTSTATUS_FROM_HRESULT(hr);
}
else
{
Status = ERROR_SUCCESS;
}
}
else
{
Status = NTSTATUS_FROM_HRESULT(hr);
}
}
return Status;
}
// Routine Description:
// - This routine is the main one in the console server IO thread.
// - It reads IO requests submitted by clients through the driver, services and completes them in a loop.
// Arguments:
// - lpParameter - PCONSOLE_API_MSG being handed off to us from the previous I/O.
// Return Value:
// - This routine never returns. The process exits when no more references or clients exist.
DWORD WINAPI ConsoleIoThread(LPVOID lpParameter)
{
auto& globals = ServiceLocator::LocateGlobals();
CONSOLE_API_MSG ReceiveMsg;
ReceiveMsg._pApiRoutines = &globals.api;
ReceiveMsg._pDeviceComm = globals.pDeviceComm;
PCONSOLE_API_MSG ReplyMsg = nullptr;
// If we were given a message on startup, process that in our context and then continue with the IO loop normally.
if (lpParameter)
{
ReceiveMsg = *(PCONSOLE_API_MSG)lpParameter;
ReceiveMsg._pApiRoutines = &globals.api;
ReceiveMsg._pDeviceComm = globals.pDeviceComm;
IoSorter::ServiceIoOperation(&ReceiveMsg, &ReplyMsg);
}
bool fShouldExit = false;
while (!fShouldExit)
{
if (ReplyMsg != nullptr)
{
LOG_IF_FAILED(ReplyMsg->ReleaseMessageBuffers());
}
// TODO: 9115192 correct mixed NTSTATUS/HRESULT
HRESULT hr = ServiceLocator::LocateGlobals().pDeviceComm->ReadIo(ReplyMsg, &ReceiveMsg);
if (FAILED(hr))
{
if (hr == HRESULT_FROM_WIN32(ERROR_PIPE_NOT_CONNECTED))
{
fShouldExit = true;
// This will not return. Terminate immediately when disconnected.
ServiceLocator::RundownAndExit(STATUS_SUCCESS);
}
RIPMSG1(RIP_WARNING, "DeviceIoControl failed with Result 0x%x", hr);
ReplyMsg = nullptr;
continue;
}
IoSorter::ServiceIoOperation(&ReceiveMsg, &ReplyMsg);
}
return 0;
}