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.