[转载]proc_mkdir与proc_create

1:建立proc文件夹
struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent);
参数1:name就是要建立的文件夹名称。
参数2:parent是要建立节点的父节点。也就是要在哪一个文件夹之下建立新文件夹,须要将那个文件夹的node

             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在这里您设置您的阅读和写做的回调。linux

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);
相关文章
相关标签/搜索