linux 在内核模块调用应用层程序

内核模块代码

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/kmod.h>

//需要调用的应用层程序
#define APPNAME "/sbin/app"

static int __init  test_init(void)
{
    int ret;
    //初始话传给引用层的参数 就相当于main(argc,argv[])函数中的argv argv[0]要为执行的程序 
    char *argv[]={APPNAME,"this is a test",NULL};
    
	//开始执行
    ret = call_usermodehelper(APPNAME,argv,NULL,UMH_WAIT_PROC);
    if(ret != 0)
    {
        printk("call failed\n");
        return -1;
    }
    return 0;
}


static void __exit test_exit(void)
{
   return;
}


module_init(test_init);
module_exit(test_exit);

MODULE_LICENSE("GPL");

应用层代码

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main(int argc,char *argv[])
{
    int fd = 0;

    fd = open("/test/abcd",O_RDWR|O_CREAT);
    if(fd < 0)
    {
        return -1;
    }
    write(fd,argv[1],strlen(argv[1]));

    close(fd);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yldfree/article/details/84665610