Purpose of `#ifdef MODULE` around module_exit ()?

I am currently reviewing the code of a โ€œthird-partyโ€ driver, trying to figure out / find out how it works. I looked at sites like this , so I understand how the basic premise works, but I donโ€™t understand, The purpose of #ifdef MODULE here. Google doesn't help much, but I think the definition refers to the 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 delete the #ifdef ? Also, why / when would the #ifdef operator need to be included?

+6
source share
1 answer

In the Linux kernel, most drivers can either be statically linked (embedded) to the kernel image itself, or created as dynamically loaded modules ( .ko ).

The MODULE macro is defined for the C file when it is compiled as part of the module, and undefined when it is created directly in the kernel.

The displayed code only defines os_driver_cleanup as a function to exit the module when it compiles as a module. However, this construct is not needed 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 #ifdef around module_exit() . You should also mention the exit function with __exit , which will correctly control the inclusion of code for your module in the modular / non-modular case.

Here is an example of the correct initialization / 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); 
+6
source

Source: https://habr.com/ru/post/976091/


All Articles