-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathWslInstaller.cpp
More file actions
195 lines (151 loc) · 5.2 KB
/
WslInstaller.cpp
File metadata and controls
195 lines (151 loc) · 5.2 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
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
WslInstaller.cpp
Abstract:
This file contains the implementation of the WslInstaller class.
--*/
#include "precomp.h"
#include "install.h"
#include "WslInstaller.h"
extern wil::unique_event g_stopEvent;
std::wstring GetMsiPackagePath()
{
#ifdef WSL_DEV_THIN_MSI_PACKAGE
static_assert(!wsl::shared::OfficialBuild);
return std::filesystem::weakly_canonical(WSL_DEV_THIN_MSI_PACKAGE).wstring();
#endif
return (wsl::windows::common::wslutil::GetBasePath() / L"wsl.msi").wstring();
}
std::optional<std::wstring> GetUpgradeLogFileLocation()
try
{
const auto key = wsl::windows::common::registry::OpenLxssMachineKey();
const auto path = wsl::windows::common::registry::ReadString(key.get(), L"MSI", L"UpgradeLogFile", L"");
if (path.empty())
{
return {};
}
// A canonical path is required because msiexec doesn't like symlinks.
return std::filesystem::weakly_canonical(path);
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
return {};
}
std::pair<UINT, std::wstring> InstallMsipackageImpl()
{
const auto logFile = GetUpgradeLogFileLocation();
std::wstring errors;
auto messageCallback = [&errors](INSTALLMESSAGE type, LPCWSTR message) {
switch (type)
{
case INSTALLMESSAGE_ERROR:
case INSTALLMESSAGE_FATALEXIT:
case INSTALLMESSAGE_WARNING:
case INSTALLMESSAGE_FILESINUSE:
case INSTALLMESSAGE_OUTOFDISKSPACE:
if (!errors.empty())
{
errors += L"\n";
}
errors += message;
break;
default:
break;
}
};
auto result = wsl::windows::common::install::UpgradeViaMsi(
GetMsiPackagePath().c_str(), L"SKIPMSIX=1", logFile.has_value() ? logFile->c_str() : nullptr, messageCallback);
// ERROR_SUCCESS_REBOOT_REQUIRED (3010) means the install succeeded but some files
// will be replaced on the next reboot. Treat as success since the service runs
// silently with no user-facing console.
const bool rebootRequired = (result == ERROR_SUCCESS_REBOOT_REQUIRED);
if (rebootRequired)
{
result = ERROR_SUCCESS;
}
WSL_LOG(
"MSIUpgradeResult",
TraceLoggingValue(result, "result"),
TraceLoggingValue(rebootRequired, "rebootRequired"),
TraceLoggingValue(errors.c_str(), "errorMessage"));
return {result, errors};
}
DWORD WINAPI InstallMsiPackage(LPVOID Context)
{
auto* installContext = reinterpret_cast<InstallContext*>(Context);
try
{
std::tie(installContext->ExitCode, installContext->Errors) = InstallMsipackageImpl();
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
installContext->Result = wil::ResultFromCaughtException();
return 0;
}
installContext->Result = S_OK;
return 0;
}
std::pair<bool, std::wstring> IsUpdateNeeded()
{
try
{
const auto key = wsl::windows::common::registry::OpenLxssMachineKey();
const auto installedVersion = wsl::windows::common::registry::ReadString(key.get(), L"MSI", L"Version", L"");
WSL_LOG(
"DetectedInstalledVersion",
TraceLoggingLevel(WINEVENT_LEVEL_INFO),
TraceLoggingValue(installedVersion.c_str(), "InstalledVersion"));
return std::make_pair(
installedVersion.empty() || wsl::windows::common::wslutil::ParseWslPackageVersion(installedVersion) < wsl::shared::PackageVersion,
installedVersion);
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
return std::make_pair(false, L"");
}
}
std::shared_ptr<InstallContext> LaunchInstall()
{
static wil::srwlock mutex;
static std::weak_ptr<InstallContext> weak_context;
auto lock = mutex.lock_exclusive();
auto [updateNeeded, existingVersion] = IsUpdateNeeded();
if (!updateNeeded)
{
return {};
}
wsl::windows::common::install::WriteInstallLog(std::format("Starting upgrade via WslInstaller. Previous version: {}", existingVersion));
// Return an existing install if any
if (auto ptr = weak_context.lock(); ptr != nullptr)
{
return ptr;
}
// Else launch a new install
auto context = std::make_shared<InstallContext>();
weak_context = std::weak_ptr<InstallContext>(context);
context->Thread = wil::unique_handle{CreateThread(nullptr, 0, &InstallMsiPackage, context.get(), 0, nullptr)};
return context;
}
HRESULT WslInstaller::Install(UINT* ExitCode, LPWSTR* Errors)
try
{
const auto context = LaunchInstall();
if (!context)
{
// This block can be reached if the installation completed after the client looked up the MSI package.
// In this case don't attempt to install and return success so the client looks up the MSI package again.
*ExitCode = 0;
*Errors = wil::make_unique_string<wil::unique_cotaskmem_string>(L"").release();
return S_OK;
}
THROW_LAST_ERROR_IF(WaitForSingleObject(context->Thread.get(), INFINITE) != WAIT_OBJECT_0);
*ExitCode = context->ExitCode;
*Errors = wil::make_unique_string<wil::unique_cotaskmem_string>(context->Errors.c_str()).release();
return context->Result;
}
CATCH_RETURN()