-
Notifications
You must be signed in to change notification settings - Fork 40
/
DistroLauncher.cpp
204 lines (166 loc) · 6.16 KB
/
DistroLauncher.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
//
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//
#include "stdafx.h"
// Commandline arguments:
#define ARG_CONFIG L"config"
#define ARG_CONFIG_DEFAULT_USER L"--default-user"
#define ARG_INSTALL L"install"
#define ARG_INSTALL_ROOT L"--root"
#define ARG_RUN L"run"
#define ARG_RUN_C L"-c"
#define ARG_HELP L"help"
// Helper class for calling WSL Functions:
// https://msdn.microsoft.com/en-us/library/windows/desktop/mt826874(v=vs.85).aspx
WslApiLoader g_wslApi(DistributionInfo::Name);
static HRESULT InstallDistribution(bool createUser);
static HRESULT SetDefaultUser(std::wstring_view userName);
HRESULT InstallDistribution(bool createUser)
{
// Register the distribution.
Helpers::PrintMessage(MSG_STATUS_INSTALLING);
HRESULT hr = g_wslApi.WslRegisterDistribution();
if (FAILED(hr)) {
return hr;
}
// Delete /etc/resolv.conf to allow WSL to generate a version based on Windows networking information.
DWORD exitCode;
hr = g_wslApi.WslLaunchInteractive(L"rm /etc/resolv.conf", true, &exitCode);
if (FAILED(hr)) {
return hr;
}
if (Ubuntu::CheckInitTasks(g_wslApi, createUser)) {
return ERROR_SUCCESS;
}
// Create a user account.
if (createUser) {
Helpers::PrintMessage(MSG_CREATE_USER_PROMPT);
std::wstring userName;
do {
userName = Helpers::GetUserInput(MSG_ENTER_USERNAME, 32);
} while (!DistributionInfo::CreateUser(userName));
// Set this user account as the default.
hr = SetDefaultUser(userName);
if (FAILED(hr)) {
return hr;
}
}
return hr;
}
HRESULT SetDefaultUser(std::wstring_view userName)
{
// Query the UID of the given user name and configure the distribution
// to use this UID as the default.
ULONG uid = DistributionInfo::QueryUid(userName);
if (uid == UID_INVALID) {
return E_INVALIDARG;
}
HRESULT hr = g_wslApi.WslConfigureDistribution(uid, WSL_DISTRIBUTION_FLAGS_DEFAULT);
if (FAILED(hr)) {
return hr;
}
return hr;
}
int DebugReportHook(int reportType, char *message, int *returnValue)
{
const auto type = [=]() -> std::string_view {
switch (reportType) {
case _CRT_WARN:
return "[WARNING]";
case _CRT_ERROR:
return "[ERROR]";
case _CRT_ASSERT:
return "[ASSERT]";
default:
return "[UNKNOWN]";
}
}();
std::cerr << type << " " << message;
exit(EXIT_FAILURE);
}
int wmain(int argc, wchar_t const *argv[])
{
_CrtSetReportHook(DebugReportHook);
// Update the title bar of the console window.
SetConsoleTitleW(DistributionInfo::WindowTitle.c_str());
// Initialize a vector of arguments.
std::vector<std::wstring_view> arguments;
for (int index = 1; index < argc; index += 1) {
arguments.push_back(argv[index]);
}
// Deal with possible help flag.
if (!arguments.empty() && arguments.front() == ARG_HELP) {
Helpers::PrintMessage(MSG_USAGE);
return 0;
}
// Ensure that the Windows Subsystem for Linux optional component is installed.
DWORD exitCode = 1;
if (!g_wslApi.WslIsOptionalComponentInstalled()) {
Helpers::PrintErrorMessage(HRESULT_FROM_WIN32(ERROR_LINUX_SUBSYSTEM_NOT_PRESENT));
if (arguments.empty()) {
Helpers::PromptForInput();
}
return exitCode;
}
// Install the distribution if it is not already.
bool installOnly = ((arguments.size() > 0) && (arguments[0] == ARG_INSTALL));
HRESULT hr = S_OK;
if (!g_wslApi.WslIsDistributionRegistered()) {
// If the "--root" option is specified, do not create a user account.
bool useRoot = ((installOnly) && (arguments.size() > 1) && (arguments[1] == ARG_INSTALL_ROOT));
hr = InstallDistribution(!useRoot);
if (FAILED(hr)) {
if (hr == HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS)) {
Helpers::PrintMessage(MSG_INSTALL_ALREADY_EXISTS);
}
} else {
Helpers::PrintMessage(MSG_INSTALL_SUCCESS);
}
exitCode = SUCCEEDED(hr) ? 0 : 1;
}
// Parse the command line arguments.
if ((SUCCEEDED(hr)) && (!installOnly)) {
if (arguments.empty()) {
hr = g_wslApi.WslLaunchInteractive(L"", false, &exitCode);
// Check exitCode to see if wsl.exe returned that it could not start the Linux process
// then prompt users for input so they can view the error message.
if (SUCCEEDED(hr) && exitCode == UINT_MAX) {
Helpers::PromptForInput();
}
} else if ((arguments[0] == ARG_RUN) ||
(arguments[0] == ARG_RUN_C)) {
std::wstring command;
for (size_t index = 1; index < arguments.size(); index += 1) {
command += L" ";
command += arguments[index];
}
hr = g_wslApi.WslLaunchInteractive(command.c_str(), true, &exitCode);
} else if (arguments[0] == ARG_CONFIG) {
hr = E_INVALIDARG;
if (arguments.size() == 3) {
if (arguments[1] == ARG_CONFIG_DEFAULT_USER) {
hr = SetDefaultUser(arguments[2]);
}
}
if (SUCCEEDED(hr)) {
exitCode = 0;
}
} else {
Helpers::PrintMessage(MSG_USAGE);
return exitCode;
}
}
// If an error was encountered, print an error message.
if (FAILED(hr)) {
if (hr == HCS_E_HYPERV_NOT_INSTALLED) {
Helpers::PrintMessage(MSG_ENABLE_VIRTUALIZATION);
} else {
Helpers::PrintErrorMessage(hr);
}
if (arguments.empty()) {
Helpers::PromptForInput();
}
}
return SUCCEEDED(hr) ? exitCode : 1;
}