如果您运行的是 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
以了解模块作者如何使用这些宏来记录他们的模块。
/* 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");
上一页 | 示例 2-6. hello-4.c | 下一页 |
首页你好世界(第 3 部分):__init和__exit | 宏 | 向上 |