zoukankan      html  css  js  c++  java
  • Linux proc_mkdir和proc_create的用法

    //功能:在proc中创建一个文件夹

    //参数1:创建的文件夹名称

    //参数2:创建的文件夹路径,就是在哪个文件夹中创建,如果是proc根目录,此参数为NULL

    //返回值:创建的文件夹路径

    struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent);

    //功能:在proc中创建一个文件

    //参数1:创建的文件的名称

    //参数2:文件的读写权限

    //参数3:创建的文件路径,即在哪个文件夹中创建,如果是proc根目录,此参数为NULL

    //参数4:此文件的操作函数file_operations

    //返回值:创建的文件路径

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

    //功能:删除proc中的文件或文件夹

    //参数:删除的文件或文件夹路径

    //返回:无

    void proc_remove(struct proc_dir_entry *de);

    #define BASE_DIR_NAME     "led"
    
    struct file_operations led_ops = {
        .owner = THIS_MODULE,
        .open = led_ctl_open,
        .release = led_ctl_release,
        .write = led_ctl_write,
    };
    
    static int led_ctl_probe(struct platform_device *pdev)
    {
        printk("%s
    ", __func__);
        
        // 在 proc 根目录创建 led 文件夹
        base = proc_mkdir(BASE_DIR_NAME, NULL);
        if(base == NULL){
            printk("%s proc create %s failed
    ", __func__, BASE_DIR_NAME);
            return -EINVAL;
        }
        
        // 在 led 文件夹中创建文件 led_ctl,文件操作函数为 led_ops
        led_ctl = proc_create("led_ctl", 0777, base, &led_ops);
        if (led_ctl == NULL) {
            printk("proc_create failed!
    ");
            // led_ctl 文件创建失败,删除上一步创建的文件夹 led
            proc_remove(base);
            return -EINVAL;
        }
    
        return 0;
    }
    
    static int led_ctl_remove(struct platform_device *pdev)
    {
        printk("%s
    ", __func__);
    
        // 删除 led_ctl 文件
        proc_remove(led_ctl);
        // 删除 led 文件夹
        proc_remove(base);
        
        return 0;
    }
  • 相关阅读:
    linux下mysql区分大小写的内容
    jar包 pom
    项目的考虑
    webservice
    MySQL外键设置中的的 Cascade、NO ACTION、Restrict、SET NULL
    JVM参数最佳实践:元空间的初始大小和最大大小
    JVM问题排查工具:Serviceability-Agent介绍
    Spring Boot 2.x基础教程:构建RESTful API与单元测试
    彻底搞懂JVM类加载器:基本概念
    如何解决90%的问题?10位阿里大牛公布方法
  • 原文地址:https://www.cnblogs.com/lialong1st/p/8317143.html
Copyright © 2011-2022 走看看