创建proc中的节点

2019-03-05 10:11:27 浏览数 (1)

#include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/proc_fs.h> #include <linux/fs.h> #define CUTBAG_DIR "CU_T-bagwell" #define CUTBAG_NODE "test_node" struct proc_dir_entry *cutbag_dir; static int __init test_init(void) {         int retval = 0;         struct proc_dir_entry *ent;         cutbag_dir = proc_mkdir(CUTBAG_DIR, NULL);         if(cutbag_dir == NULL){                 printk(KERN_WARNING "%s create errorn", CUTBAG_DIR);                 return -1;         }         ent = create_proc_entry(CUTBAG_NODE, 0, cutbag_dir);         if(ent == NULL){                 printk(KERN_WARNING, "%s create errorn", CUTBAG_NODE);                 goto CREATE_NODE_ERR;         }         return 0; CREATE_NODE_ERR:         remove_proc_entry(CUTBAG_DIR, 0);         return -1; } static void __exit test_cleanup(void) {                 remove_proc_entry(CUTBAG_NODE, cutbag_dir);                 remove_proc_entry(CUTBAG_DIR, 0); } module_init(test_init); module_exit(test_cleanup); MODULE_LICENSE("GPL");

0 人点赞