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

Tom*_*del 6 linux module linux-kernel

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

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

 cat /proc/kallsyms
Run Code Online (Sandbox Code Playgroud)

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

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

如果有人知道任何好的 doco 可以帮助我理解如何创建 seq 文件来遍历内核对象并可以发布链接(或快速示例),我将非常感激。

Cir*_*四事件 4

最小可运行示例

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

此示例的行为就像包含以下内容的文件:

0
1
2
Run Code Online (Sandbox Code Playgroud)

然而,我们只在内存中存储一​​个整数,并以迭代器的方式动态计算文件。

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

使用cat和来处理文件dd skip=以进行搜索。

0
1
2
Run Code Online (Sandbox Code Playgroud)

GitHub 上游.

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

single_open

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

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

ab
cd
Run Code Online (Sandbox Code Playgroud)

代码:

#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)
Run Code Online (Sandbox Code Playgroud)

GitHub 上游.

在 Linux 4.9.6 上测试。

看来从 Linux 5 开始,有一个向后不兼容的更改,需要您seq_file以不同的方式实现,我认为这讨论了它:seq_file 在 next 返回 NULL 后无法正常工作,并且似乎如果您不更新此您收到警告:

seq_file: buggy .next function next [module-name] did not update position index
Run Code Online (Sandbox Code Playgroud)