Files
Driver-tutorial/LKM/hello.c

44 lines
1.7 KiB
C

#include <linux/module.h> // included for all kernel module
#include <linux/kernel.h> // included for KERN_INFO
#include <linux/init.h> // included for __init and __exit macros
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Junet Hossain");
MODULE_DESCRIPTION("A Simple Hello World module");
static int __init hello_init(void){
printk(KERN_INFO "Hello world!\n");
return 0; // Non-zero return means that the module couldn't be loaded.
}
static void __exit hello_cleanup(void){
printk(KERN_INFO "Cleaning up hello module.\n");
}
module_init(hello_init);
module_exit(hello_cleanup);
/**
* Use these macros for efficient and optimized kernel modules.
*
* __init: It's a macro which is used for marking
* the function which is executed when the
* kernel is loaded. After execution, its memory
* is freed (for built-in modules). For module
* compiled as a loadable kernel module (LKM),
* __init has no effect (because the memory
* is allocated dynamically). If not used for built
* -in kernel, the memory for the init function
* remains allocated even after module initializaiton
* (wasted memory)
*
* __exit: It's a macro which is used to mark the function
* which is executed when the module is unloaded
* from the kernel. It is ignored for built-in kernel modules
* (since they can't be removed at runtime). For
* dynamically loaded modules, it ensures proper
* cleanup. If __exit is not used, the function will
* still run when removing the module, but marking
* it properly makes the code cleaner and optimized.
*/