proc文件是一种“伪文件”,用于用户访问系统内核中进程的数据,且储存于内存 创建proc文件的方法: static inline struct proc_dir_entry *proc_create(const char *name, mode_t mode,struct proc_dir_entry *parent, const struct file_operations *proc_fops) 即返回一个proc_dir_entry *指针,指向文件路径 第一个参数name为你创建的文件的文件名 第二个参数mode为创建文件的访问权限设置,可以为0,但是只读模式最好是0444(0x0124)(并不懂QAQ) 第三个参数是所属文件夹 proc对应NULL 第四个参数是一个对应操作的集合,结构很长,但是只需要我们额外设置一个open函数,大多数情况下其 他的几个必要操作可以调用系统函数解决
栗子:
static const struct file_operations proc_demo = { .owner = THIS_MODULE, .write = simple_write, //仅读取的话不需要这一行 .read = seq_read, .open = myopen, .release = single_release, };函数原型为 int (* open)( inode * , file *) inode 为记录文件所属的一个结构体 file结构用来记录一个打开的文件
为了方便起见可以使用single_open进行操作(对seq_file的一个简单封装)
int single_open(struct file *file, int (*show)(struct seq_file *, void *),void *data) { struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL); int res = -ENOMEM; //ENOMEM为一个错误代码 if (op) { op->start = single_start; op->next = single_next; op->stop = single_stop; op->show = show; //又要自己写个show函数............... res = seq_open(file, op); //好处是只需要自己写个show函数 if (!res) ((struct seq_file *)file->private_data)->private = data; //data好像是设备驱动相关的参数 else kfree(op); //出错误则无法进行操作 } return res; //返回是否成功的标志 }single_open的接入方法: 写一个open_demo放到file_operations的open中
int open_demo (inode* inode, file* file){ single_open(file,show_demo,NULL); }就是对文件的处理方式,需要自己编写 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
此外,不必要的函数还有一个proc_mkdir,主要是为了看着方便的函数
proc 文件的移除: extern void remove_proc_entry(const char *, struct proc_dir_entry *); 释放内存空间用