当前位置: 首页 > article >正文

【linux驱动开发】创建proc文件系统中的目录和文件实现

1. proc_mkdir_data

说明: 用于在 /proc 文件系统中创建一个目录
函数原型
struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode, struct proc_dir_entry *parent, void *data);
参数
const char *name: 要创建的目录的名称。
umode_t mode : 目录的权限模式,例如 0755 表示所有者可读、可写、可执行,组用户和其他用户可读、可执行。
struct proc_dir_entry *parent : 父目录的 proc_dir_entry 结构指针。如果为 NULL,则在根目录 /proc 下创建目录。
void *data: 与目录关联的用户数据。这个数据可以通过 proc_dir_entry 结构中的 data 字段访问。
返回值
成功时,返回指向新创建的 proc_dir_entry 结构的指针。
失败时,返回 NULL。

2. proc_create_data

说明: 用于在 /proc 文件系统中创建一个文件
函数原型
struct proc_dir_entry *proc_create_data(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct file_operations *fops, void *data);
参数
const char *name: 要创建的文件的名称。
umode_t mode: 文件的权限模式,例如 0644 表示所有者可读、可写,组用户和其他用户可读。
struct proc_dir_entry *parent: 父目录的 proc_dir_entry 结构指针。如果为 NULL,则在根目录 /proc 下创建文件。
const struct file_operations *fops: 文件操作结构指针,定义了文件的读取、写入等操作。
void *data: 与文件关联的用户数据。这个数据可以通过 proc_dir_entry 结构中的 data 字段访问。
返回值
成功时,返回指向新创建的 proc_dir_entry 结构的指针。
失败时,返回 NULL。

3. remove_proc_entry

说明: 用于从 /proc 文件系统中删除一个目录或文件。
函数原型
void remove_proc_entry(const char *name, struct proc_dir_entry *parent);
参数
const char *name: 要删除的目录或文件的名称。
struct proc_dir_entry *parent: 父目录的 proc_dir_entry 结构指针。如果为 NULL,则在根目录 /proc 下删除条目。
返回值
该函数没有返回值。

4. PDE_DATA

获取proc_create_data传入的私有数据。

5.示例代码

Makefile:

obj-m += hello.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/slab.h>


#define PROC_FIX_DIR "fix"

struct my_test
{
	int a;
	int b;
	int c;
	int d;
};

// 读取 proc 文件的函数
static int proc_example_read(struct seq_file *m, void *v)
{
	printk("%s: %d\n", __func__, __LINE__);
    return 0;
}

// 打开 proc 文件的函数
static int proc_example_open(struct inode *inode, struct file *file)
{
    return single_open(file, proc_example_read, PDE_DATA(inode));
}

// 写入 proc 文件的函数
static ssize_t proc_example_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
    char kbuf[64];
    int ret;
	int value = 0;
	unsigned int reg = 0;
	int cmd = 0;

	struct my_test *proc_test = PDE_DATA(file_inode(file));


    if (count > 63)
        return -EINVAL;

    if (copy_from_user(kbuf, buf, count))
        return -EFAULT;

	if(sscanf(kbuf, "%d_0x%x_%d", &cmd, &reg, &value))
	{
		if(cmd == 1)
		{
			printk("write %d to register 0x%08x \n", value, reg);
		}

		if(cmd == 2)
		{
			printk("read %d from register 0x%08x \n", value, reg);
		}
	}



    return count;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)
static const struct proc_ops proc_example_fops = {
        .proc_open           = proc_example_open,
        .proc_read           = seq_read,
        .proc_lseek          = seq_lseek,
        .proc_write          = proc_example_write,
        .proc_release        = single_release,
};
#else
static const struct file_operations proc_example_fops = {
        .open           = proc_example_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
		.write          = proc_example_write,
        .release        = single_release,
};
#endif

struct proc_dir_entry *proc_dir;
struct proc_dir_entry *proc_file;



static void proc_example_init(void)
{
	struct my_test *data = NULL;

	data = kmalloc(sizeof(*data), GFP_KERNEL);
    if (!data) {
		printk("kmalloc error\n");
		return;
    }

	data->a = 1;
	data->b = 2;
	data->c = 3;

	
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
	 /* create fix entry */
	proc_dir = proc_mkdir_data(PROC_FIX_DIR, 0755, NULL, NULL);
	if (!proc_dir) 
	{
		printk("Unable to initialize /proc/%s/\n", PROC_FIX_DIR);
		return;
	}

	// 创建 proc 文件
	proc_file = proc_create_data("value", 0644, proc_dir, &proc_example_fops, data);
	if (!proc_file) {
		remove_proc_entry(PROC_FIX_DIR, NULL);
	}
#else
	proc_dir = proc_mkdir(PROC_FIX_DIR, NULL);
	if (!proc_dir) 
	{
			printk("Unable to initialize /proc/%s/\n", PROC_FIX_DIR);
			return;
	}
	
	if (!create_proc_read_entry("value", 0644, proc_dir, &proc_example_fops, data)) 
	{
			printk("Unable to initialize /proc/%s/%s\n",PROC_FIX_DIR, "value");
			return;
	 }

#endif
	printk("Proc example module loaded\n");

	return;

}

static void __exit proc_example_exit(void)
{
    remove_proc_entry("value", proc_dir);
	remove_proc_entry(PROC_FIX_DIR, NULL);

    printk("Proc example module unloaded\n");
}

static int __init mod_init(void)
{
	proc_example_init();
	printk("hello init\n");
	return 0;
}

static void __exit mod_exit(void)
{
	proc_example_exit();
	printk("hello exit\n");
	return;
}


module_init(mod_init);
module_exit(mod_exit);


MODULE_LICENSE("GPL");



http://www.kler.cn/a/585728.html

相关文章:

  • Python 中 lambda 表达式、推导式和其他函数用法对比
  • QT中读取QSetting文件
  • Ubuntu 访问 Windows 共享文件夹
  • vue2升级Vue3--native、对inheritAttrs作用做以解释、声明的prop属性和未声明prop的属性
  • C语言内容
  • jupyter无法转换为PDF,HTMLnbconvert failed: Pandoc wasn‘t found.
  • 【sql靶场】第11、12关-post提交注入
  • 使用GitHub Actions实现Git推送自动部署到服务器
  • Python实现邮件发送功能,邮箱验证码发送(详细功能实现及环境搭建)
  • 【学习笔记】LLM技术基础
  • C++相关基础概念之入门讲解(上)
  • Qt QML实现弹球消砖块小游戏
  • WPF中查看PDF文件之MoonPdfLib类库
  • c++ auto关键字皮毛
  • ACL初级总结
  • 人工智能时代教育行业该如何转型:迎接挑战,塑造未来教育生态
  • 揭秘AI:机器学习与深度学习的奥秘
  • 浅谈数据分析及数据思维
  • SSM企业台账管理平台
  • NodeJS接口