-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Expand file tree
/
Copy pathquick_access_host.cpp
More file actions
296 lines (263 loc) · 9.65 KB
/
quick_access_host.cpp
File metadata and controls
296 lines (263 loc) · 9.65 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
#include "pch.h"
#include "quick_access_host.h"
#include <mutex>
#include <string>
#include <vector>
#include <rpc.h>
#include <new>
#include <memory>
#include <common/logger/logger.h>
#include <common/utils/process_path.h>
#include <common/interop/two_way_pipe_message_ipc.h>
#include <wil/resource.h>
extern void receive_json_send_to_main_thread(const std::wstring& msg);
namespace
{
wil::unique_handle quick_access_process;
wil::unique_handle quick_access_job;
wil::unique_handle show_event;
wil::unique_handle exit_event;
std::wstring show_event_name;
std::wstring exit_event_name;
std::wstring runner_pipe_name;
std::wstring app_pipe_name;
std::unique_ptr<TwoWayPipeMessageIPC> quick_access_ipc;
std::mutex quick_access_mutex;
bool is_process_active_locked()
{
if (!quick_access_process)
{
return false;
}
DWORD exit_code = 0;
if (!GetExitCodeProcess(quick_access_process.get(), &exit_code))
{
Logger::warn(L"QuickAccessHost: failed to read Quick Access process exit code. error={}.", GetLastError());
return false;
}
return exit_code == STILL_ACTIVE;
}
void reset_state_locked()
{
if (quick_access_ipc)
{
quick_access_ipc->end();
quick_access_ipc.reset();
}
quick_access_process.reset();
quick_access_job.reset();
show_event.reset();
exit_event.reset();
show_event_name.clear();
exit_event_name.clear();
runner_pipe_name.clear();
app_pipe_name.clear();
}
std::wstring build_event_name(const wchar_t* suffix)
{
std::wstring name = L"Local\\PowerToysQuickAccess_";
name += std::to_wstring(GetCurrentProcessId());
if (suffix)
{
name += suffix;
}
return name;
}
std::wstring build_command_line(const std::wstring& exe_path)
{
std::wstring command_line = L"\"";
command_line += exe_path;
command_line += L"\" --show-event=\"";
command_line += show_event_name;
command_line += L"\" --exit-event=\"";
command_line += exit_event_name;
command_line += L"\"";
if (!runner_pipe_name.empty())
{
command_line.append(L" --runner-pipe=\"");
command_line += runner_pipe_name;
command_line += L"\"";
}
if (!app_pipe_name.empty())
{
command_line.append(L" --app-pipe=\"");
command_line += app_pipe_name;
command_line += L"\"";
}
return command_line;
}
}
namespace QuickAccessHost
{
bool is_running()
{
std::scoped_lock lock(quick_access_mutex);
return is_process_active_locked();
}
void start()
{
Logger::info(L"QuickAccessHost::start() called");
std::scoped_lock lock(quick_access_mutex);
if (is_process_active_locked())
{
Logger::info(L"QuickAccessHost::start: process already active");
return;
}
reset_state_locked();
show_event_name = build_event_name(L"_Show");
exit_event_name = build_event_name(L"_Exit");
show_event.reset(CreateEventW(nullptr, FALSE, FALSE, show_event_name.c_str()));
if (!show_event)
{
Logger::error(L"QuickAccessHost: failed to create show event. error={}.", GetLastError());
reset_state_locked();
return;
}
exit_event.reset(CreateEventW(nullptr, FALSE, FALSE, exit_event_name.c_str()));
if (!exit_event)
{
Logger::error(L"QuickAccessHost: failed to create exit event. error={}.", GetLastError());
reset_state_locked();
return;
}
runner_pipe_name = L"\\\\.\\pipe\\powertoys_quick_access_runner_";
app_pipe_name = L"\\\\.\\pipe\\powertoys_quick_access_ui_";
UUID temp_uuid;
wchar_t* uuid_chars = nullptr;
if (UuidCreate(&temp_uuid) == RPC_S_UUID_NO_ADDRESS)
{
Logger::warn(L"QuickAccessHost: failed to create UUID for pipe names. error={}.", GetLastError());
}
else if (UuidToString(&temp_uuid, reinterpret_cast<RPC_WSTR*>(&uuid_chars)) != RPC_S_OK)
{
Logger::warn(L"QuickAccessHost: failed to convert UUID to string. error={}.", GetLastError());
}
if (uuid_chars != nullptr)
{
runner_pipe_name += std::wstring(uuid_chars);
app_pipe_name += std::wstring(uuid_chars);
RpcStringFree(reinterpret_cast<RPC_WSTR*>(&uuid_chars));
uuid_chars = nullptr;
}
else
{
const std::wstring fallback_suffix = std::to_wstring(GetTickCount64());
runner_pipe_name += fallback_suffix;
app_pipe_name += fallback_suffix;
}
HANDLE token_handle = nullptr;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token_handle))
{
Logger::error(L"QuickAccessHost: failed to open process token. error={}.", GetLastError());
reset_state_locked();
return;
}
wil::unique_handle token(token_handle);
quick_access_ipc.reset(new (std::nothrow) TwoWayPipeMessageIPC(runner_pipe_name, app_pipe_name, receive_json_send_to_main_thread));
if (!quick_access_ipc)
{
Logger::error(L"QuickAccessHost: failed to allocate IPC instance.");
reset_state_locked();
return;
}
try
{
quick_access_ipc->start(token.get());
}
catch (...)
{
Logger::error(L"QuickAccessHost: failed to start IPC server for Quick Access.");
reset_state_locked();
return;
}
const std::wstring exe_path = get_module_folderpath() + L"\\WinUI3Apps\\PowerToys.QuickAccess.exe";
if (GetFileAttributesW(exe_path.c_str()) == INVALID_FILE_ATTRIBUTES)
{
Logger::warn(L"QuickAccessHost: missing Quick Access executable at {}", exe_path);
reset_state_locked();
return;
}
const std::wstring command_line = build_command_line(exe_path);
std::vector<wchar_t> command_line_buffer(command_line.begin(), command_line.end());
command_line_buffer.push_back(L'\0');
STARTUPINFOW startup_info{};
startup_info.cb = sizeof(startup_info);
PROCESS_INFORMATION process_info{};
BOOL created = CreateProcessW(exe_path.c_str(), command_line_buffer.data(), nullptr, nullptr, FALSE, CREATE_SUSPENDED, nullptr, nullptr, &startup_info, &process_info);
if (!created)
{
Logger::error(L"QuickAccessHost: failed to launch Quick Access host. error={}.", GetLastError());
reset_state_locked();
return;
}
quick_access_process.reset(process_info.hProcess);
// Assign to job object to ensure the process is killed if the runner exits unexpectedly (e.g. debugging stop)
quick_access_job.reset(CreateJobObjectW(nullptr, nullptr));
if (quick_access_job)
{
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if (!SetInformationJobObject(quick_access_job.get(), JobObjectExtendedLimitInformation, &jeli, sizeof(jeli)))
{
Logger::warn(L"QuickAccessHost: failed to set job object information. error={}", GetLastError());
}
else
{
if (!AssignProcessToJobObject(quick_access_job.get(), quick_access_process.get()))
{
Logger::warn(L"QuickAccessHost: failed to assign process to job object. error={}", GetLastError());
}
}
}
else
{
Logger::warn(L"QuickAccessHost: failed to create job object. error={}", GetLastError());
}
ResumeThread(process_info.hThread);
CloseHandle(process_info.hThread);
}
void show()
{
start();
std::scoped_lock lock(quick_access_mutex);
if (show_event)
{
if (!SetEvent(show_event.get()))
{
Logger::warn(L"QuickAccessHost: failed to signal show event. error={}.", GetLastError());
}
}
}
void stop()
{
Logger::info(L"QuickAccessHost::stop() called");
std::unique_lock lock(quick_access_mutex);
if (exit_event)
{
SetEvent(exit_event.get());
}
if (quick_access_process)
{
const DWORD wait_result = WaitForSingleObject(quick_access_process.get(), 2000);
Logger::info(L"QuickAccessHost::stop: WaitForSingleObject result={}", wait_result);
if (wait_result == WAIT_TIMEOUT)
{
Logger::warn(L"QuickAccessHost: Quick Access process did not exit in time, terminating.");
if (!TerminateProcess(quick_access_process.get(), 0))
{
Logger::error(L"QuickAccessHost: failed to terminate Quick Access process. error={}.", GetLastError());
}
else
{
Logger::info(L"QuickAccessHost: TerminateProcess succeeded.");
WaitForSingleObject(quick_access_process.get(), 5000);
}
}
else if (wait_result == WAIT_FAILED)
{
Logger::error(L"QuickAccessHost: failed while waiting for Quick Access process. error={}.", GetLastError());
}
}
reset_state_locked();
}
}