Util: add function ffKernelModLoaded and use it

This commit is contained in:
Carter Li
2024-09-26 16:05:34 +08:00
parent e3cb52583d
commit fc05d728f3
5 changed files with 53 additions and 5 deletions
+1
View File
@@ -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
+2 -4
View File
@@ -1,14 +1,12 @@
#include "tpm.h"
#include "common/sysctl.h"
#include <sys/param.h>
#include <sys/module.h>
#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";
}
+6 -1
View File
@@ -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,
+39
View File
@@ -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 <sys/param.h>
#include <sys/module.h>
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
+5
View File
@@ -0,0 +1,5 @@
#pragma once
#include "fastfetch.h"
bool ffKernelModLoaded(const char* modName);