Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 480
janneia Avatar asked Oct 01 '14 06:10

janneia


People also ask

What is the purpose of meaning?

The purpose of meaning is a reason to support the cause of something. The meaning of purpose is the inner calling towards achieving something. The meaning of purpose in life is to discover your passion and calling. The purpose of meaning in life is to use your passion towards the betterment of the society and yourself.

Do you say for purpose of or for purposes of?

Both are correct. “For the purpose of” is followed by a gerund, and the phrase can typically be replaced with “to” followed by an infinitive. “This spoon can be used for the purpose of stirring ingredients.”

What is the purpose of for example?

You use for example to introduce and emphasize something which shows that something is true.

What is a good sentence for purpose?

His purpose was to make a profit by improving the company's performance. The teachers are enthusiastic and have a sense of purpose.


1 Answers

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);
like image 179
Jonathon Reinhart Avatar answered Oct 10 '22 00:10

Jonathon Reinhart