参考文章:
·Proc文件系统
内核proc文件系统与seq接口(4)---seq_file接口编程浅析
-
/********************* proc文件系统 *************************/
-
-
static void *scull_seq_start(struct seq_file *s, loff_t *pos)
-
{
-
if (*pos >= MEMDEV_NR_DEVS)
-
return NULL; /* No more to read */
-
return my_devices + *pos;
-
}
-
-
static void *scull_seq_next(struct seq_file *s, void *v, loff_t *pos)
-
{
-
(*pos)++;
-
if (*pos >= MEMDEV_NR_DEVS)
-
return NULL;
-
return my_devices + *pos;
-
}
-
static void scull_seq_stop(struct seq_file *s, void *v)
-
{ /* Actually, there's nothing to do here */}
-
-
-
static int scull_seq_show(struct seq_file *s, void *v)
-
{
-
struct mem_dev *dev = (struct mem_dev *) v;
-
-
seq_printf(s, "%d-%d: %s\n",MAJOR((dev->cdev).dev),MINOR((dev->cdev).dev),dev->data);
-
-
return 0;
-
}
-
-
static struct seq_operations mem_seq_ops = {
-
.start = scull_seq_start,
-
.next = scull_seq_next,
-
.stop = scull_seq_stop,
-
.show = scull_seq_show
-
};
-
-
-
static int mem_proc_open(struct inode *inode, struct file *file)
-
{
-
return seq_open(file, &mem_seq_ops);
-
}
-
-
static struct file_operations mem_proc_ops = {
-
.owner = THIS_MODULE,
-
.open = mem_proc_open, /* 只要实现这个函数 */
-
.read = seq_read,
-
.llseek = seq_lseek,
-
.release = seq_release
-
};
-
-
static struct proc_dir_entry *mydir, *pfile;
-
-
static int mem_create_proc(void)
-
{
-
mydir = proc_mkdir("mydir", NULL); // 创建目录
-
if (!mydir) {
-
printk(KERN_ERR "Can't create /proc/mydir\n");
-
return -1;
-
}
-
-
pfile = create_proc_entry("pool", 0666, mydir); // 创建文件
-
if (!pfile) {
-
printk(KERN_ERR "Can't create /proc/mydir/pool\n");
-
remove_proc_entry("mydir", NULL);
-
-
return -1;
-
}
-
else
-
pfile->proc_fops = &mem_proc_ops;
-
-
return 0;
-
}
-
-
static void mem_remove_proc(void)
-
{
-
remove_proc_entry("pool", mydir);
-
remove_proc_entry("mydir", NULL);
-
}
源码: test.rar
阅读(160) | 评论(0) | 转发(0) |