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,这一套是为了便于文件读取写入操作实现的通用接口,简化了驱动开发,后续也将有其他文章介绍。
欢迎扫码关注我的公众号!

本文详细介绍了在Linux内核中使用procfs文件系统创建文件节点和目录的方法,包括如何利用proc_create和proc_mkdir函数,以及这些操作的具体代码示例。同时,文中提及了seq_file相关API的应用,用于简化驱动开发中的文件读写操作。
3113

被折叠的 条评论
为什么被折叠?



