對於如何維護procfs中的position是有些困擾的(超過一頁的顯示時),所以後來有了seq_file,提供了iterator的interface來讀取資料,最大的好處是,position的定義就可以由programmer自行決定,比如position N是讀取第N行之類的。我們將改寫Linux Modules(III)- procfs的範例一來作為我們seq_file的例子。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
MODULE_LICENSE("GPL");
#define MAX_LINE 1000
static uint32_t *lines;
/**
* seq_start() takes a position as an argument and returns an iterator which
* will start reading at that position.
*/
static void* seq_start(struct seq_file *s, loff_t *pos)
{
uint32_t *lines;
if (*pos >= MAX_LINE) {
return NULL; // no more data to read
}
lines = kzalloc(sizeof(uint32_t), GFP_KERNEL);
if (!lines) {
return NULL;
}
*lines = *pos + 1;
return lines;
}
/**
* move the iterator forward to the next position in the sequence
*/
static void* seq_next(struct seq_file *s, void *v, loff_t *pos)
{
uint32_t *lines = v;
*pos = ++(*lines);
if (*pos >= MAX_LINE) {
return NULL; // no more data to read
}
return lines;
}
/**
* stop() is called when iteration is complete (clean up)
*/
static void seq_stop(struct seq_file *s, void *v)
{
kfree(v);
}
/**
* success return 0, otherwise return error code
*/
static int seq_show(struct seq_file *s, void *v)
{
seq_printf(s, "Line #%d: This is Brook's demo\n", *((uint32_t*)v));
return 0;
}
static struct seq_operations seq_ops = {
.start = seq_start,
.next = seq_next,
.stop = seq_stop,
.show = seq_show
};
static int proc_open(struct inode *inode, struct file *file)
{
return seq_open(file, &seq_ops);
}
static struct file_operations proc_ops = {
.owner = THIS_MODULE, // system
.open = proc_open,
.read = seq_read, // system
.llseek = seq_lseek, // system
.release = seq_release // system
};
static int __init init_modules(void)
{
struct proc_dir_entry *ent;
ent = create_proc_entry("brook", 0, NULL);
if (ent) {
ent->proc_fops = &proc_ops;
}
return 0;
}
static void __exit exit_modules(void)
{
if (lines) {
kfree(lines);
}
remove_proc_entry("brook", NULL);
}
module_init(init_modules);
module_exit(exit_modules);
首先,我們要實作start(),其主要功能就是回傳一個指向要讀取位置(position)的iterator。在我們的範例中,由於position是由0開始,而我們打算由line 1開始列印。
void * (*start) (struct seq_file *m, loff_t *pos);
接著我們要實作next(),主要功能為移動iterator到下一個位置去。在我們的範例中的iterator是指第N行,所以一到下一個iteraot就是把行數加一。
void * (*next) (struct seq_file *m, void *v, loff_t *pos);
最後要實作stop(),主要功能為當完成讀取後要執行的function,簡單來說就是cleanup。在我們的範例中,就是釋放start()所要求的resource。
void (*stop) (struct seq_file *m, void *v);
顯示的callback function為show(),要注意的是,在顯示資料的function必須使用seq_printf(),參數和printk()一樣,這樣就完成所以的function的實做了,接下來就是和procfs綁在一起了。
int (*show) (struct seq_file *m, void *v);
首先我們必須宣告struct seq_operations,並且填入我們剛剛實作的seq operations。
static struct seq_operations seq_ops = {
.start = seq_start,
.next = seq_next,
.stop = seq_stop,
.show = seq_show
};
並且呼叫seq_open()將file和seq_operatio綁在一起。
static int proc_open(struct inode *inode, struct file *file)
{
return seq_open(file, &seq_ops);
}
最後在填入file_operation,由於我們使用的是seq_file,所以,read()/llseek()/release()都只要使用seq_file提供的operations即可。
static struct file_operations proc_ops = {
.owner = THIS_MODULE, // system
.open = proc_open,
.read = seq_read, // system
.llseek = seq_lseek, // system
.release = seq_release // system
};

沒有留言:
張貼留言