1:建立proc文件夹
struct proc_dir_entry *proc_mkdir(const char *name, struct proc_dir_entry *parent);
参数1:name就是要建立的文件夹名称。
参数2:parent是要建立节点的父节点。也就是要在哪一个文件夹之下建立新文件夹,须要将那个文件夹的node
(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; }
您能够检查这个例子的更多细节: 但愿这会有所帮助。 函数
#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);