-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from dmlloyd/wsl
Add detection for WSL
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |