linux内核调用用户,linux 内核调用用户程序

实验环境:

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

代码注释:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值