How to read and write file data in Linux Kernel

Source: Internet
Author: User
How to read and write file data in Linux Kernel
 
 
Sometimes you need to read and write file data in linuxkernel-mostly in the driver to be debugged. There is no standard library available for operating files in the kernel, and some functions of the kernel need to be used. These functions mainly include: filp_open () filp_close (), vfs_read () vfs_write (), set_fs (), get_fs (), etc. These functions are available in Linux/Fs. H and ASM/uaccess. H is declared in the header file. The following describes the main steps: 1. open the file


Filp_open () can open the file in the kernel. Its prototype is as follows: strcut file * filp_open (const char * filename, int open_mode, int mode); this function returns the strcut file * structure pointer, used by subsequent function operations. The returned value is verified by is_err. Parameter description filename: indicates the name (including the path) of the file to be opened or created ). When opening a file in the kernel, pay attention to the opening time. It is easy to see that the driver that needs to open the file loads and opens the file very early, however, the device where the file to be opened is not mounted to the file system, leading to a failure to open the file. Open_mode: file opening mode. The value is similar to that of the open parameter in the standard library. You can use o_creat, o_rdwr, and o_rdonly. Mode: used to create a file. Set the read and write permissions for the created file. In other cases, you can set the limit to 0. 2. Read and Write files You can use vfs_read () and vfs_write for file read and write operations in the kernel. Before using these two functions, you must describe the get_fs () and set_fs () functions. Vfs_read () vfs_write (): ssize_t vfs_read (struct file * filp, char _ User * buffer, size_t Len, loff_t * POS ); ssize_t vfs_write (struct file * filp, const char _ User * buffer, size_t Len, loff_t * POS); Note the second Parameter Buffer of the two functions, there is a _ User modifier in front, which requires both buffer pointers to use empty memory. If the pointer to the kernel space is passed to this parameter, both functions return failure-efault. But in kernel, we generally do not easily generate user space pointers, or it is not convenient to use user space memory independently. To make the two read/write functions work correctly using the buffer pointer of the kernel space, you need to use the set_fs () function or macro (set_fs () may be macro-defined). If it is a function, the original form is as follows: void set_fs (mm_segment_t FS); the function is used to change the processing method of the kernel to check the memory address. In fact, the FS parameter of this function has only two values: user_ds, kernel_ds, it represents the user space and the kernel space respectively. By default, the kernel value is user_ds, that is, the user space address is checked and changed. To use the kernel space address in this function for checking and transforming memory addresses, you need to use set_fs (kernel_ds) for setting. Get_fs () may also be a macro definition. Its function is to obtain the current settings. The two functions are generally used: mm_segment_t old_fs; old_fs = get_fs (); set_fs (kernel_ds );...... // memory-related operations set_fs (old_fs); some other kernel functions also use parameters modified by _ User. When the kernel needs to be replaced by the memory of the kernel space, you can use a similar method. When using vfs_read () and vfs_write (), note that the last parameter is loff_t * pos. The value pointed to by POS must be initialized to indicate where the file starts reading and writing. 3. Close file read/write Int filp_close (struct file * filp, fl_owner_t ID); this function is easy to use. The second parameter generally passes a null value and uses current-> files as the real parameter. Note the following when using the above functions:1. In fact, the Linux kernel group members do not approve of reading and writing files independently in the kernel (this may affect the policy and security issues). It is best for the file content required by the kernel to be completed by the application layer. 2. Using this method to read and write files in the loadable kernel module may cause the module to fail to load, because the kernel may not have all the functions you need for export. 3. by analyzing the parameters of some of the above functions, we can see that the proper running of these functions depends on the process environment. Therefore, some functions cannot be executed in the interrupted handle or kernel code that does not belong to any process. Otherwise, a crash may occur. To avoid this, you can create a kernel thread in the kernel, put these functions in the thread environment for execution (the kernel_thread () function is used to create the kernel thread ).

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.