Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client: add some error reporting to WSL detection #5973

Merged
merged 3 commits into from
Dec 26, 2024
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
6 changes: 5 additions & 1 deletion client/hostinfo_wsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ int get_all_distros(WSL_DISTROS& distros) {
lxss_path.c_str(), 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hKey
);
if (lRet != ERROR_SUCCESS) {
msg_printf(0, MSG_INFO, "WSL: registry open failed");
return -1;
}

Expand All @@ -59,6 +60,7 @@ 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;
}

Expand Down Expand Up @@ -179,10 +181,12 @@ int get_wsl_information(WSL_DISTROS &distros) {
WSL_DISTROS all_distros;
int retval = get_all_distros(all_distros);
if (retval) return retval;
string err_msg;

WSL_CMD rs;

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

Expand Down
3 changes: 2 additions & 1 deletion lib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,10 @@ string parse_ldd_libc(const char* input) {

#ifdef _WIN32
int DOCKER_CONN::init(DOCKER_TYPE docker_type, string distro_name, bool _verbose) {
string err_msg;
cli_prog = docker_cli_prog(docker_type);
if (docker_type == DOCKER) {
int retval = ctl_wc.setup();
int retval = ctl_wc.setup(err_msg);
if (retval) return retval;
retval = ctl_wc.run_program_in_wsl(distro_name, "", true);
if (retval) return retval;
Expand Down
32 changes: 25 additions & 7 deletions lib/win_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,28 +199,46 @@ typedef HRESULT(WINAPI *PWslLaunch)(
static PWslLaunch pWslLaunch = NULL;
static HINSTANCE wsl_lib = NULL;

int WSL_CMD::setup() {
int WSL_CMD::setup(string &err_msg) {
in_read = NULL;
in_write = NULL;
out_read = NULL;
out_write = NULL;

if (!pWslLaunch) {
wsl_lib = LoadLibraryA("wslapi.dll");
if (!wsl_lib) return -1;
if (!wsl_lib) {
err_msg = "Can't load wslapi.dll";
return -1;
}
pWslLaunch = (PWslLaunch)GetProcAddress(wsl_lib, "WslLaunch");
if (!pWslLaunch) return -1;
if (!pWslLaunch) {
err_msg = "WslLaunch not in wslapi.dll";
return -1;
}
}

SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;

if (!CreatePipe(&out_read, &out_write, &sa, 0)) return -1;
if (!SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)) return -1;
if (!CreatePipe(&in_read, &in_write, &sa, 0)) return -1;
if (!SetHandleInformation(in_write, HANDLE_FLAG_INHERIT, 0)) return -1;
if (!CreatePipe(&out_read, &out_write, &sa, 0)) {
err_msg = "Can't create out pipe";
return -1;
}
if (!SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)) {
err_msg = "Can't inherit out pipe";
return -1;
}
if (!CreatePipe(&in_read, &in_write, &sa, 0)) {
err_msg = "Can't create in pipe";
return -1;
}
if (!SetHandleInformation(in_write, HANDLE_FLAG_INHERIT, 0)) {
err_msg = "Can't inherit in pipe";
return -1;
}
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/win_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct WSL_CMD {
// Use WslLaunch() to run a shell in the WSL container
// The shell will run as the default user
//
int setup();
int setup(std::string&);

// Use wsl.exe to run a shell as root in the WSL container
//
Expand Down
9 changes: 5 additions & 4 deletions samples/wsl_wrapper/wsl_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ int error(const char* where, int retval) {
//
int launch(const char* distro, const char* cmd) {
char launch_cmd[256];
string err_msg;
sprintf(launch_cmd, "echo $$; %s; touch boinc_job_done\n", cmd);
int retval = app_wc.setup();
if (retval) return error("app setup", retval);
int retval = app_wc.setup(err_msg);
if (retval) return error(err_msg.c_str(), retval);
retval = app_wc.run_program_in_wsl(distro, launch_cmd, true);
if (retval) return error("app run_program_in_wsl", retval);

Expand All @@ -117,8 +118,8 @@ int launch(const char* distro, const char* cmd) {

// set up control channel
//
retval = ctl_wc.setup();
if (retval) return error("ctl setup", retval);
retval = ctl_wc.setup(err_msg);
if (retval) return error(err_msg.c_str(), retval);
retval = ctl_wc.run_program_in_wsl(distro, "", true);
// empty string means run shell
if (retval) return error("ctl run_program_in_wsl", retval);
Expand Down
Loading