We are getting closer to looking at some actual module code. But first, we need to look at some other things that need to appear in your module source files. The kernel is a unique environment, and it imposes its own requirements on code that would interface with it. 我们越来越接近于查看一些实际的模块代码。 但首先,我们需要查看需要出现在模块源文件中的其他一些内容。 内核是一个独特的环境,它对与其交互的代码提出了自己的要求。
Most kernel code ends up including a fairly large number of header files to get definitions of functions, data types, and variables. We'll examine these files as we come to them, but there are a few that are specific to modules, and must appear in every loadable module. Thus, just about all module code has the following: 大多数内核代码最终都包含大量头文件来获取函数、数据类型和变量的定义。 我们将检查这些文件,但有一些是特定于模块的,并且必须出现在每个可加载的模块中。 因此,几乎所有模块代码都具有以下内容:
#include
#include
module.h contains a great many definitions of symbols and functions needed by loadable modules. You need init.h to specify your initialization and cleanup functions, as we saw in the "hello world" example above, and which we revisit in the next section. Most modules also include moduleparam.h to enable the passing of parameters to the module at load time; we will get to that shortly. module.h 包含大量可加载模块所需的符号和函数定义。 您需要 init.h 来指定初始化和清理函数,正如我们在上面的“hello world”示例中看到的,我们将在下一节中重新讨论。 大多数模块还包括 moduleparam.h 以在加载时将参数传递给模块; 我们很快就会谈到这一点。
It is not strictly necessary, but your module really should specify which license applies to its code. Doing so is just a matter of including a MODULE_LICENSE line: 这不是绝对必要的,但您的模块确实应该指定适用于其代码的许可证。 这样做只需包含一个 MODULE_LICENSE 行:
MODULE_LICENSE("GPL");
The specific licenses recognized by the kernel are "GPL" (for any version of the GNU General Public License), "GPL v2" (for GPL version two only), "GPL and additional rights," "Dual BSD/GPL," "Dual MPL/GPL," and "Proprietary." Unless your module is explicitly marked as being under a free license recognized by the kernel, it is assumed to be proprietary, and the kernel is "tainted" when the module is loaded. As we mentioned in Section 1.6, kernel developers tend to be unenthusiastic about helping users who experience problems after loading proprietary modules. 内核认可的特定许可证是“GPL”(适用于任何版本的 GNU 通用公共许可证)、“GPL v2”(仅适用于 GPL 版本 2)、“GPL 和附加权利”、“双 BSD/GPL”、“ 双 MPL/GPL”和“专有”。 除非您的模块被明确标记为处于内核认可的免费许可证下,否则它被假定为专有的,并且在加载模块时内核被“污染”。 正如我们在 1.6 节中提到的,内核开发人员往往不热衷于帮助在加载专有模块后遇到问题的用户。
Other descriptive definitions that can be contained within a module include MODULE_AUTHOR (stating who wrote the module), MODULE_DESCRIPTION (a human-readable statement of what the module does), MODULE_VERSION (for a code revision number; see the comments in
The various MODULE_ declarations can appear anywhere within your source file outside of a function. A relatively recent convention in kernel code, however, is to put these declarations at the end of the file. 各种 MODULE_ 声明可以出现在源文件中函数之外的任何位置。 然而,内核代码中一个相对较新的约定是将这些声明放在文件的末尾。