我已经编写了简单的内核模块来打印 Hello 内核消息。
我的模块.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
当我插入模块时,内核日志中会打印消息,但当我移除模块时,不会打印消息。当我再次插入模块时,会打印两个消息,一个是上次模块退出的消息,另一个是模块加载的消息。为什么当我插入模块时模块退出消息会打印,但移除模块时不会打印?
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!
答案1
在退出模块 printk("Goodbye, kernel\n"); 的 printk 语句中使用 '\n'