linux下notify机制(仅用于内核模块之间的通信)
1.通知链表简介
大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣。为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子系统,Linux内核提供了通知链的机制。通知链表只能够在内核的子系统之间使用,而不能够在内核与用户空间之间进行事件的通知。
通知链表是一个函数链表,链表上的每一个节点都注册了一个函数。当某个事情发生时,链表上所有节点对应的函数就会被执行。所以对于通知链表来说有一个通知方与一个接收方。在通知这个事件时所运行的函数由被通知方决定,实际上也即是被通知方注册了某个函数,在发生某个事件时这些函数就得到执行。其实和系统调用signal的思想差不多。
2.通知链表数据结构
通知链表的节点类型为notifier_block,其定义如下:
- struct notifier_block
- {
- int (*notifier_call)(struct notifier_block *self, unsigned long, void *);
- struct notifier_block *next;
- int priority;
- };
复制代码
其中最重要的就是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指向的链表上的所有节点上注册的函数。简单地说,如下所示:
- struct notifier_block *nb = *n;
- while (nb)
- {
- ret = nb->notifier_call(nb, val, v);
- if (ret & NOTIFY_STOP_MASK)
- {
- return ret;
- }
- nb = nb->next;
- }
复制代码
5.示例
在这里,写了一个简单的通知链表的代码。
实际上,整个通知链的编写也就两个过程:
首先是定义自己的通知链的头节点,并将要执行的函数注册到自己的通知链中。
其次则是由另外的子系统来通知这个链,让其上面注册的函数运行。
我这里将第一个过程分成了两步来写,第一步是定义了头节点和一些自定义的注册函数(针对该头节点的),第二步则是使用自定义的注册函数注册了一些通知链节点。分别在代码buildchain.c与regchain.c中。
发送通知信息的代码为notify.c。
代码1 buildchain.c
它的作用是自定义一个通知链表test_chain,然后再自定义两个函数分别向这个通知链中加入或删除节点,最后再定义一个函数通知这个test_chain链。
- #include <asm/uaccess.h>
- #include <linux/types.h>
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/notifier.h>
- #include <linux/init.h>
- #include <linux/types.h>
- #include <linux/module.h>
- MODULE_LICENSE("GPL");
- /*
- * 定义自己的通知链头结点以及注册和卸载通知链的外包函数
- */
- /*
- * RAW_NOTIFIER_HEAD是定义一个通知链的头部结点,
- * 通过这个头部结点可以找到这个链中的其它所有的notifier_block
- */
- static RAW_NOTIFIER_HEAD(test_chain);
- /*
- * 自定义的注册函数,将notifier_block节点加到刚刚定义的test_chain这个链表中来
- * raw_notifier_chain_register会调用notifier_chain_register
- */
- int register_test_notifier(struct notifier_block *nb)
- {
- return raw_notifier_chain_register(&test_chain, nb);
- }
- EXPORT_SYMBOL(register_test_notifier);
- int unregister_test_notifier(struct notifier_block *nb)
- {
- return raw_notifier_chain_unregister(&test_chain, nb);
- }
- EXPORT_SYMBOL(unregister_test_notifier);
- /*
- * 自定义的通知链表的函数,即通知test_chain指向的链表中的所有节点执行相应的函数
- */
- int test_notifier_call_chain(unsigned long val, void *v)
- {
- return raw_notifier_call_chain(&test_chain, val, v);
- }
- EXPORT_SYMBOL(test_notifier_call_chain);
- /*
- * init and exit
- */
- static int __init init_notifier(void)
- {
- printk("init_notifier\n");
- return 0;
- }
- static void __exit exit_notifier(void)
- {
- printk("exit_notifier\n");
- }
- module_init(init_notifier);
- module_exit(exit_notifier);
复制代码
代码2 regchain.c
该代码的作用是将test_notifier1 test_notifier2 test_notifier3这三个节点加到之前定义的test_chain这个通知链表上,同时每个节点都注册了一个函数。
- #include <asm/uaccess.h>
- #include <linux/types.h>
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/notifier.h>
- #include <linux/init.h>
- #include <linux/types.h>
- #include <linux/module.h>
- MODULE_LICENSE("GPL");
- /*
- * 注册通知链
- */
- extern int register_test_notifier(struct notifier_block*);
- extern int unregister_test_notifier(struct notifier_block*);
- static int test_event1(struct notifier_block *this, unsigned long event, void *ptr)
- {
- printk("In Event 1: Event Number is %d\n", event);
- return 0;
- }
- static int test_event2(struct notifier_block *this, unsigned long event, void *ptr)
- {
- printk("In Event 2: Event Number is %d\n", event);
- return 0;
- }
- static int test_event3(struct notifier_block *this, unsigned long event, void *ptr)
- {
- printk("In Event 3: Event Number is %d\n", event);
- return 0;
- }
- /*
- * 事件1,该节点执行的函数为test_event1
- */
- static struct notifier_block test_notifier1 =
- {
- .notifier_call = test_event1,
- };
- /*
- * 事件2,该节点执行的函数为test_event1
- */
- static struct notifier_block test_notifier2 =
- {
- .notifier_call = test_event2,
- };
- /*
- * 事件3,该节点执行的函数为test_event1
- */
- static struct notifier_block test_notifier3 =
- {
- .notifier_call = test_event3,
- };
- /*
- * 对这些事件进行注册
- */
- static int __init reg_notifier(void)
- {
- int err;
- printk("Begin to register:\n");
- err = register_test_notifier(&test_notifier1);
- if (err)
- {
- printk("register test_notifier1 error\n");
- return -1;
- }
- printk("register test_notifier1 completed\n");
- err = register_test_notifier(&test_notifier2);
- if (err)
- {
- printk("register test_notifier2 error\n");
- return -1;
- }
- printk("register test_notifier2 completed\n");
- err = register_test_notifier(&test_notifier3);
- if (err)
- {
- printk("register test_notifier3 error\n");
- return -1;
- }
- printk("register test_notifier3 completed\n");
- return err;
- }
- /*
- * 卸载刚刚注册了的通知链
- */
- static void __exit unreg_notifier(void)
- {
- printk("Begin to unregister\n");
- unregister_test_notifier(&test_notifier1);
- unregister_test_notifier(&test_notifier2);
- unregister_test_notifier(&test_notifier3);
- printk("Unregister finished\n");
- }
- module_init(reg_notifier);
- module_exit(unreg_notifier);
复制代码
代码3 notify.c
该代码的作用就是向test_chain通知链中发送消息,让链中的函数运行。
- #include <asm/uaccess.h>
- #include <linux/types.h>
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/notifier.h>
- #include <linux/init.h>
- #include <linux/types.h>
- #include <linux/module.h>
- MODULE_LICENSE("GPL");
- 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
==============================
linux下notify机制(仅用于内核模块之间的通信)的更多相关文章
- 20155202 张旭 课下作业: Linux下IPC机制
20155202张旭 Linux下IPC机制 IPC机制定义 在linux下的多个进程间的通信机制叫做IPC(Inter-Process Communication),它是多个进程之间相互沟通的一种方 ...
- 20155239吕宇轩 Linux下IPC机制
20155239吕宇轩 Linux下IPC机制 - 共享内存 原理:把所有需要使用的共享数据都存放在共享内存 区域中,任何想要访问这些共享数据的进程都必须在自己的进程地址空间中新增加一块内存区域,用来 ...
- Linux下IPC机制
Linux下IPC机制 实践要求 研究Linux下IPC机制:原理,优缺点,每种机制至少给一个示例,提交研究博客的链接 共享内存 管道 FIFO 信号 消息队列 IPC 进程间通信(IPC,Inter ...
- 【Linux下进程机制】从一道面试题谈linux下fork的运行机制
今天一位朋友去一个不错的外企面试linux开发职位,面试官出了一个如下的题目: 给出如下C程序,在linux下使用gcc编译: #include "stdio.h" #includ ...
- linux下二机制文件的查看和编辑
linux下很多命令都是二机制:/bin/下的各种命令---/bin/sh./bin/cat./bin/dmesg./bin/hostname等 如何查看这些二机制文件: xxd.hexdump 参考 ...
- [转]linux 下 使用 c / c++ 调用curl库 做通信开发
example: 1. http://curl.haxx.se/libcurl/c/example.html 2. http://www.libcurl.org/book: 1. http:/ ...
- Linux的rsync 配置,用于服务器之间远程传大量的数据
[教程主题]:rsync [课程录制]: 创E [主要内容] [1] rsync介绍 Rsync(Remote Synchronize) 是一个远程资料同步工具,可通过LAN/WAN快速同步多台主机, ...
- 网络篇:linux下select、poll、epoll之间的区别总结
select.poll.epoll之间的区别总结 select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪 ...
- Linux下fork机制详解(以PHP为例)
考:https://blog.csdn.net/jason314/article/details/5640969 1.fork简介 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统 ...
随机推荐
- PHP CALC
<html> <head> <title>PHP计算器</title> <meta http-equiv="Content-Type&q ...
- 突破XSS字符数量限制执行任意JS代码
一.综述 有些XSS漏洞由于字符数量有限制而没法有效的利用,只能弹出一个对话框来YY,本文主要讨论如何突破字符数量的限制进行有效的利用,这里对有效利用的定义是可以不受限制执行任意JS.对于跨站师们来说 ...
- Swift语言简介+快速上手
相关: Xcode 6 beta:https://developer.apple.com/xcode/downloads/ swift语言学习文档英文版:http://pan.baidu.com/s/ ...
- commons-logging和Log4j 日志管理/log4j.properties配置详解
commons-logging和Log4j 日志管理 (zz) 什么要用日志(Log)? 这个……就不必说了吧. 为什么不用System.out.println()? 功能太弱:不易于控制.如果暂时不 ...
- Linux的一个暴力破解工具九头蛇hydra
首先还是书写本文的 参考档:http://www.cnblogs.com/mchina/archive/2013/01/01/2840815.html 工具介绍:原文为官方英文解释本人给翻译下 数量最 ...
- javascript中parentNode,childNodes,children的应用详解
本篇文章是对javascript中parentNode,childNodes,children的应用进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 "parentNode&qu ...
- java笔记--使用线程池优化多线程编程
使用线程池优化多线程编程 认识线程池 在Java中,所有的对象都是需要通过new操作符来创建的,如果创建大量短生命周期的对象,将会使得整个程序的性能非常的低下.这种时候就需要用到了池的技术,比如数据库 ...
- ADF成长记1--认识ADF
2014-07-08 近段时间由于公司项目需要,开始接触Oracle ADF.都说有事没事,上百度,但是对于IT技术而言,上百度还真是不一定好使,至于谷歌嘛,很不巧的进不去了.不过网上ADF的资料当真 ...
- HDU 1029 Ignatius and the Princess IV
解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...
- gem install factory_girl
文章是从个人博客转过来的, 可以直接访问 iwangzheng.com https://github.com/thoughtbot/factory_girl https://github.com/t ...