-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwslutils.cpp
239 lines (206 loc) · 7.39 KB
/
wslutils.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
/* This file is part of wslman.
*
* wslman is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* wslman is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wslman. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wslutils.h"
#include "wslregistry.h"
#include "wslfs.h"
#include <QMessageBox>
#include <QIcon>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QtWin>
#endif
#include <process.h>
WslConsoleContext *WslConsoleContext::createConsole(const std::wstring &name,
const QIcon &icon)
{
SetConsoleTitleW(name.c_str());
auto context = new WslConsoleContext;
context->distName = name;
const QIcon distIcon = icon;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
context->distIconBig = distIcon.pixmap(32, 32).toImage().toHICON();
context->distIconSmall = distIcon.pixmap(16, 16).toImage().toHICON();
#else
context->distIconBig = QtWin::toHICON(distIcon.pixmap(32, 32));
context->distIconSmall = QtWin::toHICON(distIcon.pixmap(16, 16));
#endif
HWND hConsole = GetConsoleWindow();
SendMessageW(hConsole, WM_SETICON, ICON_BIG, (LPARAM)context->distIconBig);
SendMessageW(hConsole, WM_SETICON, ICON_SMALL, (LPARAM)context->distIconSmall);
freopen_s(&context->stdoutStream, "CONOUT$", "w", stdout);
freopen_s(&context->stderrStream, "CONOUT$", "w", stderr);
return context;
}
static unsigned _startShell(void *pvContext)
{
auto context = reinterpret_cast<WslConsoleContext *>(pvContext);
DWORD exitCode;
try {
WslApi::LaunchInteractive(context->distName.c_str(), L"", FALSE, &exitCode);
} catch (const std::runtime_error &err) {
context->errorMessage = QObject::tr("Failed to start %1: %2")
.arg(context->distName).arg(err.what());
}
fclose(context->stdoutStream);
fclose(context->stderrStream);
context->shellTerminated = true;
context->unref();
return exitCode;
}
void WslConsoleContext::startConsoleThread(QWidget *parent)
{
// Add a ref for the thread. When the thread exits, it should call unref()
ref();
unsigned threadId;
HANDLE th = reinterpret_cast<HANDLE>(_beginthreadex(nullptr, 0, &_startShell,
reinterpret_cast<void *>(this), 0, &threadId));
CloseHandle(th);
// Detach from the console once the WSL process starts using it
DWORD processList[4];
DWORD processCount = 4;
for ( ;; ) {
DWORD activeProcs = GetConsoleProcessList(processList, processCount);
if (activeProcs > 1 || shellTerminated)
break;
Sleep(100);
}
if (!errorMessage.isEmpty())
QMessageBox::critical(parent, QString(), errorMessage);
unref();
}
typedef std::tuple<std::string, uint32_t, uint32_t> UserInfo;
static std::list<UserInfo> readPasswdFile(const std::wstring &distName)
{
WslRegistry registry;
WslDistribution dist = registry.findDistByName(distName);
if (!dist.isValid())
return {};
UniqueHandle hPasswd;
try {
WslFs rootfs(dist.rootfsPath());
hPasswd = rootfs.openFile("/etc/passwd");
if (hPasswd == INVALID_HANDLE_VALUE)
return {};
} catch (const std::runtime_error &) {
return {};
}
std::string content;
for ( ;; ) {
char buffer[1024];
DWORD nRead = 0;
if (!ReadFile(hPasswd.get(), buffer, static_cast<DWORD>(std::size(buffer)),
&nRead, nullptr) || nRead == 0) {
break;
}
content += std::string_view(buffer, nRead);
}
std::list<UserInfo> result;
size_t start = 0;
for ( ;; ) {
size_t end = content.find(':', start);
if (end == content.npos)
break;
std::string_view username(content.c_str() + start, end - start);
start = end + 1;
// Password hash
end = content.find(':', start);
if (end == content.npos)
break;
start = end + 1;
// UID
uint32_t uid = static_cast<uint32_t>(std::strtoul(content.c_str() + start, nullptr, 10));
end = content.find(':', start);
if (end == content.npos)
break;
start = end + 1;
// GID
uint32_t gid = static_cast<uint32_t>(std::strtoul(content.c_str() + start, nullptr, 10));
end = content.find(':', start);
if (end == content.npos)
break;
start = end + 1;
result.emplace_back(username, uid, gid);
// Advance to next line
end = content.find('\n', start);
if (end == content.npos)
break;
start = end + 1;
}
return result;
}
QString WslUtil::getUsername(const std::wstring &distName, uint32_t uid)
{
auto passwd = readPasswdFile(distName);
for (const auto &user : passwd) {
if (std::get<1>(user) == uid)
return QString::fromStdString(std::get<0>(user));
}
return QString();
}
uint32_t WslUtil::getUid(const std::wstring &distName, const QString &username)
{
auto passwd = readPasswdFile(distName);
const std::string u8Username = username.toStdString();
for (const auto &user : passwd) {
if (std::get<0>(user) == u8Username)
return std::get<1>(user);
}
return INVALID_UID;
}
std::wstring WslUtil::fromUtf8(const std::string_view &utf8)
{
std::wstring result;
const int u8size = static_cast<int>(utf8.size());
const int wsize = MultiByteToWideChar(CP_UTF8, 0, utf8.data(), u8size, nullptr, 0);
if (wsize == 0)
return result;
result.resize(wsize);
MultiByteToWideChar(CP_UTF8, 0, utf8.data(), u8size, result.data(), wsize);
return result;
}
std::string WslUtil::toUtf8(const std::wstring_view &wide)
{
std::string result;
const int wsize = static_cast<int>(wide.size());
const int u8size = WideCharToMultiByte(CP_UTF8, 0, wide.data(), wsize,
nullptr, 0, nullptr, nullptr);
if (u8size == 0)
return result;
result.resize(u8size);
WideCharToMultiByte(CP_UTF8, 0, wide.data(), wsize,
result.data(), u8size, nullptr, nullptr);
return result;
}
bool WslUtil::checkWindowsVersion(unsigned build)
{
#if _MSC_VER
// MS's replacements for GetVersionEx don't provide the build number
# pragma warning(disable:4996)
#endif
OSVERSIONINFOEXW verInfo{sizeof(verInfo)};
if (GetVersionExW(reinterpret_cast<LPOSVERSIONINFOW>(&verInfo))) {
if (verInfo.dwMajorVersion < 10)
return false;
if (verInfo.dwMajorVersion == 10 && verInfo.dwMinorVersion == 0
&& verInfo.dwBuildNumber < build) {
return false;
}
return true;
}
#if _MSC_VER
# pragma warning(default:4996)
#endif
return false;
}