[英]Programmatically check whether a linux kernel module exists or not at runtime
我正在編寫一個C守護進程,它依賴於兩個內核模塊的存在才能完成它的工作。 該程序不直接使用這些(或任何其他)模塊。 它只需要它們存在。 因此,我想以編程方式檢查這些模塊是否已加載,以便在運行時警告用戶。
在我開始執行諸如解析/proc/modules
或lsmod
輸出之類的操作之前,某個實用程序函數是否已經存在? 類似於is_module_loaded(const char* name)
;
我很確定以前曾經問過這個問題。 但是,我想我錯過了正確的搜索條件。
沒有這樣的功能。 事實上,lsmod( lsmod.c
)的源代碼中包含以下行,它可以引導您找到解決方案:
file = fopen("/proc/modules", "r");
還有一個不推薦使用的query_module
但它現在似乎只存在於內核頭文件中。
你可以使用popen
和lsmod | grep
lsmod | grep
技巧:
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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.