#include
#include
#include
#include
#include
// 定义存储写入信息的结构体
struct priv_dev {
struct proc_dir_entry *proc;
char *buffer;
};
struct priv_dev *dev;
//proc读
int proc_read(char *page,char **start,off_t off,int count,int *eof,void *data)
{
return sprintf(page,"%s",dev->buffer);
}
//proc写
int proc_write(struct file *file,const char __user *buffer,unsigned long count,void *data)
{
if(copy_from_user(dev->buffer,buffer,count)) {
return -EFAULT;
}
return count;
}
static int __init my_init(void)
{
int ret;
dev = kzalloc(sizeof(*dev),GFP_KERNEL);
if(!dev) {
ret = -ENOMEM;
goto e_mem;
}
dev->buffer = kzalloc(0X1000,GFP_KERNEL);
if(!dev->buffer) {
ret = - ENOMEM;
goto e_buffer;
}
//创建具有读写权限的proc文件
dev->proc = create_proc_entry("my_proc",0644,NULL);
if(!dev->proc) {
ret = -EBUSY;
goto e_proc;
}
//proc操作映射到我们自己定义的函数
dev->proc->read_proc = proc_read;
dev->proc->write_proc = proc_write;
return 0;
e_proc:
kfree(dev->buffer);
e_buffer:
kfree(dev);
e_mem:
return ret;
}
//模块推出
static void __exit my_exit(void)
{
remove_proc_entry("my_proc",NULL);
kfree(dev->buffer);
kfree(dev);
}
//模块初始化
module_init(my_init);
module_exit(my_exit);
阅读(4592) | 评论(0) | 转发(0) |