Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions client/hostinfo_wsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "boinc_win.h"
#include "win_util.h"

#include "error_numbers.h"
#include "str_replace.h"
#include "client_state.h"
#include "client_msgs.h"
Expand Down Expand Up @@ -51,8 +52,12 @@ int get_all_distros(WSL_DISTROS& distros) {
LONG lRet = RegOpenKeyEx(HKEY_CURRENT_USER,
lxss_path.c_str(), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey
);
if (lRet == ERROR_FILE_NOT_FOUND) {
msg_printf(0, MSG_INFO, "WSL: registry key not found; assuming no WSL distros are installed");
return 0;
}
if (lRet != ERROR_SUCCESS) {
msg_printf(0, MSG_INFO, "WSL: registry open failed");
msg_printf(0, MSG_INFO, "WSL: registry open failed (error %ld)", lRet);
return -1;
}

Expand All @@ -64,8 +69,9 @@ int get_all_distros(WSL_DISTROS& distros) {
(LPBYTE)default_wsl_guid, &default_wsl_guid_len
);
if ((lRet != ERROR_SUCCESS) || (default_wsl_guid_len > buf_len)) {
msg_printf(0, MSG_INFO, "WSL: registry query failed");
return -1;
msg_printf(0, MSG_INFO, "WSL: registry query for DefaultDistribution failed (error %ld)", lRet);
RegCloseKey(hKey);
return 0;
}

// scan subkeys (one per distro)
Expand Down Expand Up @@ -97,6 +103,7 @@ int get_all_distros(WSL_DISTROS& distros) {
hSubKey, "State", NULL, NULL, (LPBYTE)&wsl_state, &wsl_state_len
);
if (ret != ERROR_SUCCESS || wsl_state != 1) {
RegCloseKey(hSubKey);
continue;
}

Expand Down Expand Up @@ -196,16 +203,21 @@ int get_wsl_information(WSL_DISTROS &distros) {
WSL_DISTROS all_distros;
int retval = get_all_distros(all_distros);
if (retval) return retval;
if (all_distros.distros.empty()) {
distros.distros.clear();
return 0;
}
string err_msg;

WSL_CMD rs;

if (rs.setup(err_msg)) {
msg_printf(0, MSG_INFO, "WSL setup error: %s", err_msg.c_str());
return -1;
msg_printf(0, MSG_INFO, "WSL unavailable: %s", err_msg.c_str());
return 0;
}

string reply;
bool launch_failed = false;

// loop over all WSL distros
for (WSL_DISTRO &wd: all_distros.distros) {
Expand Down Expand Up @@ -234,6 +246,9 @@ int get_wsl_information(WSL_DISTROS &distros) {
);
CloseHandle(rs.proc_handle);
update_os(wd, os_name, os_version);
} else {
launch_failed = true;
break;
}

// try reading '/etc/os-relese'
Expand All @@ -249,16 +264,17 @@ int get_wsl_information(WSL_DISTROS &distros) {
);
CloseHandle(rs.proc_handle);
update_os(wd, os_name, os_version);
} else {
launch_failed = true;
break;
}
}

// try reading '/etc/redhatrelease'
//
if (!got_both(wd)) {
const std::string command_redhatrelease = "cat " + std::string(file_redhatrelease);
if (!rs.run_program_in_wsl(
wd.distro_name, command_redhatrelease
)) {
if (!rs.run_program_in_wsl(wd.distro_name, command_redhatrelease)) {
read_from_pipe(rs.out_read, rs.proc_handle, reply, CMD_TIMEOUT);
HOST_INFO::parse_linux_os_info(
reply, redhatrelease,
Expand All @@ -267,6 +283,9 @@ int get_wsl_information(WSL_DISTROS &distros) {
);
CloseHandle(rs.proc_handle);
update_os(wd, os_name, os_version);
} else {
launch_failed = true;
break;
}
}

Expand All @@ -287,6 +306,9 @@ int get_wsl_information(WSL_DISTROS &distros) {
);
CloseHandle(rs.proc_handle);
update_os(wd, os_name_str.c_str(), os_version_str.c_str());
} else {
launch_failed = true;
break;
}
}

Expand All @@ -301,6 +323,9 @@ int get_wsl_information(WSL_DISTROS &distros) {
strip_whitespace(os_name_str);
CloseHandle(rs.proc_handle);
update_os(wd, os_name_str.c_str(), "");
} else {
launch_failed = true;
break;
}
}

Expand All @@ -315,6 +340,9 @@ int get_wsl_information(WSL_DISTROS &distros) {
strip_whitespace(os_version_str);
CloseHandle(rs.proc_handle);
update_os(wd, "", os_version_str.c_str());
} else {
launch_failed = true;
break;
}
}

Expand Down Expand Up @@ -372,6 +400,11 @@ int get_wsl_information(WSL_DISTROS &distros) {
distros.distros.push_back(wd);
}

if (launch_failed) {
msg_printf(0, MSG_INFO, "WSL commands cannot be launched; skipping WSL detection");
distros.distros.clear();
}

return 0;
}

Expand Down
5 changes: 4 additions & 1 deletion lib/win_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,17 @@ int WSL_CMD::setup_podman(const char* distro_name) {
int WSL_CMD::run_program_in_wsl(
const string distro_name, const string command, bool use_cwd
) {
proc_handle = NULL;
HRESULT ret = pWslLaunch(
boinc_ascii_to_wide(distro_name).c_str(),
boinc_ascii_to_wide(command).c_str(),
use_cwd, in_read, out_write, out_write,
&proc_handle
);
if (ret != S_OK) {
fprintf(stderr, "pWslLaunch failed: %d\n", ret);
fprintf(stderr, "pWslLaunch failed: 0x%08lx\n", ret);
proc_handle = NULL;
return ERR_NOT_FOUND;
}
return 0;
}
Expand Down