Linux Notification chain学习
来源:互联网 发布:海拉尔二中分尸案 知乎 编辑:程序博客网 时间:2024/05/21 15:39
注意:notification chain适用于内核子系统之间的信息传递,不涉及用户态。
Notification chain使用发布-订阅模型(publish-and-subscribe model):
在事件发生时,检测或产生事件的子系统作为主动一方通过通知函数来告知作为被动一方的订阅者(对此事件感兴趣的子系统)。这里有个额外要求,订阅一方要提供callback函数以供发布方调用,当然,提供什么样的callback函数完全由订阅方决定。订阅者必须知道其他子系统提供了哪些事件通知支持,以选择可以订阅的事件通知;当然,订
从某种意义上来说,notification chain实现了事件信息的共享
struct notifier_block结构
Notification chain由notifier block组成,其结构类型如下(include/linux/notifier.h):
struct notifier_block
{
int (*notifier_call)(struct notifier_block *self, unsigned long, void *);
struct notifier_block *next;
int priority;
};
实际上notification chain就是一组函数列表。通常notification chain的名字的格式为xxx_chain、xxx_notifier_chain、 xxx_notifier_list,例如reboot_notifier_list。
回调函数notifier_call
前面已经讲过函数参数的含义,注意到该函数的返回值为int类型,这里就来看一下可能的返回
NOTIFY_DONE 0x0000 对该事件不感兴趣(根据unsigned long参数)
NOTIFY_OK 0x0001 成功响应该事件
NOTIFY_STOP_MASK 0x8000 该回调函数返回后停止处理后续notifier block
NOTIFY_BAD (NOTIFY_STOP_MASK|0x0002) 出错,回调函数返回后停止处理后续notifier block
NOTIFY_STOP (NOTIFY_OK|NOTIFY_STOP_MASK) 成功响应事件,回调函数返回后停止处理后续notifier block
注意,NOTIFY_STOP和NOTIFY_BAD的定义都包含了NOTIFY_STOP_MASK。
并发访问控制在kernel/sys.c文件中定义了对notification chain进行并发访问控制的读写锁notifier_lock。系统中对所有notification chain的并发访问都是由该锁来控制。子系统通常只在boot或者加载module时注册notifier block,即修改notification chain,而大多数时间仅以只读方式来访问,因此一个锁基本不会影响系统性能。
基 本 API
下面的基本例程位于kernel/sys.c文件中要接收某些事件的通知需要先注册到支持这些事件的notification chain中:
int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
n为当前子系统提供的notifier_block,其中指明了回调函数该函数会根据notifier_block的优先级priority将n插入到list中合适位置如果不想接受已订阅事件的通知,则需要取消订阅注册:
int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
nl为notification chain
n为当前子系统提供的notifier_block当事件发生时,要通知订阅该事件的子系统:
int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
n为notification chain
val为事件类型,前面提到过一个chain可能支持多个事件,该参数用来对事件进行区分
v存放特定于事件的信息该函数会遍历chain,对chain中每个notifier block,以参数val和v调用其notifier_call函数,若notifier_call函数返回值中标志了NOTIFY_STOP_MASK(如NOTIFY_BAD、NOTIFY_STOP),则函数停止处理,返回当前notifier block的返回值;否则返回chain中最后一个notifier block的返回值。
注意:多数子系统都定义了这些基本例程的封装函数,因此很少看到对这些函数的直接调用。例如后面的例子中用到的register_reboot_notifier和unregister_reboot_notifier就是简单的封装函数。
在kernel/sys.c文件中定义了一个全局reboot_notifier_list,该chain用来挂接想在系统shutdown时执行的函数,例如进行某种清理工作。前面提到过register_reboot_notifier和unregister_reboot_notifier是对notifier_chain_register和notifier_chain_unregister简单封装,具体请参考实现,在此不再赘述。下面给出一个简单的使用notification chain的例子。该模块向reboot_notifier_list注册了一个函数myreboot,该函数在系统reboot时会简单地打印一些信息(在系统shutdown时也可以)注意到传递给myreboot的event参数值为1,而include/linux/notifier.h文件中定义了与系统关机相关的一组事件常值,而1对应于事件SYS_DOWN。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/notifier.h>
#include <linux/reboot.h>
static int myreboot(struct notifier_block *self, unsigned long event, void *data)
{
printk(KERN_ALERT "Just a test! Event code: %li! System reboot now...", event);
return NOTIFY_OK;
}
static struct notifier_block myreboot_notifier = {
.notifier_call = myreboot,
};
static int myreboot_init(void)
{
register_reboot_notifier(&myreboot_notifier);
return 0;
}
static void myreboot_exit(void)
{
unregister_reboot_notifier(&myreboot_notifier);
}
module_init(myreboot_init);
module_exit(myreboot_exit);
MODULE_AUTHOR("dj");
MODULE_DESCRIPTION("just a notify test!");
MODULE_LICENSE("Dual BSD/GPL");
- Linux Notification chain学习
- < Linux Kernel > Notification Chain
- Linux内核通知链技术-notification chain
- linux notification chain (转)
- Linux Notification 学习
- Linux Notification 学习
- Notification chain in bridge
- 内核中的notification chain浅析
- notification chain and timer interrupt
- 内核中的notification chain浅析
- Linux Notification
- Linux Notification
- Linux Notification
- Linux Notification
- notification学习
- Notification学习
- Notification学习
- Notification 学习
- mfc 绘制 圆形按钮 继承CButton
- How To Install LAMP (Linux, Apache, MySQL, PHP) On Ubuntu 11.04/11.10/12.04
- Ubuntu 12.04的人工小帮手HUD介绍
- .Net 4.5中的HttpClient试用
- 达芬奇数字媒体片上系统的架构和 Linux 启动过程
- Linux Notification chain学习
- Java时间日期格式转换 格式化后的日期解析成时间戳
- hdu 4197 Popping Balloons
- 滑动条:
- 关于修改虚拟机内存,和开启“硬件加速
- Android AVD创建及设置中各参数详解
- js字符串转化为数字 详解(转载)
- QUERY_REWRITE_ INTEGRITY的设置
- 在规定时间内清理备份目录下文件