• ubuntu动态加载模块简单模板


    1:简单代码

    #include<linux/init.h>
    #include<linux/module.h>
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("jiuyueguang");
    MODULE_DESCRIPTION("SIMPLE MODULE DRIVER");
    
    static int hello_init(void){
        printk(KERN_INFO"hello word
    ");
        return 0;
    }
    static void hello_exit(void){
        printk(KERN_INFO"hello word exit
    ");
    }
    module_init(hello_init);
    module_exit(hello_exit);

    2:查看printk输出信息

     dmesg | tail 

    3:带参数类型的模块简单代码

    #include<linux/init.h>
    #include<linux/module.h>
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("jiuyueguang");
    MODULE_DESCRIPTION("SIMPLE MODULE DRIVER");
    
    static char *name="no name";
    static int age=0;
    
    static int hello_init(void){
        printk(KERN_INFO"hello word name=%s and age=%d
    ",name ,age);
        return 0;
    }
    static void hello_exit(void){
        printk(KERN_INFO"hello word exit name=%s and age=%d 
    ",name ,age);
    }
    module_init(hello_init);
    module_exit(hello_exit);
    module_param(name,charp,S_IRUGO);
    module_param(age,int,S_IRUGO);

    安装带参数的模块:

    sudo insmod hello_param.ko    

    安装的时候没有加参数,采用程序中的默认值输出:

    hello word name=no name and age=0

    安装的时候加参数:

    sudo insmod hello_param.ko name='jiuyueguang' age=25

    输出:

    hello word name=jiuyueguang and age=25

    4:我自己的Makefile

    ifeq ($(KERNELRELEASE),)
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build 
    PWD := $(shell pwd)
    modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    modules_install:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
    clean:
        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* module* a.out
    .PHONY: modules modules_install clean
    else
        obj-m := hello_param.o    //每次修改这里就够了
    endif
  • 相关阅读:
    Linux date自己实现代码
    Linux常用指令
    awk getline命令
    文档资料记录
    数学之美番外篇:平凡而又神奇的贝叶斯方法
    最小二乘法(ZZ)
    SpringMVC简单使用教程
    Oracle设置序列方法
    Bootstrap Table的使用详解
    Html中div块居中显示
  • 原文地址:https://www.cnblogs.com/jiuyueguang/p/3186219.html
Copyright © 2020-2023  润新知