Linux Kernel proc file system usage example, Linux kernel proc example

Source: Internet
Author: User

Linux Kernel proc file system usage example, Linux kernel proc example

/*

* Kernel programming test code
*
* Copyright (C) 2014 Sun Mingbao <sunmingbao@126.com>
* Dual licensed under the MIT and/or GPL licenses.
*
*/
 
# Include <linux/init. h>
# Include <linux/module. h>
# Include <linux/types. h>
# Include <linux/kernel. h>
# Include <linux/proc_fs.h>
# Include <linux/string. h>

MODULE_AUTHOR ("Sun Mingbao <sunmingbao@126.com> ");
MODULE_DESCRIPTION ("kernel programming test code ");
MODULE_VERSION ("1.0 ");
MODULE_LICENSE ("Dual MIT/GPL ");

# Define MODULE_NAME "test"

# Define WRITE_CONSOLE (fmt, args ...)\
Do \
{\
Printk (KERN_ALERT fmt, # args );\
} While (0)

# Define DBG_PRINT (fmt, args ...)\
Do \
{\
WRITE_CONSOLE (MODULE_NAME "_ DBG: % s (% d)-% s: \ n" fmt "\ n", _ FILE __,__ LINE __,__ FUNCTION __, # args );\
} While (0)


Static struct proc_dir_entry * my_proc_dir;

Static char proc_file_contents [] = "I love kernel programming very much \ n ";
Static int proc_file_len = sizeof (proc_file_contents)-1;

Static int misc_info_read_proc (char * buffer, char ** start, off_t offset, int length, int * eof, void * data)
{
Int ret;
Ret = snprintf (buffer, length, "% s", proc_file_contents + offset );

If (ret + offset = proc_file_len)
* Eof = 1;

Return ret;
}

Static int _ init create_my_proc_entries (void)
{
My_proc_dir = proc_mkdir (MODULE_NAME, NULL );

Create_proc_read_entry ("misc_info"
, 0
, My_proc_dir
, Misc_info_read_proc
, NULL );

Return 0;
}

Static int _ init test_init (void)
{
Int retval;

DBG_PRINT ("start ");
Retval = create_my_proc_entries ();
If (retval <0)
{
Goto EXIT;
}

DBG_PRINT ("start succeed ");

EXIT:
Return retval;
}

Static void _ exit remove_my_proc_entries (void)
{
Remove_proc_entry ("misc_info", my_proc_dir );
Remove_proc_entry (MODULE_NAME, NULL );
}

Static void _ exit test_exit (void)
{
DBG_PRINT ("quit ");
Remove_my_proc_entries ();
}

Module_init (test_init );
Module_exit (test_exit );

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.