linux内核事件通知,Linux内核基础--事件通知链(notifier chain)

1.4. 简单一例:

通过上面所述,notifier_chain机制只能在内核个子系统间使用,因此,这里使用3个模块:test_notifier_chain_0、test_notifier_chain_1、test_notifier_chain_2;当 test_notifier_chain_2通过module_init初始化模块时发出事件TESTCHAIN_2_INIT;然后 test_notifier_chain_1作出相应的处理:打印 test_notifier_chain_2正在初始化。

/* test_chain_0.c :0. 申明一个通知链;1. 向内核注册通知链;2. 定义事件; 3. 导出符号,因而必需最后退出*/

#include

#include

#include

#include /* printk() */

#include /* everything() */

#define TESTCHAIN_INIT 0x52U

static RAW_NOTIFIER_HEAD(test_chain);

/* define our own notifier_call_chain */

static int call_test_notifiers(unsigned long val, void *v)

{

return raw_notifier_call_chain(&test_chain, val, v);

}

EXPORT_SYMBOL(call_test_notifiers);

/* define our own notifier_chain_register func */

static int register_test_notifier(struct notifier_block *nb)

{

int err;

err = raw_notifier_chain_register(&test_chain, nb);

if(err)

goto out;

out:

return err;

}

EXPORT_SYMBOL(register_test_notifier);

static int __init test_chain_0_init(void)

{

printk(KERN_DEBUG "I'm in test_chain_0\n");

return 0;

}

static void __exit test_chain_0_exit(void)

{

printk(KERN_DEBUG "Goodbye to test_chain_0\n");

// call_test_notifiers(TESTCHAIN_EXIT, (int *)NULL);

}

MODULE_LICENSE("GPL v2");

MODULE_AUTHOR("fishOnFly");

module_init(test_chain_0_init);

module_exit(test_chain_0_exit);

/* test_chain_1.c :1. 定义回调函数;2. 定义notifier_block;3. 向chain_0注册notifier_block;*/

#include

#include

#include

#include /* printk() */

#include /* everything() */

extern int register_test_notifier(struct notifier_block *nb);

#define TESTCHAIN_INIT 0x52U

/* realize the notifier_call func */

int test_init_event(struct notifier_block *nb, unsigned long event,

void *v)

{

switch(event){

case TESTCHAIN_INIT:

printk(KERN_DEBUG "I got the chain event: test_chain_2 is on the way of init\n");

break;

default:

break;

}

return NOTIFY_DONE;

}

/* define a notifier_block */

static struct notifier_block test_init_notifier = {

.notifier_call = test_init_event,

};

static int __init test_chain_1_init(void)

{

printk(KERN_DEBUG "I'm in test_chain_1\n");

register_test_notifier(&test_init_notifier); // 由chain_0提供的设施

return 0;

}

static void __exit test_chain_1_exit(void)

{

printk(KERN_DEBUG "Goodbye to test_clain_l\n");

}

MODULE_LICENSE("GPL");

MODULE_AUTHOR("fishOnFly");

module_init(test_chain_1_init);

module_exit(test_chain_1_exit);

/* test_chain_2.c:发出通知链事件*/

#include

#include

#include

#include /* printk() */

#include /* everything() */

extern int call_test_notifiers(unsigned long val, void *v);

#define TESTCHAIN_INIT 0x52U

static int __init test_chain_2_init(void)

{

printk(KERN_DEBUG "I'm in test_chain_2\n");

call_test_notifiers(TESTCHAIN_INIT, "no_use");

return 0;

}

static void __exit test_chain_2_exit(void)

{

printk(KERN_DEBUG "Goodbye to test_chain_2\n");

}

MODULE_LICENSE("GPL v2");

MODULE_AUTHOR("fishOnFly");

module_init(test_chain_2_init);

module_exit(test_chain_2_exit);

# Makefile

# Comment/uncomment the following line to disable/enable debugging

# DEBUG = y

# Add your debugging flag (or not) to CFLAGS

ifeq ($(DEBUG),y)

DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines

else

DEBFLAGS = -O2

endif

ifneq ($(KERNELRELEASE),)

# call from kernel build system

obj-m := test_chain_0.o test_chain_1.o test_chain_2.o

else

KERNELDIR ?= /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

modules:

$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif

clean:

rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

depend .depend dep:

$(CC) $(CFLAGS) -M *.c > .depend

ifeq (.depend,$(wildcard .depend))

include .depend

endif

[wang2@iwooing: notifier_chian]$ sudo insmod./test_chain_0.ko

[wang2@iwooing: notifier_chian]$ sudo insmod./test_chain_1.ko

[wang2@iwooing: notifier_chian]$ sudo insmod./test_chain_2.ko

[wang2@iwooing: notifier_chian]$ dmesg

[ 5950.112649] I'm in test_chain_0

[ 5956.766610] I'm in test_chain_1

[ 5962.570003] I'm in test_chain_2

[ 5962.570008] I got the chain event: test_chain_2 is on the way of init

[ 6464.042975] Goodbye to test_chain_2

[ 6466.368030] Goodbye to test_clain_l

[ 6468.371479] Goodbye to test_chain_00b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值