Linux 内核:如何使用 request_module() 和 try_module_get()
Linux Kernel: how to use request_module() and try_module_get()
我很难理解如何以正确的方式使用
try_module_get()
我觉得这很有趣 post:How to put a check in the code to ensure the inter kernel module dependency - Linux Kernel?
但我遗漏了一点:我可以让 request_module
正常工作,但我不知道如何使用 try_module_get
我解释一下:
如果我用
ret = request_module("ipt_conntrack")
模块xt_conntrack已正确插入,但标记为未使用,因为根据上述post我没有使用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;
}
我很难理解如何以正确的方式使用
try_module_get()
我觉得这很有趣 post:How to put a check in the code to ensure the inter kernel module dependency - Linux Kernel?
但我遗漏了一点:我可以让 request_module
正常工作,但我不知道如何使用 try_module_get
我解释一下:
如果我用
ret = request_module("ipt_conntrack")
模块xt_conntrack已正确插入,但标记为未使用,因为根据上述post我没有使用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;
}