您的位置:首页 > 运维架构 > Linux

Linux驱动层调用应用层程序--call_usermodehelper()

2017-07-31 17:54 579 查看

Linux驱动层调用应用层程序–call_usermodehelper()

在驱动层想要调用用户空间程序主要还是通过call_usermodehelper()这一函数,在这里记录一下;

#include<linux/init.h>
#include<linux/module.h>
#include<linux/moduleparam.h>
//#include<linux/config.h>
#include<linux/kernel.h>/*printk()*/
#include<linux/sched.h>

MODULE_LICENSE("GPL");

static __init int testDriver1_init(void){

int result=0;

char cmdPath[]="/bin/bash";

char* cmdArgv[]={cmdPath, "-c", "/bin/ls >> /tmp/list", NULL};

char* cmdEnvp[]={"HOME=/", "PATH=/sbin:/bin:/usr/bin", NULL};

result=call_usermodehelper(cmdPath,cmdArgv,cmdEnvp,UMH_WAIT_PROC);

printk(KERN_DEBUG"testDriver1 _init exec! The result of call_usermodehelper is %d\n",result);

//printk(KERN_DEBUG"testDriver1_initexec!Theprocess is \"%s\",pidis %d ,sys_getpid is %d \n",current->comm,current->pid);
printk(KERN_DEBUG"testDriver1 _init exec! The process is \"%s\",pid is %d\n",current->comm,current->pid);
return result;

}

static __exit void testDriver1_exit(void){

int result=0;

char cmdPath[]="/bin/bash";

char* cmdArgv[]={cmdPath, "-c", "/bin/ls >> /tmp/list", NULL};

char* cmdEnvp[]={"HOME=/",

"PATH=/sbin:/bin:/usr/bin",NULL};

result=call_usermodehelper(cmdPath,cmdArgv,cmdEnvp,UMH_WAIT_PROC);

printk(KERN_DEBUG"testDriver1 _exit exec! The result of call_usermodehelper is %d\n",result);

printk(KERN_DEBUG"testDriver1 _exit exec! The process is \"%s\",pid is %d \n",current->comm,current->pid);

}

module_init(testDriver1_init);

module_exit(testDriver1_exit);


对应的Makefile文件

obj-m := testDriver.o
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
rm *.o *.ko


简单记录,以防后面需要。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息