Linux kernel 通知链代码崩溃系统

Linux kernel 通知链代码崩溃系统,linux-kernel,Linux Kernel,我编写了一个简单的代码来捕获netdevice通知,并将它们的值打印到消息日志文件中。。。代码如下: #include <linux/notifier.h> #include <asm/kdebug.h> #include <linux/netdevice.h> #include <linux/inetdevice.h> int my_dev_event_handler (struct notifier_block *self,unsigned

我编写了一个简单的代码来捕获
netdevice
通知,并将它们的值打印到消息日志文件中。。。代码如下:

#include <linux/notifier.h>
#include <asm/kdebug.h> 
#include <linux/netdevice.h>
#include <linux/inetdevice.h>

int my_dev_event_handler (struct notifier_block *self,unsigned long val, void *data)
{
    printk (KERN_INFO "my_dev_event: Val=%ld, Interface=%s\n", val,((struct net_device *) data)->name);
    return 0;
}

static struct notifier_block my_dev_notifier = {
.notifier_call = my_dev_event_handler,
};

static int __init
my_init (void)
{
printk(KERN_ALERT "***Module Loaded***\n");
register_netdevice_notifier (&my_dev_notifier); 

   return 0;
}

static void __exit my_end(void)
{
    printk(KERN_ALERT "***Module Unloaded***\n");
}

module_init(my_init);
module_exit(my_end);
#包括
#包括
#包括
#包括
int my_dev_event_handler(struct notifier_block*self,unsigned long val,void*data)
{
printk(KERN_INFO“my_dev_event:Val=%ld,Interface=%s\n”,Val,((结构网络设备*)数据)->name);
返回0;
}
静态结构通知程序\u阻止我的\u开发\u通知程序={
.notifier\u call=my\u dev\u event\u handler,
};
静态int\uu init
my_init(无效)
{
printk(内核警报“***模块已加载***\n”);
注册网络设备通知程序(&我的设备通知程序);
返回0;
}
静态无效\u退出我的结束(无效)
{
printk(内核警报“***模块已卸载***\n”);
}
模块_init(我的_init);
模块退出(我的终端);
此代码编译并正确运行,每次设备启动/关闭时,它都会打印出“my_dev_event:…”行。。。但有时(并非总是)当设备上升\下降时,整个系统会冻结。。。现在我有两个问题: 1-为什么系统冻结?这个代码有什么问题吗?
2-如果有更好的方法在设备连接/断开连接时通知我的内核模块…

我看到的唯一问题是
my_end
没有注销通知程序。
卸载模块后,这可能会导致崩溃或冻结。这是因为指向代码的指针仍保留在Linux数据结构中,但代码不再存在

关于另一种方式-我认为您使用的是正确的方式来获取这些通知