Iterate over all modules in a kernel module

你离开我真会死。 提交于 2019-12-11 05:54:46

问题


I'm a bit confused by the variables available inside the Kernel. How would I go about iterating over all modules inside my own kernel module? I found modules being used in the kernel code. Can I do something along the lines of

struct module *mod;

list_for_each_entry(mod, &modules, list) {
  printk(KERN_INFO "%s\n", mod->name);
}

回答1:


There is no direct way for iterate over list of modules.

Head of the module's list is modules variable, which is statically defined in kernel/module.c, so it is not accessible for code outside of this file.


If list of module's name is needed for debugging purposes, you may use WARN_ON() macro or similar:

WARN_ON(0);

this will print message about warning and some additional information, which among other things will contain list of modules.




回答2:


Found a workaround to solve this.

struct list_head *list;
struct module *mod;
struct module *mine = find_module("YOUR_MODULE_NAME");

list_for_each(list, &mine->list){
  mod = list_entry(list, struct module, list);
  do_stuff_with_mod(mod);
}

This uses the method find_module from kernel/module. You will need to specify your module to be licensed under GPL or otherwise it will fail to load (compilation will be successful though).

Disclaimer

I don't think this method is the best approach but it is solid enough to work with simple tasks like printk



来源:https://stackoverflow.com/questions/43558217/iterate-over-all-modules-in-a-kernel-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!