内核通知链例子

如果模块A要发信息给模块B,可采用内核通知链的方式,这里提供最简单的例子。

notifier_chain.c

#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/export.h>
static BLOCKING_NOTIFIER_HEAD(test_chain_head);

int register_test_notifier(struct notifier_block *nb)
{
    return blocking_notifier_chain_register(&test_chain_head, nb);
}
EXPORT_SYMBOL(register_test_notifier);

int unregister_test_notifier(struct notifier_block *nb)
{
    return blocking_notifier_chain_unregister(&test_chain_head, nb);
}
EXPORT_SYMBOL(unregister_test_notifier);

int test_notifier_call_chain(unsigned long val)
{
    int ret;
    ret = blocking_notifier_call_chain(&test_chain_head, val, NULL);
    return notifier_to_errno(ret);
}
EXPORT_SYMBOL(test_notifier_call_chain);

MODULE_LICENSE("Dual BSD/GPL");


notifier_chain.h

#ifndef _NOTIFIER_CHAIN_H_
#define _NOTIFIER_CHAIN_H_
extern int register_test_notifier(struct notifier_block *nb);
extern int unregister_test_notifier(struct notifier_block *nb);
extern int test_notifier_call_chain(unsigned long val);
#endif


发信息producer.c

#include <linux/module.h>
#include <linux/delay.h>
#include "notifier_chain.h"

static int producer_init(void) 
{ 
	printk(KERN_INFO "%s\n",__FUNCTION__);
	test_notifier_call_chain(1);
	msleep(1000);
	test_notifier_call_chain(1000);
	return 0; 
} 
     
static void producer_exit(void) 
{      
	printk(KERN_INFO "%s\n",__FUNCTION__);
} 
     
module_init(producer_init); 
module_exit(producer_exit); 
MODULE_LICENSE("Dual BSD/GPL");

接收信息consumer.c

#include <linux/module.h>
#include "notifier_chain.h"

static int test_notify(struct notifier_block *self, unsigned long action, void *dev)
{
	switch (action) {
	case 1:
			printk("action=%ld\n",action);
			
		break;
	case 1000:
			printk("action=%ld\n",action);
		break;
	}
	return NOTIFY_OK;
}

static struct notifier_block test_nb = {
	.notifier_call = test_notify,
};

static int consumer_init(void) 
{ 
	printk(KERN_INFO "%s\n",__FUNCTION__);
	register_test_notifier(&test_nb);
	return 0;
}
static void consumer_exit(void) 
{      
	printk(KERN_INFO "%s\n",__FUNCTION__);
	unregister_test_notifier(&test_nb);
} 
     
module_init(consumer_init); 
module_exit(consumer_exit);  
MODULE_LICENSE("Dual BSD/GPL");

相应的Makefile为

ifeq ($(KERNELRELEASE),)

KERNELDIR_OUT ?= /home/w/M8974AAAAANLYD4275/out/target/product/msm8974/obj/KERNEL_OBJ
DBG_CROSS_COMPILE ?= /home/w/M8974AAAAANLYD4275/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-
PWD :=$(shell pwd)
modules:
	$(MAKE) -C $(KERNELDIR_OUT) M=$(PWD) ARCH=arm CROSS_COMPILE=$(DBG_CROSS_COMPILE) modules
modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions modules.order  Module.symvers app
.PHONY: modules modules_install clean
else
	obj-m+= notifier_chain.o
	obj-m+= consumer.o
	obj-m+= producer.o
endif


先insmod notifier_chain.ko,consumer.ko,再insmod producer.ko,就可从打印信息中看到producer发给consumer的信息了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值