码迷,mamicode.com
首页 > 其他好文 > 详细

简单的procfs模型

时间:2014-09-10 12:36:10      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   文件   cti   sp   on   c   amp   

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>


static struct proc_dir_entry *root, *jiffies_file, *kbuf_file, *symlink,
*symlink;
//struct fb_data_t foo_data,bar_data;


static ssize_t proc_jiffies_read(struct file * file, char __user * buf,
size_t size, loff_t * offset)
{
if (*offset != 0)
return 0;


*offset += sprintf(buf, "jiffies=%ld\n", jiffies);


return *offset;
}


static int proc_jiffies_open(struct inode * inode, struct file * file)
{
file->private_data = inode->i_private;
return 0;
}


static struct file_operations jiffies_fops =
{ .owner = THIS_MODULE, .open = proc_jiffies_open, .read = proc_jiffies_read, };


static int kbuf_open(struct inode * inode, struct file * file)
{
file->private_data = inode->i_private;
return 0;
}


static char kbuf[128] =
{ 0 };


static ssize_t kbuf_read(struct file * file, char __user * buf, size_t size,
loff_t * offset)
{
if (*offset > 128)
{
return 0;
}


if (*offset + size > 128)
{
size = 128 - *offset;
}


copy_to_user(buf, kbuf + *offset, size);
*offset += size;


return size;


}


static ssize_t kbuf_write(struct file * file, char __user * buf, size_t size,
loff_t * offset)
{
if (*offset > 128)
{
return 0;
}


if (*offset + size > 128)
{
size = 128 - *offset;
}


copy_from_user(kbuf + *offset, buf, size);
*offset += size;


return size;
}


static struct file_operations kbuf_fops =
{ .owner = THIS_MODULE, .open = kbuf_open, .read = kbuf_read, .write =
kbuf_write, };


static int __init porcfs_init(void)
{
printk("###dragon### %s\n",__FUNCTION__);
int ret = 0;
/*在/proc下创建一个根目录*/
root = proc_mkdir("dragon",NULL);
if(root == NULL)
{
ret = -ENOMEM;
goto out;
}


/*创建读取jiffies的proc文件*/
jiffies_file = proc_create("jiffies", 0444, root, &jiffies_fops);
if(jiffies_file==NULL)
{
ret = -ENOMEM;
goto out;
}


kbuf_file = proc_create("kbuf", 0444, root, &kbuf_fops);
if(kbuf_file==NULL)
{
ret = -ENOMEM;
goto out;
}


symlink = proc_symlink("jiffies2",root,"jiffies");
if(symlink == NULL)
{
ret = -ENOMEM;
goto out;
}


return ret;
out:
proc_remove(root);
return ret;
}


static void procfs_exit(void)
{
printk("###dragon###\n", __FUNCTION__);
proc_remove(root);
return;
}


module_init(porcfs_init);
module_exit(procfs_exit);
MODULE_AUTHOR("dragon");
MODULE_LICENSE("GPL");

简单的procfs模型

标签:style   io   ar   文件   cti   sp   on   c   amp   

原文地址:http://blog.csdn.net/dragon101788/article/details/39178621

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!