我很难理解如何正确地使用
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 "参数有关,而这个参数在这种情况下并不适用.你能告诉我正确的方向吗?
谢谢你的帮助
也许像这样的东西会有用。我还没有测试过。
/**
* 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;
}