Linux kernel (without create_proc_entry func) proc File System Instance __linux

Source: Internet
Author: User
proc File System

The/proc directory is the Linux proc file system, which contains the kernel configuration information, network configuration system, and the state of the process are all based on the PID clearly directory. In short, you can find the basic configuration of the kernel. Kernel update

In the process of the kernel iteration, there are always some interfaces that are discarded, and create_proc_entry is one of them.
Vaguely remember that last year I had been trying to write a proc file system test, until last month to have time to do this thing. Let me try the reason is not to test the create_proc_entry function of the discard, the Internet can search examples are basically using this function. Sample Code

#include <linux/module.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <asm/

uaccess.h> #include <linux/slab.h> static char *buffer = NULL;
  static int hello_proc_show (struct seq_file *m, void *v) {seq_printf (M, "Hello proc!\n");
  seq_printf (M, "buffer=%s\n", buffer);

  int i = 0; if (buffer!= NULL) {if (buffer[0] = = ' s ') {for (i = 0; i < i++) seq_printf (M, "i =%d\
    n ", i);
} return 0; 
  Static ssize_t hello_proc_write (struct file *file, const char *buffer, size_t len, loff_t *off) {int user_len = 0;
  User_len = Len;
  Buffer = (char *) kmalloc (user_len+1, Gfp_kernel);
  memset (buffer, 0, User_len + 1);
    if (copy_from_user (buffer, buffer, User_len)) {PRINTK ("Hello_proc error\n");
  Return-efault;
  } printk ("userlen=%d\n", User_len);
return User_len; static int Hello_proc_open (struct inode *inode, struct file *file) {return Single_open (file, Hello_proc_show, NULL); static const struct File_operations hello_proc_fops = {. Owner = This_module,. Open = Hello_proc_open,. Read = SE

Q_read,. Write = Hello_proc_write,. Llseek = Seq_lseek,. Release = Single_release,};
  static int __init hello_proc_init (void) {proc_create ("Hello_proc", 0666, NULL, &hello_proc_fops);
return 0;

} static void __exit hello_proc_exit (void) {Remove_proc_entry ("Hello_proc", NULL);}
Module_license ("GPL");
Module_init (Hello_proc_init); Module_exit (Hello_proc_exit);
Sample Code Comment

Function: It creates a Hello_proc file in the/proc directory,

echo s >/proc/hello_proc
Cat/proc/hello_proc
This will have output information.

Seq_file is the kernel-provided sequence interface, not the details, this way when the data is large memory allocation does not need you to care about, very practical.

Source Address: Qianguozheng

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.