I have written simple kernel module to print Hello kernel msg.
my_module.c
#include <linux/module.h>
#include <linux/init.h>
/* Meta Information */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("BHAGWAT");
MODULE_DESCRIPTION("A HELLO WORLD MODULE");
/*this function is called when module is loaded into the kernel*/
static int __init ModuleInit(void)
{
printk("Hello, kernel!\n");
return 0;
}
/*this function is called when module is removed from kernel*/
static void __exit ModuleExit(void)
{
printk("Goodbye, kernel");
}
module_init(ModuleInit);
module_exit(ModuleExit);
Makefile
obj-m += my_module.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
when I insert the module I get msg printed in kernel logs, but when I remove the module msg does not get printed. When I again insert the module, two msg get printed one for last module exit and other for module load. Why module exit msg get printed later when i insert the module but not when removed the module.
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ sudo insmod my_module.ko
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ dmesg | tail
[ 24.595403] rfkill: input handler enabled
[ 30.244336] rfkill: input handler disabled
[ 145.426767] my_module: module verification failed: signature and/or required key missing - tainting kernel
[ 145.427022] Hello, kernel!
[ 217.026559] Goodbye, kernel
[ 498.489388] Hello, kernel!
[ 524.139613] Goodbye, kernel
[ 528.270128] Hello, kernel!
[ 577.360611] Goodbye, kernel
[ 587.700237] Hello, kernel!
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ sudo rmmod my_module
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ dmesg | tail
[ 24.595403] rfkill: input handler enabled
[ 30.244336] rfkill: input handler disabled
[ 145.426767] my_module: module verification failed: signature and/or required key missing - tainting kernel
[ 145.427022] Hello, kernel!
[ 217.026559] Goodbye, kernel
[ 498.489388] Hello, kernel!
[ 524.139613] Goodbye, kernel
[ 528.270128] Hello, kernel!
[ 577.360611] Goodbye, kernel
[ 587.700237] Hello, kernel!
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ sudo insmod my_module.ko
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ dmesg | tail
[ 145.426767] my_module: module verification failed: signature and/or required key missing - tainting kernel
[ 145.427022] Hello, kernel!
[ 217.026559] Goodbye, kernel
[ 498.489388] Hello, kernel!
[ 524.139613] Goodbye, kernel
[ 528.270128] Hello, kernel!
[ 577.360611] Goodbye, kernel
[ 587.700237] Hello, kernel!
[ 667.900373] Goodbye, kernel
[ 676.245356] Hello, kernel!
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ sudo rmmod my_module
bhagwat@bhagwat:~/bhagwatws/Personal/device_driver$ dmesg | tail
[ 145.426767] my_module: module verification failed: signature and/or required key missing - tainting kernel
[ 145.427022] Hello, kernel!
[ 217.026559] Goodbye, kernel
[ 498.489388] Hello, kernel!
[ 524.139613] Goodbye, kernel
[ 528.270128] Hello, kernel!
[ 577.360611] Goodbye, kernel
[ 587.700237] Hello, kernel!
[ 667.900373] Goodbye, kernel
[ 676.245356] Hello, kernel!