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

Add detection for WSL #312

Merged
merged 1 commit into from
May 17, 2024
Merged
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
57 changes: 57 additions & 0 deletions os/src/main/java/io/smallrye/common/os/Linux.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.smallrye.common.os;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* Utilities pertaining to the Linux operating system.
*/
public final class Linux {
private Linux() {
}

/**
* {@return true if the operating system is the Windows Subsystem for Linux, or false if it is not}
*/
public static boolean isWSL() {
return WSL.version >= 1;
}

/**
* {@return true if the WSL version is 2 or later, or false if it is not}
*/
public static boolean isWSLv2() {
return WSL.version >= 2;
}

/**
* Lazy constants for WSL.
*/
private static final class WSL {
private static final int version;

static {
if (OS.current() != OS.LINUX) {
version = 0;
} else {
int v;
try {
String procVersion = Files.readString(Path.of("/proc/version"));
if (procVersion.contains("Microsoft")) {
// likely version 1
v = 1;
} else if (procVersion.contains("microsoft")) {
// likely version 2 or newer
v = 2;
} else {
v = 0;
}
} catch (IOException e) {
v = 0;
}
version = v;
}
}
}
}