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

来源:互联网 发布:淘宝小二误判如何申诉 编辑:程序博客网 时间:2024/05/19 00:08

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.oKERNEL_DIR := /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)all:    make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modulesclean:    rm *.o *.ko

简单记录,以防后面需要。。。

阅读全文
0 0