extern int test_notifier_call_chain(unsigned long val, void *v);
/*
* 向通知链发送消息以触发注册了的函数
*/
static int __init call_notifier(void)
{
int err;
printk("Begin to notify:\n");
/*
* 调用自定义的函数,向test_chain链发送消息
*/
printk("==============================\n");
err = test_notifier_call_chain(1, NULL);
printk("==============================\n");
if (err)
printk("notifier_call_chain error\n");
return err;
}
static void __exit uncall_notifier(void)
{
printk("End notify\n");
}
module_init(call_notifier);
module_exit(uncall_notifier);
复制代码
Makefile文件
obj-m:=buildchain.o regchain.o notify.o
KERNELDIR:=/lib/modules/$(shell uname -r)/build
default:
make -C $(KERNELDIR) M=$(shell pwd) modules
复制代码
运行:
make
insmod buildchain.ko
insmod regchain.ko
insmod notify.ko
复制代码
这样就可以看到通知链运行的效果了
下面是我在自己的机器上面运行得到的结果:
init_notifier Begin to register: register test_notifier1 completed register test_notifier2 completed register test_notifier3 completed Begin to notify: ============================== In Event 1: Event Number is 1 In Event 2: Event Number is 1 In Event 3: Event Number is 1 ==============================