|
| 1 | +/* test_chain_0.c :0. 申明一个通知链;1. 向内核注册通知链;2. 定义事件; 3. 导出符号,因而必需最后退出*/ |
| 2 | + |
| 3 | +#include <linux/notifier.h> |
| 4 | +#include <linux/module.h> |
| 5 | +#include <linux/init.h> |
| 6 | +#include <linux/kernel.h> /* printk() */ |
| 7 | +#include <linux/fs.h> /* everything() */ |
| 8 | + |
| 9 | +#define TESTCHAIN_INIT 0x52U |
| 10 | +static RAW_NOTIFIER_HEAD(test_chain); |
| 11 | + |
| 12 | +/* define our own notifier_call_chain */ |
| 13 | +static int call_test_notifiers(unsigned long val, void *v) |
| 14 | +{ |
| 15 | + return raw_notifier_call_chain(&test_chain, val, v); |
| 16 | +} |
| 17 | +EXPORT_SYMBOL(call_test_notifiers); |
| 18 | + |
| 19 | +/* define our own notifier_chain_register func */ |
| 20 | + static int register_test_notifier(struct notifier_block *nb) |
| 21 | +{ |
| 22 | + int err; |
| 23 | + err = raw_notifier_chain_register(&test_chain, nb); |
| 24 | + |
| 25 | + if(err) |
| 26 | + goto out; |
| 27 | + |
| 28 | +out: |
| 29 | + return err; |
| 30 | +} |
| 31 | + |
| 32 | +EXPORT_SYMBOL(register_test_notifier); |
| 33 | + |
| 34 | +static int __init test_chain_0_init(void) |
| 35 | +{ |
| 36 | + printk(KERN_DEBUG "I'm in test_chain_0\n"); |
| 37 | + |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +static void __exit test_chain_0_exit(void) |
| 42 | +{ |
| 43 | + printk(KERN_DEBUG "Goodbye to test_chain_0\n"); |
| 44 | +// call_test_notifiers(TESTCHAIN_EXIT, (int *)NULL); |
| 45 | +} |
| 46 | + |
| 47 | +MODULE_LICENSE("GPL v2"); |
| 48 | +MODULE_AUTHOR("fishOnFly"); |
| 49 | + |
| 50 | +module_init(test_chain_0_init); |
| 51 | +module_exit(test_chain_0_exit); |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +/* test_chain_1.c :1. 定义回调函数;2. 定义notifier_block;3. 向chain_0注册notifier_block;*/ |
| 60 | +#include <linux/notifier.h> |
| 61 | +#include <linux/module.h> |
| 62 | +#include <linux/init.h> |
| 63 | + |
| 64 | +#include <linux/kernel.h> /* printk() */ |
| 65 | +#include <linux/fs.h> /* everything() */ |
| 66 | + |
| 67 | +extern int register_test_notifier(struct notifier_block *nb); |
| 68 | +#define TESTCHAIN_INIT 0x52U |
| 69 | + |
| 70 | +/* realize the notifier_call func */ |
| 71 | +int test_init_event(struct notifier_block *nb, unsigned long event, |
| 72 | + void *v) |
| 73 | +{ |
| 74 | + switch(event){ |
| 75 | + case TESTCHAIN_INIT: |
| 76 | + printk(KERN_DEBUG "I got the chain event: test_chain_2 is on the way of init\n"); |
| 77 | + break; |
| 78 | + |
| 79 | + default: |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + return NOTIFY_DONE; |
| 84 | +} |
| 85 | +/* define a notifier_block */ |
| 86 | +static struct notifier_block test_init_notifier = { |
| 87 | + .notifier_call = test_init_event, |
| 88 | +}; |
| 89 | +static int __init test_chain_1_init(void) |
| 90 | +{ |
| 91 | + printk(KERN_DEBUG "I'm in test_chain_1\n"); |
| 92 | + register_test_notifier(&test_init_notifier);//<span style="white-space:pre"> </span>// 由chain_0提供的设施 |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +static void __exit test_chain_1_exit(void) |
| 97 | +{ |
| 98 | + printk(KERN_DEBUG "Goodbye to test_clain_l\n"); |
| 99 | +} |
| 100 | + |
| 101 | +MODULE_LICENSE("GPL"); |
| 102 | +MODULE_AUTHOR("fishOnFly"); |
| 103 | + |
| 104 | +module_init(test_chain_1_init); |
| 105 | +module_exit(test_chain_1_exit); |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +/* test_chain_2.c:发出通知链事件*/ |
| 122 | + |
| 123 | +#include <linux/notifier.h> |
| 124 | +#include <linux/module.h> |
| 125 | +#include <linux/init.h> |
| 126 | +#include <linux/kernel.h> /* printk() */ |
| 127 | +#include <linux/fs.h> /* everything() */ |
| 128 | + |
| 129 | +extern int call_test_notifiers(unsigned long val, void *v); |
| 130 | +#define TESTCHAIN_INIT 0x52U |
| 131 | + |
| 132 | +static int __init test_chain_2_init(void) |
| 133 | +{ |
| 134 | + printk(KERN_DEBUG "I'm in test_chain_2\n"); |
| 135 | + call_test_notifiers(TESTCHAIN_INIT, "no_use"); |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
| 139 | + |
| 140 | +static void __exit test_chain_2_exit(void) |
| 141 | +{ |
| 142 | + printk(KERN_DEBUG "Goodbye to test_chain_2\n"); |
| 143 | +} |
| 144 | + |
| 145 | +MODULE_LICENSE("GPL v2"); |
| 146 | +MODULE_AUTHOR("fishOnFly"); |
| 147 | + |
| 148 | +module_init(test_chain_2_init); |
| 149 | +module_exit(test_chain_2_exit); |
| 150 | + |
| 151 | +/* |
| 152 | +[wang2@iwooing: notifier_chian]$ sudo insmod./test_chain_0.ko |
| 153 | +[wang2@iwooing: notifier_chian]$ sudo insmod./test_chain_1.ko |
| 154 | +[wang2@iwooing: notifier_chian]$ sudo insmod./test_chain_2.ko |
| 155 | + |
| 156 | + |
| 157 | +[wang2@iwooing: notifier_chian]$ dmesg |
| 158 | + |
| 159 | +[ 5950.112649] I'm in test_chain_0 |
| 160 | +[ 5956.766610] I'm in test_chain_1 |
| 161 | +[ 5962.570003] I'm in test_chain_2 |
| 162 | +[ 5962.570008] I got the chain event: test_chain_2 is on the way of init |
| 163 | + |
| 164 | +[ 6464.042975] Goodbye to test_chain_2 |
| 165 | +[ 6466.368030] Goodbye to test_clain_l |
| 166 | +[ 6468.371479] Goodbye to test_chain_0 |
| 167 | +
|
| 168 | +
|
| 169 | +
|
| 170 | +
|
| 171 | +# Makefile |
| 172 | + |
| 173 | +# Comment/uncomment the following line to disable/enable debugging |
| 174 | +# DEBUG = y |
| 175 | + |
| 176 | + |
| 177 | +# Add your debugging flag (or not) to CFLAGS |
| 178 | +ifeq ($(DEBUG),y) |
| 179 | + DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines |
| 180 | +else |
| 181 | + DEBFLAGS = -O2 |
| 182 | +endif |
| 183 | + |
| 184 | + |
| 185 | +ifneq ($(KERNELRELEASE),) |
| 186 | +# call from kernel build system |
| 187 | + |
| 188 | +obj-m := test_chain_0.o test_chain_1.o test_chain_2.o |
| 189 | + |
| 190 | +else |
| 191 | + |
| 192 | +KERNELDIR ?= /lib/modules/$(shell uname -r)/build |
| 193 | +PWD := $(shell pwd) |
| 194 | + |
| 195 | +modules: |
| 196 | + $(MAKE) -C $(KERNELDIR) M=$(PWD) modules |
| 197 | + |
| 198 | +endif |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +clean: |
| 203 | + rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions |
| 204 | + |
| 205 | +depend .depend dep: |
| 206 | + $(CC) $(CFLAGS) -M *.c > .depend |
| 207 | + |
| 208 | + |
| 209 | +ifeq (.depend,$(wildcard .depend)) |
| 210 | +include .depend |
| 211 | +endif |
| 212 | +*/ |
0 commit comments