gpt4 book ai didi

linux - 如何在 Linux 内核模块中使用 seq_file?

转载 作者:太空狗 更新时间:2023-10-29 11:22:08 24 4
gpt4 key购买 nike

大家好,我是 Linux 新手,想知道如何在模块中使用 Linux 序列文件来遍历内核对象。

我所知道的是我可以使用命令:

 cat /proc/kallsyms

查看可用的符号,根据我在谷歌上阅读的内容,列表中带有“D”或“d”的符号是指向数据结构的指针。

虽然我知道如何创建模块的基础知识,但互联网上关于如何使用 seq 操作的示例并不统一,我有点困惑。

如果有人知道任何好的 doco 可以帮助我理解如何创建一个 seq 文件来遍历内核对象并且可以发布一个链接(或一个简单的例子),我将不胜感激。

最佳答案

最小可运行示例

内核文档包含 Documentation/filesystems/seq_file.txt 下的示例,但这是一个带有循环终止的可运行版本。

这个例子的行为就像一个包含以下内容的文件:

0
1
2

但是,我们只在内存中存储一​​个整数并以迭代器方式即时计算文件。

该文件适用于 readlseek 系统调用,但没有等效的 write 系统调用: How to implement a writable proc file by using seq_file in a driver module

使用 catdd skip= 查找文件。

#include <asm/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/printk.h> /* pr_info */
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
#include <linux/slab.h>
#include <uapi/linux/stat.h> /* S_IRUSR */

MODULE_LICENSE("GPL");

static int max = 2;
module_param(max, int, S_IRUSR | S_IWUSR);

static struct dentry *debugfs_file;

/* Called at the beginning of every read.
*
* The return value is passsed to the first show.
* It normally represents the current position of the iterator.
* It could be any struct, but we use just a single integer here.
*
* NULL return means stop should be called next, and so the read will be empty..
* This happens for example for an ftell that goes beyond the file size.
*/
static void *start(struct seq_file *s, loff_t *pos)
{
loff_t *spos;

pr_info("start pos = %llx\n", (unsigned long long)*pos);
spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
if (!spos || *pos >= max)
return NULL;
*spos = *pos;
return spos;
}

/* The return value is passed to next show.
* If NULL, stop is called next instead of show, and read ends.
*
* Can get called multiple times, until enough data is returned for the read.
*/
static void *next(struct seq_file *s, void *v, loff_t *pos)
{
loff_t *spos;

spos = v;
pr_info("next pos = %llx\n", (unsigned long long)*pos);
if (*pos >= max)
return NULL;
*pos = ++*spos;
return spos;
}

/* Called at the end of every read. */
static void stop(struct seq_file *s, void *v)
{
pr_info("stop\n");
kfree(v);
}

/* Return 0 means success, SEQ_SKIP ignores previous prints, negative for error. */
static int show(struct seq_file *s, void *v)
{
loff_t *spos;

spos = v;
pr_info("show pos = %llx\n", (unsigned long long)*spos);
seq_printf(s, "%llx\n", (long long unsigned)*spos);
return 0;
}

static struct seq_operations my_seq_ops = {
.next = next,
.show = show,
.start = start,
.stop = stop,
};

static int open(struct inode *inode, struct file *file)
{
pr_info("open\n");
return seq_open(file, &my_seq_ops);
}

static struct file_operations fops = {
.owner = THIS_MODULE,
.llseek = seq_lseek,
.open = open,
.read = seq_read,
.release = seq_release
};

static int myinit(void)
{
debugfs_file = debugfs_create_file(
"lkmc_seq_file", S_IRUSR, NULL, NULL, &fops);
if (debugfs_file) {
return 0;
} else {
return -EINVAL;
}
}

static void myexit(void)
{
debugfs_remove(debugfs_file);
}

module_init(myinit)
module_exit(myexit)

GitHub upstream .

请注意 seq_file API 如何使编写read 文件操作变得更加容易。

single_open

如果您预先拥有整个读取输出,single_open 是一个更方便的 seq_file 版本。

此示例的行为类似于包含以下内容的文件:

ab
cd

代码:

#include <asm/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/printk.h> /* pr_info */
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
#include <uapi/linux/stat.h> /* S_IRUSR */

MODULE_LICENSE("GPL");

static struct dentry *debugfs_file;

static int show(struct seq_file *m, void *v)
{
seq_printf(m, "ab\ncd\n");
return 0;
}

static int open(struct inode *inode, struct file *file)
{
return single_open(file, show, NULL);
}

static const struct file_operations fops = {
.llseek = seq_lseek,
.open = open,
.owner = THIS_MODULE,
.read = seq_read,
.release = single_release,
};

static int myinit(void)
{
debugfs_file = debugfs_create_file(
"lkmc_seq_file_single", S_IRUSR, NULL, NULL, &fops);
if (debugfs_file) {
return 0;
} else {
return -EINVAL;
}
}

static void myexit(void)
{
debugfs_remove(debugfs_file);
}

module_init(myinit)
module_exit(myexit)

GitHub upstream .

在 Linux 4.9.6 上测试。

似乎从 Linux 5 开始,有一个向后不兼容的更改,要求您以不同的方式实现 seq_file,我想这就是在谈论它:seq_file not working properly after next returns NULL看起来如果你不更新这个你会收到警告:

seq_file: buggy .next function next [module-name] did not update position index

关于linux - 如何在 Linux 内核模块中使用 seq_file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25399112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com