proc_create() example for kernel module

前端 未结 2 1921
北恋
北恋 2020-12-13 04:45

Can someone give me proc_create() example?

Earlier they used create_proc_entry() in the kernel but now they are using proc_create()<

相关标签:
2条回答
  • 2020-12-13 05:21

    This example will create a proc entry which enables reading access. I think you can enable other kinds of access by changing the mode argument passed to the function. I haven't passed a parent directory because there is no need to. The structure file_operations is where you setup your reading and writing callbacks.

    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;
    }
    

    You can check this example for more details: https://www.linux.com/learn/linux-training/37985-the-kernel-newbie-corner-kernel-debugging-using-proc-qsequenceq-files-part-1

    0 讨论(0)
  • 2020-12-13 05:31

    Here is a 'hello_proc' code, which makes use of the newer 'proc_create()' interface.

    #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);
    

    This code has been taken from http://pointer-overloading.blogspot.com/2013/09/linux-creating-entry-in-proc-file.html

    0 讨论(0)
提交回复
热议问题