在内核模块中执行shell命令 [英] Execute shell command in kernel module
问题描述
是否可以在内核模块中执行shell命令.我知道我们可以使用 system
子例程在用户空间C代码中完成此操作.
我正在调试一个存在内存泄漏问题的内核模块.在无限循环中执行insmod和rmmod module.ko之后,系统会在几分钟之内用8G RAM耗尽内存.
在调用负责释放内存的API之前和之后,使用 free
命令了解内存状态会很有帮助,这样我就可以知道API是否正在工作.
这就是我调试的方式.如果有其他方法,请分享.
Is it possible to execute shell command in kernel module. I know that we can do it in user space C code using system
subroutine.
I am debugging a kernel module which has memory leak issue. After doing insmod and rmmod module.ko in an infinite loop, system goes out of memory within few minutes with 8G RAM.
It would be helpful to know memory status using free
command before and after the call to API responsible to free memory so i can know that API is working or not.
This is the way i am debugging. Please share if there is any other way to do so.
推荐答案
您可以使用 call_usermodehelper
函数.请参见 LXR#1 或 LXR#2 .
You can use call_usermodehelper
function. See the example of how to use it at the LXR#1 or LXR#2.
UPD:
argv[0] = "/bin/bash";
argv[1] = "-c";
argv[2] = "/usr/bin/free";
argv[3] = NULL;
envp[0] = "HOME=/";
envp[1] = "TERM=linux";
envp[2] = "PATH=/sbin:/usr/sbin:/bin:/usr/bin";
envp[3] = NULL;
call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
这篇关于在内核模块中执行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!