如果您正在运行 2.4 或更高版本的内核,那么在加载之前的示例模块时,您可能已经注意到类似这样的内容
# insmod hello-3.o Warning: loading hello-3.o will taint the kernel: no license See http://www.tux.org/lkml/#export-tainted for information about tainted modules Hello, world 3 Module hello-3 loaded, with warnings |
在 2.4 及更高版本的内核中,设计了一种机制来识别在 GPL(及其友好的协议)下许可的代码,以便可以警告人们该代码不是开源的。这是通过以下宏实现的MODULE_LICENSE()宏,这在下一段代码中演示。 通过将许可证设置为 GPL,您可以阻止打印警告。 此许可证机制在以下文件中定义和记录linux/module.h.
类似地,MODULE_DESCRIPTION()用于描述模块的功能,MODULE_AUTHOR()声明模块的作者,以及MODULE_SUPPORTED_DEVICE()声明模块支持的设备类型。
这些宏都在以下文件中定义linux/module.h并且内核本身不使用它们。 它们仅用于文档,并且可以通过像 objdump 这样的工具查看。 作为给读者的练习,尝试在以下目录中 grep 搜索linux/drivers以查看模块作者如何使用这些宏来记录他们的模块。
示例 2-6. hello-4.c
/* hello-4.c - Demonstrates module documentation. * * Copyright (C) 2001 by Peter Jay Salzman * * 08/02/2006 - Updated by Rodrigo Rubira Branco <rodrigo@kernelhacking.com> */ /* Kernel Programming */ #define MODULE #define LINUX #define __KERNEL__ #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #define DRIVER_AUTHOR "Peter Jay Salzman <p@dirac.org>" #define DRIVER_DESC "A sample driver" int init_hello_3(void); void cleanup_hello_3(void); static int init_hello_4(void) { printk(KERN_ALERT "Hello, world 4\n"); return 0; } static void 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: */ MODULE_LICENSE("GPL"); // Get rid of taint message by declaring code as 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"); |