内核通知链 学习笔记 【转】

简介:
最近在看《深入理解Linux 网络内幕》一书,学习了一下书中讲到的内核通知链方面的知识,写了一个读书笔记和一点代码来加深理解,希望能够对大家有一点帮助。内核通知链在网络方面得到了广泛的使用。


1.通知链表简介
    大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣。为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子系统,Linux内核提供了通知链的机制。通知链表只能够在内核的子系统之间使用,而不能够在内核与用户空间之间进行事件的通知。
    通知链表是一个函数链表,链表上的每一个节点都注册了一个函数。当某个事情发生时,链表上所有节点对应的函数就会被执行。所以对于通知链表来说有一个通知方与一个接收方。在通知这个事件时所运行的函数由被通知方决定,实际上也即是被通知方注册了某个函数,在发生某个事件时这些函数就得到执行。其实和系统调用signal的思想差不多。

2.通知链表数据结构
    通知链表的节点类型为notifier_block,其定义如下:
  1. struct notifier_block
  2. {
  3.     int (*notifier_call)(struct notifier_block *self, unsigned long, void *);
  4.     struct notifier_block *next;
  5.     int priority;
  6. };
复制代码

    其中最重要的就是notifier_call这个函数指针,表示了这个节点所对应的要运行的那个函数。next指向下一个节点,即当前事件发生时还要继续执行的那些节点。

3.注册通知链
    在通知链注册时,需要有一个链表头,它指向这个通知链表的第一个元素。这样,之后的事件对该链表通知时就会根据这个链表头而找到这个链表中所有的元素。
    注册的函数是:
int notifier_chain_register(struct notifier_block **nl, struct notifier_block *n)
    也即是将新的节点n加入到nl所指向的链表中去。
    卸载的函数是:
int notifier_chain_unregister(strut notifier_block **nl, struct notifier_block *n)
    也即是将节点n从nl所指向的链表中删除。

4.通知链表
    当有事件发生时,就使用notifier_call_chain向某个通知链表发送消息。
int notifier_call_chain(struct notifier_block **nl, unsigned long val, void *v)
    这个函数是按顺序运行nl指向的链表上的所有节点上注册的函数。简单地说,如下所示:
  1.     struct notifier_block *nb = *n;
  2.     while (nb)
  3.     {
  4.         ret = nb->notifier_call(nb, val, v);
  5.         if (ret & NOTIFY_STOP_MASK)
  6.         {
  7.             return ret;
  8.         }
  9.         nb = nb->next;
  10.     }
复制代码


5.示例
    在这里,写了一个简单的通知链表的代码。

    实际上,整个通知链的编写也就两个过程:
    首先是定义自己的通知链的头节点,并将要执行的函数注册到自己的通知链中。
    其次则是由另外的子系统来通知这个链,让其上面注册的函数运行。

    我这里将第一个过程分成了两步来写,第一步是定义了头节点和一些自定义的注册函数(针对该头节点的),第二步则是使用自定义的注册函数注册了一些通知链节点。分别在代码buildchain.c与regchain.c中。
    发送通知信息的代码为notify.c。

代码1 buildchain.c
    它的作用是自定义一个通知链表test_chain,然后再自定义两个函数分别向这个通知链中加入或删除节点,最后再定义一个函数通知这个test_chain链。

  1. #include <asm/uaccess.h>
  2. #include <linux/types.h>
  3. #include <linux/kernel.h>
  4. #include <linux/sched.h>
  5. #include <linux/notifier.h>
  6. #include <linux/init.h>
  7. #include <linux/types.h>
  8. #include <linux/module.h>
  9. MODULE_LICENSE("GPL");
  10. /*
  11. * 定义自己的通知链头结点以及注册和卸载通知链的外包函数
  12. */
  13. /*
  14. * RAW_NOTIFIER_HEAD是定义一个通知链的头部结点,
  15. * 通过这个头部结点可以找到这个链中的其它所有的notifier_block
  16. */
  17. static RAW_NOTIFIER_HEAD(test_chain);
  18. /*
  19. * 自定义的注册函数,将notifier_block节点加到刚刚定义的test_chain这个链表中来
  20. * raw_notifier_chain_register会调用notifier_chain_register
  21. */
  22. int register_test_notifier(struct notifier_block *nb)
  23. {
  24.         return raw_notifier_chain_register(&test_chain, nb);
  25. }
  26. EXPORT_SYMBOL(register_test_notifier);
  27. int unregister_test_notifier(struct notifier_block *nb)
  28. {
  29.         return raw_notifier_chain_unregister(&test_chain, nb);
  30. }
  31. EXPORT_SYMBOL(unregister_test_notifier);
  32. /*
  33. * 自定义的通知链表的函数,即通知test_chain指向的链表中的所有节点执行相应的函数
  34. */
  35. int test_notifier_call_chain(unsigned long val, void *v)
  36. {
  37.         return raw_notifier_call_chain(&test_chain, val, v);
  38. }
  39. EXPORT_SYMBOL(test_notifier_call_chain);
  40. /*
  41. * init and exit
  42. */
  43. static int __init init_notifier(void)
  44. {
  45.         printk("init_notifier\n");
  46.         return 0;
  47. }
  48. static void __exit exit_notifier(void)
  49. {
  50.         printk("exit_notifier\n");
  51. }
  52. module_init(init_notifier);
  53. module_exit(exit_notifier);
