【驱动】——seq_file使用指南
seq_file只是在普通的文件read中加入了内核缓冲的功能,从而实现顺序多次遍历,读取大数据量的简单接口。seq_file一般只提供只读接口,在使用seq_file操作时,主要靠下述四个操作来完成内核自定义缓冲区的遍历的输出操作,其中pos作为遍历的iterator,在seq_read函数中被多次使用,用以定位当前从内核自定义链表中读取的当前位置,当多次读取时,pos非常重要,且pos总是遵循从0,1,2...end+1遍历的次序,其即必须作为遍历内核自定义链表的下标,也可以作为返回内容的标识。但是我在使用中仅仅将其作为返回内容的标示,并没有将其作为遍历链表的下标,从而导致返回数据量大时造成莫名奇妙的错误,注意:start返回的void*v如果非0,被show输出后,在作为参数传递给next函数,next可以对其修改,也可以忽略;当next或者start返回NULL时,在seq_open中控制路径到达seq_end。
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方法始终会首先调用;
next函数应将迭代器移动到下一个位置,并在序列中没有其他项目时返回NULL;
stop做清除工作;
在上述调用之间,内核会调用 show 方法来将实际的数据输出到用户空间。需要使用如下一组特殊函数来处理数据:
int seq_printf(struct seq_file *sfile, const char *fmt, ...);
int seq_putc(struct seq_file *sfile, char c);
int seq_puts(struct seq_file *sfile, const char *s);
值得注意的是,在设计上,seq_file的代码不会在 start 和 stop 的调用之间执行其他的非原子操作。我们可以确信,start调用之后马上就会有对stop的调用。因此在start方法中获取信号量或者自旋锁是安全的。
定义了完整的操作函数,我们必须将这些函数打包并和 /proc 中的某个文件连接起来。首先要填充一个 seq_operations 结构:
static struct seq_operations seq_ops = {
    .start = seq_start,
    .next = seq_next,
    .stop = seq_stop,
    .show = seq_show
};
有了这个结构我们可以创建一个open方法,将文件连接到seq_file操作:
static int proc_open(struct inode *inode, struct file *file){
    return seq_open(file, &scull_seq_ops);
}
对seq_open的调用将file结构和我们上面定义的顺序操作连接在一起。open是唯一一个必须由我们自己实现的文件操作,因此我们的file_operations结构可如下定义:
static struct file_operations proc_ops = {
    .owner = THIS_MODULE,
    .open = proc_open,
    .read = seq_read,
    .llseek = seq_lseek,
    .release = seq_release
};
这里,我们指定了我们自己的open方法,但对其他的file_operations成员,我们使用了已经定义好的 seq_read, seq_lseek, seq_release方法。
最后,我们建立实际的 /proc 文件;
entry = create_porc_entyr("sequence", 0, NULL);
if (entry)
entry->proc_fops = &scull_proc_ops;
例一:现在我们使用seq_file获取0-100的数:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <linux/seq_file.h>
#include <linux/proc_fs.h> #define MAX_NUM 100
static void *ct_seq_start(struct seq_file *s, loff_t *pos){
loff_t *spos = kmalloc(sizeof(loff_t), GFP_KERNEL); if (*pos > MAX_NUM)
return NULL; if (!*spos){
return NULL;
}
*spos = *pos;
return spos;
}
static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos){
loff_t *spos = (loff_t *)v; *pos = ++(*spos);
if (*pos > MAX_NUM){
return NULL;
}
return spos;
} static void ct_seq_stop(struct seq_file *s, void *v){
printk("ct_seq_stop!\n");
kfree(v);
} static int ct_seq_show(struct seq_file *s, void *v){
loff_t *spos = (loff_t *)v;
seq_printf(s, "%lld\n", *spos);
return ;
}
static const struct seq_operations ct_seq_ops = {
.start = ct_seq_start,
.next = ct_seq_next,
.stop = ct_seq_stop,
.show = ct_seq_show
}; static int ct_open(struct inode *inode, struct file *file){
printk("ct_open!\n");
return seq_open(file, &ct_seq_ops);
} static const struct file_operations ct_file_ops = {
.owner = THIS_MODULE,
.open = ct_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release
};
static int ct_init(void){
struct proc_dir_entry *entry;
entry = create_proc_entry("sequence", , NULL);
if (entry)
entry->proc_fops = &ct_file_ops;
return ;
}
static void ct_exit(void){
remove_proc_entry("sequence", NULL);
}
module_init(ct_init);
module_exit(ct_exit); MODULE_LICENSE("GPL");
cat /proc/sequence 遍可以获取0-100的数字;
例二:使用seq_file获取链表中的数据:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <linux/seq_file.h>
#include <linux/proc_fs.h>
#include <linux/string.h> #define MAX_NUM 5
struct node {
char buf[];
struct node *next;
};
struct node *head = NULL;
static int init_data(void){
int i;
struct node *tmp = NULL;
char ch = 'a';
for (i = ; i < MAX_NUM; i++){
tmp = (struct node *)kmalloc(sizeof(struct node), GFP_KERNEL);
if (!tmp){
return -ENOMEM;
}
memset(tmp->buf, , sizeof(tmp->buf));
memset(tmp->buf, ch + i, sizeof(tmp->buf) - );
tmp->next = NULL;
if (!head){
head = tmp;
}
else {
tmp->next = head;
head = tmp;
}
}
return ;
}
static int free_data(void){
struct node *tmp = NULL; while (head){
tmp = head;
head = head->next;
kfree(tmp);
}
return ;
} static void *ct_seq_start(struct seq_file *s, loff_t *pos){
struct node *tmp = head;
int index = *pos + ; printk("seq start!\n");
if (!*pos){
return head;
}
while (index--){
if (!tmp){
return NULL;
}
tmp = tmp->next;
} return tmp;
} static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos){
struct node *tmp = (struct node *)v; printk("seq next!\n");
*pos = *pos + ;
tmp = tmp->next;
if (!tmp){
return NULL;
} return tmp;
} static void ct_seq_stop(struct seq_file *s, void *v){
printk("ct_seq_stop!\n");
}
static int ct_seq_show(struct seq_file *s, void *v){
struct node *tmp = (struct node *)v;
printk("seq show!\n");
seq_printf(s, "%s\n", tmp->buf);
return ;
} static const struct seq_operations ct_seq_ops = {
.start = ct_seq_start,
.next = ct_seq_next,
.stop = ct_seq_stop,
.show = ct_seq_show
}; static int ct_open(struct inode *inode, struct file *file){
printk("ct_open!\n");
return seq_open(file, &ct_seq_ops);
} static const struct file_operations ct_file_ops = {
.owner = THIS_MODULE,
.open = ct_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release
}; static int ct_init(void){
struct proc_dir_entry *entry;
init_data();
entry = create_proc_entry("sequence", , NULL);
if (entry)
entry->proc_fops = &ct_file_ops;
return ;
}
static void ct_exit(void){
free_data();
remove_proc_entry("sequence", NULL);
}
module_init(ct_init);
module_exit(ct_exit); MODULE_LICENSE("GPL");
cat /proc/sequence 会把链表中的数据输出,此时我们使用 dmesg 查看内核输出信息;

