内核通知链_notifier
linux子系统
http://blog.chinaunix.net/uid-27411029-id-4931300.html
例程:
http://blog.chinaunix.net/uid-23069658-id-4364171.html
你和通知链用于内核模块之前互相的一种通知机制,通知链的原理是链表;
通知链的使用上框架为下:
A:模块管理通知链
1.使用各种变种去定义自己的通知链,以后其他的想要使用这个通知链的就可以将要执行的事件加在各个通知链上
static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
2.封装一个接口提供给其他模块使用的注册通知事件的接口:
int register_xxxx_handler(struct notifier_block *nb)
{
return atomic_notifier_chain_register(&restart_handler_list, nb);
}
EXPORT_SYMBOL_GPL(register_xxxx_handler);
restart_handler_list是步骤1中定义的通知链,nb是通知链事件
int unregister_xxxx_handler(struct notifier_block *nb)
{
return atomic_notifier_chain_unregister(&restart_handler_list, nb);
};
EXPORT_SYMBOL_GPL(unregister_xxxx_handler);
为撤销事件注册的接口
3.执行通知链上的事件:
void do_notifier(char *cmd)
{
atomic_notifier_call_chain(&restart_handler_list, 1, NULL);
}
也可以将一个函数通过参数3传递到模块B中的事件函数中,参数2是传递给事件的值,具体要看事件怎么处理
B:注册通知事件,供A模块中的利用通知链来调用
struct notifier_block notifire_handler;定义一个通知链事件对象
定义自己的事件:
static int notifire_XXX_handle(struct notifier_block *this, unsigned long mode,
void *cmd)
{
xxxxxx
}
将事件函数绑定至通知链事件对象:
wnotifire_handler.notifier_call = notifire_XXX_handle;
notifire_handler.priority = 128;
利用A模块中提供的注册事件接口注册事件
unregister_xxxx_handler(&wnotifire_handler);
ok,完毕