From efbd20857baf3bbd40631d34b89aa509c6f62627 Mon Sep 17 00:00:00 2001
From: Jiahao XU <Jiahao_XU@outlook.com>
Date: Sat, 23 Sep 2023 09:55:12 +1000
Subject: [PATCH] Fix `detect-targets` on ubuntu 20.04, glibc 2.31 (#1379)

* Fix `detect-targets` on ubuntu 20.04, glibc 2.31

Fixed #1375 and fixed #1378

glibc 2.31 does not support `--version`, so we need to detect and
fallback to passing an executable linked with that glibc to check if it
is indeed glibc.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* Fix compilation faillure on ubuntu-20.04

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

---------

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
---
 .github/workflows/ci.yml                  | 10 +++++++++-
 crates/detect-targets/src/detect/linux.rs | 13 +++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index c90073e3..0c10c8f3 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -153,10 +153,18 @@ jobs:
           alpine test.sh "$TARGET"
 
   detect-targets-ubuntu-test:
-    runs-on: ubuntu-latest
+    strategy:
+      fail-fast: false
+      matrix:
+        os:
+          - ubuntu-20.04
+          - ubuntu-latest
+    runs-on: ${{ matrix.os }}
     steps:
     - uses: actions/checkout@v4
     - uses: Swatinem/rust-cache@v2
+      with:
+        key: ${{ matrix.os }}
     - name: Build detect-targets
       run: cargo build --bin detect-targets
     - name: Run test in ubuntu
diff --git a/crates/detect-targets/src/detect/linux.rs b/crates/detect-targets/src/detect/linux.rs
index b2c2756f..a945c881 100644
--- a/crates/detect-targets/src/detect/linux.rs
+++ b/crates/detect-targets/src/detect/linux.rs
@@ -108,6 +108,19 @@ You are not meant to run this directly.
         } else {
             None
         }
+    } else if status.code() == Some(127) {
+        // On Ubuntu 20.04 (glibc 2.31), the `--version` flag is not supported
+        // and it will exit with status 127.
+        let status = Command::new(cmd)
+            .arg("/bin/true")
+            .stdin(Stdio::null())
+            .stdout(Stdio::null())
+            .stderr(Stdio::null())
+            .status()
+            .await
+            .ok()?;
+
+        status.success().then_some(Libc::Gnu)
     } else {
         None
     }