我有一个基于平台驱动程序的Linux内核模块。
我已经在那里实现了probe()
和remove()
方法。
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
}
现在,当用户执行rmmod <myModule>
随后remove()
被调用的方法。在这里,我执行了一些条件检查,并得知用户不应该在rmmod
这里执行。在这里,我不想执行任何清理并使此rmmod失败。
我曾尝试返回-1
或-EBUSY
in,remove()
但仍在rmmod <myModule>
卸载后仍然显示在lsmod
的输出中。
有什么方法可以停止在remove()
方法中卸载模块吗?
一个人不能取消(或停止)已经由rmmod
(或其他方式)发起的模块卸载。但是可以通过调用它来防止模块卸载try_module_get
:
// Before your module enters into the state, when its unloading is not desirable.
// Prevent unloading of the module
if(!try_module_get(THIS_MODULE)) {
<failed to prevent module unloading>
}
<....> // This code will be protected from the module's unloading
// Allow the module to be unloaded again
module_put(THIS_MODULE);
在(成功)调用try_module_get
生效时,rmmod
立即拒绝模块卸载,而不执行任何模块的代码。
我不确定try_module_get
从module_init
函数调用是否成功,从函数调用module_exit
肯定会失败。但在所有其他地方,此调用应会成功。
至于module_put
调用,则无需从调用的同一函数执行该调用try_module_get
。您可能根本不会打电话module_put
,但是如果可能的话,应该避免这种情况。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句