/*
* 被通知者goddess.c
*/
#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>
#include <linux/kthread.h>
#include <linux/delay.h>
MODULE_LICENSE("GPL");
#define PHY_REQ 0 //物质需求
#define SPR_REQ 1 //精神需求
#define REQ_MAX 2
extern void get_random_bytes(void* buf,int nbytes);
static struct task_struct *requirments_thread = NULL;
static RAW_NOTIFIER_HEAD(requirment_chain); //定义一个原始通知链
/*
* 如果谁想追求本女王,就来献殷勤吧
*/
int register_godness_notifier(struct notifier_block *nb)
{
return raw_notifier_chain_register(&requirment_chain, nb);
}
EXPORT_SYMBOL(register_godness_notifier);
/*
* 伺候不起的,赶紧Get out as soon as
*/
int unregister_godness_notifier(struct notifier_block *nb)
{
return raw_notifier_chain_unregister(&requirment_chain, nb);
}
EXPORT_SYMBOL(unregister_godness_notifier);
/*
* 本女王开始提需求了,看看谁能才是真心的。
*/
int call_godness_notifier_chain(unsigned long val, void *v)
{
return raw_notifier_call_chain(&requirment_chain, val, v);
}
EXPORT_SYMBOL(call_godness_notifier_chain);
static int make_requirment_thread(void *data)
{
unsigned int requirment_type = 0;
msleep(10000);
printk("[Godness]requirment type: %d \n",requirment_type);
call_godness_notifier_chain(requirment_type,NULL);
requirment_type = 1;
printk("[Godness]requirment type: %d \n",requirment_type);
call_godness_notifier_chain(requirment_type,NULL);
return 0;
}
static int __init godness_init_notifier(void)
{
printk("[Attention]The Godness coming into the world!\n");
requirments_thread = kthread_run(make_requirment_thread,NULL,"Godness_requirments_thread");
return 0;
}
static void __exit godness_exit_notifier(void)
{
printk("[Attention]The Godness leaving out!\n");
}
module_init(godness_init_notifier);
module_exit(godness_exit_notifier);
/*
* 通知者diors.c
*/
#include <linux/notifier.h>
extern int register_godness_notifier(struct notifier_block*);
extern int unregister_godness_notifier(struct notifier_block*);
static int godness_need_music(struct notifier_block *this, unsigned long event, void *ptr)
{
if(event != 1) //我又没钱,给不了你大房子、气派的车子...
{
return NOTIFY_DONE; //Don't care
}
printk("Diors >>> Hi girl,This is a classic Music disk,take it.\n");
return NOTIFY_OK;
}
static struct notifier_block music_notifier =
{
.notifier_call = godness_need_music,
.priority = 2,
};
static int __init diors_register(void)
{
int err;
printk("Diors --- Diors register music_requirment !!!\n");
err = register_godness_notifier(&music_notifier);
if (err)
{
printk("Refused!\n");
return -1;
}
return err;
}
static void __exit diors_unregister(void)
{
unregister_godness_notifier(&music_notifier);
printk("Diors >>> Tuhao is giving up Godness!(What a pity)\n");
}
module_init(diors_register);
module_exit(diors_unregister);
/*
* 被通知者tuhao.c
*/
#include <linux/notifier.h>
extern int register_godness_notifier(struct notifier_block*);
extern int unregister_godness_notifier(struct notifier_block*);
static int baby_need_money(struct notifier_block *this, unsigned long event, void *ptr)
{
if(event != 0) //不是金钱需求关我鸟事
{
return NOTIFY_DONE; //Don't care
}
printk("Tuhao >>> Hi Baby,$$$$$$$$ 么么哒 \n");
return NOTIFY_OK;
}
static struct notifier_block cash_notifier =
{
.notifier_call = baby_need_money,
.priority = 2,
};
static int __init tuhao_register(void)
{
int err;
printk("Tuhao --- Tuhao register cash_requirment !!!\n");
err = register_godness_notifier(&cash_notifier);
if (err){
printk("Refused!\n");
return -1;
}
return err;
}
static void __exit tuhao_unregister(void)
{
unregister_godness_notifier(&cash_notifier);
printk("Tuhao --- Tuhao is giving up Godness!(Son of bitch)\n");
}
module_init(tuhao_register);
module_exit(tuhao_unregister);
/*
* 被通知者 gfs.c
*/
#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_godness_notifier(struct notifier_block*);
extern int unregister_godness_notifier(struct notifier_block*);
static int sweet_heart_requirments(struct notifier_block *this, unsigned long event, void *ptr)
{
switch(event){
case 0:
printk("GFS >>> Hi honey,the VISA card is ready for you! \n");
break;
case 1:
printk("GFS >>> Hi honey,let me play the piano for you! \n");
break;
default:
break;
}
return 0;
}
static struct notifier_block honey_notifier =
{
.notifier_call = sweet_heart_requirments,
.priority = 5,
};
static int __init GFS_register(void)
{
int err;
printk("GFS --- GFS register honey_requirment !!!\n");
err = register_godness_notifier(&honey_notifier);
if (err){
printk("Refused!\n");
return -1;
}
return err;
}
/*
* 卸载刚刚注册了的通知链
*/
static void __exit GFS_unregister(void)
{
unregister_godness_notifier(&honey_notifier);
printk("GFS >>> GFS broke up with Godness!(How sadness)\n");
}
module_init(GFS_register);
module_exit(GFS_unregister);
/*
* 测试
*/
[root@localhost]# insmod goddess.ko;insmod diors.ko;insmod gfs.ko ;insmod tuhao.ko
[root@localhost]# rmmod goddess diors gfs tuhao
[root@localhost]# dmesg
[Attention]The Godness coming into the world!
Diors --- Diors register music_requirment !!!
GFS --- GFS register honey_requirment !!!
Tuhao --- Tuhao register cash_requirment !!!
[Godness]requirment type: 0
GFS >>> Hi honey,the VISA card is ready for you!
Tuhao >>> Hi Baby,$$$$$$$$ 么么哒
[Godness]requirment type: 1
GFS >>> Hi honey,let me play the piano for you!
Diors >>> Hi girl,This is a classic Music disk,take it.
Diors >>> Tuhao is giving up Godness!(What a pity)
Tuhao >>> Tuhao is giving up Godness!(Son of bitch)
GFS >>> GFS broke up with Godness!(How sadness)
[Attention]The Godness leaving out!
阅读(699) | 评论(0) | 转发(0) |