cli/tunnels/
wsl_detect.rs

1/*---------------------------------------------------------------------------------------------
2 *  Copyright (c) Microsoft Corporation. All rights reserved.
3 *  Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
5
6use crate::log;
7
8#[cfg(not(windows))]
9pub fn is_wsl_installed(_log: &log::Logger) -> bool {
10	false
11}
12
13#[cfg(windows)]
14pub fn is_wsl_installed(log: &log::Logger) -> bool {
15	use std::path::PathBuf;
16
17	use crate::util::command::new_std_command;
18
19	let system32 = {
20		let sys_root = match std::env::var("SystemRoot") {
21			Ok(s) => s,
22			Err(_) => return false,
23		};
24
25		let is_32_on_64 = std::env::var("PROCESSOR_ARCHITEW6432").is_ok();
26		let mut system32 = PathBuf::from(sys_root);
27		system32.push(if is_32_on_64 { "Sysnative" } else { "System32" });
28		system32
29	};
30
31	// Windows builds < 22000
32	let mut maybe_lxss = system32.join("lxss");
33	maybe_lxss.push("LxssManager.dll");
34	if maybe_lxss.exists() {
35		trace!(log, "wsl availability detected via lxss");
36		return true;
37	}
38
39	// Windows builds >= 22000
40	let maybe_wsl = system32.join("wsl.exe");
41	if maybe_wsl.exists() {
42		if let Ok(s) = new_std_command(maybe_wsl).arg("--status").output() {
43			if s.status.success() {
44				trace!(log, "wsl availability detected via subprocess");
45				return true;
46			}
47		}
48	}
49
50	trace!(log, "wsl not detected");
51
52	false
53}