From 3a48af58c24fe99ee62ca2409ff580fb3522c22f Mon Sep 17 00:00:00 2001 From: Mario Limonciello Date: Wed, 16 Jun 2021 13:46:38 -0500 Subject: [PATCH] trivial: move kernel version check into common library code This will be used by more than one plugin in an upcoming commit. --- libfwupdplugin/fu-common.c | 53 +++++++++++++++++++++ libfwupdplugin/fu-common.h | 2 + libfwupdplugin/fwupdplugin.map | 6 +++ plugins/thunderbolt/fu-plugin-thunderbolt.c | 23 +-------- 4 files changed, 62 insertions(+), 22 deletions(-) diff --git a/libfwupdplugin/fu-common.c b/libfwupdplugin/fu-common.c index 14149ecd0..a79733c04 100644 --- a/libfwupdplugin/fu-common.c +++ b/libfwupdplugin/fu-common.c @@ -21,6 +21,10 @@ #include #endif +#ifdef HAVE_UTSNAME_H +#include +#endif + #ifdef HAVE_LIBARCHIVE #include #include @@ -34,6 +38,7 @@ #include "fwupd-error.h" #include "fu-common-private.h" +#include "fu-common-version.h" #include "fu-firmware.h" #include "fu-volume-private.h" @@ -2569,6 +2574,54 @@ fu_common_kernel_locked_down (void) #endif } +/** + * fu_common_check_kernel_version : + * @minimum_kernel: (not nullable): The minimum kernel version to check against + * @error: (nullable): optional return location for an error + * + * Determines if the system is running at least a certain required kernel version + * + * Since: 1.6.2 + **/ +gboolean +fu_common_check_kernel_version (const gchar *minimum_kernel, GError **error) +{ +#ifdef HAVE_UTSNAME_H + struct utsname name_tmp; + + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + g_return_val_if_fail (minimum_kernel == NULL, FALSE); + + memset (&name_tmp, 0, sizeof(struct utsname)); + if (uname (&name_tmp) < 0) { + g_set_error_literal (error, + FWUPD_ERROR, + FWUPD_ERROR_INTERNAL, + "failed to read kernel version"); + return FALSE; + } + if (fu_common_vercmp_full (name_tmp.release, + minimum_kernel, + FWUPD_VERSION_FORMAT_TRIPLET) < 0) { + g_set_error (error, + FWUPD_ERROR, + FWUPD_ERROR_INTERNAL, + "kernel %s doesn't meet minimum %s", + name_tmp.release, minimum_kernel); + return FALSE; + } + + return TRUE; +#else + g_set_error_literal (error, + FWUPD_ERROR, + FWUPD_ERROR_INTERNAL, + "platform doesn't support checking for minimum Linux kernel"); + return FALSE; +#endif + +} + /** * fu_common_cpuid: * @leaf: the CPUID level, now called the 'leaf' by Intel diff --git a/libfwupdplugin/fu-common.h b/libfwupdplugin/fu-common.h index 21ac7db52..f03e0c224 100644 --- a/libfwupdplugin/fu-common.h +++ b/libfwupdplugin/fu-common.h @@ -371,6 +371,8 @@ gchar *fu_common_strsafe (const gchar *str, gchar *fu_common_strjoin_array (const gchar *separator, GPtrArray *array); gboolean fu_common_kernel_locked_down (void); +gboolean fu_common_check_kernel_version (const gchar *minimum_kernel, + GError **error); gboolean fu_common_cpuid (guint32 leaf, guint32 *eax, guint32 *ebx, diff --git a/libfwupdplugin/fwupdplugin.map b/libfwupdplugin/fwupdplugin.map index 3e0508e35..527235f3c 100644 --- a/libfwupdplugin/fwupdplugin.map +++ b/libfwupdplugin/fwupdplugin.map @@ -815,3 +815,9 @@ LIBFWUPDPLUGIN_1.6.1 { fu_version_string; local: *; } LIBFWUPDPLUGIN_1.6.0; + +LIBFWUPDPLUGIN_1.6.2 { + global: + fu_common_check_kernel_version; + local: *; +} LIBFWUPDPLUGIN_1.6.1; diff --git a/plugins/thunderbolt/fu-plugin-thunderbolt.c b/plugins/thunderbolt/fu-plugin-thunderbolt.c index c04cca4fb..94116c940 100644 --- a/plugins/thunderbolt/fu-plugin-thunderbolt.c +++ b/plugins/thunderbolt/fu-plugin-thunderbolt.c @@ -7,7 +7,6 @@ #include "config.h" #include -#include #include "fu-thunderbolt-device.h" #include "fu-thunderbolt-firmware.h" @@ -17,33 +16,13 @@ static gboolean fu_plugin_thunderbolt_safe_kernel (FuPlugin *plugin, GError **error) { g_autofree gchar *minimum_kernel = NULL; - struct utsname name_tmp; - - memset (&name_tmp, 0, sizeof(struct utsname)); - if (uname (&name_tmp) < 0) { - g_debug ("Failed to read current kernel version"); - return TRUE; - } minimum_kernel = fu_plugin_get_config_value (plugin, "MinimumKernelVersion"); if (minimum_kernel == NULL) { g_debug ("Ignoring kernel safety checks"); return TRUE; } - - if (fu_common_vercmp_full (name_tmp.release, - minimum_kernel, - FWUPD_VERSION_FORMAT_TRIPLET) < 0) { - g_set_error (error, - FWUPD_ERROR, - FWUPD_ERROR_INTERNAL, - "kernel %s may not have full Thunderbolt support", - name_tmp.release); - return FALSE; - } - g_debug ("Using kernel %s (minimum %s)", name_tmp.release, minimum_kernel); - - return TRUE; + return fu_common_check_kernel_version (minimum_kernel, error); } gboolean