From e2c8fed7b3b3ae6e4be8356dd229917a32e07d4e Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Wed, 30 Aug 2023 21:30:59 +0200 Subject: [PATCH] unix: get mainline kernel version in Ubuntu (#4131) In Ubuntu, the kernel version reported by `uname()` follows the versioning format that Ubuntu uses for their kernels which does not have a direct correspondence with the mainline kernel version they're based on. Get that version from `/proc/version_signature` as documented in: https://wiki.ubuntu.com/Kernel/FAQ#Kernel.2FFAQ.2FGeneralVersionRunning.How_can_we_determine_the_version_of_the_running_kernel.3F --- src/unix/linux.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/unix/linux.c b/src/unix/linux.c index 44c02837..bf102fe9 100644 --- a/src/unix/linux.c +++ b/src/unix/linux.c @@ -323,11 +323,22 @@ unsigned uv__kernel_version(void) { unsigned major; unsigned minor; unsigned patch; + char v_sig[256]; version = atomic_load_explicit(&cached_version, memory_order_relaxed); if (version != 0) return version; + /* Check /proc/version_signature first as it's the way to get the mainline + * kernel version in Ubuntu. The format is: + * Ubuntu ubuntu_kernel_version mainline_kernel_version + * For example: + * Ubuntu 5.15.0-79.86-generic 5.15.111 + */ + if (0 == uv__slurp("/proc/version_signature", v_sig, sizeof(v_sig))) + if (3 == sscanf(v_sig, "Ubuntu %*s %u.%u.%u", &major, &minor, &patch)) + goto calculate_version; + if (-1 == uname(&u)) return 0; @@ -359,6 +370,7 @@ unsigned uv__kernel_version(void) { } } +calculate_version: version = major * 65536 + minor * 256 + patch; atomic_store_explicit(&cached_version, version, memory_order_relaxed);