内核通知链例子

来源:互联网 发布:sql having用法 编辑:程序博客网 时间:2024/05/19 20:45

如果模块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_OBJDBG_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) modulesmodules_install:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_installclean:rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions modules.order  Module.symvers app.PHONY: modules modules_install cleanelseobj-m+= notifier_chain.oobj-m+= consumer.oobj-m+= producer.oendif


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

0 0
原创粉丝点击