From ad87c9bf022a541b4324e5e522284a10269eb58a Mon Sep 17 00:00:00 2001 From: maxxedev <5051664+maxxedev@users.noreply.github.com> Date: Fri, 29 Nov 2024 22:53:52 -0800 Subject: [PATCH] Improve container detection if current process is pid 1 --- .../org/apache/commons/lang3/RuntimeEnvironment.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/RuntimeEnvironment.java b/src/main/java/org/apache/commons/lang3/RuntimeEnvironment.java index 66b93d86cb9..00a6ffa10e7 100644 --- a/src/main/java/org/apache/commons/lang3/RuntimeEnvironment.java +++ b/src/main/java/org/apache/commons/lang3/RuntimeEnvironment.java @@ -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; @@ -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). *
@@ -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"); }