Skip to content

Commit

Permalink
Merge pull request #727 from WhitewaterFoundry/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
crramirez committed Jan 24, 2022
2 parents d2dbc18 + 9437080 commit f0e5075
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 326 deletions.
3 changes: 3 additions & 0 deletions ARM64/pengwin.json
@@ -0,0 +1,3 @@
{
"copied from x64": 1
}
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,13 @@
Existing users can update immediately by running $ pengwin-setup update

22.1.5:
* Fix an error message shown at launch in WSL 1 about xdpyinfo when vcxsrv is installed.
* Switch Azure CLI installer to bullseye repos.
* Switch Powershell installer to bullseye repos.
* Improve the performance in the pengwin.exe config --default-user <username>.
* Finally fixed the problem that Pengwin didn't launch in Windows Terminal on specific configurations.
* If you have Windows 11, Windows Terminal 1.12, and have Windows Terminal as your default console, now when you open Pengwin from the Start Menu, it will show the correct profile on Windows Terminal.

22.1.0:
* Show a better message in WSL2 when the Virtual Machine Platform Windows feature is not enabled
* Keep the previous Debian repo for compatibility with packages expecting buster
Expand Down
90 changes: 16 additions & 74 deletions DistroLauncher/DistributionInfo.cpp
Expand Up @@ -5,80 +5,21 @@

#include "stdafx.h"

