Linux内核notify通知笔录

前沿

本篇文章围绕三个问题来讲述,并解释案例代码演示,从而更进一步的了解该机制。

1.Notify机制是什么?

notify是一种内核子模块相互之间通信一种机制。如设备状态变化,电源事件等场景。另外它是一种通过事件驱动的通信机制,允许某个组建在特定事件发生时主动通知其他组件。内核中依据不同的上下文存在多种类型的形式,每种形式都有其特定的功能应用场景。

2.Notify实现原理?

该机制模式就好比发布订阅机制一样,发布者发布消息,订阅者订阅相关的数信息。当有事件发生时,通过通知链调用订阅者提前注册的数据机制,然后订阅者收到对应的消息处理。有中断触发方式,上下文触发方式。

3.Notify内核中如何使用?

发布消息:

cpp 复制代码
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>

// 定义全局通知链头
BLOCKING_NOTIFIER_HEAD(my_notifier_chain);
EXPORT_SYMBOL(my_notifier_chain);  // 确保正确导出

// 发送通知函数
int send_notification(unsigned long val, void *v)
{

    printk(KERN_INFO "MODULE A: Notifier chain head: %px\n", my_notifier_chain.head);
    if (my_notifier_chain.head == NULL) {
        printk(KERN_ERR "MODULE A: Notifier chain is empty!\n");
    }
    printk(KERN_INFO "MODULE A: Sending notification (val=%lu)\n", val);
    int ret = blocking_notifier_call_chain(&my_notifier_chain, val, v);
    printk(KERN_INFO "MODULE A: Notification result: %d\n", ret);
    return ret;
}
EXPORT_SYMBOL(send_notification);

// 添加设备文件操作以手动触发通知
static ssize_t trigger_write(struct file *filp, const char __user *buf, size_t count, loff_t *off)
{
    char msg[100];
    if (copy_from_user(msg, buf, count))
        return -EFAULT;
    msg[count] = '\0';
    
    send_notification(100, msg);
    return count;
}

static struct file_operations fops = {
    .write = trigger_write,
};

static int major;

static int __init module_a_init(void)
{
    major = register_chrdev(0, "notify_trigger", &fops);
    printk(KERN_INFO "MODULE A: Loaded (major=%d)\n", major);
    printk(KERN_INFO "MODULE A: Use 'echo \"message\" > /dev/notify_trigger' to test\n");
    return 0;
}

static void __exit module_a_exit(void)
{
    unregister_chrdev(major, "notify_trigger");
    printk(KERN_INFO "MODULE A: Unloaded\n");
}

module_init(module_a_init);
module_exit(module_a_exit);
MODULE_LICENSE("GPL");

订阅消息:

cpp 复制代码
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/kernel.h>

// 声明外部符号
extern struct blocking_notifier_head my_notifier_chain;

// 回调函数
static int my_notifier_call(struct notifier_block *nb, 
                          unsigned long val, void *v)
{
    printk(KERN_INFO "MODULE B: ===== NOTIFICATION RECEIVED =====\n");
    printk(KERN_INFO "MODULE B: Event value: %lu\n", val);
    printk(KERN_INFO "MODULE B: Message: %s\n", (char *)v);
    printk(KERN_INFO "MODULE B: ===== END NOTIFICATION =====\n");
    return NOTIFY_OK;
}

// 定义通知块
static struct notifier_block my_notifier = {
    .notifier_call = my_notifier_call,
    .priority = 0,
};

static int __init module_b_init(void)
{
    int ret;
    
    // 注册通知回调
    ret = blocking_notifier_chain_register(&my_notifier_chain, &my_notifier);
    
    if (ret) {
        printk(KERN_ERR "MODULE B: Registration failed: %d\n", ret);
        return ret;
    }
    
    printk(KERN_INFO "MODULE B: Successfully registered notifier\n");
    printk(KERN_INFO "MODULE B: Current notifier chain: %px\n", &my_notifier_chain);
    return 0;
}

static void __exit module_b_exit(void)
{
    blocking_notifier_chain_unregister(&my_notifier_chain, &my_notifier);
    printk(KERN_INFO "MODULE B: Unregistered notifier\n");
}

module_init(module_b_init);
module_exit(module_b_exit);
MODULE_LICENSE("GPL");
MODULE_SOFTDEP("pre: module_a");

Makefile:

cpp 复制代码
obj-m := module_a.o module_b.o

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all:
	$(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean

按照先写入module_a之后在写入module_b,然后在系统/dev目录下创建设备,之后通过该对应的设备填充信息触发a模块调用通知链从而b模块收到对应的消息。如下输入日志:

cpp 复制代码
[36699.868674] module_a: loading out-of-tree module taints kernel.
[36699.869060] module_a: module verification failed: signature and/or required key missing - tainting kernel
[36699.871047] MODULE A: Loaded (major=242)
[36699.871048] MODULE A: Use 'echo "message" > /dev/notify_trigger' to test
[36705.974380] MODULE B: Successfully registered notifier
[36705.974381] MODULE B: Current notifier chain: ffffffffc09300e0
[36763.437614] MODULE A: Notifier chain head: ffffffffc094d000
[36763.437616] MODULE A: Sending notification (val=100)
[36763.437617] MODULE B: ===== NOTIFICATION RECEIVED =====
[36763.437617] MODULE B: Event value: 100
[36763.437618] MODULE B: Message: Hello World

[36763.437618] MODULE B: ===== END NOTIFICATION =====
[36763.437619] MODULE A: Notification result: 1

以上就是notify学习笔记,后续还会更新内核源代码详细实现过程!

相关推荐
珹洺6 分钟前
MyBatis实战指南(三)MyBatis常用配置详解(XML配置,环境配置,类型别名,属性与映射器)
xml·java·运维·数据库·sql·tomcat·mybatis
巨龙之路6 分钟前
【TDengine源码阅读】taosMemoryDbgInit函数
大数据·linux·c语言·tdengine
Watink Cpper1 小时前
[Protobuf] 快速上手:安全高效的序列化指南
linux·开发语言·protobuf
Z_z在努力2 小时前
【RabbitMQ运维】集群搭建
运维·分布式·rabbitmq
m0_719084112 小时前
nginx的一些配置的意思
运维·服务器·nginx
Space Chars6 小时前
【大前端】使用NodeJs HTTP模块创建web服务器、SSE通讯
服务器·前端·http
zym大哥大7 小时前
网络层IP协议
linux·服务器·网络
长天一色7 小时前
Docker 镜像调试最佳实践
运维·docker·容器
五花肉村长7 小时前
Linux-读者写著问题和读写锁
linux·运维·服务器·开发语言·数据库·visualstudio
凯雀安全8 小时前
在kali中搞个jdk1.8.,又不破坏环境
linux·运维·服务器