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;
    }
  • 相关阅读:
    lanya
    Apple watch ,小米微信通知
    jenkins grunt 自动构建流程
    刷机步骤
    ipad忘记了锁屏密码,已经越狱了
    ar
    如何在ubuntu中安装php
    阿里云
    docker swarm 集群及可视化界面的安装及配置
    https://github.com/gaoyangxiaozhu/DockerVI
  • 原文地址:https://www.cnblogs.com/lialong1st/p/8317143.html
Copyright © 2011-2022 走看看