span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; }.cm-searching {background: #ffa; background: rgba(255, 255, 0, .4);}.cm-force-border { padding-right: .1px; }@media print { .CodeMirror div.CodeMirror-cursors {visibility: hidden;}}.cm-tab-wrap-hack:after { content: ""; }span.CodeMirror-selectedtext { background: none; }.CodeMirror-activeline-background, .CodeMirror-selected {transition: visibility 0ms 100ms;}.CodeMirror-blur .CodeMirror-activeline-background, .CodeMirror-blur .CodeMirror-selected {visibility:hidden;}.CodeMirror-blur .CodeMirror-matchingbracket {color:inherit !important;outline:none !important;text-decoration:none !important;}
-->
li {list-style-type:decimal;}ol.wiz-list-level2 > li {list-style-type:lower-latin;}ol.wiz-list-level3 > li {list-style-type:lower-roman;}blockquote {padding:0 12px;padding:0 0.75rem;}blockquote > :first-child {margin-top:0;}blockquote > :last-child {margin-bottom:0;}img {border:0;max-width:100%;height:auto !important;margin:2px 0;}table {border-collapse:collapse;border:1px solid #bbbbbb;}td, th {padding:4px 8px;border-collapse:collapse;border:1px solid #bbbbbb;min-height:28px;word-break:break-all;box-sizing: border-box;}.wiz-hide {display:none !important;}
-->

作者

彭东林
pengdonglin137@163.com
 

平台

Linux-4.14.13
Qemu + vexpress
 

概述

从内核中导出信息到用户空间有很多方法,可以自己去实现file_operations的read函数或者mmap函数,但是这种方法不够简单,而且也会有一些限制,比如一次read读取大于1页时,驱动里就不得不去进行复杂的缓冲区管理。为此,就需要学习一下seq_file的用法,为了更简单和方便,内核提供了single_xxx系列接口,它是对seq_file的进一步封装。
 

正文

示例程序
 #include <linux/init.h>
#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/debugfs.h>
#include <linux/fs.h> static struct dentry *seq_file_demo_dir; static int seq_file_demo_show(struct seq_file *seq, void *v)
{
seq_printf(seq, "Hello World\n");
return ;
} static int seq_file_demo_open(struct inode *inode, struct file *file)
{
return single_open(file, &seq_file_demo_show, NULL);
} static const struct file_operations seq_file_demo_fops = {
.owner = THIS_MODULE,
.open = seq_file_demo_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
}; static int __init seq_file_demo_init(void)
{
seq_file_demo_dir = debugfs_create_file("seq_file_demo", , NULL,
NULL, &seq_file_demo_fops);
return ;
} static void __exit seq_file_demo_exit(void)
{
if (seq_file_demo_dir)
debugfs_remove(seq_file_demo_dir);
} module_init(seq_file_demo_init);
module_exit(seq_file_demo_exit);
MODULE_LICENSE("GPL");
上面的demo在/sys/kernel/debug/下创建了一个"seq_file_demo",读取这个文件会得到"Hello World"字符串。上面的函数中需要我们实现的只有一个:seq_file_demo_show,直接调用seq_file提供的输出函数即可,不用我们去考虑缓冲区的分配、释放以及越界等问题,可以尽情畅快的输出。
上面的代码里有些标准化的函数,看着有些碍眼,所以在Linux-4.15的include/linux/seq_file.h中提供了下面的宏定义:
 #define DEFINE_SHOW_ATTRIBUTE(__name)                    \
static int __name ## _open(struct inode *inode, struct file *file) \
{ \
return single_open(file, __name ## _show, inode->i_private); \
} \
\
static const struct file_operations __name ## _fops = { \
.owner = THIS_MODULE, \
.open = __name ## _open, \
.read = seq_read, \
.llseek = seq_lseek, \
.release = single_release, \
}

利用上面的宏可以对我们的驱动做进一步简化:

 #include <linux/init.h>
#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/debugfs.h>
#include <linux/fs.h> static struct dentry *seq_file_demo_dir; static int seq_file_demo_show(struct seq_file *seq, void *v)
{
seq_printf(seq, "Hello World\n");
return ;
}
DEFINE_SHOW_ATTRIBUTE(seq_file_demo); static int __init seq_file_demo_init(void)
{
seq_file_demo_dir = debugfs_create_file("seq_file_demo", , NULL,
NULL, &seq_file_demo_fops);
return ;
} static void __exit seq_file_demo_exit(void)
{
if (seq_file_demo_dir)
debugfs_remove(seq_file_demo_dir);
} module_init(seq_file_demo_init);
module_exit(seq_file_demo_exit);
MODULE_LICENSE("GPL");
这样我们只需要专心实现show函数即可。
如果看一下single_open,会发现它是对seq_file的进一步封装:
 int single_open(struct file *file, int (*show)(struct seq_file *, void *),
void *data)
{
struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
int res = -ENOMEM; if (op) {
op->start = single_start;
op->next = single_next;
op->stop = single_stop;
op->show = show;
res = seq_open(file, op);
if (!res)
((struct seq_file *)file->private_data)->private = data;
else
kfree(op);
}
return res;
}

上面设置了single_xx的start、next以及stop回调函数,实现很简单:

 static void *single_start(struct seq_file *p, loff_t *pos)
{
return NULL + (*pos == );
} static void *single_next(struct seq_file *p, void *v, loff_t *pos)
{
++*pos;
return NULL;
} static void single_stop(struct seq_file *p, void *v)
{
}

其中,*ops表示的是要输出的元素的索引编号,从0开始,依次递增;

single_start的返回值表示要输出的元素的首地址,这个函数的作用是找到索引号为*pos的元素,并返回该元素的首地址,此外也可以做一些加锁的操作
single_next的入参中v表示刚刚show过的元素的首地址,*pos是该元素的索引,这个函数的目的是计算并返回下一个要show的元素的首地址以及索引号
single_stop里可以做一些释放锁的操作
show需要自己实现,向用户show出当前元素的相关信息
 
 
未完待续

seq_file学习(1)—— single_open的更多相关文章

  1. seq_file学习(2)—— seq_file

    li {list-style-type:decimal;}ol.wiz-list-level2 > li {list-style-type:lower-latin;}ol.wiz-list-le ...

  2. Linux内核学习笔记之seq_file接口创建可读写proc文件

    转自:http://blog.csdn.net/mumufan05/article/details/45803219 学习笔记与个人理解,如有错误,欢迎指正. 温馨提示:建议跟着注释中的编号顺序阅读代 ...

  3. linux内核数据结构学习总结

    目录 . 进程相关数据结构 ) struct task_struct ) struct cred ) struct pid_link ) struct pid ) struct signal_stru ...

  4. Introduction the naive“scull” 《linux设备驱动》 学习笔记

    Introduction the naive "scull" 首先.什么是scull? scull (Simple Character Utility for Loading Lo ...

  5. linux内核seq_file接口

    seq相关头文件linux/seq_file.h,seq相关函数的实现在fs/seq_file.c.seq函数最早是在2001年就引入了,但以前内核中一直用得不多,而到了2.6内核后,许多/proc的 ...

  6. itop4412学习-超级块操作

    1. 先看下超级块支持的函数列表,文件路径\4412_SCP精英版\Android源码\iTop4412_Kernel_3.0_20140521\iTop4412_Kernel_3.0\include ...

  7. LKM rootkit:Reptile学习

    简介 Reptile是github上一个很火的linux lkm rootkit,最近学习了一些linux rootkit的内容,在这里记录一下. 主要是分析reptile的实现 Reptile的使用 ...

  8. 设备驱动基础学习--/proc下增加节点

    在需要创建一个由一系列数据顺序组合而成的/proc虚拟文件或一个较大的/proc虚拟文件时,推荐使用seq_file接口. 数据结构struct seq_fille定义在include/linux/s ...

  9. Linux内核驱动学习(三)字符型设备驱动之初体验

    Linux字符型设备驱动之初体验 文章目录 Linux字符型设备驱动之初体验 前言 框架 字符型设备 程序实现 cdev kobj owner file_operations dev_t 设备注册过程 ...

随机推荐

  1. jQuery插件学习之选项卡Tab

    在网站开发中经常会用到选项卡功能,为了节省一下写代码时间,封装了一下tab插件,方便调用. 来看一下效果: tab-1 tab-2 tab-3 tabs-1-panel tabs-2-panel ta ...

  2. ssh登录报错-bash fork retry Resource temporarily unavailable

  3. 批处理命令调用WINRAR对文件进行压缩

    命令: winrar a 参数  压缩后的文件 压缩源文件 如压缩apk文件,代码如下 echo apk文件不能直接下载,所以压缩apk成rar来下载 d: cd D:\Program Files\W ...

  4. seafile增加邮件服务功能

    这个很简单哈,直接上配置.此处我用的163邮箱 vim /opt/seafile/conf/seahub_settings.py ### 163邮箱配置测试 ### EMAIL_USE_SSL = F ...

  5. LoadRunner之IP欺骗

     一.启动IP欺骗,弹出提示窗口:      二.将本机改为静态IP: 1.查看当前的IP相关信息: 2.更改IP:  三.正式设置模拟IP号段: 1.重复步骤一,会弹出如下弹窗,选择第二项并下一步: ...

  6. Spring(四)使用注解注入Bean

    注解简单介绍 是代码里面的特殊标记,使用注解完成功能. 注解写法@ 注解名称(属性名=属性值). 注解可以作用在类.方法.属性上面. 使用流程: 在ApplicationContext.xml中开启注 ...

  7. VS2017动态链接库(.dll)的生成与使用

    转 https://blog.csdn.net/m0_37170593/article/details/76445972 这里以VS2017为例子,讲解一下动态链接库(.dll)的生成与使用. 一.动 ...

  8. ddctf2019--web部分writeup

    0x00前言 上周五开始的DDCTF 2019,整个比赛有一周,题目整体来说感觉很不错,可惜我太菜了,做了4+1道题,还是要努力吧 0x01 web 滴~ 打开看着url,就像文件包含 文件名1次he ...

  9. web服务搭建

  10. scrapy 安装

    windows 1.wheelpip install wheel2.lxmlhttp://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml3.PyOpensslhttp ...