I am currently looking through the code of a "third-party" driver in an attempt to figure out/learn how it functions. I've had a look at sites such as this one, so I sort of understand how the basic premise works, but I don't understand the purpose of #ifdef MODULE
here. Google isn't really much help, but I think the definition refers to a kernel module? (I am also completely new to this.)
module_init(os_driver_init);
#ifdef MODULE
module_exit(os_driver_cleanup);
#endif
My question is, what happens if I remove the #ifdef
statement? Also, why/when would it be necessary to include the #ifdef
statement?
In the Linux kernel, most drivers can be either statically linked (built-in) to the kernel image itself, or built as dynamically-loaded modules (.ko
files).
The MODULE
macro is defined for a C file when it is being compiled as part of a module, and undefined when it is being built directly into the kernel.
The code you're showing is only defining os_driver_cleanup
as a module-exit function when it is being compiled as a module. However, this construct is unnecessary in modern kernel code; include/linux/init.h
defines module_exit()
as a macro, whose implementation depends on #ifdef MODULE
.
Basically, you should always provide an exit function, and leave off the #ifdef
around module_exit()
. You should also mark your exit function with __exit
, which will properly control inclusion of the code for your in the modular/non-modular case.
Here's an example of proper init/exit code.
static int __init foo_init(void)
{
/* Register driver, etc. */
}
static void __exit foo_cleanup(void)
{
/* Unregister driver, etc. */
}
module_init(foo_init);
module_exit(foo_cleanup);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments