proc_mkdir与proc_create

1:创建proc文件夹
struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent);
参数1:name就是要创建的文件夹名称。
参数2:parent是要创建节点的父节点。也就是要在哪个文件夹之下创建新文件夹,需要将那个文件夹的
             proc_dir_entry传入。
    如果是在/proc目录下创建文件夹,parent为NULL。
  例如:   struct proc_dir_entry *mytest_dir = proc_mkdir("mytest", NULL);

2: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);
参数1:name就是要创建的文件名。
参数2:mode是文件的访问权限,以UGO的模式表示(如0666)。
参数3:parent与proc_mkdir中的parent类似。也是父文件夹的proc_dir_entry对象。
参数4:proc_fops就是该文件的操作函数了。

例如: struct proc_dir_entry *mytest_file = proc_create("mytest", 0x0644, mytest_dir, mytest_proc_fops);

3:proc_create()例子内核模块
(1):这个例子将创建一个proc入口使读取访问。我想你可以通过改变使其他类型的访问的mode传递给该函数。我没有通过一个父目录下有没有必要。结构file_operations在这里您设置您的阅读和写作的回调。
struct proc_dir_entry *proc_file_entry;
static const struct file_operations proc_file_fops = {
 .owner = THIS_MODULE,
 .open = open_callback,
 .read = read_callback,
};
int __init init_module(void){
 proc_file_entry = proc_create("proc_file_name", 0, NULL, &proc_file_fops);
 if(proc_file_entry == NULL)
 return -ENOMEM;
 return 0;
}
您可以检查这个例子的更多细节: 希望这会有所帮助。 
(2):这里是一个'hello_proc“代码,它较新的'proc_create()接口。
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
static int hello_proc_show(struct seq_file *m, void *v) {
 seq_printf(m, "Hello proc!\n");
 return 0;
}
static int hello_proc_open(struct inode *inode, struct file *file) {
 return single_open(file, hello_proc_show, NULL);
}
static const struct file_operations hello_proc_fops = {
 .owner = THIS_MODULE,
 .open = hello_proc_open,
 .read = seq_read,
 .llseek = seq_lseek,
 .release = single_release,
};
static int __init hello_proc_init(void) {
 proc_create("hello_proc", 0, NULL, &hello_proc_fops);
 return 0;
}
static void __exit hello_proc_exit(void) {
 remove_proc_entry("hello_proc", NULL);
}
MODULE_LICENSE("GPL");
module_init(hello_proc_init);
module_exit(hello_proc_exit);

猜你喜欢

转载自blog.csdn.net/ma111000522/article/details/51356709