Linux kernel programming or kernel module programming file read-write and signal transmission problems

Source: Internet
Author: User

when the Linux kernel is programmed, kernel code execution can only directly access the data in memory, and the file system on the hard disk must be indirectly read and written by the kernel. General kernel operation file read and write in three ways: 1. Through the/proc/file as a bridge to complete the hard disk file system interaction with the kernel; 2. Interact through the IOCTL; 3. Directly read and write files using the functions of the virtual file system Vfs_read (), Vfs_write (). Three ways to implement the method on the web there are many detailed tutorials, you can refer to. Here are three ways to compare.

proc mechanism is a very old file read and write, Universal good, the implementation is also mature, the use of their own implementation of the upper layer of the Read and write functions, not complicated, but slightly cumbersome, a big advantage is the user space program can be real-time through the/proc to the core layer to send signal instructions,/proc/ When the file is written by the user-space program, the kernel reading function is automatically triggered, which can be used to control the kernel mode and parameters. Here is a section of the kernel read function code that can be triggered, which has a judgment on the signal:

static int proc_signal_write (struct file *file, const char __user *buffer,                unsigned long count, void *data) {        Char Procfs_buffer[procfs_buf_max_size];        if (count>procfs_buf_max_size) count = procfs_buf_max_size;        if (Copy_from_user (procfs_buffer, buffer, count))         {                return-efault;        } if (procfs_buffer[0]== ' 0 ') signal = 0;if (procfs_buffer[0]== ' 1 ') signal = 1;if (procfs_buffer[0]== ' 2 ') {Delete_whitelist (Wlhead); Wlend = Wlhead;}        return count;}

the IOCTL mechanism and the kernel communication mechanism under Windows are similar, can also realize the transmission and control of the signal, realize the code is more cumbersome, but it is more convenient to use, no longer say.

The virtual file system method, is three methods, the file reads and writes the simplest, the most convenient, the core code function uses is like the ordinary user space program's reading and writing function, but this method cannot realize the user Space command to the kernel space trigger or the control signal transmission, the use must notice, The virtual file system and the file system on the hard disk need to be converted when the memory is mapped, as shown in the following example:

struct file *fp;mm_segment_t fs;loff_t pos;fp = Filp_open ("/usr/error_log", O_wronly | o_append| o_creat,0644), if (Is_err (FP)) {PRINTK ("Create file error\n"); return;} FS = Get_fs (); Set_fs (kernel_ds);p OS = 0;vfs_write (FP, "   ", 3, &pos); Vfs_write (FP, Hash_val, strlen (Hash_val), & Amp;pos); Vfs_write (FP, "   ", 3, &pos); Vfs_write (FP, Hash_name, strlen (hash_name), &pos); Vfs_write (FP, "\ n" , 1, &pos); Filp_close (fp,null); Set_fs (FS);





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.