现在我们把结构体中的buf数据变成1024,MAX_NUM改为10,然后执行 cat /proc/sequence 然后再 dmesg 看看;
结果为:
ct_open!
seq start!
seq show!
seq next!
seq show!
seq next!
seq show!
seq next!
seq show!
ct_seq_stop!
seq start!
seq show!
seq next!
seq show!
seq next!
seq show!
seq next!
seq show!
ct_seq_stop!
seq start!
seq show!
seq next!
seq show!
seq next!
seq show!
seq next!
ct_seq_stop!
seq start!
ct_seq_stop!
我们发现中间执行了 stop ,而不是像上面一样执行 start->show->next->show->next->stop。原因就是某次调用 show 的时候发现 seq_file 中的buf满了【buf——4K】。
【驱动】——seq_file使用指南的更多相关文章
- [译] MongoDB Java异步驱动快速指南
		导读 mongodb-java-driver是mongodb的Java驱动项目. 本文是对MongoDB-java-driver官方文档 MongoDB Async Driver Quick Tour ... 
- 基于ABP落地领域驱动设计-00.目录和小结
		<实现领域驱动设计> -- 基于 ABP Framework 实现领域驱动设计实用指南 翻译缘由 自 ABP vNext 1.0 开始学习和使用该框架,被其优雅的设计和实现吸引,适逢 AB ... 
