add a kernel module notify chain example, compile the buildchain first before regchain

This commit is contained in:
zeroway.lin
2016-07-25 11:21:34 +00:00
parent 3a9d32c22e
commit e0811ce35d
4 changed files with 151 additions and 1 deletions

View File

@ -1 +1,4 @@
obj-m += myled.o
obj-y += myled.o
obj-m += mycpuinfo.o
obj-m += hello.o
obj-m += buildchain.o regchain.o notify.o

37
debug/buildchain.c Normal file
View File

@ -0,0 +1,37 @@
#include <linux/module.h>
#include <linux/init.h>
#include <linux/notifier.h>
static RAW_NOTIFIER_HEAD(test_chain);
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);
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);
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);
MODULE_LICENSE("GPL");

27
debug/notify.c Normal file
View File

@ -0,0 +1,27 @@
#include <linux/module.h>
#include <linux/init.h>
#include <linux/notifier.h>
extern int test_notifier_call_chain(unsigned long val, void*v);
static int __init call_notifier(void)
{
int err;
printk("Begin to notify:\n");
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("Endnotify\n");
}
module_init(call_notifier);
module_exit(uncall_notifier);
MODULE_LICENSE("GPL");

83
debug/regchain.c Normal file
View File

@ -0,0 +1,83 @@
#include <linux/module.h>
#include <linux/init.h>
#include <linux/notifier.h>
extern int unregister_test_notifier(struct notifier_block*);
extern int register_test_notifier(struct notifier_block *nb);
static int test_event1(struct notifier_block *this, unsigned long event, void *ptr)
{
printk("In Event 1: Event Number is %ld\n",event);
return 0;
}
static int test_event2(struct notifier_block *this, unsigned long event, void *ptr)
{
printk("In Event 2: Event Number is %ld\n",event);
return 0;
}
static int test_event3(struct notifier_block *this, unsigned long event, void *ptr)
{
printk("In Event 3: Event Number is %ld\n",event);
return 0;
}
static struct notifier_block test_notifier1 =
{
.notifier_call = test_event1,
};
static struct notifier_block test_notifier2 =
{
.notifier_call = test_event2,
};
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);
MODULE_LICENSE("GPL");