直接上代码
static int brcm_device_event(struct notifier_block *unused, unsigned long event,
void *ptr)
{
printk(KERN_INFO "brcm_device_event = %d\n", event);
return NOTIFY_DONE;
}
static struct notifier_block brcm_notifier_block __read_mostly = {
.notifier_call = brcm_device_event,
};模块初始化部分
ret = register_netdevice_notifier(&brcm_notifier_block);
if (ret < 0) {
printk(KERN_INFO "register_netdevice_notifier failed\n");
return ret;
}
模块退出需要unregister
unregister_netdevice_notifier(&brcm_notifier_block);root@ZTE:/tmp# insmod bdcom-audit.ko
[ 414.723299] dpi_init start--
[ 414.726366] brcm_device_event = 5
[ 414.729743] brcm_device_event = 1
[ 414.733102] brcm_device_event = 5
[ 414.736428] brcm_device_event = 1
[ 414.739779] brcm_device_event = 5
[ 414.743155] brcm_device_event = 1
[ 414.746527] brcm_device_event = 5
[ 414.749887] brcm_device_event = 1
[ 414.753249] brcm_device_event = 5
[ 414.756603] brcm_device_event = 5
[ 414.759928] brcm_device_event = 5
[ 414.763280] brcm_device_event = 5
[ 414.766657] brcm_device_event = 5
[ 414.770033] brcm_device_event = 1
[ 414.773460] brcm_device_event = 5
[ 414.776823] brcm_device_event = 1
[ 414.780144] brcm_device_event = 5
[ 414.783500] brcm_device_event = 5
本文展示了一个使用brcm_device_event函数实现的设备事件监听器的代码示例。该监听器通过注册netdevice_notifier来捕获并打印特定的网络设备事件。在模块加载时进行注册,并在模块卸载时取消注册。
908

被折叠的 条评论
为什么被折叠?



