| 1 | use std::process::Command; |
| 2 | |
| 3 | use crate::types::{LibcVersions, Version}; |
| 4 | |
| 5 | pub fn get_libc_version_output(app: &str) -> Result<String, String> { |
| 6 | let output = Command::new(app) |
| 7 | .args(["--version"]) |
| 8 | .output() |
| 9 | .map_err(|e| format!("failed to execute ldd: {e}"))?; |
| 10 | |
| 11 | let output_str = format!( |
| 12 | "{}\n{}", |
| 13 | String::from_utf8_lossy(&output.stdout), |
| 14 | String::from_utf8_lossy(&output.stderr) |
| 15 | ) |
| 16 | .trim() |
| 17 | .to_string(); |
| 18 | |
| 19 | Ok(output_str) |
| 20 | } |
| 21 | |
| 22 | pub fn get_os_libc_versions() -> Result<LibcVersions, String> { |
| 23 | let ldd_output = get_libc_version_output("ldd")?; |
| 24 | |
| 25 | let glibc = if let Ok(version_str) = ldd_output_to_glibc_version_str(&ldd_output) { |
| 26 | parse_glibc_version(version_str) |
| 27 | } else { |
| 28 | None |
| 29 | }; |
| 30 | |
| 31 | let musl_output = get_libc_version_output("musl-ldd").unwrap_or(ldd_output); |
| 32 | |
| 33 | let musl = parse_musl_version(&musl_output); |
| 34 | |
| 35 | Ok(LibcVersions { glibc, musl }) |
| 36 | } |
| 37 | |
| 38 | fn ldd_output_to_glibc_version_str(output_str: &str) -> Result<&str, String> { |
| 39 | for line in output_str.lines() { |
| 40 | if line.contains("ldd (") { |
| 41 | if let Some(idx) = line.find(')') { |
| 42 | let after = &line[idx + 1..]; |
| 43 | if let Some(tok) = find_version_token(after) { |
| 44 | return Ok(tok); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | Err("no glibc version in ldd output".to_string()) |
| 50 | } |
| 51 | |
| 52 | fn parse_glibc_version(version: &str) -> Option<Version> { |
| 53 | let mut parts = version.split('.').map(|s| s.parse::<usize>()); |
| 54 | match (parts.next(), parts.next()) { |
| 55 | (Some(Ok(major)), Some(Ok(minor))) => Some(Version { major, minor }), |
| 56 | _ => None, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn parse_musl_version(output: &str) -> Option<Version> { |
| 61 | if let Some(pos) = output.find("musl libc") { |
| 62 | let rest = &output[pos..]; |
| 63 | if let Some(tok) = find_version_token(rest) { |
| 64 | return parse_glibc_version(tok); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | for line in output.lines() { |
| 69 | if let Some(rest) = line.strip_prefix("Version ") { |
| 70 | if let Some(tok) = find_version_token(rest) { |
| 71 | return parse_glibc_version(tok); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | None |
| 77 | } |
| 78 | |
| 79 | fn find_version_token(s: &str) -> Option<&str> { |
| 80 | for token in s.split(|c: char| !(c.is_ascii_digit() || c == '.')) { |
| 81 | if token.is_empty() { |
| 82 | continue; |
| 83 | } |
| 84 | if token.contains('.') { |
| 85 | let mut parts = token.split('.'); |
| 86 | if let (Some(a), Some(b)) = (parts.next(), parts.next()) { |
| 87 | if a.chars().all(|c| c.is_ascii_digit()) && b.chars().all(|c| c.is_ascii_digit()) { |
| 88 | return Some(token); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | None |
| 94 | } |
| 95 | |
| 96 | #[cfg(test)] |
| 97 | mod tests { |
| 98 | use super::*; |
| 99 | use crate::types::{LibcVersions, Version}; |
| 100 | |
| 101 | #[test] |
| 102 | fn parse_glibc_ldd_output() { |
| 103 | let out = r#"ldd (GNU libc) 2.12 |
| 104 | Copyright (C) 2010 Free Software Foundation, Inc."#; |
| 105 | let ver_res = ldd_output_to_glibc_version_str(out); |
| 106 | assert!(ver_res.is_ok()); |
| 107 | let ver_str = ver_res.unwrap(); |
| 108 | assert_eq!(ver_str, "2.12"); |
| 109 | let parsed = parse_glibc_version(ver_str); |
| 110 | let parsed = parsed.unwrap(); |
| 111 | assert_eq!(parsed.major, 2); |
| 112 | assert_eq!(parsed.minor, 12); |
| 113 | |
| 114 | let out2 = r#"ldd (Ubuntu GLIBC 2.41-6ubuntu1.1) 2.41 |
| 115 | Copyright (C) 2024 Free Software Foundation, Inc. |
| 116 | This is free software; see the source for copying conditions. There is NO |
| 117 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 118 | Written by Roland McGrath and Ulrich Drepper. |
| 119 | "#; |
| 120 | let ver_res2 = ldd_output_to_glibc_version_str(out2); |
| 121 | assert!(ver_res2.is_ok()); |
| 122 | let ver_str2 = ver_res2.unwrap(); |
| 123 | assert_eq!(ver_str2, "2.41"); |
| 124 | let parsed2 = parse_glibc_version(ver_str2); |
| 125 | let parsed2 = parsed2.unwrap(); |
| 126 | assert_eq!(parsed2.major, 2); |
| 127 | assert_eq!(parsed2.minor, 41); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn parse_musl_from_ldd_output() { |
| 132 | let out = "musl libc (x86_64)\nVersion 1.2.3\n"; |
| 133 | let parsed = parse_musl_version(out); |
| 134 | let parsed = parsed.unwrap(); |
| 135 | assert_eq!(parsed.major, 1); |
| 136 | assert_eq!(parsed.minor, 2); |
| 137 | |
| 138 | let out2 = "musl libc (x86_64) 1.3.0\n"; |
| 139 | let parsed2 = parse_musl_version(out2); |
| 140 | let parsed2 = parsed2.unwrap(); |
| 141 | assert_eq!(parsed2.major, 1); |
| 142 | assert_eq!(parsed2.minor, 3); |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn get_version_fallbacks() { |
| 147 | let out = "some random output not containing versions"; |
| 148 | assert!(ldd_output_to_glibc_version_str(out).is_err()); |
| 149 | assert!(parse_musl_version(out).is_none()); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn get_os_libc_versions_both_none() { |
| 154 | // Simulate output with neither glibc nor musl present |
| 155 | let out = "random output without libc info"; |
| 156 | let glibc = parse_glibc_version(out); |
| 157 | let musl = parse_musl_version(out); |
| 158 | assert!(glibc.is_none()); |
| 159 | assert!(musl.is_none()); |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn get_os_libc_versions_glibc_only() { |
| 164 | // Simulate glibc output |
| 165 | let out = r#"ldd (GNU libc) 2.17\nCopyright (C) 2013 Free Software Foundation, Inc."#; |
| 166 | let glibc = parse_glibc_version("2.17"); |
| 167 | let musl = parse_musl_version(out); |
| 168 | assert!(glibc.is_some()); |
| 169 | assert!(musl.is_none()); |
| 170 | let v = glibc.unwrap(); |
| 171 | assert_eq!(v.major, 2); |
| 172 | assert_eq!(v.minor, 17); |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn get_os_libc_versions_musl_only() { |
| 177 | // Simulate musl output |
| 178 | let out = "musl libc (x86_64)\nVersion 1.1.24\n"; |
| 179 | let glibc = parse_glibc_version(out); |
| 180 | let musl = parse_musl_version(out); |
| 181 | assert!(glibc.is_none()); |
| 182 | assert!(musl.is_some()); |
| 183 | let v = musl.unwrap(); |
| 184 | assert_eq!(v.major, 1); |
| 185 | assert_eq!(v.minor, 1); |
| 186 | } |
| 187 | |
| 188 | #[test] |
| 189 | fn libc_versions_display() { |
| 190 | let both = LibcVersions { |
| 191 | glibc: Some(Version { major: 2, minor: 31 }), |
| 192 | musl: Some(Version { major: 1, minor: 2 }), |
| 193 | }; |
| 194 | assert_eq!(both.to_string(), "glibc 2.31 | musl 1.2"); |
| 195 | let only_glibc = LibcVersions { |
| 196 | glibc: Some(Version { major: 2, minor: 17 }), |
| 197 | musl: None, |
| 198 | }; |
| 199 | assert_eq!(only_glibc.to_string(), "glibc 2.17 | musl <not detected>"); |
| 200 | let only_musl = LibcVersions { |
| 201 | glibc: None, |
| 202 | musl: Some(Version { major: 1, minor: 1 }), |
| 203 | }; |
| 204 | assert_eq!(only_musl.to_string(), "glibc <not detected> | musl 1.1"); |
| 205 | let none = LibcVersions { |
| 206 | glibc: None, |
| 207 | musl: None, |
| 208 | }; |
| 209 | assert_eq!(none.to_string(), "glibc <not detected> | musl <not detected>"); |
| 210 | } |
| 211 | } |