代码之家  ›  专栏  ›  技术社区  ›  Basilevs

如何使用find\u模块?

  •  6
  • Basilevs  · 技术社区  · 15 年前

    如何使用linux内核的 find_module() 功能?

    1. 那是不是意味着我应该 之前我的模块代码中有一个锁 搜索指向另一个的指针?
    2. 非模块内核代码?

    上下文

    我正在调试一组一起工作的内核模块。

    模块A调用模块B的函数。在模块A的函数C的某个点上,模块B的使用计数失效。我已经 determined 这不是在模块B的功能中发生的。我想从C调试模块B的使用计数。要做到这一点,我将使用 find_module() 获取指向B的指针。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Michael F    15 年前

    我建议在你的代码中多加一点防御性:

    #include <linux/module.h>
    #include <linux/capability.h>
    
    int do_my_work(void)
    {
        struct module *mod;
        char name[MODULE_NAME_LEN];
        int ret, forced = 0;
    
        if (!capable(CAP_SYS_MODULE) || modules_disabled)
            return -EPERM;
    
        /* Set up the name, yada yada */
        name[MODULE_NAME_LEN - 1] = '\0';
    
        /* Unless you absolutely need an uninterruptible wait, do this. */
        if (mutex_lock_interruptible(&module_mutex) != 0) {
            ret = -EINTR;
            goto out_stop;
        }
    
        mod = find_module(name);
        if (!mod) {
            ret = -ENOENT;
            goto out;
        }
    
        if (!list_empty(&mod->modules_which_use_me)) {
            /* Debug it. */
        }
    
    out:
        mutex_unlock(&module_mutex);
    out_stop:
        return(ret);
    }
    

    模块\u互斥 是由内核在对模块的各种操作中获取的。他们都在里面

    • 等待模块被无人引用(使用)。
    • 当/proc文件系统需要一个模块列表时(oprofile和co.使用这个)。
    • 跟踪点相关代码;遍历和更新跟踪点。
        2
  •  0
  •   JayM    15 年前

    1) 是的。得到 module_mutex 在你的模块里打电话之前 find_module()

    2) 它不在模块代码之外使用

    例子:

    struct module *mod;
    
    mutex_lock(&module_mutex);
    
    mod = find_module("MODULE_NAME");
    
    if(!mod) {
        printk("Could not find module\n");
        return;
    }
    
    mutex_unlock(&module_mutex);