Linux Kernel Learning Notes Seq_file interface to create read-write proc files __linux

Source: Internet
Author: User
Tags documentation

Learn notes and personal understanding, if there are errors, please correct me.

Warm tip: It is recommended to follow the numbering sequence in the comments to read the code

Test method: Cat/proc/abc_proc

echo arbitrary string >/proc/abc_pro (requires root permission)


/************************************************* example of using the Seq_file interface to implement a writable proc file is applicable to kernel AUTHOR:ZHANGN after 3.10 date:2015 -5-17 *************************************************/#include <linux/module.h> #include <linux/sched.h > #include <linux/uaccess.h> #include <linux/proc_fs.h> #include <linux/fs.h> #include <linux/

seq_file.h> #include <linux/slab.h> static char *str = NULL; /*5, the show function is to output the kernel data to user space will be called on the proc file output/static int my_proc_show (struct seq_file *m, void *v) {/* This can not use PR
    Functions such as INTFK to use the seq_file output of a set of special functions see LDD3 91 Pages/seq_printf (M, "current kernel",%ld\n);
    seq_printf (M, "Str is%s\n", str);
return 0;
                             /*3, implement the Open and write functions/static ssize_t my_proc_write (struct file *file, const char __user *buffer,
    size_t count, loff_t *f_pos) {char *tmp = Kzalloc ((count+1), Gfp_kernel);
    if (!tmp) Return-enomem; if (Copy_from_user (TMP, buffer,Count)) {Kfree (TMP);
    return efault;
    } kfree (str);
    str = tmp;
return count; static int My_proc_open (struct inode *inode, struct file *file) {/*4, calling the Single_open binding seq_show function pointer in the Open function needs to say
      Clearly, the SEQ interface described in LDD3 uses the call Seq_open function to invoke the following form: Return Sep_open (file, &scull_seq_ops); Scull_seq_ops for struct seq_operations struct body in which the show function pointer is bound to be prepared seq_operations struct body while calling the Single_open function just specify show The function of the pointer can be personal guess is in the Single_open function of the implementation of the seq_operations structure as to whether it is not know, do not see the specific implementation of interested students can refer to the documentation: Documentation\filesy Stems\seq_file.txt about the third parameter, its type should be viod*, some places in the kernel are passed in null, some places are passed in the inode->i_private, and there are other values passed in to see data in single
      How the _open function is used: if (!res) (struct seq_file *) file->private_data)->private = data;
      Data is a private member of the SEQ_FILE structure.
      So how is data really used?
      The first parameter of the show function is found to be the Seq_file type, in which the private member of the Seq_file can be converted to the corresponding type for use. In other words, you can pass the data parameter to the show function through the private member of Seq_file.
    return Single_open (file, my_proc_show, NULL); /*2, fill in the flie_operations structure that is called in the Proc_create function for its own function, SEQ and single start for the kernel implementation of the function, the direct fill on the line open must fill the function here for details see LDD3 93 page/static struct file_operations My_fops = {. Owner = This_module,. Open = My_proc_open,. Release = S

Ingle_release,. Read = Seq_read,. Llseek = Seq_lseek,. Write = My_proc_write,};
    static int __init my_init (void) {struct proc_dri_entry *file; /*3.10 the new interface of the kernel's proc file needs to be associated file_operations*//*1, first to invoke the function to create the proc file, you need to bind flie_operations*/filename = proc_create ("A
    Bc_proc ", 0644, NULL, &my_fops);
    if (!file) Return-enomem;
return 0;
    /*6, delete proc file */static void __exit my_exit (void) {Remove_proc_entry ("Abc_proc", NULL);
Kfree (str);
} module_init (My_init);
Module_exit (My_exit);
Module_license ("GPL"); Module_author ("Zhangn");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.