Skip to main content

uucore/mods/
os.rs

1// This file is part of the uutils coreutils package.
2//
3// For the full copyright and license information, please view the LICENSE
4// file that was distributed with this source code.
5// spell-checker:ignore (path) osrelease
6
7//! Test if the program is running under WSL
8//! ref: <https://github.com/microsoft/WSL/issues/4555> @@ <https://archive.is/dP0bz>
9
10/// Test if the program is running under WSL version 1
11pub fn is_wsl_1() -> bool {
12    #[cfg(target_os = "linux")]
13    {
14        if is_wsl_2() {
15            return false;
16        }
17        if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") {
18            if let Ok(s) = std::str::from_utf8(&b) {
19                let a = s.to_ascii_lowercase();
20                return a.contains("microsoft") || a.contains("wsl");
21            }
22        }
23    }
24    false
25}
26
27/// Test if the program is running under WSL version 2
28pub fn is_wsl_2() -> bool {
29    #[cfg(target_os = "linux")]
30    {
31        if let Ok(b) = std::fs::read("/proc/sys/kernel/osrelease") {
32            if let Ok(s) = std::str::from_utf8(&b) {
33                let a = s.to_ascii_lowercase();
34                return a.contains("wsl2");
35            }
36        }
37    }
38    false
39}
40
41/// Test if the program is running under WSL
42pub fn is_wsl() -> bool {
43    is_wsl_1() || is_wsl_2()
44}