Search code examples
linux-kernellinux-device-driver

proc_create() example for kernel module


Can someone give me proc_create() example?

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


Solution

  • 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://web.archive.org/web/20140912140321/http://www.linux.com/learn/linux-training/37985-the-kernel-newbie-corner-kernel-debugging-using-proc-qsequenceq-files-part-1