http://blog.chinaunix.net/uid-24432676-id-2607766.html
另
http://www.cnblogs.com/qq78292959/archive/2012/06/13/2547335.html
Seq_file文件系统实例剖析的两篇文章 2011-05-07 21:51:29
分类: LINUX
文章一:
Seq_file File System
针对proc文件的不足而诞生了Seq_file。
Seq_file的实现基于proc文件。使用Seq_file,用户必须抽象出一个链接对象,然后可以依次遍历这个链接对象。这个链接对象可以是链表,数组,哈希表等等。
编程接口
Seq_file必须实现四个操作函数:start(), next(), show(), stop()。
struct seq_operations {
void * (*start) (struct seq_file *m, loff_t *pos);
void (*stop) (struct seq_file *m, void *v);
void * (*next) (struct seq_file *m, void *v, loff_t *pos);
int (*show) (struct seq_file *m, void *v);
};
start():
主要实现初始化工作,在遍历一个链接对象开始时,调用。返回一个链接对象的偏移或者SEQ_START_TOKEN(表征这是所有循环的开始)。出错返回ERR_PTR。
stop():
当所有链接对象遍历结束时调用。主要完成一些清理工作。
next():
用来在遍历中寻找下一个链接对象。返回下一个链接对象或者NULL(遍历结束)。
show():
对遍历对象进行操作的函数。主要是调用seq_printf(), seq_puts()之类的函数,打印出这个对象节点的信息。
下图描述了seq_file函数对一个链表的遍历。
2、重要的数据结构
除了struct seq_operations以外,另一个最重要的数据结构是struct seq_file:
struct seq_file {
char *buf;
size_t size;
size_t from;
size_t count;
loff_t index;
u64 version;
struct mutex lock;
const struct seq_operations *op;
void *private;
};
该结构会在seq_open函数调用中分配,然后作为参数传递给每个seq_file的操作函数。Privat变量可以用来在各个操作函数之间传递参数。
3、Seq_file使用示例:
#include /* for use of init_net*/
#include /* We‘re doing kernel work */
#include /* Specifically, a module */
#include /* Necessary because we use proc fs */
#include /* for seq_file */
#define PROC_NAME "my_seq_proc"
MODULE_AUTHOR("Dreamice:
[email protected]
");
MODULE_LICENSE("GPL");
static void *my_seq_start(struct seq_file *s, loff_t *pos)
{
static unsigned long counter = 0;
printk(KERN_INFO"Invoke start\n");
/* beginning a new sequence ? */
if ( *pos == 0 )
{
/* yes => return a non null value to begin the sequence */
printk(KERN_INFO"pos == 0\n");
return &counter;
}
else
{
/* no => it‘s the end of the sequence, return end to stop reading */
*pos = 0;
printk(KERN_INFO"pos != 0\n");
return NULL;
}
}
static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
unsigned long *tmp_v = (unsigned long *)v;
printk(KERN_INFO"Invoke next\n");
(*tmp_v)++;
(*pos)++;
return NULL;
}
static void my_seq_stop(struct seq_file *s, void *v)
{
printk(KERN_INFO"Invoke stop\n");
/* nothing to do, we use a static value in start() */
}
static int my_seq_show(struct seq_file *s, void *v)
{
printk(KERN_INFO"Invoke show\n");
loff_t *spos = (loff_t *) v;
seq_printf(s, "%Ld\n", *spos);
return 0;
}
static struct seq_operations my_seq_ops = {
.start = my_seq_start,
.next = my_seq_next,
.stop = my_seq_stop,
.show = my_seq_show
};
static int my_open(struct inode *inode, struct file *file)
{
return seq_open(file, &my_seq_ops);
};
static struct file_operations my_file_ops = {
.owner = THIS_MODULE,
.open = my_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release
};
int init_module(void)
{
struct proc_dir_entry *entry;
entry = create_proc_entry(PROC_NAME, 0, init_net.proc_net);
if (entry) {
entry->proc_fops = &my_file_ops;
}
printk(KERN_INFO"Initialze my_seq_proc success!\n");
return 0;
}
/**
* This function is called when the module is unloaded.
*
*/
void cleanup_module(void)
{
remove_proc_entry(PROC_NAME, init_net.proc_net);
printk(KERN_INFO"Remove my_seq_proc success!\n");
}
该程序在/proc/net下注册一个my_seq_proc文件。有兴趣的朋友可以测试一下。
总结待续
[ 本帖最后由 dreamice 于 2008-11-13 21:03 编辑 ]
seq.jpg
(10.69 KB) 2008-11-13 18:04
执行流程图
文章二
内容简介: 本 UNIX 为了能给大家一个具体形象的认识,我们首先来看一段用序列文件接口通过proc文件导出内核双向循环链接表的实例代码: #include <linux/kernel.h> static struct mutex lock; static void add_one(void) mutex_lock(&lock); static ssize_t _seq_write(struct file *file, const char __user * buffer, static int _seq_show(struct seq_file *m, void *p) seq_printf(m, "value: %d\n", data->value); static void *_seq_start(struct seq_file *m, loff_t *pos) static void *_seq_next(struct seq_file *m, void *p, loff_t *pos) static void _seq_stop(struct seq_file *m, void *p) static struct seq_operations _seq_ops = { static int _seq_open(struct inode *inode, struct file *file) static struct file_operations _seq_fops = { static void clean_all(struct list_head *head) while (!list_empty(head)) { static int __init init(void) mutex_init(&lock); add_one(); entry = create_proc_entry("my_data",S_IWUSR | S_IRUGO, NULL); return 0; static void __exit fini(void) module_init(init);
struct seq_operations { start(): 2、重要的数据结构 seq_hello.c #define PROC_NAME "test_proc" typedef struct item user_item items[4]; MODULE_AUTHOR("ZHANG JIE:[email protected]"); static void *my_seq_start(struct seq_file *s, loff_t *pos) if ( *pos == 0 ) { /* yes => return a non null value to begin the sequence */ return &counter; } else { /* no => it‘s the end of the sequence, return end to stop reading */ *pos = 0; return NULL; }}static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos){ unsigned long *tmp_v = (unsigned long *)v; if (*pos < MAX_LINES) { (*tmp_v)++; (*pos)++; return tmp_v; } else { *pos = 0; return NULL; }}static void my_seq_stop(struct seq_file *s, void *v){ printk("Invoke stop\n");}static int my_seq_show(struct seq_file *s, void *v){ int i; loff_t *spos = (loff_t *) v; for (i = 0; i < 4; i++) { items[i].key = *spos; } items[0].value = ‘0‘; items[1].value = ‘1‘; items[2].value = ‘2‘; items[3].value = ‘3‘; seq_printf(s, "%ld=%c,%ld=%c,%ld=%c,%ld=%c;\n", items[0].key, items[0].value, items[1].key, items[1].value, items[2].key, items[2].value, items[3].key, items[3].value); return 0;}static struct seq_operations my_seq_ops = { .start = my_seq_start, .next = my_seq_next, .stop = my_seq_stop, .show = my_seq_show};static int my_open(struct inode *inode, struct file *file){ return seq_open(file, &my_seq_ops);};static struct file_operations my_file_ops = { .owner = THIS_MODULE, .open = my_open, .read = seq_read, .llseek = seq_lseek, .release = seq_release};int init_module(void){ struct proc_dir_entry *entry; entry = create_proc_entry(PROC_NAME, 0, init_net.proc_net); if (entry) { entry->proc_fops = &my_file_ops; } printk(KERN_INFO"Initialze /proc/net/test_proc success!\n"); return 0;}void cleanup_module(void){ remove_proc_entry(PROC_NAME, init_net.proc_net); printk(KERN_INFO"Remove /proc/net/test_proc success!\n");}Makefileobj-m := seq_hello.oKDIR := /lib/modules/$(shell uname -r)/buildPWD := $(shell pwd)default: $(MAKE) -C $(KDIR) M=$(PWD) modulesclean: $(RM) *.o *.mod.c *.ko *.symvers *.markers *.order[[email protected]:~/Desktop/net/seq]# cat /proc/net/test_proc
|