首页 > 代码库 > 简单的procfs模型

简单的procfs模型

#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 = http://www.mamicode.com/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 = http://www.mamicode.com/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模型