1
votes

I write kernel module which lists all modules loaded in system. But when is end list of modules, loop are working. I tried everything to stop this loop, but finally I cant do this. I added function printk which write module name (it makes kernel oops when tries write name module which doesn`t exist). Can anyone help me? I need list of modules to continue write this module. Source:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/list.h>

struct list_head modules_list;
struct list_head *m;
struct module *ops_mod;
int agh = 0;
char *x = (char) 0;

void list_module(void)
{
  modules_list = THIS_MODULE->list;
  for(m = (&modules_list)->next; m != (&modules_list); m = m->next) {
    printk(KERN_INFO "List iteration %d", agh);
    ops_mod = list_entry(m, struct module, list);
    if ((m)->next == NULL) {
      printk(KERN_WARNING "MISC list: NULL pointer");
      return;
    }
    printk(KERN_INFO "after iteration %d", agh);
    printk(KERN_INFO "print name %d", agh);
    agh++;
    if (&ops_mod->mkobj.kobj.name == NULL)
      return;
    else {
        printk("Adress: %d", ops_mod);
        printk("MISC: ok %s", ops_mod->mkobj.kobj.name);
      }
    //ops_mod = NULL;
  }
}

int init_module(void)
{
  list_module();
  return 0;
}

void cleanup_module(void){}
MODULE_LICENSE("GPL");
1
The something is wrong with your end of loop condition (m != (&modules_list)). is this a circular list? - Arash
I dont know. Its global modules list - Wiktoria Lewicka
Proper way for iterate over list is using list_for_each(), list_for_each_entry() or similar macro. But all these macros requires head of the list, and you have no access to it (see question stackoverflow.com/questions/43558217/…). THIS_MODULE->list is an element of the list, not a head. - Tsyvarev

1 Answers

0
votes

Normally you'd hold module_mutex, and use a list helper, something like this:

struct module *mod;
mutex_lock(&module_mutex);
list_for_each_entry(mod, &THIS_MODULE->list, list)
    printk(KERN_INFO "%s", mod->name);
mutex_unlock(&module_mutex);

But you're doing this from within init_module, so I think the issue may be that your module isn't set up yet.