seq_file学习(2)—— seq_file
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;}
-->
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;}
-->
作者
平台
概述

正文
一、seq_demo驱动
#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/debugfs.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/slab.h>
static struct dentry *seq_demo_dir;
static LIST_HEAD(seq_demo_list);
static DEFINE_MUTEX(seq_demo_lock);
struct seq_demo_node {
char name[10];
struct list_head list;
};
static void *seq_demo_start(struct seq_file *s, loff_t *pos)
{
mutex_lock(&seq_demo_lock);
return seq_list_start(&seq_demo_list, *pos);
}
static void *seq_demo_next(struct seq_file *s, void *v, loff_t *pos)
{
return seq_list_next(v, &seq_demo_list, pos);
}
static void seq_demo_stop(struct seq_file *s, void *v)
{
mutex_unlock(&seq_demo_lock);
}
static int seq_demo_show(struct seq_file *s, void *v)
{
struct seq_demo_node *node = list_entry(v, struct seq_demo_node, list);
seq_printf(s, "name: %s, addr: 0x%p\n", node->name, node);
return 0;
}
static const struct seq_operations seq_demo_ops = {
.start = seq_demo_start,
.next = seq_demo_next,
.stop = seq_demo_stop,
.show = seq_demo_show,
};
static int seq_demo_open(struct inode *inode, struct file *file)
{
return seq_open(file, &seq_demo_ops);
}
static const struct file_operations seq_demo_fops = {
.owner = THIS_MODULE,
.open = seq_demo_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
static int __init seq_demo_init(void)
{
int i;
struct seq_demo_node *node;
for (i = 0; i < 7; i++) {
node = kzalloc(sizeof(struct seq_demo_node), GFP_KERNEL);
sprintf(node->name, "node%d", i);
INIT_LIST_HEAD(&node->list);
list_add_tail(&node->list, &seq_demo_list);
}
seq_demo_dir = debugfs_create_file("seq_demo", 0444, NULL,
NULL, &seq_demo_fops);
return 0;
}
static void __exit seq_demo_exit(void)
{
struct seq_demo_node *node_pos, *node_n;
if (seq_demo_dir) {
debugfs_remove(seq_demo_dir);
list_for_each_entry_safe(node_pos, node_n, &seq_demo_list, list)
if (node_pos) {
printk("%s: release %s\n", __func__, node_pos->name);
kfree(node_pos);
}
}
}
module_init(seq_demo_init);
module_exit(seq_demo_exit);
MODULE_LICENSE("GPL");
#include <linux/init.h>
#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/debugfs.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/slab.h> static struct dentry *seq_demo_dir;
static LIST_HEAD(seq_demo_list);
static DEFINE_MUTEX(seq_demo_lock); struct seq_demo_node {
char name[];
struct list_head list;
}; static void *seq_demo_start(struct seq_file *s, loff_t *pos)
{
mutex_lock(&seq_demo_lock); return seq_list_start(&seq_demo_list, *pos);
} static void *seq_demo_next(struct seq_file *s, void *v, loff_t *pos)
{
return seq_list_next(v, &seq_demo_list, pos);
} static void seq_demo_stop(struct seq_file *s, void *v)
{
mutex_unlock(&seq_demo_lock);
} static int seq_demo_show(struct seq_file *s, void *v)
{
struct seq_demo_node *node = list_entry(v, struct seq_demo_node, list); seq_printf(s, "name: %s, addr: 0x%p\n", node->name, node); return ;
} static const struct seq_operations seq_demo_ops = {
.start = seq_demo_start,
.next = seq_demo_next,
.stop = seq_demo_stop,
.show = seq_demo_show,
}; static int seq_demo_open(struct inode *inode, struct file *file)
{
return seq_open(file, &seq_demo_ops);
} static const struct file_operations seq_demo_fops = {
.owner = THIS_MODULE,
.open = seq_demo_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
}; static int __init seq_demo_init(void)
{
int i;
struct seq_demo_node *node; for (i = ; i < ; i++) {
node = kzalloc(sizeof(struct seq_demo_node), GFP_KERNEL);
sprintf(node->name, "node%d", i); INIT_LIST_HEAD(&node->list);
list_add_tail(&node->list, &seq_demo_list);
} seq_demo_dir = debugfs_create_file("seq_demo", , NULL,
NULL, &seq_demo_fops);
return ;
} static void __exit seq_demo_exit(void)
{
struct seq_demo_node *node_pos, *node_n; if (seq_demo_dir) {
debugfs_remove(seq_demo_dir);
list_for_each_entry_safe(node_pos, node_n, &seq_demo_list, list)
if (node_pos) {
printk("%s: release %s\n", __func__, node_pos->name);
kfree(node_pos);
}
}
} module_init(seq_demo_init);
module_exit(seq_demo_exit);
MODULE_LICENSE("GPL");
下面是运行结果:
[root@vexpress mnt]# cat /d/seq_demo
name: node0, addr: 0xef252000
name: node1, addr: 0xef252680
name: node2, addr: 0xef252380
name: node3, addr: 0xef252740
name: node4, addr: 0xef252b00
name: node5, addr: 0xee80e480
name: node6, addr: 0xeeb9fd40
name: node7, addr: 0xeeb9fd00
二、分析
struct list_head *seq_list_start(struct list_head *head, loff_t pos)
{
struct list_head *lh; list_for_each(lh, head)
if (pos-- == )
return lh; return NULL;
}
struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
{
struct list_head *lh; lh = ((struct list_head *)v)->next;
++*ppos;
return lh == head ? NULL : lh;
}
seq_file学习(2)—— seq_file的更多相关文章
- seq_file学习(1)—— single_open
span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror ...
- Seq_file文件系统实例剖析
http://blog.chinaunix.net/uid-24432676-id-2607766.html 另 http://www.cnblogs.com/qq78292959/archive/2 ...
- linux内核seq_file接口
seq相关头文件linux/seq_file.h,seq相关函数的实现在fs/seq_file.c.seq函数最早是在2001年就引入了,但以前内核中一直用得不多,而到了2.6内核后,许多/proc的 ...
- 序列文件(seq_file)接口
转载:http://blog.csdn.net/gangyanliang/article/details/7244664 内容简介: 本文主要讲述序列文件(seq_file)接口的内核实现,如何使用它 ...
- 设备驱动基础学习--/proc下增加节点
在需要创建一个由一系列数据顺序组合而成的/proc虚拟文件或一个较大的/proc虚拟文件时,推荐使用seq_file接口. 数据结构struct seq_fille定义在include/linux/s ...
- Linux内核学习笔记之seq_file接口创建可读写proc文件
转自:http://blog.csdn.net/mumufan05/article/details/45803219 学习笔记与个人理解,如有错误,欢迎指正. 温馨提示:建议跟着注释中的编号顺序阅读代 ...
- Non Lasting Storage File System、procfs、sysfs
catalog . 引言 . proc文件系统 . 简单的文件系统 . sysfs 0. 引言 传统上,文件系统用于在块设备上持久存储数据,但也可以使用文件系统来组织.提供.交换并不存储在块设备上的信 ...
- The Kernel Newbie Corner: Kernel Debugging with proc "Sequence" Files--Part 3
转载:https://www.linux.com/learn/linux-career-center/44184-the-kernel-newbie-corner-kernel-debugging-w ...
- imx51-linux的cpuinfo之分析
这两天客户提出来,我们的平板cat /proc/cpuinfo出来的信息中的serial怎么是0. 客户就是上帝啊,没办法,分析找问题贝. 我们先看一下目前的cat /proc/cpuinfo的信息: ...
随机推荐
- 基于nopcommerce b2c开源项目的精简版开发框架Nop.Framework
http://www.17ky.net/soft/70612.html?v=1#0-sqq-1-39009-9737f6f9e09dfaf5d3fd14d775bfee85 项目详细介绍 该开源项目是 ...
- python函数式编程——返回函数
1.函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 2.闭包 注意到返回的函数在其定义内部引用了局部变量args,所以,当一个函数返回了一个函数后,其内部的局部变量还 ...
- sparkStreaming消费kafka-1.0.1方式:direct方式(存储offset到Hbase)
话不多说,可以看上篇博文,关于offset存储到zookeeper https://www.cnblogs.com/niutao/p/10547718.html 本篇博文主要告诉你如何将offset写 ...
- HDU2732 Leapin' Lizards 网络流 最大流 SAP
原文链接http://www.cnblogs.com/zhouzhendong/p/8362002.html 题目传送门 - HDU2732 题意概括 给你一个网格,网格上的一些位置上有一只蜥蜴,所有 ...
- 做生活的有心人——xxx系统第一阶段总结
2017秋,桃子已经步入大学三年级了,觉得格外幸运 因为现在,有了学习的动力. 如果你和我一样也是在大学中后部分才意识到,自己是个大人了,思维模式开始转变开始融入一些前所未有的认知,觉得自己渺小得如沧 ...
- 如何将.SQL文件的数据导入到Mysql的数据库中
一.用cmd的调试环境导入.sql文件中的数据: WinR键打开cmd输入: MySQL -u root -p 进入MySQL后MySQL>use DR; MySQL> source ...
- CentOS 通过yum在线安装MySQL5.7
CentOS 通过yum在线安装MySQL5.7 Step1: 检测系统是否自带安装mysql # yum list installed | grep mysql Step2: 删除系统自带的mysq ...
- Masquerade strikes back Gym - 101911D (数学)
Quite often the jury of Saratov SU use the problem "Masquerade" in different practice sess ...
- 从零搭建 ES 搜索服务(二)基础搜索
一.前言 上篇介绍了 ES 的基本概念及环境搭建,本篇将结合实际需求介绍整个实现过程及核心代码. 二.安装 ES ik 分析器插件 2.1 ik 分析器简介 GitHub 地址:https://git ...
- 开发环境之git:团队协作git工作流与常用命令
此篇文章只是一篇傻瓜式的,记录工作中比较规范且常见的一个git工作流需要用到的命令,让你可以快速的开始工作.而不是一些长篇大论的理论知识,如果你有用过sourcetree或者其它图形化工具,结合你正在 ...