【驱动】——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内核驱动
随机推荐
- LINQ中in的实现方法-LINQ To Entities如何实现查询 select * from tableA where id in (1,2,3,4)
如果用in是字符串类型无问题,可以直接这样用 ).Where(entity => urls.Contains((entity.NavigateUrl == null ? "" ...
- php实现ZIP压缩文件解压缩(转)
测试使用了两个办法都可以实现: 第一个:需要开启配置php_aip.dll <?php //需开启配置 php_zip.dll //phpinfo(); header("Content ...
- java 执行mysql 8.0.11存储过程报错The user specified as a definer ('root'@'10.%.%.%') does not exist解决办法
执行存储过程,报错 java.sql.SQLException: The user specified as a definer ('root'@'10.%.%.%') does not exist ...
- Java代码通过API操作HBase的最佳实践
HBase提供了丰富的API.这使得用Java连接HBase非常方便. 有时候大家会使用HTable table=new HTable(config,tablename);的方式来实例化一个HTabl ...
- 由初始化线程池引发的NoClassDefFoundError 异常分析
今天说的异常是一个很不常见的异常,至少我不经常见到这个异常.首先先看下NoClassDefFoundError官方定义 : Java Virtual Machine is not able to fi ...
- Ubuntu 14.04 静态IP设置, ping通win7
ubuntu 14.04以后不支持 /etc/init.d/networing restart命令,使用ifdown et0 和ifup eth0 使用echo $?查看上一次命令执行的结果 ifdo ...
- unfolding maps支持中文
由于项目需要,选择了processing+unfolding的方式进行数据可是话的开发,unfolding maps是一个非常强大的地图工具,processing可以灵活自如的表达各种图形/效果.使用 ...
- svn提交遇到冲突解决方法
冲突:如果提交时候发现冲突了先不急着提交,否则会产生冲突文件. 解决步骤: 1.将本地文件先复制一份 2.svn revert(恢复到没修改前版本) -> svn update(更新当前最新版本 ...
- 如何在Linux平台下安装JDK
原文链接:http://android.eoe.cn/topic/android_sdk 1.下载Java的开发包JDK: JDK有好几个类型版本,我们只需要选择Java SE类型的版本就行了. 进入 ...
- iframe调用父页面函数用法举例
iframe如何调用父页面函数. window.parent.xxxxx();//xxxxx()代表父页面方法具体列子如下,其中包括easyUI的右键和单击事件parent.jspbody部分代码 & ...