内核 procfs 使用笔记

procfs 文件创建

首先从一个参考实例开始:

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

static int test_proc_open(struct inode *inode, struct file *file)
{
    return single_open(file, test_proc_show, PDE_DATA(inode));
}

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

在 driver probe 函数中初始化创建 proc 文件节点:

proc_create("test", S_IRUGO, NULL, &test_proc_fops);

这个函数原型为:

struct proc_dir_entry *proc_create(const char *name, umode_t mode,
                   struct proc_dir_entry *parent,
                   const struct file_operations *proc_fops);

这个函数的第3个参数如果为NULL,那么将会直接在/proc目录中创建文件节点。

procfs 目录创建

实例代码如下:

struct proc_dir_entry *dir_entry = NULL;
dir_entry = proc_mkdir("testdir", NULL);

这样会在 /proc 目录下创建一个 testdir 子目录。第二个参数为 NULL 表示直接在 /proc 目录下创建子目录。如果为指定的 entry ,那么就会在对应的目录下创建子目录。

本文介绍的 procfs 文件节点是用于向用户空间传递内核驱动信息的一种方式,当然可以有其他的方式,后续再其他文章进行介绍,除了这个本文还使用了 seq_file 相关的API,这一套是为了便于文件读取写入操作实现的通用接口,简化了驱动开发,后续也将有其他文章介绍。


欢迎扫码关注我的公众号!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值