zoukankan      html  css  js  c++  java
  • 内核模块传参

    1.使用module_param()函数传递参数

    hello.c

    #include <linux/init.h>
    #include <linux/module.h>
    #include <linux/moduleparam.h>
    
    MODULE_LICENSE("GPL");
    
    static char *who="world";
    static int times=1;    //先声明变量(static型),然后调用module_param 
    module_param(times,int,S_IRUGO);
    module_param(who,charp,S_IRUGO);
    static int hello_init(void)
    {
        int i;
        for(i=0;i<times;i++)
        {
            printk(KERN_ALERT "(%d) hello ,%s 
    ",i,who);
        }
        return 0;
    }
     static void hello_exit(void)
     {
         printk(KERN_ALERT "Goodbye,%s!
    ",who);
     }
    
     module_init(hello_init);
     module_exit(hello_exit);

    makefile

    #General Purpose Makefile for cross compile Linux Kernel module
    ifneq ($(KERNELRELEASE),)
    obj-m := hello.o  #+=是连接字符串
    else
    
    ARCH := arm    
    CROSS_COMPILE := arm-linux-gnueabihf-
    KERN_DIR := /home/zqh/lichee/linux-zero-4.14.y  #选择内核路径
    PWD :=$(shell pwd)   #当前路径
    
    all:
            make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERN_DIR) M=$(PWD) modules    
    
    clean:                                   
            make -C $(KERN_DIR) M=$(shell pwd) modules clean
            rm -rf modules.order
    endif
     

    2.现象

    # insmod hello.ko who="mom" times=5
    # dmesg | tail -10
    [    6.057104] RTL8723BS: set pairwise key camid:4, addr:e6:02:9b:c8:c8:41, kid:0, type:AES
    [    6.068026] RTL8723BS: set group key camid:5, addr:e6:02:9b:c8:c8:41, kid:2, type:AES
    [  129.211272] hello: loading out-of-tree module taints kernel.
    [  129.217970] (0) hello ,world 
    [  151.453028] Goodbye,world!
    [  383.426642] (0) hello ,mom 
    [  383.429541] (1) hello ,mom 
    [  383.432419] (2) hello ,mom 
    [  383.435295] (3) hello ,mom 
    [  383.438304] (4) hello ,mom 
  • 相关阅读:
    应用服务器安装
    datasnap的线程池
    压缩OLEVARIANT数据
    服务端日志记录
    提交主从表的多个已经修改的数据
    MySQL与PostgreSQL相比哪个更好?
    Vue入门常用指令详解
    Laravel模型事件的实现原理详解
    Git 遇到了 early EOF indexpack failed 问题
    Laravel 代码开发最佳实践
  • 原文地址:https://www.cnblogs.com/ZQQH/p/8550978.html
Copyright © 2011-2022 走看看