Linux Module编程
最简单Module例子
#include <linux/module.h> /* Needed by all modules */
#include <linux/configfs.h> /* Needed for KERN_ALERT */
#include <linux/init.h> /* Needed for the module-macros */
#include <linux/kernel.h>
static int hello_init(void) // Module entry function specified by module_init()
{
printk(KERN_ALERT "Hello,world!\n");
return 0;
}
static void hello_exit(void) //Module exit function specified by module_exit()
{
printk(KERN_ALERT "Goodbye,cruel world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL"); //should always exist or you’ll get a warning
MODULE_AUTHOR("BENSON"); //optional
MODULE_DESCRIPTION("STUDY_MODULE"); //optional
对应Makefile文件
obj-m +=Hello.o
KDIR:=/lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
在Hello文件夹下执行make,
编译成功后,在Hello文件夹下会生成许多文件,其中有一个Hello.ko
加载内核模块 sudo insmod Hello.ko
列出已有的内核模块 lsmod
移除指定模块 sudo rmmod Hello.ko
向Module传递参数
#include <linux/module.h> /* Needed by all modules */
#include <linux/configfs.h> /* Needed for KERN_ALERT */
#include <linux/init.h> /* Needed for the module-macros */
#include <linux/kernel.h>
static int a = 1;
static int hello_init(void) // Module entry function specified by module_init()
{
printk(KERN_ALERT "Hello,world!\n");
return 0;
}
static void hello_exit(void) //Module exit function specified by module_exit()
{
printk(KERN_ALERT "Goodbye,cruel world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
module_param(a, int, S_IRUGO);
MODULE_LICENSE("Dual BSD/GPL"); //should always exist or you’ll get a warning
MODULE_AUTHOR("BENSON"); //optional
MODULE_DESCRIPTION("STUDY_MODULE"); //optional
代码中增加module_param定义。Makefile文件不变。使用方法:
sudo insmod Hello.ko a=90
Module直接调用
第一个Module:
#include <linux/module.h> /* Needed by all modules */
#include <linux/configfs.h> /* Needed for KERN_ALERT */
#include <linux/init.h> /* Needed for the module-macros */
#include <linux/kernel.h>
static int hello_init(void) // Module entry function specified by module_init()
{
printk(KERN_ALERT "Hello,world!\n");
return 0;
}
static void hello_exit(void) //Module exit function specified by module_exit()
{
printk(KERN_ALERT "Goodbye,cruel world!\n");
}
int add(int a, int b) {
return a+b;
}
int sub(int a, int b) {
return a-b;
}
EXPORT_SYMBOL(add);
EXPORT_SYMBOL(sub);
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL"); //should always exist or you’ll get a warning
MODULE_AUTHOR("BENSON"); //optional
MODULE_DESCRIPTION("STUDY_MODULE"); //optional
EXPORT_SYMBOL导出add和sub两个函数。
第二个Module:
<头文件>
#ifndef _ADD_SUB_H_
#define _ADD_SUB_H_
int add_integer(int a, int b);
int sub(int a, int b);
#endif
<代码>
#include <linux/module.h> /* Needed by all modules */
#include <linux/configfs.h> /* Needed for KERN_ALERT */
#include <linux/init.h> /* Needed for the module-macros */
#include <linux/kernel.h>
static int hello_init(void) // Module entry function specified by module_init()
{
printk(KERN_ALERT "Hello,world!\n");
int result = 0;
result = add(3, 4);
result = sub(9, 7);
return 0;
}
static void hello_exit(void) //Module exit function specified by module_exit()
{
printk(KERN_ALERT "Goodbye,cruel world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL"); //should always exist or you’ll get a warning
MODULE_AUTHOR("BENSON"); //optional
MODULE_DESCRIPTION("STUDY_MODULE"); //optional
obj-m +=Hello.o
KDIR:=/lib/modules/$(shell uname -r)/build
EXTRA_CFLAGS += -I ....##header目录
KBUILDER_EXTRA_SYMBOLS = .....#第一个模块的Module.symvers
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
Makefile中增加EXTRA_CFLAGS和KBUILDER_EXTRA_SYMBOLS项,使用方法与普通相同。