复制代码


代码2 regchain.c
    该代码的作用是将test_notifier1 test_notifier2 test_notifier3这三个节点加到之前定义的test_chain这个通知链表上,同时每个节点都注册了一个函数。

  1. #include <asm/uaccess.h>
  2. #include <linux/types.h>
  3. #include <linux/kernel.h>
  4. #include <linux/sched.h>
  5. #include <linux/notifier.h>
  6. #include <linux/init.h>
  7. #include <linux/types.h>
  8. #include <linux/module.h>
  9. MODULE_LICENSE("GPL");
  10. /*
  11. * 注册通知链
  12. */
  13. extern int register_test_notifier(struct notifier_block*);
  14. extern int unregister_test_notifier(struct notifier_block*);
  15. static int test_event1(struct notifier_block *this, unsigned long event, void *ptr)
  16. {
  17.         printk("In Event 1: Event Number is %d\n", event);
  18.         return 0;
  19. }
  20. static int test_event2(struct notifier_block *this, unsigned long event, void *ptr)
  21. {
  22.         printk("In Event 2: Event Number is %d\n", event);
  23.         return 0;
  24. }
  25. static int test_event3(struct notifier_block *this, unsigned long event, void *ptr)
  26. {
  27.         printk("In Event 3: Event Number is %d\n", event);
  28.         return 0;
  29. }
  30. /*
  31. * 事件1,该节点执行的函数为test_event1
  32. */
  33. static struct notifier_block test_notifier1 =
  34. {
  35.         .notifier_call = test_event1,
  36. };
  37. /*
  38. * 事件2,该节点执行的函数为test_event1
  39. */
  40. static struct notifier_block test_notifier2 =
  41. {
  42.         .notifier_call = test_event2,
  43. };
  44. /*
  45. * 事件3,该节点执行的函数为test_event1
  46. */
  47. static struct notifier_block test_notifier3 =
  48. {
  49.         .notifier_call = test_event3,
  50. };
  51. /*
  52. * 对这些事件进行注册
  53. */
  54. static int __init reg_notifier(void)
  55. {
  56.         int err;
  57.         printk("Begin to register:\n");
  58.        
  59.         err = register_test_notifier(&test_notifier1);
  60.         if (err)
  61.         {
  62.                 printk("register test_notifier1 error\n");
  63.                 return -1;
  64.         }
  65.         printk("register test_notifier1 completed\n");
  66.         err = register_test_notifier(&test_notifier2);
  67.         if (err)
  68.         {
  69.                 printk("register test_notifier2 error\n");
  70.                 return -1;
  71.         }
  72.         printk("register test_notifier2 completed\n");
  73.         err = register_test_notifier(&test_notifier3);
  74.         if (err)
  75.         {
  76.                 printk("register test_notifier3 error\n");
  77.                 return -1;
  78.         }
  79.         printk("register test_notifier3 completed\n");
  80.         return err;
  81. }
  82. /*
  83. * 卸载刚刚注册了的通知链
  84. */
  85. static void __exit unreg_notifier(void)
  86. {
  87.         printk("Begin to unregister\n");
  88.         unregister_test_notifier(&test_notifier1);
  89.         unregister_test_notifier(&test_notifier2);
  90.         unregister_test_notifier(&test_notifier3);
  91.         printk("Unregister finished\n");
  92. }
  93. module_init(reg_notifier);
  94. module_exit(unreg_notifier);
复制代码


代码3 notify.c
    该代码的作用就是向test_chain通知链中发送消息,让链中的函数运行。
  1. #include <asm/uaccess.h>
  2. #include <linux/types.h>
  3. #include <linux/kernel.h>
  4. #include <linux/sched.h>
  5. #include <linux/notifier.h>
  6. #include <linux/init.h>
  7. #include <linux/types.h>
  8. #include <linux/module.h>
  9. MODULE_LICENSE("GPL");
  10. extern int test_notifier_call_chain(unsigned long val, void *v);
  11. /*
  12. * 向通知链发送消息以触发注册了的函数
  13. */
  14. static int __init call_notifier(void)
  15. {
  16.         int err;
  17.         printk("Begin to notify:\n");
  18. /*
  19. * 调用自定义的函数,向test_chain链发送消息
  20. */
  21.         printk("==============================\n");
  22.         err = test_notifier_call_chain(1, NULL);
  23.         printk("==============================\n");
  24.         if (err)
  25.                 printk("notifier_call_chain error\n");
  26.         return err;
  27. }
  28. static void __exit uncall_notifier(void)
  29. {
  30.         printk("End notify\n");
  31. }
  32. module_init(call_notifier);
  33. module_exit(uncall_notifier);
复制代码


