Ubuntu/linux Mint Three ways to create a proc file (i)

Source: Internet
Author: User
Tags sprintf linux mint

When doing kernel-driven development, you can use the files under/proc to get the appropriate information for debugging.

Most of the files under/proc are read-only, but for the completeness of the example, a write method is provided.


Method One: Use Create_proc_entry to create the proc file (simple, but the write operation has the danger of buffer overflow);

Method Two: Use Proc_create and Seq_file to create the proc file (method three concise);

Method Three: Use Proc_create_data and Seq_file to create the proc file (more cumbersome, but more complete);

Example four: an example of using a kernel-linked list in a proc file (method three).

--------------------------------------------------------------------------------------------------------------- -----

One

PROC_TEST01.C Source

#include <linux/module.h> #include <linux/sched.h>//jiffies#include <linux/proc_fs.h> #include <linux/uaccess.h>//copy_to|from_user () static char *str = Null;//proc file read function static int my_proc_read (char *page, Char **start, off_t off, int count, int *eof, void *data) {int ret = 0;ret = sprintf (page, "Current kernel time is%ld\n", jiffies); ret + = sprintf (Page+ret, "str is%s\n", str); return ret;} Write function for proc file static int my_proc_write (struct file *filp, const char __user *buf, unsigned long count, void *data) {//Allocate temporary buffer Char *tmp = Kzalloc ((count+1), Gfp_kernel), if (!tmp) return-enomem;//copies the user-state write string to the kernel space//copy_to|from_user (to,from , CNT) if (Copy_from_user (TMP, BUF, Count)) {Kfree (TMP); return-efault;} The old space of Str is freed and the TMP is assigned to Strkfree (str); str = Tmp;return count;} static int __init my_init (void) {struct proc_dir_entry *file;//create proc file = Create_proc_entry ("Jif", 0666, NULL); File) {PRINTK ("cannot create/proc/jif\n"); return-1;} Associating a created file with a read-write function File->read_proc = my_proc_read;file->write_proc = My_proc_write;return 0;} static void __exit my_exit (void) {//delete proc file remove_proc_entry ("Jif", NULL); Kfree (str);} Module_init (My_init); Module_exit (My_exit); Module_author ("Aran"); Module_license ("GPL");

Makefile File:

obj-m:= proc_test01.okernel:=/lib/modules/' uname-r '/build #for mint/ubuntu#kernel: =/lib/modules/' uname-r '/source # For Redhatall:make-c $ (KERNEL) m= ' pwd ' modulesinstall:make-c $ (KERNEL) m= ' pwd ' modules_installdepmod-aclean:make-c $ (K Ernel) m= ' pwd ' clean

Test Results:

Because I am using Linxu mint, create_proc_entry () function has been deleted, it cannot be tested on this machine, but tested on redhat6.4, use methods and tests refer to the following method.

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.