实验环境:
CentOS Linux release 7.4.1708,3.10.0-693.el7.x86_64
参考:
内核 API,第 1 部分:从内核调用用户空间应用程序
https://www.ibm.com/developerworks/cn/linux/l-user-space-apps/index.html
demo:
#include static int umh_test( void )
{
struct subprocess_info *sub_info;
char *argv[] = { "/usr/bin/touch", "/abc.txt", NULL };
static char *envp[] = {
"HOME=/",
"TERM=linux",
"PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL };
sub_info = call_usermodehelper_setup( argv[0], argv, envp, GFP_KERNEL , NULL, NULL, NULL);
if (sub_info == NULL) {
printk("call_usermodehelper_setup error");
return -ENOMEM;
}
return call_usermodehelper_exec( sub_info, UMH_WAIT_PROC );
}
static int __init sample_init(void)
{
printk("usermode-helper start\n");
umh_test();
printk("usermode-helper end\n");
return 0;
}
static void __exit sample_exit(void)
{
printk("usermode-helper exit\n");
}
module_init(sample_init);
module_exit(sample_exit);
MODULE_AUTHOR("XXX");
MODULE_LICENSE("GPL");
如果要执行类似 echo ddddd > /abc.txt 的操作就不行了, abc.txt中没有内容。应该是没有终端,标准输出 的问题。
在Makefile中加入CONFIG_MODULE_SIG=n
以免insmod时报错:[ 474.856017] sample: loading out-of-tree module taints kernel.
[ 474.856159] sample: module verification failed: signature and/or required key missing - tainting kernel
代码注释: