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

janneia

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?

Jonathon Reinhart

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

correctly close struct file * on module_exit

From Dev

Purpose of python antigravity module

From Dev

What is the purpose of module.require?

From Dev

Purpose of :: before include module in ruby

From Dev

purpose of #ifdef before main() in program

From Dev

What is the purpose of the tf.contrib module in Tensorflow?

From Dev

What is the purpose of the tf.contrib module in Tensorflow?

From Dev

What is the purpose of canberra-gtk-module?

From Dev

What is the purpose of syntax support of npm module depcheck?

From Dev

What is the purpose of the pam_stress module?

From Dev

What does purpose use #ifdef and #if in C++

From Dev

What are the purpose of default arguments in JavaScript IIFE from a TypeScript module?

From Dev

purpose of installing a perl module with apt-get instead of cpan

From Dev

What is the purpose of user interface module in css3?

From Dev

GCC missing braces around initializer with static module level variable

From Dev

Working around formula 256 character limit in data validation in openpyxl module

From Dev

ES6 angular service module not being passed around correctly?

From Dev

No such module '####'

From Dev

Purpose of parentheses around expressions joined by logical "and"

From Java

What is the purpose of Node.js module.exports and how do you use it?

From Dev

Can't get my head around "Injector already created, can not register a module!" error

From Dev

Using #ifdef preprocessor around virtual functions causes runtime error in program linked against libraries

From Dev

Batch EXIT followed by '|' - what is the purpose of this?

From Dev

How to tell X.org to reload input device module? (Working around suspend-to-ram crash on Acer laptop)

From Dev

IFDEF in if condition

From Dev

KVM what is the purpose of EXIT_QUALIFICATION

From Java

Is there a command to exit a module when imported, like return for a function

From Dev

Exit an imported module without exiting the main program - Python

From Dev

Exit from loading Node module early without stopping process

Related Related

  1. 1

    correctly close struct file * on module_exit

  2. 2

    Purpose of python antigravity module

  3. 3

    What is the purpose of module.require?

  4. 4

    Purpose of :: before include module in ruby

  5. 5

    purpose of #ifdef before main() in program

  6. 6

    What is the purpose of the tf.contrib module in Tensorflow?

  7. 7

    What is the purpose of the tf.contrib module in Tensorflow?

  8. 8

    What is the purpose of canberra-gtk-module?

  9. 9

    What is the purpose of syntax support of npm module depcheck?

  10. 10

    What is the purpose of the pam_stress module?

  11. 11

    What does purpose use #ifdef and #if in C++

  12. 12

    What are the purpose of default arguments in JavaScript IIFE from a TypeScript module?

  13. 13

    purpose of installing a perl module with apt-get instead of cpan

  14. 14

    What is the purpose of user interface module in css3?

  15. 15

    GCC missing braces around initializer with static module level variable

  16. 16

    Working around formula 256 character limit in data validation in openpyxl module

  17. 17

    ES6 angular service module not being passed around correctly?

  18. 18

    No such module '####'

  19. 19

    Purpose of parentheses around expressions joined by logical "and"

  20. 20

    What is the purpose of Node.js module.exports and how do you use it?

  21. 21

    Can't get my head around "Injector already created, can not register a module!" error

  22. 22

    Using #ifdef preprocessor around virtual functions causes runtime error in program linked against libraries

  23. 23

    Batch EXIT followed by '|' - what is the purpose of this?

  24. 24

    How to tell X.org to reload input device module? (Working around suspend-to-ram crash on Acer laptop)

  25. 25

    IFDEF in if condition

  26. 26

    KVM what is the purpose of EXIT_QUALIFICATION

  27. 27

    Is there a command to exit a module when imported, like return for a function

  28. 28

    Exit an imported module without exiting the main program - Python

  29. 29

    Exit from loading Node module early without stopping process

HotTag

Archive