chapter 2. hello world
内核模块证书和内核模块文档说明如果你在使用2.4或更新的内核,当你加载你的模块时,你也许注意到了这些输出信息:
#
insmod hello-3.owarning: loading hello-3.o will taint the kernel: no license
see for information about tainted modules
hello, world 3
module hello-3 loaded, with warnings
在2.4或更新的内核中,一种识别代码是否在gpl许可下发布的机制被引入,因此人们可以在使用非公开的源代码产品时得到警告。这通过在下一章展示的宏module_license()当你设置在gpl证书下发布你的代码时,你可以取消这些警告。这种证书机制在头文件linux/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 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_description()用来描述模块的用途。 宏module_author()用来声明模块的作者。宏module_supported_device() 声明模块支持的设备。
这些宏都在头文件linux/module.h定义, 并且内核本身并不使用这些宏。它们只是用来提供识别信息,可用工具程序像objdump查看。 作为一个练习,使用grep从目录linux/drivers看一看这些模块的作者是如何 为他们的模块提供识别信息和档案的。
example 2-6. hello-4.c
/*
* hello-4.c - demonstrates module documentation.
*/
#include
#include
#include
#define driver_author "peter jay salzman "
#define driver_desc "a sample driver"
static int __init init_hello_4(void)
{
printk(kern_alert "hello, world 4\n");
return 0;
}
static void __exit cleanup_hello_4(void)
{
printk(kern_alert "goodbye, world 4\n");
}
module_init(init_hello_4);
module_exit(cleanup_hello_4);
/*
* you can use strings, like this:
*/
/*
* 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 */
/*
* this module uses /dev/testdevice. the module_supported_device macro might
* be used in the future to help automatic configuration of modules, but is
* currently unused other than for documentation purposes.
*/
module_supported_device("testdevice");
阅读(1488) | 评论(0) | 转发(0) |