A similar question appeared already one time for C++ but the answers and the question itself weren't really satisfying.
I've read a .c File
(github link), which included the <linux/module.h>
and passed its static functions to module_init(foo)
and module_exit(foo)
.
So what is the general purpose of a module, of the <linux/module.h> file
in this context, and of especially those two methods?
It is for the Linux Kernel Module. Section 1.1 mentions:
So, you want to write a kernel module. [..] now you want to get to where the real action is, What exactly is a kernel module? Modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel [..]. For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system.
Then, in section 2.3:
The macros
module_init()
andmodule_exit()
macros initialize and cleanup your functions.
Example:
module_init(hello_2_init);
module_exit(hello_2_exit);
where both this dymmy functions call printk();
saying hello/goodbye world.
In section 3.1.1:
A module always begins with either the
init_module()
or the function you specify withmodule_init()
call. This is the entry function for modules; it tells the kernel what functionality the module provides and sets up the kernel to run the module's functions when they're needed.All modules end by calling either
cleanup_module()
or the function you specify with themodule_exit()
call. This is the exit function for modules; it undoes whatever entry function did. It unregisters the functionality that the entry function registered.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With