Makefile文件
  1. obj-m:=buildchain.o regchain.o notify.o
  2. KERNELDIR:=/lib/modules/$(shell uname -r)/build
  3. default:
  4.         make -C (KERNELDIR)M=(KERNELDIR)M=(shell pwd) modules
复制代码


运行:
  1. make
  2. insmod buildchain.ko
  3. insmod regchain.ko
  4. insmod notify.ko
复制代码


这样就可以看到通知链运行的效果了

下面是我在自己的机器上面运行得到的结果:


QUOTE:
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
==============================















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sky-heaven/p/5355239.html ,如需转载请自行联系原作者


相关文章
|
3天前
|
Linux 调度 数据库
|
5月前
|
监控 安全 API
7.2 Windows驱动开发:内核注册并监控对象回调
在笔者上一篇文章`《内核枚举进程与线程ObCall回调》`简单介绍了如何枚举系统中已经存在的`进程与线程`回调,本章`LyShark`将通过对象回调实现对进程线程的`句柄`监控,在内核中提供了`ObRegisterCallbacks`回调,使用这个内核`回调`函数,可注册一个`对象`回调,不过目前该函数`只能`监控进程与线程句柄操作,通过监控进程或线程句柄,可实现保护指定进程线程不被终止的目的。
31 0
7.2 Windows驱动开发:内核注册并监控对象回调
|
5月前
|
监控 安全 API
7.1 Windows驱动开发:内核监控进程与线程回调
在前面的文章中`LyShark`一直在重复的实现对系统底层模块的枚举,今天我们将展开一个新的话题,内核监控,我们以`监控进程线程`创建为例,在`Win10`系统中监控进程与线程可以使用微软提供给我们的两个新函数来实现,此类函数的原理是创建一个回调事件,当有进程或线程被创建或者注销时,系统会通过回调机制将该进程相关信息优先返回给我们自己的函数待处理结束后再转向系统层。
62 0
7.1 Windows驱动开发:内核监控进程与线程回调
|
11月前
|
开发者
驱动开发:基于事件同步的反向通信
在之前的文章中`LyShark`一直都在教大家如何让驱动程序与应用层进行`正向通信`,而在某些时候我们不仅仅只需要正向通信,也需要反向通信,例如杀毒软件如果驱动程序拦截到恶意操作则必须将这个请求动态的转发到应用层以此来通知用户,而这种通信方式的实现有多种,通常可以使用创建Socket套接字的方式实现,亦或者使用本章所介绍的通过`事件同步`的方法实现反向通信。
170 0
|
11月前
|
Windows
驱动开发:内核扫描SSDT挂钩状态
在笔者上一篇文章`《驱动开发:内核实现SSDT挂钩与摘钩》`中介绍了如何对`SSDT`函数进行`Hook`挂钩与摘钩的,本章将继续实现一个新功能,如何`检测SSDT`函数是否挂钩,要实现检测`挂钩状态`有两种方式,第一种方式则是类似于`《驱动开发:摘除InlineHook内核钩子》`文章中所演示的通过读取函数的前16个字节与`原始字节`做对比来判断挂钩状态,另一种方式则是通过对比函数的`当前地址`与`起源地址`进行判断,为了提高检测准确性本章将采用两种方式混合检测。
179 0
|
API
HarmonyOS系统内核中使用事件标志的方法
大家好,今天主要和大家聊一聊,如何利用HarmonyOS系统中事件标志。
219 0
HarmonyOS系统内核中使用事件标志的方法
|
监控
驱动开发:内核注册并监控对象回调
在笔者上一篇文章`《驱动开发:内核枚举进程与线程ObCall回调》`简单介绍了如何枚举系统中已经存在的`进程与线程`回调,本章`LyShark`将通过对象回调实现对进程线程的`句柄`监控,在内核中提供了`ObRegisterCallbacks`回调,使用这个内核`回调`函数,可注册一个`对象`回调,不过目前该函数`只能`监控进程与线程句柄操作,通过监控进程或线程句柄,可实现保护指定进程线程不被终止的目的。
405 0
驱动开发:内核注册并监控对象回调
|
监控
驱动开发:内核监控进程与线程回调
在前面的文章中`LyShark`一直在重复的实现对系统底层模块的枚举,今天我们将展开一个新的话题,内核监控,我们以`监控进程线程`创建为例,在`Win10`系统中监控进程与线程可以使用微软提供给我们的两个新函数来实现,此类函数的原理是创建一个回调事件,当有进程或线程被创建或者注销时,系统会通过回调机制将该进程相关信息优先返回给我们自己的函数待处理结束后再转向系统层。
333 0
驱动开发:内核监控进程与线程回调
|
消息中间件 测试技术 API
FreeRTOS记录(七、FreeRTOS信号量、事件标志组、邮箱和消息队列、任务通知的关系)
我们在前面单独介绍过FreeRTOS的任务通知和消息队列, 但是在FreeRTOS中任务间的通讯还有信号量,邮箱,事件组标志等可以使用 这篇文章就这些成员与消息队列和任务通知的关系进行说明分析
809 0
FreeRTOS记录(七、FreeRTOS信号量、事件标志组、邮箱和消息队列、任务通知的关系)