-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathServiceLocator.cpp
361 lines (289 loc) · 10.6 KB
/
ServiceLocator.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "../inc/ServiceLocator.hpp"
#include "InteractivityFactory.hpp"
#pragma hdrstop
using namespace Microsoft::Console::Types;
using namespace Microsoft::Console::Interactivity;
#pragma region Private Static Member Initialization
std::unique_ptr<IInteractivityFactory> ServiceLocator::s_interactivityFactory;
std::unique_ptr<IConsoleControl> ServiceLocator::s_consoleControl;
std::unique_ptr<IConsoleInputThread> ServiceLocator::s_consoleInputThread;
std::unique_ptr<IConsoleWindow> ServiceLocator::s_consoleWindow;
std::unique_ptr<IWindowMetrics> ServiceLocator::s_windowMetrics;
std::unique_ptr<IAccessibilityNotifier> ServiceLocator::s_accessibilityNotifier;
std::unique_ptr<IHighDpiApi> ServiceLocator::s_highDpiApi;
std::unique_ptr<ISystemConfigurationProvider> ServiceLocator::s_systemConfigurationProvider;
void (*ServiceLocator::s_oneCoreTeardownFunction)() = nullptr;
Globals ServiceLocator::s_globals;
bool ServiceLocator::s_pseudoWindowInitialized = false;
wil::unique_hwnd ServiceLocator::s_pseudoWindow = nullptr;
#pragma endregion
#pragma region Public Methods
void ServiceLocator::SetOneCoreTeardownFunction(void (*pfn)()) noexcept
{
FAIL_FAST_IF(nullptr != s_oneCoreTeardownFunction);
s_oneCoreTeardownFunction = pfn;
}
void ServiceLocator::RundownAndExit(const HRESULT hr)
{
static std::atomic<bool> locked;
// MSFT:40146639
// The premise of this function is that 1 thread enters and 0 threads leave alive.
// We need to prevent anyone from calling us until we actually ExitProcess(),
// so that we don't TriggerTeardown() twice. LockConsole() can't be used here,
// because doing so would prevent the render thread from progressing.
if (locked.exchange(true, std::memory_order_relaxed))
{
// If we reach this point, another thread is already in the process of exiting.
// There's a lot of ways to suspend ourselves until we exit, one of which is "sleep forever".
Sleep(INFINITE);
}
// MSFT:15506250
// In VT I/O Mode, a client application might die before we've rendered
// the last bit of text they've emitted. So give the VtRenderer one
// last chance to paint before it is killed.
if (s_globals.pRender)
{
s_globals.pRender->TriggerTeardown();
}
// MSFT:40226902 - HOTFIX shutdown on OneCore, by leaking the renderer, thereby
// reducing the change for existing race conditions to turn into deadlocks.
#ifndef NDEBUG
// By locking the console, we ensure no background tasks are accessing the
// classes we're going to destruct down below (for instance: CursorBlinker).
s_globals.getConsoleInformation().LockConsole();
#endif
// A History Lesson from MSFT: 13576341:
// We introduced RundownAndExit to give services that hold onto important handles
// an opportunity to let those go when we decide to exit from the console for various reasons.
// This was because Console IO Services (ConIoSvcComm) on OneCore editions was holding onto
// pipe and ALPC handles to talk to CSRSS ConIoSrv.dll to broker which console got display/keyboard control.
// If we simply run straight into TerminateProcess, those handles aren't necessarily released right away.
// The terminate operation can have a rundown period of time where APCs are serviced (such as from
// a DirectX kernel callback/flush/cleanup) that can take substantially longer than we expect (several whole seconds).
// This rundown happens before the final destruction of any outstanding handles or resources.
// If someone is waiting on one of those handles or resources outside our process, they're stuck waiting
// for our terminate rundown and can't continue execution until we're done.
// We don't want to have other execution in the system get stuck, so this is a great
// place to clean up and notify any objects or threads in the system that have to cleanup safely before
// we head into TerminateProcess and tear everything else down less gracefully.
// TODO: MSFT: 14397093 - Expand graceful rundown beyond just the Hot Bug input services case.
// MSFT:40226902 - HOTFIX shutdown on OneCore, by leaking the renderer, thereby
// reducing the change for existing race conditions to turn into deadlocks.
#ifndef NDEBUG
delete s_globals.pRender;
s_globals.pRender = nullptr;
#endif
if (s_oneCoreTeardownFunction)
{
s_oneCoreTeardownFunction();
}
// MSFT:40226902 - HOTFIX shutdown on OneCore, by leaking the renderer, thereby
// reducing the change for existing race conditions to turn into deadlocks.
#ifndef NDEBUG
s_consoleWindow.reset(nullptr);
#endif
ExitProcess(hr);
}
#pragma region Creation Methods
[[nodiscard]] NTSTATUS ServiceLocator::CreateConsoleInputThread(_Outptr_result_nullonfailure_ IConsoleInputThread** thread)
{
auto status = STATUS_SUCCESS;
if (s_consoleInputThread)
{
status = STATUS_INVALID_HANDLE;
}
else
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateConsoleInputThread(s_consoleInputThread);
if (NT_SUCCESS(status))
{
*thread = s_consoleInputThread.get();
}
}
}
return status;
}
[[nodiscard]] HRESULT ServiceLocator::CreateAccessibilityNotifier()
{
// Can't create if we've already created.
if (s_accessibilityNotifier)
{
return E_UNEXPECTED;
}
if (!s_interactivityFactory)
{
RETURN_IF_NTSTATUS_FAILED(ServiceLocator::LoadInteractivityFactory());
}
RETURN_IF_NTSTATUS_FAILED(s_interactivityFactory->CreateAccessibilityNotifier(s_accessibilityNotifier));
return S_OK;
}
#pragma endregion
#pragma region Set Methods
[[nodiscard]] NTSTATUS ServiceLocator::SetConsoleControlInstance(_In_ std::unique_ptr<IConsoleControl>&& control)
{
if (s_consoleControl)
{
NT_RETURN_NTSTATUS(STATUS_INVALID_HANDLE);
}
else if (!control)
{
NT_RETURN_NTSTATUS(STATUS_INVALID_PARAMETER);
}
else
{
s_consoleControl = std::move(control);
}
return STATUS_SUCCESS;
}
[[nodiscard]] NTSTATUS ServiceLocator::SetConsoleWindowInstance(_In_ IConsoleWindow* window)
{
auto status = STATUS_SUCCESS;
if (s_consoleWindow)
{
status = STATUS_INVALID_HANDLE;
}
else if (!window)
{
status = STATUS_INVALID_PARAMETER;
}
else
{
s_consoleWindow.reset(window);
}
return status;
}
#pragma endregion
#pragma region Location Methods
IConsoleWindow* ServiceLocator::LocateConsoleWindow()
{
return s_consoleWindow.get();
}
IConsoleControl* ServiceLocator::LocateConsoleControl()
{
auto status = STATUS_SUCCESS;
if (!s_consoleControl)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateConsoleControl(s_consoleControl);
}
}
LOG_IF_NTSTATUS_FAILED(status);
return s_consoleControl.get();
}
IConsoleInputThread* ServiceLocator::LocateConsoleInputThread()
{
return s_consoleInputThread.get();
}
IHighDpiApi* ServiceLocator::LocateHighDpiApi()
{
auto status = STATUS_SUCCESS;
if (!s_highDpiApi)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateHighDpiApi(s_highDpiApi);
}
}
LOG_IF_NTSTATUS_FAILED(status);
return s_highDpiApi.get();
}
IWindowMetrics* ServiceLocator::LocateWindowMetrics()
{
auto status = STATUS_SUCCESS;
if (!s_windowMetrics)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateWindowMetrics(s_windowMetrics);
}
}
LOG_IF_NTSTATUS_FAILED(status);
return s_windowMetrics.get();
}
IAccessibilityNotifier* ServiceLocator::LocateAccessibilityNotifier()
{
return s_accessibilityNotifier.get();
}
ISystemConfigurationProvider* ServiceLocator::LocateSystemConfigurationProvider()
{
auto status = STATUS_SUCCESS;
if (!s_systemConfigurationProvider)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateSystemConfigurationProvider(s_systemConfigurationProvider);
}
}
LOG_IF_NTSTATUS_FAILED(status);
return s_systemConfigurationProvider.get();
}
Globals& ServiceLocator::LocateGlobals()
{
return s_globals;
}
// Method Description:
// - Retrieves the pseudo console window, or attempts to instantiate one.
// Arguments:
// - owner: (defaults to 0 `HWND_DESKTOP`) the HWND that should be the initial
// owner of the pseudo window.
// Return Value:
// - a reference to the pseudoconsole window.
HWND ServiceLocator::LocatePseudoWindow(const HWND owner)
{
auto status = STATUS_SUCCESS;
if (!s_pseudoWindowInitialized)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
HWND hwnd;
status = s_interactivityFactory->CreatePseudoWindow(hwnd, owner);
s_pseudoWindow.reset(hwnd);
}
s_pseudoWindowInitialized = true;
}
LOG_IF_NTSTATUS_FAILED(status);
return s_pseudoWindow.get();
}
#pragma endregion
#pragma region Private Methods
[[nodiscard]] NTSTATUS ServiceLocator::LoadInteractivityFactory()
{
auto status = STATUS_SUCCESS;
if (s_interactivityFactory.get() == nullptr)
{
s_interactivityFactory = std::make_unique<InteractivityFactory>();
status = NT_TESTNULL(s_interactivityFactory.get());
}
return status;
}
#pragma endregion