From cced8b939cadd650dda5622d6a2654e7cb016ddd Mon Sep 17 00:00:00 2001 From: doubcrick <369021118@qq.com> Date: Thu, 26 Feb 2026 13:44:44 +0800 Subject: [PATCH] #issue #1382 Fix musl detection when /proc/self/map_files exists but does not contain musl Improve musl detection logic to avoid false negatives when /proc/self/map_files exists but lacks musl entriesIn some container environments, /proc/self/map_files exists but does not expose musl paths,causing isMusl() to incorrectly return false and load the glibc native library.This patch improves detection robustness by checking musl loader files and /proc/self/maps. --- src/main/java/org/sqlite/util/OSInfo.java | 52 ++++++++++++++++------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/sqlite/util/OSInfo.java b/src/main/java/org/sqlite/util/OSInfo.java index d20991f227..dbb6e89ee2 100644 --- a/src/main/java/org/sqlite/util/OSInfo.java +++ b/src/main/java/org/sqlite/util/OSInfo.java @@ -145,22 +145,44 @@ public static boolean isAndroidTermux() { } public static boolean isMusl() { - Path mapFilesDir = Paths.get("/proc/self/map_files"); - try (Stream dirStream = Files.list(mapFilesDir)) { - return dirStream - .map( - path -> { - try { - return path.toRealPath().toString(); - } catch (IOException e) { - return ""; - } - }) - .anyMatch(s -> s.toLowerCase().contains("musl")); + if (hasMuslLoader()) { + return true; + } + + if (mapsContainMusl()) { + return true; + } + + // fallback check Alpine + return isAlpineLinux(); + } + + private static boolean hasMuslLoader() { + return checkMuslLoaderInDir("/lib") || checkMuslLoaderInDir("/usr/lib"); + } + + private static boolean checkMuslLoaderInDir(String dir) { + try (Stream paths = Files.list(Paths.get(dir))) { + return paths + .map(Path::getFileName) + .map(Path::toString) + .anyMatch(name -> + name.startsWith("ld-musl-") && name.endsWith(".so.1")); } catch (Exception ignored) { - // fall back to checking for alpine linux in the event we're using an older kernel which - // may not fail the above check - return isAlpineLinux(); + return false; + } + } + + private static boolean mapsContainMusl() { + return checkMapsContainMusl("/proc/self/maps") || checkMapsContainMusl("/proc/self/map_files"); + } + + private static boolean checkMapsContainMusl(String dir) { + try (Stream lines = Files.lines(Paths.get(dir))) { + return lines.anyMatch(line -> + line.toLowerCase(Locale.US).contains("musl")); + } catch (Exception ignored) { + return false; } }