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

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

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


#include
#include
#include
//#include
#include/*printk()*/
#include

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

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

你可能感兴趣的:(学习总结,Linux,驱动开发,C/C++)