0

我正在努力了解如何以正确的方式使用

try_module_get()

我发现了这篇有趣的帖子:如何检查代码以确保内核间模块依赖 - Linux Kernel?

但我错过了一点:我可以request_module正常工作,但我不知道如何使用try_module_get

我解释:

如果我使用

ret = request_module("ipt_conntrack")

模块 xt_conntrack 已正确插入,但标记为未使用,因为根据上面的帖子,我没有使用try_module_get.

但是我怎么能打电话try_module_get呢?该函数需要一个struct module参数,我不知道如何为 xt_conntrack 模块填充该参数。我找到了几个例子,但都与“THIS_MODULE”参数有关,在这种情况下不适用。你能指出我正确的方向吗?

谢谢你的帮助

4

1 回答 1

1

也许这样的事情会奏效。我没有测试过。

/**
 * try_find_module_get() - Try and get reference to named module
 * @name: Name of module
 *
 * Attempt to find the named module.  If not found, attempt to request the module by
 * name and try to find it again.  If the module is found (possibly after requesting the
 * module), try and get a reference to it.
 *
 * Return:
 * * pointer to module if found and got a reference.
 * * NULL if module not found or failed to get a reference.
 */
static struct module *try_find_module_get(const char *name)
{
    struct module *mod;

    mutex_lock(&module_mutex);
    /* Try and find the module. */
    mod = find_module(name);
    if (!mod) {
        mutex_unlock(&module_mutex);
        /* Not found.  Try and request it. */
        if (request_module(name))
            return NULL;  /* Failed to request module. */
        mutex_lock(&module_mutex);
        /* Module requested.  Try and find it again. */
        mod = find_module(name);
    }
    /* Try and get a reference if module found. */
    if (mod && !try_module_get(mod))
        mod = NULL;  /* Failed to get a reference. */
    mutex_unlock(&module_mutex);
    return mod;
}
于 2020-05-13T16:01:09.203 回答