- 3000本IT书籍下载地址
		http://www.shouce.ren/post/d/id/112300 黑客攻防实战入门与提高.pdfhttp://www.shouce.ren/post/d/id/112299 黑 ... 
- ABP Framework 研习社经验总结(6.28-7.2)
		ABP Framework 研习社经验总结(6.28-7.2) 研习社初衷 在翻译 <实现领域驱动设计>-- 基于 ABP Framework 实现领域驱动设计实用指南 时,因为DDD理论 ... 
- Redis 小白指南(一)- 简介、安装、GUI 和 C# 驱动介绍
		Redis 小白指南(一)- 简介.安装.GUI 和 C# 驱动介绍 目录 简介 安装 入门指令 GUI 工具 C# 驱动介绍 简介 ANSI C 编写,开源,基于内存,可持久化,一个键值对的数据库, ... 
- Linux 桌面玩家指南:11. 在同一个硬盘上安装多个 Linux 发行版以及为 Linux 安装 Nvidia 显卡驱动
		特别说明:要在我的随笔后写评论的小伙伴们请注意了,我的博客开启了 MathJax 数学公式支持,MathJax 使用$标记数学公式的开始和结束.如果某条评论中出现了两个$,MathJax 会将两个$之 ... 
- Infrastructure as Code 行为驱动开发指南 https://www.ibm.com/developerworks/cn/devops/d-bbd-guide-iac/index.html
		Infrastructure as Code 行为驱动开发指南 https://www.ibm.com/developerworks/cn/devops/d-bbd-guide-iac/index.h ... 
- 业务驱动的全景监控体系在阿里的应用 | 阿里巴巴DevOps实践指南
		编者按:本文源自阿里云云效团队出品的<阿里巴巴DevOps实践指南>,扫描上方二维码或前往:https://developer.aliyun.com/topic/devops,下载完整版电 ... 
- linux内核驱动学习指南
		1. 参考链接 小白的博客 ONE_Tech 你为什么看不懂Linux内核驱动源码? 求教怎么学习linux内核驱动 
随机推荐
- 转 redis 锁
			原文链接: http://www.promptness.cn/article/34 前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的 ... 
- C#基础第九天-作业-储蓄账户(SavingAccount)和信用账户(CreditAccount)
			要求1:完成以下两种账户类型的编码.银行的客户分为两大类:储蓄账户(SavingAccount)和信用账户(CreditAccount),两种的账户类型的区别在于:储蓄账户不允许透支,而信用账户可以透 ... 
- js判断是否IE浏览器
			//ie? if (!!window.ActiveXObject || "ActiveXObject" in window){ //是 alert(1); }else{ //不是 ... 
- Android应用的基本原理
			原文:http://android.eoe.cn/topic/android_sdk 应用基础-Application Fundamentals Android应用程序以java作为开发语言.用And ... 
- HTML5学习笔记(二十):JavaScript中的标准对象
			这里提到的标准对象指ECMAScript中定义的对象,无论JavaScript运行那种环境(浏览器.Node.js)下都存在的对象. typeof 在JavaScript的世界里,一切都是对象. 但是 ... 
- DIOCP开源项目-DIOCP3重写笔记-1
			这几天在在重新DIOCP3,基本工作已经初步完成,进入测试阶段,今天加入排队投递,本认为是个很简单的工作,稍微不注意,留了两个坑,调了7,8个小时,刚刚总算找到问题,记录一下, 关于排队投递的流程 这 ... 
- Docker 构建网络服务后本机不能访问
			Docker 构建网络服务后本机不能访问 起因 使用tornado构建了一个服务,测试都没有问题 使用docker构建镜像,使用docker run image_name启动服务 使用浏览器访问 12 ... 
- linux远程拷贝命令-scp
			因为某种原因需要远程一个CentOS主机,只能通过ssh访问并下载文件.搞了半天不知道怎么处理文件,比如上传和下载.那就学习下吧. 基本命令格式 由于使用ssh,登录之后的本机地址是不需要给出的.但是 ... 
- 【转】Java HotSpot(TM) 64-Bit Server VM warning: Insufficient space for shared memory file:
			使用命令:JPS #jps 报错了 $jps Java HotSpot(TM) Server VM warning: Insufficient space for shared memory fil ... 
- Quo JS多种触摸手势轻量级JavaScript库
			http://www.uedsc.com/quo-js.html http://quojs.tapquo.com/ 
