linux内核模块基础知识

来源:互联网 发布:java txt换行符 编辑:程序博客网 时间:2024/05/29 18:36

#include<linux/module.h>

#include<linux/init.h> -----这两个头文件所有模块都有

MODULE_LICENSE(“Dual BSD/GPL”);  ----声明许可证

MODULE_AUTHOR(“XXX”)   ----作者

 

EXPORT_SYMBOL(func)            ----将给定函数func设置为其他模块可调用

使用方法{

1)在模块函数定义之后使用EXPORT_SYMBOL(函数名)  模块A
   2在掉用该函数的模块中使用extern对之声明   模块B
   3首先加载定义该函数的模块A,再加载调用该函数的模块B

 

mode A.c      static int func1(void)
{
        printk("In Func: %s.../n",__func__);
        return 0;
}

EXPORT_SYMBOL(func1);

 

modeB.c extern int func1(void);

static int func2(void)
{
       func1();
        printk("In Func: %s.../n",__func__);
        return 0;
}

 

#insmod ./mod1.ko      (执行)
#insmod ./mod2.ko
#rmmod mod2
#rmmod mod1

}

 

module_param(name,type,perm) -------向当前模块传入参数

{

name:变量名type:数据类型(intcharpperm:读写

static char *who= "world";

static int times = 1;

module_param(times,int,S_IRUSR);

module_param(who,charp,S_IRUSR);

# insmod hello  who="world"  times=5        (执行)

}

 

insmod  xxx.ko

rmmod  xxx.ko

 

Hello.c

#include<linux/module.h>

#include<linux/init.h>

static int hello_init(void)

{

printk(KERN_ALERT "hello world.\n");

return 0;

}

static int hello_exit(void)

{

printk(KERN_ALERT "good bye.\n")

}

 

module_init(hello_init);

module_exit(hello_exit);

Makefile

 

ifneq ($(KERNELRELEASE),)

obj-m := hello.o

else

KDIR := /home/linux-2.6.29

all:

make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-

clean:

rm -f *.ko *.o *.mod.o *.mod.c *.symvers  modul*

endif

 


insmod hello.ko

rmmod hello.ko