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

Improve container detection if current process is pid 1 #1322

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 12 additions & 0 deletions src/main/java/org/apache/commons/lang3/RuntimeEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.commons.lang3;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
Expand Down Expand Up @@ -76,9 +77,17 @@ static Boolean inDocker() {
*/
// Could be public at a later time.
static Boolean inPodman() {
if (isPid1()) {
return "podman".equals(System.getenv("container"));
}
return containsLine("/proc/1/environ", "container=podman");
}

private static boolean isPid1() {
String name = ManagementFactory.getRuntimeMXBean().getName();
return name.startsWith("1@");
}

/**
* Tests whether we are running in a Windows Subsystem for Linux (WSL).
* <p>
Expand All @@ -89,6 +98,9 @@ static Boolean inPodman() {
*/
// Could be public at a later time.
static Boolean inWsl() {
if (isPid1()) {
return "wslcontainer_host_id".equals(System.getenv("container"));
}
return containsLine("/proc/1/environ", "container=wslcontainer_host_id");
}

Expand Down