Use the proc file system to allow communication between Linux kernel space and user space

Source: Internet
Author: User

1. symptom and Problem Description

1. You need to directly interact with the kernel through scripts in linux user mode, such as getting/setting values of certain variables in the kernel.

2. dynamic configuration is required when the Linux kernel is started.

2. Key process and root cause analysis

The/proc file system is a virtual file system through which a new method can be used to communicate between Linux kernel space and user space. In the/proc file system, we can read and write virtual files as a means to communicate with the kernel entity, but unlike normal files, the contents of these virtual files are dynamically created.

3. Add case files

Note:

Demonstrate opening a buffer in the kernel and then reading and writing it in the User Mode

For example,

Read-CAT/proc/ooxx

Write-echo "mydata">/proc/ooxx.

Code List:

 # Define item_name "ooxx" <br/> static struct proc_dir_entry * proc_entry; <br/> static char g_buf [page_size]; </P> <p> ssize_t ooxx_write (struct file * PF, const char _ User * Buf, unsigned long Len, void * Data) <br/>{< br/> memset (g_buf, 0, page_size); <br/> If (copy_from_user (g_buf, Buf, Len )) {<br/> printk ("copy_from_user fail. /n "); <br/> return-efault; <br/>}< br/> return strlen (g_buf); <br/>}</P> <p> Int ooxx_read (char * Page, char ** start, off_t off, int count, int * EOF, void * Data) <br/>{< br/> sprintf (page, "% s/n", g_buf); <br/> return strlen (g_buf); <br/>}</P> <p> int _ init demo_proc_init (void) <br/>{</P> <p> printk ("% s: Enter. /n ", _ function _); <br/> sprintf (g_buf," ooxx/N "); </P> <p>/* The third parameter null indicates creating */<br/> proc_entry = create_proc_entry (item_name, s_irugo | s_iwugo, null) under the proc root directory ); </P> <p> If (null! = Proc_entry) {<br/> printk ("OK/N"); <br/> proc_entry-> read_proc = ooxx_read; <br/> proc_entry-> write_proc = ooxx_write; <br/> proc_entry-> owner = this_module; <br/>}< br/> printk ("% s: Leave. /n ", _ function _); <br/> return 0; <br/>}</P> <p> void _ exit demo_proc_exit (void) <br/>{< br/> printk ("% s: Enter. /n ", _ function _); <br/> If (null! = Proc_entry) {<br/> printk ("release/N"); <br/> remove_proc_entry (item_name, & proc_root ); <br/>}< br/> printk ("% s: Leave. /n ", _ function _); <br/> return; <br/>}</P> <p> module_license (" GPL "); <br/> module_init (demo_proc_init); <br/> module_exit (demo_proc_exit); <br/>

 

For details, refer:Use the/proc file system to access the content of the Linux kernel-The Virtual File System opens a communication window between the kernel space and the user space.

Related Article

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.