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

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).

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

Two

PROC_TEST02.C Source

#include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/fs.h >//for basic filesystem#include <linux/proc_fs.h>//for the proc filesystem#include <linux/seq_file.h>/ /For sequence files#include <linux/jiffies.h>//for jiffies#include <linux/slab.h>//for Kzalloc, kfree# Include <linux/uaccess.h>//for Copy_from_user#define buf_size 128//global varstatic char *str = null;//Seq_operat  Ions-showstatic int jif_show (struct seq_file *m, void *v) {char buf[buf_size];int ret = 0;ret = sprintf (buf, "current Kernel time is%llu\n ", (unsigned long Long) get_jiffies_64 ()); ret + = sprintf (buf + ret," str is%s\n ", str); seq_printf (M , "%s", buf); return 0; //!! Must be 0, or would show nothing t.t}//file_operations-writestatic ssize_t jif_write (struct file *file, const char _ _user *buffer, size_t count, loff_t *f_pos) {//Allocate temporary buffer char *tmp = Kzalloc ((count+1), gfp_kernel); if (!tmp) return-enomem;/ /user-state write characterstring copy to Kernel space//copy_to|from_user (to,from,cnt) if (Copy_from_user (TMP, buffer, count)) {Kfree (TMP); return-efault;} The old space of Str is freed and the TMP is assigned to Strkfree (str); str = Tmp;return count;}  Seq_operations-openstatic int jif_open (struct inode *inode, struct file *file) {return Single_open (file, Jif_show, NULL);} static const struct File_operations jif_fops = {. owner= this_module,.open= jif_open,.read= seq_read,.write = jif_write,.l lseek= seq_lseek,.release= single_release,};//Module initstatic int __init jif_init (void) {struct proc_dir_entry* jif_ File;jif_file = Proc_create ("Jif", 0, NULL, &jif_fops); if (null = = Jif_file) {return-enomem;} return 0;} module exitstatic void __exit jif_exit (void) {Remove_proc_entry ("Jif", NULL); Kfree (str);} Module_init (Jif_init); Module_exit (Jif_exit); Module_author ("Aran"); Module_license ("GPL");

Makefile File:

obj-m:= proc_test02.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:


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.