Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a module in C?

Tags:

c

linux-kernel

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?

like image 479
Lavair Avatar asked Dec 03 '18 07:12

Lavair


1 Answers

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() and module_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 with module_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 the module_exit() call. This is the exit function for modules; it undoes whatever entry function did. It unregisters the functionality that the entry function registered.

like image 153
gsamaras Avatar answered Oct 05 '22 03:10

gsamaras