Linux Kernel module function call and namespace-general Linux technology-Linux programming and kernel information. The following is a detailed description. How does the kernel module start and end?
User Programs generally start from the main () function and execute a series of commands and end the program after the command execution is complete. The kernel modules are a little different. The kernel module either starts from the init_module function or the function call specified by the macro module_init. This is the entry function of the kernel module. It tells the kernel module to provide functional extensions and prepare the kernel to call it as needed. After this is done, the function execution is complete. The module does nothing before being called by the kernel.
All modules can either call cleanup_module or use the function specified by the macro module_exit. This is the exit function of the module. It revokes everything done by the entry function. For example, you can deregister the function registered by the entry function.
All modules must have entry functions and exit functions. Since we have more than one way to define these two functions, I will try to describe them using the "entry function" and "exit function. But when I only use init_module and cleanup_module, I want you to understand what I mean.
Function called by the module
Programmers do not always write all the functions they use. A common basic example is printf (). You use these C Standard libraries, library functions provided by libc. These functions (like printf () do not actually enter your program before the connection. During the connection, these function calls will point to the library you are calling, so that your code can finally be executed.
The kernel modules are different. In the hello world module, you may have noticed that the printk () function we use does not contain the standard I/O library. This is because the module is connected to the target file only when the insmod is loaded. The symbolic links of the functions to be used are provided by the kernel itself. That is to say, the functions you can use in the kernel module only come from the kernel itself. If you are interested in the function symbolic links provided by the kernel, take a look at the file/proc/kallsyms.
Note the differences between database functions and system calls. Library functions are high-level and fully run in the user space, providing programmers with more convenient interfaces for calling systems that actually complete real transactions behind the scenes. System calls run in kernel mode and are provided by the kernel itself. The standard C library function printf () can be considered as a general output statement, but what it actually does is to convert the data into a formatted string and call the system to call write () output these strings. Do you want to see which system calls are used by printf? This is easy to compile the following code.