proc相关操作

一、数据结构

/proc下每个文件对应下面一个数据结构
struct proc_dir_entry {
    unsigned int low_ino;
    unsigned short namelen;
    const char *name;
    mode_t mode;
    nlink_t nlink;
    uid_t uid;
    gid_t gid;
    loff_t size;
    const struct inode_operations *proc_iops;
    /*
     * NULL ->proc_fops means \"PDE is going away RSN\" or
     * \"PDE is just created\". In either case, e.g. ->read_proc won\'t be
     * called because it\'s too late or too early, respectively.
     *
     * If you\'re allocating ->proc_fops dynamically, save a pointer
     * somewhere.
     */
    const struct file_operations *proc_fops;
    get_info_t *get_info;        //
    struct module *owner;
    struct proc_dir_entry *next, *parent, *subdir;
    void *data;
    read_proc_t *read_proc;        //
    write_proc_t *write_proc;    //
    atomic_t count;     /* use count */
    int pde_users;  /* number of callers into module in progress */
    spinlock_t pde_unload_lock; /* proc_fops checks and pde_users bumps */
    struct completion *pde_unload_completion;
    shadow_proc_t *shadow_proc;    //
};

这个结构比较重要的就是3个钩子函数,分别用于 read write info操作

1、read操作:
typedef int(read_proc_t)(char *page,char **start,off_t off, int count, int *eof, void *data);
page:指示用来写入数据的缓冲区;
off和count:与read函数对应的参数相同;
start和eof:用于读取大于1个page数据时实现。

2、write操作:
typedef int (write_proc_t)(struct file *file, const char __user *buffer, unsigned long count, void *data);
filp 参数实际上是一个打开文件结构(可以忽略这个参数)。
buffer 参数是用户空间要写入的数据。缓冲区地址实际上是一个用户空间的缓冲区,不能直接读取它。
len 参数定义了在 buffer 中有多少数据要被写入。
data 参数是一个指向私有数据的指针。

3、info操作

typedef int (get_info_t)(char *, char **, off_t, int);


 

二、proc API

创建目录
struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent)

name: 文件夹的名字
parent: 确定文件所在目录,如果置NULL,则位置为/proc下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值