I wrote a simple code to capture the netdevice notifications and simply print their value out to the messages log file … here’s the code :
#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);
this code compiles and runs correctly, it prints out the “my_dev_event:…” line every time a device goes up/off … but sometimes (not always) the entire system freezes when a device goes up\down … now I have two questions here:
1- why is the system freezing? anything wrong with this code?
2- if there’s a better way to notify my kernel module when a device goes connected/disconnected …
The only problem I see is that
my_enddoesn’t unregister the notifier.This can cause crashes or freezes after you’ve unloaded your module. This is because a pointer to your code remains in Linux data structures, but your code is no longer there.
Regarding an alternative way – I think you’re using the correct way to get these notifications.