From 88d6a1fd9b4b62729bd55a76e727f5f1d0aeef20 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 20 Dec 2022 10:01:49 +0000 Subject: [PATCH] adding KERNEL_VERSION macro for linux. --- libc-test/semver/linux.txt | 1 + libc-test/test/linux_kernel_version.rs | 17 +++++++++++++++++ src/unix/linux_like/mod.rs | 8 ++++++++ 3 files changed, 26 insertions(+) create mode 100644 libc-test/test/linux_kernel_version.rs diff --git a/libc-test/semver/linux.txt b/libc-test/semver/linux.txt index 087b1fc10c586..688dbd4778b7f 100644 --- a/libc-test/semver/linux.txt +++ b/libc-test/semver/linux.txt @@ -1146,6 +1146,7 @@ J1939_PGN_ADDRESS_COMMANDED J1939_PGN_MAX J1939_PGN_PDU1_MAX J1939_PGN_REQUEST +KERNEL_VERSION KEYCTL_ASSUME_AUTHORITY KEYCTL_CHOWN KEYCTL_CLEAR diff --git a/libc-test/test/linux_kernel_version.rs b/libc-test/test/linux_kernel_version.rs new file mode 100644 index 0000000000000..c5687edad5601 --- /dev/null +++ b/libc-test/test/linux_kernel_version.rs @@ -0,0 +1,17 @@ +//! Compare libc's KERNEL_VERSION macro against a specific kernel version. + +extern crate libc; + +#[cfg( + target_os = "linux", +)] +mod t { + use libc; + + #[test] + fn test_kernel_version() { + unsafe { + assert_eq!(libc::KERNEL_VERSION(6, 0, 0), 393216); + } + } +} diff --git a/src/unix/linux_like/mod.rs b/src/unix/linux_like/mod.rs index 8e738d87b889f..e2e73b3300e2a 100644 --- a/src/unix/linux_like/mod.rs +++ b/src/unix/linux_like/mod.rs @@ -1615,6 +1615,14 @@ safe_f! { pub {const} fn IPTOS_ECN(x: u8) -> u8 { x & ::IPTOS_ECN_MASK } + + #[allow(ellipsis_inclusive_range_patterns)] + pub {const} fn KERNEL_VERSION(a: u32, b: u32, c: u32) -> u32 { + ((a << 16) + (b << 8)) + match c { + 0 ... 255 => c, + _ => 255, + } + } } extern "C" {