void RunProcess(LPWSTR cmdline)
HRESULT DistributionInfo::ChangeDefaultUserInWslConf(const std::wstring_view userName)
{
// additional information
STARTUPINFOW si;
PROCESS_INFORMATION pi;
DWORD exitCode = 0;

// set the size of the structures
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
wchar_t commandLine[255];
_swprintf_p(commandLine, _countof(commandLine),
L"if [ $(grep -c \"\\[user\\]\" /etc/wsl.conf) -eq \"0\" ]; then echo -e \"\\n[user]\\ndefault=%1$s\">>/etc/wsl.conf; else sed -i \"s/\\(default=\\)\\(.*\\)/\\1%1$s/\" /etc/wsl.conf ; fi",
std::wstring(userName).c_str());

// start the program up
CreateProcessW
(
nullptr, // the path
cmdline, // Command line
nullptr, // Process handle not inheritable
nullptr, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // Opens file in a separate console
nullptr, // Use parent's environment block
nullptr, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
);

// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);

// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

int RetrieveWindowsVersion()
{
wchar_t value[80];
DWORD size = sizeof(value);

// ReSharper disable once CppTooWideScope
const auto status = RegGetValueW(HKEY_LOCAL_MACHINE,
L"Software\\Microsoft\\Windows NT\\CurrentVersion",
L"CurrentBuild",
RRF_RT_REG_SZ,
nullptr,
&value,
&size
);

if (status == ERROR_SUCCESS)
if (const auto hr = g_wslApi.WslLaunchInteractive(commandLine, true, &exitCode); FAILED(hr) || exitCode != 0)
{
const auto valueInt = std::stoi(value);

return valueInt;
return hr;
}

return -1;
}

void DistributionInfo::ChangeDefaultUserInWslConf(const std::wstring_view userName)
{
// ReSharper disable once CppTooWideScope
const int version = RetrieveWindowsVersion();
if (version < 18362)
{
return;
}

wchar_t buff[255];
_swprintf_p(buff, _countof(buff),
L"wsl.exe -d %1$s -u root -- if [ $(grep -c \"\\[user\\]\" /etc/wsl.conf) -eq \"0\" ]; then echo -e \"\\n[user]\\ndefault=%2$s\">>/etc/wsl.conf; else sed -i \"s/\\(default=\\)\\(.*\\)/\\1%2$s/\" /etc/wsl.conf ; fi",
Name.c_str(), std::wstring(userName).c_str());

RunProcess(buff);
return 0;
}

bool DistributionInfo::CreateUser(std::wstring_view userName)
Expand All @@ -88,7 +29,7 @@ bool DistributionInfo::CreateUser(std::wstring_view userName)
std::wstring commandLine = L"/usr/sbin/adduser --quiet --gecos '' ";
commandLine += userName;
auto hr = g_wslApi.WslLaunchInteractive(commandLine.c_str(), true, &exitCode);
if ((FAILED(hr)) || (exitCode != 0))
if (FAILED(hr) || exitCode != 0)
{
return false;
}
Expand All @@ -97,7 +38,7 @@ bool DistributionInfo::CreateUser(std::wstring_view userName)
commandLine = L"/usr/sbin/usermod -aG adm,cdrom,sudo,dip,plugdev ";
commandLine += userName;
hr = g_wslApi.WslLaunchInteractive(commandLine.c_str(), true, &exitCode);
if ((FAILED(hr)) || (exitCode != 0))
if (FAILED(hr) || exitCode != 0)
{
// Delete the user if the group add command failed.
commandLine = L"/usr/sbin/deluser ";
Expand All @@ -114,7 +55,7 @@ ULONG DistributionInfo::QueryUid(std::wstring_view userName)
// Create a pipe to read the output of the launched process.
HANDLE readPipe;
HANDLE writePipe;
SECURITY_ATTRIBUTES sa{sizeof(sa), nullptr, true};
SECURITY_ATTRIBUTES sa{sizeof sa, nullptr, true};
auto uid = UID_INVALID;
if (CreatePipe(&readPipe, &writePipe, &sa, 0))
{
Expand All @@ -124,14 +65,15 @@ ULONG DistributionInfo::QueryUid(std::wstring_view userName)

HANDLE child;
// ReSharper disable once CppTooWideScope
// ReSharper disable once CppTooWideScopeInitStatement
auto hr = g_wslApi.WslLaunch(command.c_str(), true, GetStdHandle(STD_INPUT_HANDLE), writePipe,
GetStdHandle(STD_ERROR_HANDLE), &child);
if (SUCCEEDED(hr))
{
// Wait for the child to exit and ensure process exited successfully.
WaitForSingleObject(child, INFINITE);
DWORD exitCode;
if ((GetExitCodeProcess(child, &exitCode) == false) || (exitCode != 0))
if (GetExitCodeProcess(child, &exitCode) == false || exitCode != 0)
{
hr = E_INVALIDARG;
}
Expand All @@ -140,11 +82,11 @@ ULONG DistributionInfo::QueryUid(std::wstring_view userName)
if (SUCCEEDED(hr))
{
// ReSharper disable once CppTooWideScope
char buffer[64];
char buffer[64]{};
DWORD bytesRead;

// Read the output of the command from the pipe and convert to a UID.
if (ReadFile(readPipe, buffer, (sizeof(buffer) - 1), &bytesRead, nullptr))
if (ReadFile(readPipe, buffer, sizeof buffer - 1, &bytesRead, nullptr))
{
buffer[bytesRead] = ANSI_NULL;
try
Expand Down
6 changes: 3 additions & 3 deletions DistroLauncher/DistributionInfo.h
Expand Up @@ -13,10 +13,10 @@ namespace DistributionInfo
//
// WARNING: This value must not change between versions of your app,
// otherwise users upgrading from older versions will see launch failures.
const std::wstring Name = L"WLinux";
const std::wstring NAME = L"WLinux";

// The title bar for the console window while the distribution is installing.
const std::wstring WindowTitle = L"Pengwin";
const std::wstring WINDOW_TITLE = L"Pengwin";

// Create and configure a user account.
bool CreateUser(std::wstring_view userName);
Expand All @@ -25,5 +25,5 @@ namespace DistributionInfo
ULONG QueryUid(std::wstring_view userName);

// Changes the default user in /etc/wsl.conf
void ChangeDefaultUserInWslConf(std::wstring_view userName);
HRESULT ChangeDefaultUserInWslConf(std::wstring_view userName);
}
36 changes: 25 additions & 11 deletions DistroLauncher/DistroLauncher.cpp
Expand Up @@ -26,12 +26,13 @@ using namespace Windows::Storage;

// 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);
// ReSharper disable once CppInconsistentNaming
WslApiLoader g_wslApi(DistributionInfo::NAME);

static HRESULT InstallDistribution(bool createUser);
static HRESULT SetDefaultUser(std::wstring_view userName);

HRESULT InstallDistribution(bool createUser)
HRESULT InstallDistribution(const bool createUser)
{
// Register the distribution.
Helpers::PrintMessage(MSG_STATUS_INSTALLING);
Expand Down Expand Up @@ -75,19 +76,30 @@ 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.
const auto uid = DistributionInfo::QueryUid(userName);
const ULONG uid = DistributionInfo::QueryUid(userName);
if (uid == UID_INVALID)
{
return E_INVALIDARG;
}

const auto hr = g_wslApi.WslConfigureDistribution(uid, WSL_DISTRIBUTION_FLAGS_DEFAULT);
// Set the default user as root, so ChangeDefaultUserInWslConf chan make the change
HRESULT hr = g_wslApi.WslConfigureDistribution(0, WSL_DISTRIBUTION_FLAGS_DEFAULT);
if (FAILED(hr))
{
return hr;
}

DistributionInfo::ChangeDefaultUserInWslConf(userName);
hr = DistributionInfo::ChangeDefaultUserInWslConf(userName);
if (FAILED(hr))
{
return hr;
}

hr = g_wslApi.WslConfigureDistribution(uid, WSL_DISTRIBUTION_FLAGS_DEFAULT);
if (FAILED(hr))
{
return hr;
}

return hr;
}
Expand All @@ -98,6 +110,7 @@ int RetrieveCurrentTheme()
DWORD size = sizeof value;

// ReSharper disable once CppTooWideScope
// ReSharper disable once CppTooWideScopeInitStatement
const auto status = RegGetValueW(HKEY_CURRENT_USER,
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
L"AppsUseLightTheme",
Expand All @@ -109,7 +122,7 @@ int RetrieveCurrentTheme()

if (status == ERROR_SUCCESS)
{
return value;
return static_cast<int>(value);
}

return -1;
Expand Down Expand Up @@ -141,7 +154,8 @@ int RetrieveWindowsVersion();
fire_and_forget ShowPengwinUi()
{
// ReSharper disable once CppTooWideScope
const auto file =
// ReSharper disable once CppTooWideScopeInitStatement
const auto& file =
co_await ApplicationData::Current().LocalFolder().TryGetItemAsync(L"MicrosoftStoreEngagementSDKId.txt");

if (! file)
Expand All @@ -156,10 +170,10 @@ fire_and_forget ShowPengwinUi()
}

// ReSharper disable once IdentifierTypo
int wmain(int argc, const wchar_t* argv[])
int wmain(const int argc, const wchar_t* argv[])
{
// Update the title bar of the console window.
SetConsoleTitleW(DistributionInfo::WindowTitle.c_str());
SetConsoleTitleW(DistributionInfo::WINDOW_TITLE.c_str());

// Initialize a vector of arguments.
std::vector<std::wstring_view> arguments;
Expand All @@ -178,7 +192,7 @@ int wmain(int argc, const wchar_t* argv[])
Helpers::PromptForInput();
}

return exitCode;
return static_cast<int>(exitCode);
}

// Install the distribution if it is not already.
Expand Down Expand Up @@ -255,7 +269,7 @@ int wmain(int argc, const wchar_t* argv[])
else
{
Helpers::PrintMessage(MSG_USAGE);
return exitCode;
return static_cast<int>(exitCode);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Pengwin/Package.appxmanifest
Expand Up @@ -11,7 +11,7 @@
<Identity
Name="WhitewaterFoundryLtd.Co.16571368D6CFF"
Publisher="CN=9879127B-9E92-4DE5-9C32-0B1F09F95DCF"
Version="22.1.2.0" />
Version="22.1.5.0" />

<Properties>
<DisplayName>Pengwin</DisplayName>
Expand Down

0 comments on commit f0e5075

Please sign in to comment.