linux内核文档 proc,Linux内核proc文件系统使用示例

/*

* kernel programming test code

*

* Copyright (C) 2014 Sun Mingbao

* Dual licensed under the MIT and/or GPL licenses.

*

*/

#include

#include

#include

#include

#include

#include

MODULE_AUTHOR("Sun Mingbao ");

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

原文:http://blog.csdn.net/crazycoder8848/article/details/42580835

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值