您的位置:首页 > 运维架构 > Linux

Linux 内核模块编程二

2013-09-29 11:15 393 查看
主要整理一下模块编程的license问题和初始化和去初始化使用到的宏

/*
*  hello-4.c - Demonstrates module documentation.
*/
#include <linux/module.h>	/* Needed by all modules */
#include <linux/kernel.h>	/* Needed for KERN_INFO */
#include <linux/init.h>		/* Needed for the macros */
#define DRIVER_AUTHOR "Peter Jay Salzman <p@dirac.org>"
#define DRIVER_DESC   "A sample driver"

static int __init init_hello_4(void)
{
printk(KERN_INFO "Hello, world 4\n");
return 0;
}

static void __exit cleanup_hello_4(void)
{
printk(KERN_INFO "Goodbye, world 4\n");
}

module_init(init_hello_4);
module_exit(cleanup_hello_4);

/*
* Get rid of taint message by declaring code as GPL.
*/
MODULE_LICENSE("GPL");

/*
* Or with defines, like this:
*/
MODULE_AUTHOR(DRIVER_AUTHOR);	/* Who wrote this module? */
MODULE_DESCRIPTION(DRIVER_DESC);	/* What does this module do */


这里模块初始化和去初始化使用了module_init()与module_exit()宏,这样我们真正的初始化和去初始化函数就可以随意命名

关于内核模块的license在/linux-3.6.10/include/linux/module.h中有与比较详细的说明

一下摘自module.h

/*
* The following license idents are currently accepted as indicating free
* software modules
*
*	"GPL"				[GNU Public License v2 or later]
*	"GPL v2"			[GNU Public License v2]
*	"GPL and additional rights"	[GNU Public License v2 rights and more]
*	"Dual BSD/GPL"			[GNU Public License v2
*					 or BSD license choice]
*	"Dual MIT/GPL"			[GNU Public License v2
*					 or MIT license choice]
*	"Dual MPL/GPL"			[GNU Public License v2
*					 or Mozilla license choice]
*
* The following other idents are available
*
*	"Proprietary"			[Non free products]
*
* There are dual licensed components, but when running with Linux it is the
* GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
* is a GPL combined work.
*
* This exists for several reasons
* 1.	So modinfo can show license info for users wanting to vet their setup
*	is free
* 2.	So the community can ignore bug reports including proprietary modules
* 3.	So vendors can do likewise based on their own policies
*/


MODULE_LICENSE()指定使用的license

MODULE_AUTHOR()指定模块的作者

MODULE_DESCRIPTION()对模块的一个描述,它有什么功能可以做什么

__init

__initdata

__exit

__exitdata

仅用于模块初始化和清除阶段的函数(__init和__exit)和数据(__initdata和__exitdata)

__init标记,它对内核来讲是一种暗示,表明该函数仅在初始化期间使用。在模块装载之后,模块装载器就会将初始化函数扔掉,这样可以将该函数占用的内存释放出来,以作他用

__exit标记,表示该代码仅用于模块的卸载。如果模块被直接内嵌到内核中,或者内核的配置不允许卸载模块,则标记为__exit的函数将被简单的丢弃。也就是所被标记为__exit的函数只能在模块被卸载或者系统关闭的时候调用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: