一、数据结构
/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下。