[LTP] [Patch v2 1/4] lib: add module is loaded check
Li Wang
li.wang@linux.dev
Thu Oct 8 07:21:38 CEST 2026
Add a helper that checks whether a kernel module is loaded by
looking it up in /proc/modules. Module names are normalized so that
hyphens match the underscores used by the kernel.
Signed-off-by: Li Wang <li.wang@linux.dev>
---
include/tst_module.h | 1 +
lib/tst_module.c | 39 +++++++++++++++++++++++++++++++++++++++
2 files changed, 40 insertions(+)
diff --git a/include/tst_module.h b/include/tst_module.h
index e55321d19..9b789e289 100644
--- a/include/tst_module.h
+++ b/include/tst_module.h
@@ -49,5 +49,6 @@ static inline void tst_requires_module_signature_disabled(void)
void tst_modprobe(const char *mod_name, char *const argv[]);
void tst_module_reload(const char *mod_name, char *const argv[]);
+bool tst_module_is_loaded(const char *mod_name);
#endif /* TST_MODULE_H */
diff --git a/lib/tst_module.c b/lib/tst_module.c
index cbd2415d5..db341ec57 100644
--- a/lib/tst_module.c
+++ b/lib/tst_module.c
@@ -21,6 +21,7 @@
*/
#define _GNU_SOURCE
+#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -174,3 +175,41 @@ void tst_module_reload(const char *mod_name, char *const argv[])
tst_module_unload_(NULL, mod_name);
tst_modprobe(mod_name, argv);
}
+
+bool tst_module_is_loaded(const char *mod_name)
+{
+ char line[4096];
+ size_t mod_len = strlen(mod_name);
+ FILE *file;
+
+ file = fopen("/proc/modules", "r");
+ if (!file)
+ return false;
+
+ while (fgets(line, sizeof(line), file)) {
+ char *p = line;
+ size_t i;
+
+ while (*p && !isspace(*p))
+ p++;
+
+ if ((size_t)(p - line) != mod_len)
+ continue;
+
+ for (i = 0; i < mod_len; i++) {
+ char a = (line[i] == '-') ? '_' : line[i];
+ char b = (mod_name[i] == '-') ? '_' : mod_name[i];
+
+ if (a != b)
+ break;
+ }
+
+ if (i == mod_len) {
+ fclose(file);
+ return true;
+ }
+ }
+
+ fclose(file);
+ return false;
+}
--
2.56.0
More information about the ltp
mailing list