I am writing a C daemon, which depends on the existence of two kernel modules in order to do its job. The program does not directly use these (or any other) modules. It only needs them to exist. Therefore, I would like to programmatically check whether these modules are already loaded or not, in order to warn the user at runtime.
Before I start to do things like parsing /proc/modules
or lsmod
output, does a utility function already exist somewhere?
Something like is_module_loaded(const char* name)
;
I am pretty sure this has been asked before. However, I think I am missing the correct terms to search for this.
You can use popen
and lsmod | grep
trick:
FILE *fd = popen("lsmod | grep module_name", "r");
char buf[16];
if (fread (buf, 1, sizeof (buf), fd) > 0) // if there is some result the module must be loaded
printf ("module is loaded\n");
else
printf ("module is not loaded\n");
There is no such function. In fact, the source code of lsmod (lsmod.c
) has the following line in it which should lead you to your solution:
file = fopen("/proc/modules", "r");
There is also a deprecated query_module
but it appears to only exist in kernel headers these days.
来源:https://stackoverflow.com/questions/12978794/programmatically-check-whether-a-linux-kernel-module-exists-or-not-at-runtime