Execute shell command in kernel module

痴心易碎 提交于 2021-02-06 12:49:22

问题


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.


回答1:


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);



回答2:


You can't run a shell command in a kernel module. Code in a kernel module can cause a command to be run, but it will run in a normal user process.

free will be of little help with kernel memory leaks.
It would be much better to wrap all allocations and frees in your kernel code, and maintain a counter of allocated memory.




回答3:


Its not possible to run a shell command from inside the kernel. You can instead read the /proc/meminfo file by calling the suitable procfs API to read the /proc/meminfo file. That virtual file has useful memory stats about the system memory.



来源:https://stackoverflow.com/questions/11193648/execute-shell-command-in-kernel-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!