From fc05d728f3a46a5101c8c78e2862439f49921b0f Mon Sep 17 00:00:00 2001 From: Carter Li Date: Thu, 26 Sep 2024 16:05:34 +0800 Subject: [PATCH] Util: add function `ffKernelModLoaded` and use it --- CMakeLists.txt | 1 + src/detection/tpm/tpm_bsd.c | 6 ++--- src/detection/zpool/zpool_linux.c | 7 +++++- src/util/kernelMod.c | 39 +++++++++++++++++++++++++++++++ src/util/kernelMod.h | 5 ++++ 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/util/kernelMod.c create mode 100644 src/util/kernelMod.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c959e5ab3..634a99d08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -424,6 +424,7 @@ set(LIBFASTFETCH_SRC src/util/base64.c src/util/FFlist.c src/util/FFstrbuf.c + src/util/kernelMod.c src/util/path.c src/util/platform/FFPlatform.c src/util/smbiosHelper.c diff --git a/src/detection/tpm/tpm_bsd.c b/src/detection/tpm/tpm_bsd.c index f51adb8ad..53dbba539 100644 --- a/src/detection/tpm/tpm_bsd.c +++ b/src/detection/tpm/tpm_bsd.c @@ -1,14 +1,12 @@ #include "tpm.h" #include "common/sysctl.h" - -#include -#include +#include "util/kernelMod.h" const char* ffDetectTPM(FFTPMResult* result) { if (ffSysctlGetString("dev.tpmcrb.0.%desc", &result->description) != NULL) { - if (modfind("tpm") < 0) return "TPM kernel module is not loaded"; + if (ffKernelModLoaded("tpm")) return "`tpm` kernel module is not loaded"; return "TPM device is not found"; } diff --git a/src/detection/zpool/zpool_linux.c b/src/detection/zpool/zpool_linux.c index c183c3785..dcbdbfd90 100644 --- a/src/detection/zpool/zpool_linux.c +++ b/src/detection/zpool/zpool_linux.c @@ -1,6 +1,7 @@ #include "zpool.h" #ifdef FF_HAVE_LIBZFS +#include "util/kernelMod.h" #ifdef __sun #define FF_DISABLE_DLOPEN @@ -82,7 +83,11 @@ const char* ffDetectZpool(FFlist* result /* list of FFZpoolResult */) FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libzfs, libzfs_init); libzfs_handle_t* handle = fflibzfs_init(); - if (!handle) return "libzfs_init() failed"; + if (!handle) + { + if (!ffKernelModLoaded("zfs")) return "`zfs` kernel module is not loaded"; + return "libzfs_init() failed"; + } __attribute__((__cleanup__(cleanLibzfs))) FFZfsData data = { .handle = handle, diff --git a/src/util/kernelMod.c b/src/util/kernelMod.c new file mode 100644 index 000000000..94e11ec70 --- /dev/null +++ b/src/util/kernelMod.c @@ -0,0 +1,39 @@ +#include "kernelMod.h" + +#if __linux__ +#include "common/io/io.h" + +bool ffKernelModLoaded(const char* modName) +{ + static FFstrbuf modules; + if (modules.chars == NULL) + { + ffStrbufInitS(&modules, "\n"); + ffAppendFileBuffer("/proc/modules", &modules); + } + + if (modules.length == 0) return false; + + uint32_t len = (uint32_t) strlen(modName); + if (len > 250) return false; + + char temp[256]; + temp[0] = '\n'; + memcpy(temp + 1, modName, len); + temp[1 + len] = ' '; + return memmem(modules.chars, modules.length, temp, len + 2) != NULL; +} +#elif __FreeBSD__ +#include +#include + +bool ffKernelModLoaded(const char* modName) +{ + return modfind(modName) >= 0; +} +#else +bool ffKernelModLoaded(FF_MAYBE_UNUSED const char* modName) +{ + return true; // Don't generate kernel module related errors +} +#endif diff --git a/src/util/kernelMod.h b/src/util/kernelMod.h new file mode 100644 index 000000000..74f6f9292 --- /dev/null +++ b/src/util/kernelMod.h @@ -0,0 +1,5 @@ +#pragma once + +#include "fastfetch.h" + +bool ffKernelModLoaded(const char* modName);