Linux kernel proc File system Usage Example

Source: Internet
Author: User

/*

* Kernel Programming test code
*
* Copyright (C) Sun Mingbao <[email protected]>
* 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 <[email protected]>");
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);

Linux kernel proc File system Usage Example

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.