[Linux Kernel]proc Learning notes (i)

Source: Internet
Author: User

1, create the API for the node under the/proc file system
Http://www.cnblogs.com/ziziwu/archive/2011/10/20/2218975.html
struct Proc_dir_entry *create_proc_entry (const char *name, mode_t mode, struct proc_dir_entry *parent);
The parameters are/proc file name, mask, parent directory, respectively.

[CPP]View Plaincopy
  1. struct Proc_dir_entry {
  2. unsigned int low_ino;
  3. umode_t mode;
  4. nlink_t Nlink;
  5. kuid_t uid;
  6. Panax Notoginseng kgid_t GID;
  7. loff_t size;
  8. inode_operations *proc_iops, a const struct;
  9. The const struct file_operations *proc_fops;
  10. proc_dir_entry *next, *parent, *subdir;
  11. *data void;
  12. atomic_t count; / * Use count * /
  13. atomic_t In_use; /* Number of callers into module in progress; * /
  14. */* negative-s going away RSN * /
  15. *pde_unload_completion struct completion;
  16. The list_head pde_openers of the struct; /* Who does ->open, but not->release * /
  17. spinlock_t Pde_unload_lock; / * Proc_fops checks and pde_users bumps * /
  18. U8 Namelen;
  19. (+ char name[];
  20. 51};
[CPP]View Plaincopy
    1.    
    2. 2, which implements the functions of the read and Write proc file system nodes, Read_proc and Write_proc function    respectively;
    3.   prototype function: int  (*read_ Proc) (char *page, char **start, off_t  offset, int count, int *eof, < span class= "keyword" >void *data)   
    4.   prototype function: typedef int  (write_proc_t) ( Struct file *file, const char __user  *buffer, unsigned long count, void  *data);   

http://blog.csdn.net/wbd880419/article/details/6637102
For read_proc_t

1. First parameter: Why is it called page? The answer is that if a read operation is invoked on the proc file, the kernel allocates a page-size buffer. How to output data that is larger than one page depends on the second and third parameters.
To understand the 23rd parameter, recall the system calls associated with the file operation:
int open (const char *pathname, int flags);
off_t lseek (int fildes, off_t offset, int whence);
ssize_t Read (int fd, void *buf, size_t count);
ssize_t Write (int fd, const void *buf, size_t count);
for proc files, a read operation can read at most one page of data, and if you need to read the data larger than one page, you need to save the read return value.
Then use Lseek to set offset, and then call read again. Back to the description of the parameter:
2. Start and off parameters: off corresponds to the lseek inside the offset (Lseek whence for the Seek_end,offset negative case, the transmission of off is zero, the specific reason for archaeology).
If you do not set the value of *start, the off value can only be between [0, Count-1], and the size of the data that can be read is: Count-off. You can understand the system copy.
[Page + off, page+count-1] between the data into the user's buffer. If the off value is out of range, read will not be reading the data.
If the value of *start is set, the system considers that the address that *start points to is the address specified by off, and the value of off is ignored, and the system copies [start, Start+count-1]
Data to user space. Of course we may need the value off when we implement the positioning of the start address.
3. The count parameter is consistent with the count in read
4. EOF parameter, set this parameter to indicate that you do not want to provide data again, God horse meaning?
If you do not set this parameter, for the above said start is empty, READ_PROC returns after the system is found (Count-off) < count
will then write a request to read off the size of the data.
For example, there are read operations such as:
Lseek (FD, 2, seek_set); Read (FD, Buff_r, 30)
The first call to Read_proc, off=2, count=30, because we did not set the value of start, will read 28 bytes of data, because there is no set of *eof, the system will be issued again Read_proc
The second call to Read_proc, off=30, count=2, in the description of the parameter start, mentions, for this call, the system default read data (resentment Ah, why issued)
So read returns 28
If this parameter is set, there will not be a second time to be issued.
5. Data parameter, this is the parameter reserved for the driver.
For the Write_proc function, the parameter is very simple, need to explain only one point, is the second parameter of Write_proc buffer is the address of the user state, need to use Copy_from_user
The data is copied from the user state into the kernel-state buffer.

[Linux Kernel]proc Learning notes (i)

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.